-
Notifications
You must be signed in to change notification settings - Fork 6
/
OMF.cs
455 lines (364 loc) · 11.6 KB
/
OMF.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
using System;
using System.Collections.Generic;
using System.IO;
namespace OMF_Editor
{
public class AnimationsContainer
{
public int SectionId;
public uint SectionSize;
public int SectionId2;
public uint SectionSize2;
public int AnimsCount;
public short AnimsParamsCount;
public string FileName;
public List<AnimVector> Anims = new List<AnimVector>();
public List<AnimationParams> AnimsParams = new List<AnimationParams>();
public void RecalcSectionSize()
{
uint new_size = 0;
foreach(AnimVector anim in Anims)
{
anim.RecalcSectionSize();
new_size += anim.GetSize();
}
new_size += 12;
SectionSize = new_size;
// Recalc second section size
new_size = 6; // motions count ( short )
foreach (AnimationParams anim in AnimsParams)
{
uint temp = (uint)anim.Size(bone_cont.OGF_V);
new_size += temp;
}
new_size += (uint)bone_cont.Size();
bone_cont.SectionSize = new_size;
}
public string GetFileName()
{
return Path.GetFileNameWithoutExtension(FileName);
}
public void RecalcAnimNum()
{
AnimsCount = AnimsParams.Count;
AnimsParamsCount = (short)AnimsParams.Count;
}
public void RecalcAllAnimIndex()
{
short i = 0;
foreach(AnimationParams anm in AnimsParams)
{
anm.MotionID = i;
i++;
}
i = 1;
foreach (AnimVector anm in Anims)
{
anm.SectionId = i;
i++;
}
}
public void GunslingerRepair()
{
short i = 0;
foreach (AnimationParams anm in AnimsParams)
{
anm.MotionID = i;
i++;
}
i = 1;
foreach (AnimVector anm in Anims)
{
anm.Name = AnimsParams[i - 1].Name;
anm.SectionId = i;
i++;
}
}
public BoneContainer bone_cont;
public void AddAnim(AnimVector vector)
{
Anims.Add(vector);
}
public void AddAnimParams(AnimationParams param)
{
AnimsParams.Add(param);
}
public short GetMotionVersion()
{
return bone_cont.OGF_V;
}
public AnimationsContainer(BinaryReader reader, OMFEditor editor)
{
SectionId = reader.ReadInt32();
SectionSize = reader.ReadUInt32();
SectionId2 = reader.ReadInt32();
SectionSize2 = reader.ReadUInt32();
AnimsCount = reader.ReadInt32();
int count = AnimsCount;
for (int i = 0; i < count; i++)
{
AnimVector vector = new AnimVector
{
SectionId = reader.ReadInt32(),
SectionSize = reader.ReadUInt32(),
Name = editor.ReadSuperString(reader)
};
int size = (int)vector.SectionSize - (vector.Name.Length + 1);
vector.data = reader.ReadBytes(size);
AddAnim(vector);
}
}
public void WriteAnimationContainer(BinaryWriter writer, OMFEditor editor)
{
writer.Write(SectionId);
writer.Write(SectionSize);
writer.Write(SectionId2);
writer.Write(SectionSize2);
writer.Write(AnimsCount);
foreach (AnimVector anim in Anims)
{
writer.Write(anim.SectionId);
writer.Write(anim.SectionSize);
editor.WriteSuperString(writer, anim.Name);
writer.Write(anim.data);
}
}
}
public class AnimVector
{
public int SectionId;
public uint SectionSize;
public string Name;
public byte[] data;
public int GetNumKeys()
{
return BitConverter.ToInt32(data,0);
}
public uint GetSize()
{
return SectionSize + 8;
}
public void RecalcSectionSize()
{
SectionSize = (uint)(Name.Length + 1 + data.Length);
}
public string MotionName
{
get { return Name; }
}
}
public class BoneParts
{
public string Name;
public short Count;
public List<BoneVector> bones = new List<BoneVector>();
public int Size()
{
int new_size = 0;
foreach(BoneVector bone in bones)
{
new_size += bone.Size();
}
return new_size + Name.Length + 3;
}
}
public class BoneVector
{
public string Name;
public uint ID;
public int Size()
{
return Name.Length + 5;
}
}
public class BoneContainer
{
public int SectionId;
public uint SectionSize;
public short OGF_V;
public short Count;
public int Size()
{
int new_size = 0;
foreach(BoneParts bonesparts in parts)
{
new_size += bonesparts.Size();
}
return new_size;
}
public List<BoneParts> parts = new List<BoneParts>();
public BoneContainer(BinaryReader reader, OMFEditor editor)
{
SectionId = reader.ReadInt32();
SectionSize = reader.ReadUInt32();
OGF_V = reader.ReadInt16();
Count = reader.ReadInt16();
bool empty_bone_names = false;
for (int i = 0; i < Count; i++)
{
BoneParts bonesparts = new BoneParts
{
Name = editor.ReadSuperString(reader),
Count = reader.ReadInt16()
};
for (int n = 0; n < bonesparts.Count; n++)
{
BoneVector sbone = new BoneVector();
if(!empty_bone_names)
sbone.Name = editor.ReadSuperString(reader);
else
sbone.Name = "";
if(sbone.Name == "" && !empty_bone_names)
{
reader.BaseStream.Position -= 1;
empty_bone_names = true;
}
sbone.ID = reader.ReadUInt32();
bonesparts.bones.Add(sbone);
}
parts.Add(bonesparts);
}
}
public void WriteBoneCont(BinaryWriter writer, OMFEditor editor)
{
writer.Write(SectionId);
writer.Write(SectionSize);
writer.Write(OGF_V);
writer.Write(Count);
foreach (BoneParts bone in parts)
{
editor.WriteSuperString(writer, bone.Name);
writer.Write(bone.Count);
foreach (BoneVector sbone in bone.bones)
{
editor.WriteSuperString(writer, sbone.Name);
writer.Write(sbone.ID);
}
}
}
}
public class MotionMark
{
public string Name;
public int Count;
public List<MotionMarkParams> m_params = new List<MotionMarkParams>();
public MotionMark()
{
}
public MotionMark(BinaryReader reader, OMFEditor editor)
{
Name = editor.ReadMotionMarkString(reader);
Count = reader.ReadInt32();
for (int n = 0; n < Count; n++)
{
MotionMarkParams param = new MotionMarkParams(reader);
m_params.Add(param);
}
}
public uint Size()
{
uint temp = (uint)(Name.Length + 8 * m_params.Count + 6);
return temp;
}
public void WriteMotionMark(BinaryWriter writer, OMFEditor editor)
{
editor.WriteMarkString(writer, Name);
writer.Write(m_params!=null? m_params.Count : 0);
if(m_params!= null)
{
foreach (MotionMarkParams param in m_params)
{
writer.Write(param.t0);
writer.Write(param.t1);
}
}
}
}
public class MotionMarkParams
{
public float t0;
public float t1;
public MotionMarkParams()
{
t0 = t1 = 0;
}
public MotionMarkParams(BinaryReader reader)
{
t0 = reader.ReadSingle();
t1 = reader.ReadSingle();
}
}
public class AnimationParams
{
public string Name { get; set; }
public int Flags { get; set; }
public short BoneOrPart { get; set; }
public short MotionID { get; set; }
public float Speed { get; set; }
public float Power { get; set; }
public float Accrue { get; set; }
public float Falloff { get; set; }
public int MarksCount { get; set; }
public uint Size(short motion_version)
{
uint new_size = 0;
if(motion_version == 4)
{
new_size += 4;
if (m_marks != null && m_marks.Count > 0)
{
foreach (MotionMark mark in m_marks)
{
uint temp = mark.Size();
new_size += temp;
}
}
}
return new_size + (uint)(Name.Length + 25);
}
public List<MotionMark> m_marks; // = new List<MotionMark>();
public AnimationParams(BinaryReader reader, OMFEditor editor, short motion_version)
{
Name = editor.ReadSuperString(reader);
Flags = reader.ReadInt32();
BoneOrPart = reader.ReadInt16();
MotionID = reader.ReadInt16();
Speed = reader.ReadSingle();
Power = reader.ReadSingle();
Accrue = reader.ReadSingle();
Falloff = reader.ReadSingle();
MarksCount = 0;
if (motion_version == 4)
{
MarksCount = reader.ReadInt32();
if (MarksCount > 0)
{
m_marks = new List<MotionMark>();
for (int i = 0; i < MarksCount; i++)
{
MotionMark newmark = new MotionMark(reader, editor);
m_marks.Add(newmark);
}
}
}
}
public void WriteAnimationParams(BinaryWriter writer, OMFEditor editor, short motion_version)
{
editor.WriteSuperString(writer, Name);
writer.Write(Flags);
writer.Write(BoneOrPart);
writer.Write(MotionID);
writer.Write(Speed);
writer.Write(Power);
writer.Write(Accrue);
writer.Write(Falloff);
if (motion_version != 4) return;
writer.Write(m_marks != null ? m_marks.Count : 0);
if(m_marks != null)
{
foreach (MotionMark mark in m_marks)
mark.WriteMotionMark(writer, editor);
}
}
}
}