@@ -9,6 +9,7 @@ namespace MorcuTool
99 public class MaterialData
1010 {
1111 public string filename ;
12+
1213
1314 public List < Param > parameters = new List < Param > ( ) ;
1415 public enum MaterialParameter : uint {
@@ -37,12 +38,22 @@ public class Param {
3738 public Subfile diffuse_texture ;
3839
3940
40- public Param ( byte [ ] bytes , int pos ) {
41+ public Param ( byte [ ] bytes , int pos , bool useBigEndian ) {
4142
42- paramType = ( MaterialParameter ) Utility . ReadInt32BigEndian ( bytes , pos ) ; pos += 4 ;
43- unk = Utility . ReadInt32BigEndian ( bytes , pos ) ; pos += 4 ;
44- dataSizeDividedBy4 = Utility . ReadInt32BigEndian ( bytes , pos ) ; pos += 4 ;
45- dataOffset = Utility . ReadInt32BigEndian ( bytes , pos ) ;
43+ if ( useBigEndian )
44+ {
45+ paramType = ( MaterialParameter ) Utility . ReadInt32BigEndian ( bytes , pos ) ; pos += 4 ;
46+ unk = Utility . ReadInt32BigEndian ( bytes , pos ) ; pos += 4 ;
47+ dataSizeDividedBy4 = Utility . ReadInt32BigEndian ( bytes , pos ) ; pos += 4 ;
48+ dataOffset = Utility . ReadInt32BigEndian ( bytes , pos ) ;
49+ }
50+ else {
51+ paramType = ( MaterialParameter ) BitConverter . ToInt32 ( bytes , pos ) ; pos += 4 ;
52+ unk = BitConverter . ToInt32 ( bytes , pos ) ; pos += 4 ;
53+ dataSizeDividedBy4 = BitConverter . ToInt32 ( bytes , pos ) ; pos += 4 ;
54+ dataOffset = BitConverter . ToInt32 ( bytes , pos ) ;
55+ }
56+
4657 }
4758 }
4859
@@ -51,21 +62,44 @@ public MaterialData(Subfile basis)
5162 parameters = new List < Param > ( ) ;
5263 filename = basis . filename ;
5364
54- int pos = 0x10 ;
65+ int pos = 0x04 ;
66+
67+ bool useBigEndian = false ;
68+
69+ if ( Utility . ReadInt32BigEndian ( basis . filebytes , pos ) == 0x01 )
70+ {
71+ //msk and msa; subsequent data is big-endian
72+ pos = 0x10 ;
73+ useBigEndian = true ;
74+ }
75+ else {
76+ //mysims; subsequent data is little-endian
77+ pos = 0x40 ; //while it doesn't look like it, MySims MATD files start with 0x2C of extra data, even before the MATD magic. So in total there are 0x40 bytes before the actual data start (including 4 extra ones just after the MATD magic)
78+ useBigEndian = false ;
79+ }
5580
5681 // a lot of the offsets are measured from 0x10
5782
5883 //now read the parameters!
5984
6085 pos += 0x08 ;
86+ int lengthOfDataSection ;
87+ int numberOfParams ;
6188
62- int lengthOfDataSection = Utility . ReadInt32BigEndian ( basis . filebytes , pos ) ; pos += 4 ;
63- int numberOfParams = Utility . ReadInt32BigEndian ( basis . filebytes , pos ) ; pos += 4 ;
89+ if ( useBigEndian )
90+ {
91+ lengthOfDataSection = Utility . ReadInt32BigEndian ( basis . filebytes , pos ) ; pos += 4 ;
92+ numberOfParams = Utility . ReadInt32BigEndian ( basis . filebytes , pos ) ; pos += 4 ;
93+ }
94+ else {
95+ lengthOfDataSection = BitConverter . ToInt32 ( basis . filebytes , pos ) ; pos += 4 ;
96+ numberOfParams = BitConverter . ToInt32 ( basis . filebytes , pos ) ; pos += 4 ;
97+ }
6498
6599 //now the param list begins. Each param has 0x10 bytes describing it, with an offset to later in the file (falling within the data section, but measured from 0x10)
66100
67101 for ( int i = 0 ; i < numberOfParams ; i ++ ) {
68- parameters . Add ( new Param ( basis . filebytes , pos ) ) ;
102+ parameters . Add ( new Param ( basis . filebytes , pos , useBigEndian ) ) ;
69103 pos += 0x10 ;
70104 }
71105
@@ -77,13 +111,20 @@ public MaterialData(Subfile basis)
77111
78112 switch ( parameter . paramType ) {
79113 case MaterialParameter . diffuseMap :
80- ulong hash_of_diffuse_texture = ( ulong ) Utility . ReadUInt32BigEndian ( basis . filebytes , pos ) ; pos += 4 ;
81- hash_of_diffuse_texture |= ( ( ulong ) Utility . ReadUInt32BigEndian ( basis . filebytes , pos ) ) << 32 ; pos += 4 ;
82- if ( hash_of_diffuse_texture == 0x58CC31A1AC11E901 ) {
83- Console . WriteLine ( "Here it is!!" ) ;
114+ ulong hash_of_diffuse_texture ;
115+ uint typeID_of_diffuse_texture ;
116+
117+ if ( useBigEndian )
118+ {
119+ hash_of_diffuse_texture = ( ulong ) Utility . ReadUInt32BigEndian ( basis . filebytes , pos ) ; pos += 4 ;
120+ hash_of_diffuse_texture |= ( ( ulong ) Utility . ReadUInt32BigEndian ( basis . filebytes , pos ) ) << 32 ; pos += 4 ;
121+ typeID_of_diffuse_texture = Utility . ReadUInt32BigEndian ( basis . filebytes , pos ) ; pos += 4 ;
122+ }
123+ else {
124+ hash_of_diffuse_texture = ( ulong ) BitConverter . ToUInt32 ( basis . filebytes , pos ) ; pos += 4 ;
125+ hash_of_diffuse_texture |= ( ( ulong ) BitConverter . ToUInt32 ( basis . filebytes , pos ) ) << 32 ; pos += 4 ;
126+ typeID_of_diffuse_texture = BitConverter . ToUInt32 ( basis . filebytes , pos ) ; pos += 4 ;
84127 }
85-
86- uint typeID_of_diffuse_texture = Utility . ReadUInt32BigEndian ( basis . filebytes , pos ) ; pos += 4 ;
87128
88129 parameter . diffuse_texture = global . activePackage . FindFileByHashAndTypeID ( hash_of_diffuse_texture , typeID_of_diffuse_texture ) ;
89130 break ;
0 commit comments