-
Notifications
You must be signed in to change notification settings - Fork 1
/
BinaryMDL.cs
473 lines (423 loc) · 15.1 KB
/
BinaryMDL.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using MDL;
namespace MDLUtility2
{
public class BinaryMDL
{
BinaryMDLHeader binaryHeader;
public List<MDLLight> lights = new List<MDLLight>();
public List<MDLFrameData> frames = new List<MDLFrameData>();
public List<string> textures = new List<string>();
public MDLObject mainObject;
public float FrameVal;
public BinaryMDLHeader BinaryHeader
{
get { return binaryHeader; }
set { binaryHeader = value; }
}
List<String> importedLibriesList = new List<String>();
public List<String> ImportedLibriesList
{
get { return importedLibriesList; }
set { importedLibriesList = value; }
}
List<ImportedSymbol> importedSymbolList = new List<ImportedSymbol>();
public List<ImportedSymbol> ImportedSymbolList
{
get { return importedSymbolList; }
set { importedSymbolList = value; }
}
List<String> exportedSymbolsList = new List<String>();
public List<String> ExportedSymbolsList
{
get { return exportedSymbolsList; }
set { exportedSymbolsList = value; }
}
List<SymbolData> exportedSymbolsDataList = new List<SymbolData>();
public List<SymbolData> ExportedSymbolsDataList
{
get { return exportedSymbolsDataList; }
set { exportedSymbolsDataList = value; }
}
public BinaryMDL(MDL.MDLFile mdlf)
{
this.binaryHeader._1_MDLMagic = 0xDEBADF00;
binaryHeader._2_MDLVersion = mdlf.header.MDLVersion;
binaryHeader._3_ImportedNameSpacesCount = mdlf.header.ImportedNameSpacesCount;
binaryHeader._4_ImportedSymbolCount = mdlf.header.ImportedSymoblCount;
binaryHeader._5_ExportedSymbolCount = mdlf.header.ExportedSymbolCount;
binaryHeader._6_OtherObjectCount = mdlf.header.OtherObjectsCount;
if(mdlf.Lights!=null)
lights = new List<MDLLight>(mdlf.Lights);
if (mdlf.FrameDatas != null)
frames = new List<MDLFrameData>(mdlf.FrameDatas);
if (mdlf.Textures != null)
textures = new List<string>(mdlf.Textures);
mainObject = mdlf.RootObject;
FrameVal = mdlf.FrameVal;
}
public BinaryMDL(System.IO.BinaryReader br)
{
binaryHeader.fillFromReader(br); // Load the Header Info.
if (binaryHeader.isValid())
{
// read in the importedNamespaces
for (int i = 0; i < binaryHeader._3_ImportedNameSpacesCount; i++)
{
importedLibriesList.Add(AlignedString.ReadAlignedString(br));
}
// read in the importedSymbols
for (int i = 0; i < binaryHeader._4_ImportedSymbolCount; i++)
{
importedSymbolList.Add(new ImportedSymbol(br));
}
// read in the exportedSymbols
for (int i = 0; i < binaryHeader._5_ExportedSymbolCount; i++)
{
exportedSymbolsList.Add(AlignedString.ReadAlignedString(br));
}
// read in the exportedSymbols Data
for (int i = 0; i < binaryHeader._5_ExportedSymbolCount; i++)
{
exportedSymbolsDataList.Add(new SymbolData(br, importedSymbolList));
}
}
}
}
public struct BinaryMDLHeader
{
public UInt32 _1_MDLMagic; // should be 3736788736
public Int32 _2_MDLVersion;
public Int32 _3_ImportedNameSpacesCount; // usually 1
public Int32 _4_ImportedSymbolCount; // usually 1
public Int32 _5_ExportedSymbolCount; // usually 1
public Int32 _6_OtherObjectCount; // always should be 0
public void fillFromReader(System.IO.BinaryReader br)
{
_1_MDLMagic = br.ReadUInt32();
if (isValid())
{
_2_MDLVersion = br.ReadInt32();
_3_ImportedNameSpacesCount = br.ReadInt32();
_4_ImportedSymbolCount = br.ReadInt32();
_5_ExportedSymbolCount = br.ReadInt32();
_6_OtherObjectCount = br.ReadInt32();
}
}
public bool isValid()
{
return _1_MDLMagic == 3736788736;
}
}
public class ImportedSymbol
{
Int32 index;
String value;
public Int32 Index
{
get { return index; }
set { index = value; }
}
public String Value
{
get { return this.value; }
set { this.value = value; }
}
public ImportedSymbol(System.IO.BinaryReader br)
{
// read in the index
index = br.ReadInt32();
// read in the alignedString
value = AlignedString.ReadAlignedString(br);
}
}
public class AlignedString
{
string value = "";
public string Value
{
get { return this.value; }
set { this.value = value; }
}
public AlignedString(System.IO.BinaryReader br)
{
this.value = ReadAlignedString(br);
}
public static string ReadAlignedString(System.IO.BinaryReader br)
{
string value = "";
// ignore any null padding in the front.
while (br.PeekChar() == '\0')// loop over the embeded name.
br.ReadChar(); // discard the null
while (br.PeekChar() != '\0')// loop over the embeded name.
value += br.ReadChar();
br.ReadChar();//discard the null terminating the end of the string.
// if we are not alligned, then drop some bytes.
while (br.BaseStream.Position % 4 != 0)
br.ReadChar(); // should be null padding for alignment
return value;
}
}
public class SymbolData
{
Int32 index, unk1, unk2, unk3;
object data;
SymbolDataTypes indexType;
public Int32 Index
{
get { return index; }
set { index = value; }
}
public SymbolDataTypes DataType
{
get { return indexType; }
}
public object Data
{
get { return data; }
set { data = value; }
}
public BinarySurface DataAsBinarySurface
{
get { return data as BinarySurface; }
}
public SymbolData(System.IO.BinaryReader br, List<ImportedSymbol> symbolList)
{
do
{
index = br.ReadInt32();
} while (index != 9);
index = br.ReadInt32();
string DataSubType = symbolList[index].Value;
// figure out the procedure to do.
Int32 DataTypeIndex = br.ReadInt32(); // This is usually 7 I think..
// Which is type (SymbolDataTypes)7 or ObjectBinary
// so this should probabally be read before this method is entered.
// And be used as a type selector.
if (symbolList.Count > index)
indexType = (SymbolDataTypes)DataTypeIndex;
switch (indexType)
{
case SymbolDataTypes.ObjectFloat:
if (DataSubType == "frame")
{
float val = br.ReadSingle();
}
break;
case SymbolDataTypes.ObjectString:
// stack.Push(new StringValue(ReadString()));
break;
case SymbolDataTypes.ObjectTrue:
// stack.Push(new Boolean(true));
break;
case SymbolDataTypes.ObjectFalse:
// stack.Push(new Boolean(false));
break;
case SymbolDataTypes.ObjectList:
Int32 listCount = br.ReadInt32();
break;
case SymbolDataTypes.ObjectApply:
// stack.Push(ReadApply(stack));
Int32 tmp = br.ReadInt32();
break;
case SymbolDataTypes.ObjectBinary:
if (DataSubType == "ImportImage")// we need to switch over something here.
{
data = new BinarySurface(br);
}
else if (DataSubType == "MeshGeo")
{
LoadMDLMesh(br);
}
else if (DataSubType == "LightsGeo")
{
}
else if (DataSubType == "FrameData")
{
}
else if (DataSubType == "TextureGeo")
{
}
else if (DataSubType == "LODGeo")
{
}
else if (DataSubType == "GroupGeo")
{
}
else if (DataSubType == "time")
{
}
break;
case SymbolDataTypes.ObjectReference:
// stack.Push(m_pobjects[ReadDWORD()]);
break;
case SymbolDataTypes.ObjectImport:
// stack.Push(m_pobjectImports[ReadDWORD()]);
break;
case SymbolDataTypes.ObjectPair:
// stack.Push(ReadPair(stack));
break;
case SymbolDataTypes.ObjectEnd:
break;
default:
break;
}
// Read the object End.
// advance position to the null int at the end of the data.
int lookingForNull = -1;
do
{
lookingForNull = br.ReadInt32();
} while (lookingForNull != 0);
}
private static MDLObject LoadMDLMesh(System.IO.BinaryReader br)
{
MDLMesh mesh = new MDLMesh();
mesh.nvertex = br.ReadInt32();
mesh.nfaces = br.ReadInt32();
mesh.vertices = new MDLVertice[mesh.nvertex];
for (int n = 0; n < mesh.nvertex; n++)
{
// read vertice
MDLVertice vert = new MDLVertice();
vert.x = br.ReadSingle();
vert.y = br.ReadSingle(); ;
vert.z = br.ReadSingle(); ;
vert.mx = br.ReadSingle(); ;
vert.my = br.ReadSingle(); ;
vert.nx = br.ReadSingle(); ;
vert.ny = br.ReadSingle(); ;
vert.nz = br.ReadSingle(); ;
mesh.vertices[n] = vert;
}
mesh.faces = new ushort[mesh.nfaces];
for (int n = 0; n < mesh.nfaces; n++)
mesh.faces[n] = br.ReadUInt16();
MDLObject mdlo = MDLFile.NewMDLObject();
mdlo.mesh = mesh;
mdlo.type = MDLType.mdl_mesh;
return mdlo;
}
}
public enum BinaryDataTypes
{
ImportImage
}
public enum SymbolDataTypes
{
ObjectEnd = 0,
ObjectFloat = 1,
ObjectString = 2,
ObjectTrue = 3,
ObjectFalse = 4,
ObjectList = 5,
ObjectApply = 6,
ObjectBinary = 7,
ObjectReference = 8,
ObjectImport = 9,
ObjectPair = 10,
ImportImage = 11,
}
public class BinarySurface
{
int unk1, unk2, unk3;
BinarySurfaceInfo header;
byte[] pixelData;
Bitmap myBMP;
public Bitmap bmp
{
get { return myBMP; }
// set { myBMP = value; }
}
internal BinarySurfaceInfo Header
{
get { return header; }
set { header = value; }
}
public byte[] PixelData
{
get { return pixelData; }
set { pixelData = value; }
}
public BinarySurface(System.IO.BinaryReader br)
{
// unk2 = br.ReadInt32();
//unk3 = br.ReadInt32();
header.fillFromReader(br);
int numBytes = header._3_pitch * header._2_y * (header._4_bitCount / 16);
PixelData = new byte[numBytes];
PixelData = br.ReadBytes(numBytes);
reloadBmpFromPixelData();
}
public void reloadBmpFromPixelData()
{
int bpp = header._4_bitCount;
int width = header._1_x;
int height = header._2_y;
myBMP = null;
if (bpp == 16)
{
myBMP = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format16bppRgb565);
}
else if (bpp == 24)
{
myBMP = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
}
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, myBMP.Width, myBMP.Height);
System.Drawing.Imaging.BitmapData bmpData =
myBMP.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
myBMP.PixelFormat);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int numBytes = bmpData.Stride * bmpData.Height;
byte[] rgbValues = PixelData;//new byte[bytes];
bool succeded = false;
try
{
// Copy the RGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, numBytes);
succeded = true;
}
catch (Exception ex)
{
}
if (succeded == false)
{
// attempt to load with accuall scanwidth in header..
numBytes = header._3_pitch * header._2_y * (header._4_bitCount / 16);
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, numBytes);
}
// Unlock the bits.
myBMP.UnlockBits(bmpData);
}
}
public struct BinarySurfaceInfo
{
public Int32 _1_x;
public Int32 _2_y;
public Int32 _3_pitch;
public Int32 _4_bitCount;
public Int32 _5_redMask;
public Int32 _6_greenMask;
public Int32 _7_blueMask;
public Int32 _8_alphaMask;
public bool _9_bColorKey;
public void fillFromReader(System.IO.BinaryReader br)
{
_1_x = br.ReadInt32();
_2_y = br.ReadInt32();
_3_pitch = br.ReadInt32();
_4_bitCount = br.ReadInt32();
_5_redMask = br.ReadInt32();
_6_greenMask = br.ReadInt32();
_7_blueMask = br.ReadInt32();
_8_alphaMask = br.ReadInt32();
_9_bColorKey = Convert.ToBoolean(br.ReadInt32());
}
}
}