-
Notifications
You must be signed in to change notification settings - Fork 0
/
XFile.cs
1445 lines (1271 loc) · 63.5 KB
/
XFile.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace D3DX
{
namespace Mesh
{
/// <summary>
/// Represents a DirectX SDK mesh. (.x)
/// </summary>
public class XFile
{
public XHeader Header { get; } = new XHeader();
public List<XTemplate> Templates { get; } = new List<XTemplate>();
public List<XObject> Objects { get; } = new List<XObject>();
public XFile(XHeader header)
{
Header = header;
}
public XFile(BinaryReader reader)
{
Header = new XHeader(reader);
XReader xReader = new XReader(reader, Header, Templates, Objects);
foreach (XToken token in xReader.Tokens)
{
System.Diagnostics.Debug.WriteLine(token.ToString());
}
while (!xReader.EndOfStream)
{
if (xReader.PeekToken().ID == XToken.TokenID.TEMPLATE)
{
Templates.Add(new XTemplate(xReader));
}
else
{
Objects.Add(new XObject(xReader));
}
}
}
public void Write(BinaryWriter writer)
{
Header.Write(writer);
XWriter xWriter = new XWriter();
foreach (XTemplate template in Templates)
{
Console.WriteLine(" Writing template '" + template.Name + "'...");
template.Write(xWriter);
}
foreach (XObject obj in Objects)
{
Console.WriteLine(" Writing object '" + obj.Name + "'...");
obj.Write(xWriter);
}
foreach (XToken token in xWriter.Tokens)
{
System.Diagnostics.Debug.WriteLine(token.ToString());
token.Write(writer, Header);
}
}
}
public class XHeader
{
private const uint FORMAT_MAGIC = 0x20666F78; // 'xof '
public enum HeaderFormat : uint
{
Binary = 0x206E6962, // 'bin '
Text = 0x20747874, // 'txt '
Compressed = 0x20706D63, // 'cmp '
}
public byte VersionMain { get; } = 3;
public byte VersionSub { get; } = 3;
public HeaderFormat Format { get; } = HeaderFormat.Binary;
public int Precision { get; } = 32;
public XHeader()
{
}
public XHeader(byte versionMain, byte versionSub, HeaderFormat format, int precision)
{
VersionMain = versionMain;
VersionSub = versionSub;
Format = format;
Precision = precision;
}
public XHeader(BinaryReader reader)
{
uint magic = reader.ReadUInt32();
if (magic != FORMAT_MAGIC)
throw new FormatException("Bad XFile magic!");
VersionMain = Byte.Parse(Encoding.ASCII.GetString(reader.ReadBytes(2)));
VersionSub = Byte.Parse(Encoding.ASCII.GetString(reader.ReadBytes(2)));
Format = (HeaderFormat)reader.ReadUInt32();
Precision = Int32.Parse(Encoding.ASCII.GetString(reader.ReadBytes(4)));
}
public void Write(BinaryWriter writer)
{
writer.Write(FORMAT_MAGIC);
writer.Write(Encoding.ASCII.GetBytes(VersionMain.ToString().PadLeft(2, '0')));
writer.Write(Encoding.ASCII.GetBytes(VersionSub.ToString().PadLeft(2, '0')));
writer.Write((uint)Format);
writer.Write(Encoding.ASCII.GetBytes(Precision.ToString().PadLeft(4, '0')));
}
}
public class XToken
{
public enum TokenID : ushort
{
INVALID = 0,
// Data-carrying tokens
NAME = 1,
STRING = 2,
INTEGER = 3,
GUID = 5,
INTEGER_LIST = 6,
FLOAT_LIST = 7,
// Syntax tokens
OBRACE = 10,
CBRACE = 11,
OPAREN = 12,
CPAREN = 13,
OBRACKET = 14,
CBRACKET = 15,
OANGLE = 16,
CANGLE = 17,
DOT = 18,
COMMA = 19,
SEMICOLON = 20,
TEMPLATE = 31,
// Primitive type tokens
WORD = 40,
DWORD = 41,
FLOAT = 42,
DOUBLE = 43,
CHAR = 44,
UCHAR = 45,
SWORD = 46,
SDWORD = 47,
VOID = 48,
LPSTR = 49,
UNICODE = 50,
CSTRING = 51,
ARRAY = 52
}
public TokenID ID { get; private set; } = TokenID.INVALID;
private string _nameData = "";
public string NameData
{
get { if (ID != TokenID.NAME) throw new InvalidOperationException(); return _nameData; }
set { ID = TokenID.NAME; _nameData = value; }
}
private string _stringData = "";
private TokenID _stringTerminator = TokenID.INVALID;
public string StringData
{
get { if (ID != TokenID.STRING) throw new InvalidOperationException(); return _stringData; }
set { ID = TokenID.STRING; _stringData = value; }
}
public TokenID StringTerminator
{
get { if (ID != TokenID.STRING) throw new InvalidOperationException(); return _stringTerminator; }
set { if (ID != TokenID.STRING) throw new InvalidOperationException(); _stringTerminator = value; }
}
private int _integerData = 0;
public int IntegerData
{
get { if (ID != TokenID.INTEGER) throw new InvalidOperationException(); return _integerData; }
set { ID = TokenID.INTEGER; _integerData = value; }
}
private Guid _guidData = Guid.Empty;
public Guid GUIDData
{
get { if (ID != TokenID.GUID) throw new InvalidOperationException(); return _guidData; }
set { ID = TokenID.GUID; _guidData = value; }
}
private List<int> _integerListData = null;
public List<int> IntegerListData
{
get { if (ID != TokenID.INTEGER_LIST) throw new InvalidOperationException(); return _integerListData; }
set { ID = TokenID.INTEGER_LIST; _integerListData = value; }
}
private List<double> _floatListData = null;
public List<double> FloatListData
{
get { if (ID != TokenID.FLOAT_LIST) throw new InvalidOperationException(); return _floatListData; }
set { ID = TokenID.FLOAT_LIST; _floatListData = value; }
}
public XToken(TokenID id)
{
ID = id;
if (id == TokenID.FLOAT_LIST)
FloatListData = new List<double>();
else if (id == TokenID.INTEGER_LIST)
IntegerListData = new List<int>();
else if (id == TokenID.STRING)
StringTerminator = TokenID.SEMICOLON;
}
public XToken(BinaryReader reader, XHeader header)
{
ID = (TokenID)reader.ReadUInt16();
if (ID == TokenID.NAME)
{
int count = reader.ReadInt32();
NameData = Encoding.ASCII.GetString(reader.ReadBytes(count));
}
else if (ID == TokenID.STRING)
{
int count = reader.ReadInt32();
StringData = Encoding.ASCII.GetString(reader.ReadBytes(count));
StringTerminator = (TokenID)reader.ReadUInt16();
}
else if (ID == TokenID.INTEGER)
{
IntegerData = reader.ReadInt32();
}
else if (ID == TokenID.GUID)
{
GUIDData = new Guid(reader.ReadInt32(), reader.ReadInt16(), reader.ReadInt16(), reader.ReadBytes(8));
}
else if (ID == TokenID.INTEGER_LIST)
{
int count = reader.ReadInt32();
IntegerListData = new List<int>();
for (int i = 0; i < count; i++)
{
IntegerListData.Add(reader.ReadInt32());
}
}
else if (ID == TokenID.FLOAT_LIST)
{
int count = reader.ReadInt32();
FloatListData = new List<double>();
for (int i = 0; i < count; i++)
{
if (header.Precision == 32)
{
FloatListData.Add(reader.ReadSingle());
}
else if (header.Precision == 64)
{
FloatListData.Add(reader.ReadDouble());
}
else
{
throw new ArgumentOutOfRangeException("XToken: Header's precision was not 32 or 64.");
}
}
}
}
public void Write(BinaryWriter writer, XHeader header)
{
writer.Write((ushort)ID);
if (ID == TokenID.NAME)
{
byte[] bytes = Encoding.ASCII.GetBytes(NameData);
writer.Write((int)bytes.Length);
writer.Write(bytes);
}
else if (ID == TokenID.STRING)
{
byte[] bytes = Encoding.ASCII.GetBytes(StringData);
writer.Write((int)bytes.Length);
writer.Write(bytes);
writer.Write((ushort)StringTerminator);
}
else if (ID == TokenID.INTEGER)
{
writer.Write((int)IntegerData);
}
else if (ID == TokenID.GUID)
{
writer.Write(GUIDData.ToByteArray());
}
else if (ID == TokenID.INTEGER_LIST)
{
writer.Write((int)IntegerListData.Count);
foreach (int i in IntegerListData)
writer.Write((int)i);
}
else if (ID == TokenID.FLOAT_LIST)
{
writer.Write((int)FloatListData.Count);
foreach (double d in FloatListData)
{
if (header.Precision == 64)
writer.Write((double)d);
else if (header.Precision == 32)
writer.Write((float)d);
else
throw new ArgumentOutOfRangeException("XToken: Header's precision was not 32 or 64!");
}
}
}
public override string ToString()
{
string result = ID.ToString();
if (ID == TokenID.NAME)
{
result += ": '" + NameData + "'";
}
else if (ID == TokenID.STRING)
{
result += ": '" + StringData + "'" + StringTerminator.ToString();
}
else if (ID == TokenID.INTEGER)
{
result += ": " + IntegerData;
}
else if (ID == TokenID.GUID)
{
result += ": " + GUIDData.ToString();
}
else if (ID == TokenID.INTEGER_LIST)
{
result += ": {" + String.Join(",", IntegerListData) + "}";
}
else if (ID == TokenID.FLOAT_LIST)
{
result += ": {" + String.Join(",", FloatListData) + "}";
}
else if (ID == TokenID.OBRACE)
{
result = "{";
}
else if (ID == TokenID.CBRACE)
{
result = "}";
}
else if (ID == TokenID.OBRACKET)
{
result = "[";
}
else if (ID == TokenID.CBRACKET)
{
result = "]";
}
return result;
}
}
public class XReader
{
private static Dictionary<string, XTemplate> _nativeTemplates = null;
public static Dictionary<string, XTemplate> NativeTemplates
{
get
{
if (_nativeTemplates == null)
{
_nativeTemplates = new Dictionary<string, XTemplate>();
// Time to init all the builtin templates... ugh
XTemplate animation = new XTemplate("Animation", new Guid("3D82AB4F-62DA-11cf-AB39-0020AF71E433"), new List<XTemplateRestriction>());
_nativeTemplates.Add(animation.Name, animation);
XTemplate animationKey = new XTemplate("AnimationKey", new Guid("10DD46A8-775B-11CF-8F52-0040333594A3"), null);
animationKey.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "keyType"));
animationKey.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "nKeys"));
animationKey.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.NAME) { NameData = "TimedFloatKeys" }, "keys", new List<XToken>(new XToken[] { new XToken(XToken.TokenID.NAME) { NameData = "nKeys" } })));
_nativeTemplates.Add(animationKey.Name, animationKey);
XTemplate animationOptions = new XTemplate("AnimationOptions", new Guid("E2BF56C0-840F-11cf-8F52-0040333594A3"), null);
animationOptions.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "openclosed"));
animationOptions.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "positionquality"));
_nativeTemplates.Add(animationOptions.Name, animationOptions);
XTemplate animationSet = new XTemplate("AnimationSet", new Guid("3D82AB50-62DA-11cf-AB39-0020AF71E433"), new List<XTemplateRestriction>(new XTemplateRestriction[] { new XTemplateRestriction("Animation", animation.GUID) }));
_nativeTemplates.Add(animationSet.Name, animationSet);
XTemplate animTicksPerSecond = new XTemplate("AnimTicksPerSecond", new Guid("9E415A43-7BA6-4a73-8743-B73D47E88476"), null);
animTicksPerSecond.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "AnimTicksPerSecond"));
_nativeTemplates.Add(animTicksPerSecond.Name, animTicksPerSecond);
XTemplate boolean = new XTemplate("Boolean", new Guid("537da6a0-ca37-11d0-941c-0080c80cfa7b"), null);
boolean.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "truefalse"));
_nativeTemplates.Add(boolean.Name, boolean);
XTemplate boolean2d = new XTemplate("Boolean2d", new Guid("4885AE63-78E8-11cf-8F52-0040333594A3"), null);
boolean2d.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.NAME) { NameData = boolean.Name }, "u"));
boolean2d.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.NAME) { NameData = boolean.Name }, "v"));
_nativeTemplates.Add(boolean2d.Name, boolean2d);
XTemplate colorRGB = new XTemplate("ColorRGB", new Guid("D3E16E81-7835-11cf-8F52-0040333594A3"), null);
colorRGB.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.FLOAT), "red"));
colorRGB.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.FLOAT), "green"));
colorRGB.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.FLOAT), "blue"));
_nativeTemplates.Add(colorRGB.Name, colorRGB);
XTemplate colorRGBA = new XTemplate("ColorRGBA", new Guid("35FF44E0-6C7C-11cf-8F52-0040333594A3"), null);
colorRGBA.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.FLOAT), "red"));
colorRGBA.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.FLOAT), "green"));
colorRGBA.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.FLOAT), "blue"));
colorRGBA.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.FLOAT), "alpha"));
_nativeTemplates.Add(colorRGBA.Name, colorRGBA);
//CompressedAnimationSet
XTemplate coords2d = new XTemplate("Coords2d", new Guid("F6F23F44-7686-11cf-8F52-0040333594A3"), null);
coords2d.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.FLOAT), "u"));
coords2d.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.FLOAT), "v"));
_nativeTemplates.Add(coords2d.Name, coords2d);
// DeclData
// EffectDWord
// EffectFloats
// EffectInstance
// EffectParamDWord
// EffectParamFloats
// EffectParamString
// EffectString
// FaceAdjecency
// FloatKeys
XTemplate frame = new XTemplate("Frame", new Guid("3D82AB46-62DA-11CF-AB39-0020AF71E433"), new List<XTemplateRestriction>());
_nativeTemplates.Add(frame.Name, frame);
XTemplate frameTransformMatrix = new XTemplate("FrameTransformMatrix", new Guid("F6F23F41-7686-11cf-8F52-0040333594A3"), null);
frameTransformMatrix.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.NAME) { NameData = "Matrix4x4" }, "frameMatrix"));
_nativeTemplates.Add(frameTransformMatrix.Name, frameTransformMatrix);
// FVFData
// Guid
// IndexedColor
XTemplate indexedColor = new XTemplate("IndexedColor", new Guid("1630B820-7842-11cf-8F52-0040333594A3"), null);
indexedColor.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "index"));
indexedColor.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.NAME) { NameData = "ColorRGBA" }));
_nativeTemplates.Add(indexedColor.Name, indexedColor);
XTemplate material = new XTemplate("Material", new Guid("3D82AB4D-62DA-11CF-AB39-0020AF71E433"), new List<XTemplateRestriction>());
material.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.NAME) { NameData = "ColorRGBA" }, "faceColor"));
material.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.FLOAT), "power"));
material.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.NAME) { NameData = "ColorRGB" }, "specularColor"));
material.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.NAME) { NameData = "ColorRGB" }, "emissiveColor"));
_nativeTemplates.Add(material.Name, material);
// MaterialWrap
XTemplate matrix4x4 = new XTemplate("Matrix4x4", new Guid("F6F23F45-7686-11cf-8F52-0040333594A3"), null);
matrix4x4.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.FLOAT), "matrix", new List<XToken>() { new XToken(XToken.TokenID.INTEGER) { IntegerData = 16 } }));
_nativeTemplates.Add(matrix4x4.Name, matrix4x4);
XTemplate mesh = new XTemplate("Mesh", new Guid("3D82AB44-62DA-11CF-AB39-0020AF71E433"), new List<XTemplateRestriction>());
mesh.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "nVertices"));
mesh.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.NAME) { NameData = "Vector" }, "vertices", new List<XToken>() { new XToken(XToken.TokenID.NAME) { NameData = "nVertices" } }));
mesh.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "nFaces"));
mesh.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.NAME) { NameData = "MeshFace" }, "faces", new List<XToken>() { new XToken(XToken.TokenID.NAME) { NameData = "nFaces" } }));
_nativeTemplates.Add(mesh.Name, mesh);
XTemplate meshFace = new XTemplate("MeshFace", new Guid("3D82AB5F-62DA-11cf-AB39-0020AF71E433"), null);
meshFace.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "nFaceVertexIndices", null));
meshFace.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "faceVertexIndices", new List<XToken>() { new XToken(XToken.TokenID.NAME) { NameData = "nFaceVertexIndices" } }));
_nativeTemplates.Add(meshFace.Name, meshFace);
// MeshFaceWraps
XTemplate meshMaterialList = new XTemplate("MeshMaterialList", new Guid("F6F23F42-7686-11CF-8F52-0040333594A3"), new List<XTemplateRestriction>() { new XTemplateRestriction(material.Name, material.GUID) });
meshMaterialList.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "nMaterials"));
meshMaterialList.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "nFaceIndexes"));
meshMaterialList.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "faceIndexes", new List<XToken>() { new XToken(XToken.TokenID.NAME) { NameData = "nFaceIndexes" } }));
_nativeTemplates.Add(meshMaterialList.Name, meshMaterialList);
XTemplate meshNormals = new XTemplate("MeshNormals", new Guid("F6F23F43-7686-11cf-8F52-0040333594A3"), null);
meshNormals.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "nNormals"));
meshNormals.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.NAME) { NameData = "Vector" }, "normals", new List<XToken>() { new XToken(XToken.TokenID.NAME) { NameData = "nNormals" } }));
meshNormals.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "nFaceNormals"));
meshNormals.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.NAME) { NameData = "MeshFace" }, "faceNormals", new List<XToken>() { new XToken(XToken.TokenID.NAME) { NameData = "nFaceNormals" } }));
_nativeTemplates.Add(meshNormals.Name, meshNormals);
XTemplate meshTextureCoords = new XTemplate("MeshTextureCoords", new Guid("F6F23F40-7686-11cf-8F52-0040333594A3"), null);
meshTextureCoords.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "nTextureCoords"));
meshTextureCoords.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.NAME) { NameData = "Coords2d" }, "textureCoords", new List<XToken>() { new XToken(XToken.TokenID.NAME) { NameData = "nTextureCoords" } }));
_nativeTemplates.Add(meshTextureCoords.Name, meshTextureCoords);
XTemplate meshVertexColors = new XTemplate("MeshVertexColors", new Guid("1630B821-7842-11cf-8F52-0040333594A3"), null);
meshVertexColors.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "nVertexColors"));
meshVertexColors.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.NAME) { NameData = "IndexedColor" }, "vertexColors", new List<XToken>() { new XToken(XToken.TokenID.NAME) { NameData = "nVertexColors" } }));
_nativeTemplates.Add(meshVertexColors.Name, meshVertexColors);
// Patch
// PatchMesh
// PatchMesh9
// PMAttributeRange
// PMInfo
// PMVSplitRecord
// SkinWeights
XTemplate skinWeights = new XTemplate("SkinWeights", new Guid("6F0D123B-BAD2-4167-A0D0-80224F25FABB"), null);
skinWeights.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.LPSTR), "transformNodeName"));
skinWeights.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "nWeights"));
skinWeights.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "vertexIndices", new List<XToken>() { new XToken(XToken.TokenID.NAME) { NameData = "nWeights" } }));
skinWeights.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.FLOAT), "weights", new List<XToken>() { new XToken(XToken.TokenID.NAME) { NameData = "nWeights" } }));
skinWeights.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.NAME) { NameData = "Matrix4x4" }, "matrixOffset"));
_nativeTemplates.Add(skinWeights.Name, skinWeights);
XTemplate textureFilename = new XTemplate("TextureFilename", new Guid("A42790E1-7810-11cf-8F52-0040333594A3"), null);
textureFilename.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.LPSTR), "filename"));
_nativeTemplates.Add(textureFilename.Name, textureFilename);
// TimedFloatKeys
XTemplate vector = new XTemplate("Vector", new Guid("3D82AB5E-62DA-11cf-AB39-0020AF71E433"), null);
vector.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.FLOAT), "x"));
vector.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.FLOAT), "y"));
vector.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.FLOAT), "z"));
_nativeTemplates.Add(vector.Name, vector);
XTemplate vertexDuplicationIndices = new XTemplate("VertexDuplicationIndices", new Guid("B8D65549-D7C9-4995-89CF-53A9A8B031E3"), null);
vertexDuplicationIndices.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "nIndices"));
vertexDuplicationIndices.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "nOriginalVertices"));
vertexDuplicationIndices.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "indices", new List<XToken>() { new XToken(XToken.TokenID.NAME) { NameData = "nIndices" } }));
_nativeTemplates.Add(vertexDuplicationIndices.Name, vertexDuplicationIndices);
XTemplate vertexElement = new XTemplate("VertexElement", new Guid("F752461C-1E23-48f6-B9F8-8350850F336F"), null);
vertexElement.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "Type"));
vertexElement.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "Method"));
vertexElement.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "Usage"));
vertexElement.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.DWORD), "UsageIndex"));
_nativeTemplates.Add(vertexElement.Name, vertexElement);
XTemplate xSkinMeshHeader = new XTemplate("XSkinMeshHeader", new Guid("3CF169CE-FF7C-44ab-93C0-F78F62D172E2"), null);
xSkinMeshHeader.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.WORD), "nMaxSkinWeightsPerVertex"));
xSkinMeshHeader.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.WORD), "nMaxSkinWeightsPerFace"));
xSkinMeshHeader.Members.Add(new XTemplateMember(new XToken(XToken.TokenID.WORD), "nBones"));
_nativeTemplates.Add(xSkinMeshHeader.Name, xSkinMeshHeader);
}
return _nativeTemplates;
}
}
public List<XToken> Tokens { get; } = new List<XToken>();
public List<XTemplate> Templates { get; } = new List<XTemplate>();
public List<XObject> Objects { get; } = new List<XObject>();
public int Position { get; set; } = 0;
public bool EndOfStream { get => Position >= Tokens.Count; }
public XReader(BinaryReader reader, XHeader header, List<XTemplate> templates, List<XObject> objects)
{
Templates = templates;
Objects = objects;
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
Tokens.Add(new XToken(reader, header));
}
}
public XReader(List<XToken> tokens, List<XTemplate> templates, List<XObject> objects)
{
Tokens = tokens;
Templates = templates;
Objects = objects;
}
public XToken PeekToken()
{
if (Position < Tokens.Count)
return Tokens[Position];
return null;
}
public XToken ReadToken()
{
if (Position < Tokens.Count)
{
return Tokens[Position++];
}
return null;
}
public XTemplate FindTemplate(string name)
{
foreach (XTemplate template in Templates)
if (template.Name == name)
return template;
if (NativeTemplates.ContainsKey(name))
return NativeTemplates[name];
return null;
}
public XTemplate FindTemplate(Guid guid)
{
foreach (XTemplate template in Templates)
if (template.GUID == guid)
return template;
foreach (XTemplate template in NativeTemplates.Values)
if (template.GUID == guid)
return template;
return null;
}
public XObject FindObject(string name)
{
foreach (XObject obj in Objects)
if (obj.Name == name)
return obj;
return null;
}
public XObject FindObject(string name, Guid classGUID)
{
if (classGUID == Guid.Empty)
return FindObject(name);
foreach (XObject obj in Objects)
{
if (obj.Name == name)
{
if (obj.DataType.ID == XToken.TokenID.NAME)
{
if (FindTemplate(obj.DataType.NameData)?.GUID == classGUID)
return obj;
}
}
}
return null;
}
}
public class XWriter
{
public List<XToken> Tokens { get; } = new List<XToken>();
public void Write(XToken token)
{
Tokens.Add(token);
}
}
/// <summary>
/// Allows a specified type as a child of an instance of the owning <see cref="XTemplate"/>.
/// </summary>
public class XTemplateRestriction
{
public string Name = null;
public Guid GUID = Guid.Empty;
public XTemplateRestriction(string name, Guid guid)
{
Name = name;
GUID = guid;
}
public XTemplateRestriction(XReader reader)
{
if (reader.PeekToken().ID != XToken.TokenID.NAME)
throw new FormatException("XTemplateOption: template_option_part must start with a NAME token!");
Name = reader.ReadToken().NameData;
if (reader.PeekToken().ID == XToken.TokenID.GUID)
GUID = reader.ReadToken().GUIDData;
}
public void Write(XWriter writer)
{
writer.Write(new XToken(XToken.TokenID.NAME) { NameData = Name });
if (GUID != Guid.Empty)
writer.Write(new XToken(XToken.TokenID.GUID) { GUIDData = GUID });
}
public override string ToString()
{
return "'" + Name + "' (" + GUID.ToString() + ")";
}
}
public class XTemplateMember
{
public XToken DataType { get; } = null;
public string Name { get; } = null;
public List<XToken> Dimensions { get; } = null;
public XTemplateMember(XToken dataType, string name = null, List<XToken> dimensions = null)
{
DataType = dataType;
Name = name;
Dimensions = dimensions;
if (dimensions?.Count == 0)
throw new ArgumentException("If 'dimensions' is not null, it must contain at least one dimension.");
}
public XTemplateMember(XReader reader)
{
bool isArray = reader.PeekToken().ID == XToken.TokenID.ARRAY;
if (isArray)
{
reader.ReadToken();
Dimensions = new List<XToken>();
}
DataType = reader.ReadToken();
// Read name if it's there
if (reader.PeekToken().ID == XToken.TokenID.NAME)
{
Name = reader.ReadToken().NameData;
}
else if (isArray)
{
throw new FormatException("XTemplateMember: array members must have a NAME token!");
}
// Read dimensions
while (isArray && reader.PeekToken().ID == XToken.TokenID.OBRACKET)
{
reader.ReadToken(); // Open bracket
Dimensions.Add(reader.ReadToken()); // Name or Integer
if (reader.ReadToken().ID != XToken.TokenID.CBRACKET)
throw new FormatException("XTemplateMember: dimension instances must have a CBRACE token after the dimension!");
}
if (reader.ReadToken().ID != XToken.TokenID.SEMICOLON)
throw new FormatException("XTemplateMember: array, primitive, or template_reference must end with a SEMICOLON token!");
}
public void Write(XWriter writer)
{
bool isArray = Dimensions != null;
if (isArray)
writer.Write(new XToken(XToken.TokenID.ARRAY));
writer.Write(DataType);
if (Name != null)
writer.Write(new XToken(XToken.TokenID.NAME) { NameData = Name });
if (isArray)
{
foreach (XToken token in Dimensions)
{
writer.Write(new XToken(XToken.TokenID.OBRACKET));
writer.Write(token);
writer.Write(new XToken(XToken.TokenID.CBRACKET));
}
}
writer.Write(new XToken(XToken.TokenID.SEMICOLON));
}
public XObjectMember ReadMember(XTemplate template, List<XObjectMember> previousMembers, XReader reader, XObjectReader objReader)
{
XObjectMember result = new XObjectMember(Name, DataType);
int totalItems = 1;
if (Dimensions != null)
{
foreach (XToken token in Dimensions)
{
if (token.ID == XToken.TokenID.INTEGER)
{
totalItems *= token.IntegerData;
}
else if (token.ID == XToken.TokenID.NAME)
{
int? dimensionSize = null;
foreach (XTemplateMember member in template.Members)
{
if (member.Name == token.NameData)
{
foreach (XObjectMember m in previousMembers)
{
if (m.Name == member.Name)
{
dimensionSize = m.Values[0] as int?;
if (dimensionSize == null)
throw new FormatException("XTemplateMember: Member listed as dimension must hold integers!");
break;
}
}
break;
}
}
if (dimensionSize == null)
throw new FormatException("XTemplateMember: Couldn't find the member listed as a dimension!");
else
totalItems *= dimensionSize.Value;
}
}
}
for (int i = 0; i < totalItems; i++)
{
if (DataType.ID == XToken.TokenID.NAME)
{
// Read template instance member
XTemplate structType = reader.FindTemplate(DataType.NameData);
if (structType == null)
throw new FormatException("XTemplateMember: Can't find template '" + DataType.NameData + "'!");
XObjectStructure structure = new XObjectStructure(structType, previousMembers, reader, objReader);
result.Values.Add(structure);
}
else if (DataType.ID == XToken.TokenID.WORD || DataType.ID == XToken.TokenID.DWORD
|| DataType.ID == XToken.TokenID.CHAR || DataType.ID == XToken.TokenID.UCHAR
|| DataType.ID == XToken.TokenID.SWORD || DataType.ID == XToken.TokenID.SDWORD)
{
// Read integer member
if (!objReader.HasInteger())
throw new FormatException("XTemplateMember: Member data should be an integer!");
result.Values.Add(objReader.Read());
}
else if (DataType.ID == XToken.TokenID.FLOAT || DataType.ID == XToken.TokenID.DOUBLE)
{
// Read float member
if (!objReader.HasFloat())
throw new FormatException("XTemplateMember: Member data should be a float!");
result.Values.Add(objReader.Read());
}
else if (DataType.ID == XToken.TokenID.LPSTR)
{
// Read string member
if (!objReader.HasString())
throw new FormatException("XTemplateMember: Member data should be a string!");
result.Values.Add(objReader.Read());
}
else
{
throw new FormatException("XTemplateMember: token " + DataType.ID.ToString() + " is not a valid member type!");
}
}
return result;
}
public override string ToString()
{
string result = DataType.ToString();
if (Name != null)
result += " " + Name;
if (Dimensions != null)
{
foreach (XToken dimension in Dimensions)
{
result += "[" + dimension.ToString() + "]";
}
}
return result + ";";
}
}
public class XTemplate
{
public enum TemplateRestrictionState
{
Closed = 0,
Restricted = 1,
Open = 2,
}
public string Name { get; } = "";
public Guid GUID { get; } = Guid.Empty;
public TemplateRestrictionState RestrictionState { get; } = TemplateRestrictionState.Closed;
public List<XTemplateRestriction> Restrictions { get; } = new List<XTemplateRestriction>();
public List<XTemplateMember> Members = new List<XTemplateMember>();
public XTemplate(string name, Guid guid, List<XTemplateRestriction> restrictions = null)
{
Name = name;
GUID = guid;
Restrictions = restrictions;
if (Restrictions == null)
{
RestrictionState = TemplateRestrictionState.Closed;
}
else if (Restrictions.Count == 0)
{
RestrictionState = TemplateRestrictionState.Open;
}
else
{
RestrictionState = TemplateRestrictionState.Restricted;
}
}
public XTemplate(XReader reader)
{
if (reader.ReadToken().ID != XToken.TokenID.TEMPLATE)
throw new FormatException("Template must start with TEMPLATE token!");
// Name
XToken name = reader.ReadToken();
if (name.ID != XToken.TokenID.NAME)
throw new FormatException("Template name must be a NAME token!");
Name = name.NameData;
if (reader.ReadToken().ID != XToken.TokenID.OBRACE)
throw new FormatException("Expected OBRACE in template");
// GUID
XToken guid = reader.ReadToken();
if (guid.ID != XToken.TokenID.GUID)
throw new Exception("Template GUID must be a GUID token!");
GUID = guid.GUIDData;
// Read members_list until closing brace of template or open brace of options
while (reader.PeekToken().ID != XToken.TokenID.CBRACE && reader.PeekToken().ID != XToken.TokenID.OBRACE)
{
// Read a member
Members.Add(new XTemplateMember(reader));
}
// Read template_option_info
if (reader.PeekToken().ID == XToken.TokenID.OBRACE)
{
if (reader.PeekToken().ID == XToken.TokenID.DOT)
{
// Confirm ellipsis
if (reader.ReadToken().ID != XToken.TokenID.DOT)
throw new FormatException("Ellipses have more than one dots...");
if (reader.ReadToken().ID != XToken.TokenID.DOT)
throw new FormatException("Ellipses have more than two dots...");
RestrictionState = TemplateRestrictionState.Open;
if (reader.ReadToken().ID != XToken.TokenID.CBRACE)
throw new FormatException("XTemplate: Restriction ellipses must end with a CBRACE token!");
}
else
{
// Read template_option_list
while (reader.PeekToken().ID != XToken.TokenID.CBRACE)
{
Restrictions.Add(new XTemplateRestriction(reader));
}
RestrictionState = TemplateRestrictionState.Restricted;
}
}
if (reader.ReadToken().ID != XToken.TokenID.CBRACE)
throw new FormatException("XTemplate: template must end with CBRACE token!");
}
public void Write(XWriter writer)
{
writer.Write(new XToken(XToken.TokenID.TEMPLATE));
writer.Write(new XToken(XToken.TokenID.NAME) { NameData = Name });
writer.Write(new XToken(XToken.TokenID.OBRACE));
writer.Write(new XToken(XToken.TokenID.GUID) { GUIDData = GUID });
foreach (XTemplateMember member in Members)
{
member.Write(writer);
}
if (RestrictionState != TemplateRestrictionState.Closed)
{
writer.Write(new XToken(XToken.TokenID.OBRACE));
if (RestrictionState == TemplateRestrictionState.Open)
{
writer.Write(new XToken(XToken.TokenID.DOT));
writer.Write(new XToken(XToken.TokenID.DOT));
writer.Write(new XToken(XToken.TokenID.DOT));
}
else if (RestrictionState == TemplateRestrictionState.Restricted)
{
foreach (XTemplateRestriction restriction in Restrictions)
restriction.Write(writer);