Skip to content

Commit 3bcb9bc

Browse files
committed
Fix reading of XOBJ files
1 parent 205ef1f commit 3bcb9bc

File tree

3 files changed

+140
-14
lines changed

3 files changed

+140
-14
lines changed

Assets/Scripts/OpenTS2/Files/Formats/DBPF/SimsObjectCodec.cs

+137-13
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public override AbstractAsset Deserialize(byte[] bytes, ResourceKey tgi, DBPFFil
6262

6363
Debug.Log($" Data array offset: {reader.Position:X}");
6464

65-
/*
6665
// 8 unknown shorts called "data".
6766
var dataArray = new short[8];
6867
for (var i = 0; i < dataArray.Length; i++)
@@ -72,7 +71,7 @@ public override AbstractAsset Deserialize(byte[] bytes, ResourceKey tgi, DBPFFil
7271
Debug.Log($"dataArray: [{string.Join(", ", dataArray)}]");
7372

7473
// Next is a number of shorts that depends on the exact version of the file.
75-
uint numShorts = 0x57 + 6;
74+
uint numShorts = 0x58;
7675
for (var i = 0; i < numShorts; i++)
7776
{
7877
reader.ReadInt16();
@@ -84,6 +83,17 @@ public override AbstractAsset Deserialize(byte[] bytes, ResourceKey tgi, DBPFFil
8483
reader.ReadInt16();
8584
}
8685

86+
// Inventory token.
87+
var tokenGUID = reader.ReadUInt32();
88+
var tokenFlags = reader.ReadUInt16();
89+
var numTokenProperties = reader.ReadUInt32();
90+
for (var i = 0; i < numTokenProperties; i++)
91+
{
92+
reader.ReadUInt16();
93+
}
94+
Debug.Log($"InventoryToken(tokenGUID={tokenGUID}, tokenFlags={tokenFlags}, numTokenProps={numTokenProperties})");
95+
96+
8797
// Next is the number of object arrays. Each being a short array itself.
8898
var numObjectArrays = reader.ReadInt16();
8999
var shortArrays = new List<short[]>(numObjectArrays);
@@ -111,9 +121,9 @@ public override AbstractAsset Deserialize(byte[] bytes, ResourceKey tgi, DBPFFil
111121

112122
Debug.Log($" Position before strings: 0x{reader.Position:X}");
113123
// A number of material subsitution strings.
114-
var numMaterialSubstitues = reader.ReadInt16();
115-
Debug.Log($" numMaterialSubstitues: {numMaterialSubstitues}");
116-
for (var i = 0; i < numMaterialSubstitues; i++)
124+
var materialSubstitutes = reader.ReadInt16();
125+
Debug.Log($" numMaterialSubstitues: {materialSubstitutes}");
126+
for (var i = 0; i < materialSubstitutes; i++)
117127
{
118128
var materialSubstitute = reader.ReadVariableLengthPascalString();
119129
Debug.Log($"materialSubstitute: {materialSubstitute}");
@@ -122,21 +132,135 @@ public override AbstractAsset Deserialize(byte[] bytes, ResourceKey tgi, DBPFFil
122132
var persistentFlag = reader.ReadUInt16();
123133
Debug.Log($"persistentFlag: {persistentFlag}");
124134

135+
// Read the cTSTreeStack, a set of cTreeStackElems, probably the edith execution stack?
136+
var numStackFrames = reader.ReadInt32();
137+
Debug.Log($" numStackFrames: {numStackFrames}");
138+
reader.ReadUInt32(); // unknown
139+
for (var i = 0; i < numStackFrames; i++)
140+
{
141+
var objectID = reader.ReadUInt16();
142+
var treeID = reader.ReadUInt16();
143+
var nodeNum = reader.ReadUInt16();
144+
145+
var numLocals = reader.ReadByte();
146+
Debug.Log($"- numLocals: {numLocals}");
147+
148+
var numParams = reader.ReadByte();
149+
150+
var runningObjId = reader.ReadUInt16();
151+
var runningOnObjId = reader.ReadUInt16();
152+
153+
var frameParams = new short[numParams];
154+
for (var j = 0; j < frameParams.Length; j++)
155+
{
156+
frameParams[j] = reader.ReadInt16();
157+
}
158+
159+
var locals = new short[numLocals];
160+
for (var j = 0; j < locals.Length; j++)
161+
{
162+
locals[j] = reader.ReadInt16();
163+
}
164+
165+
var primState = reader.ReadInt32();
166+
167+
// next part is related to loading the cITSBehavior
168+
var behavSaveType = reader.ReadUInt16();
169+
170+
Debug.Log($"- objectID: {objectID}, bhav: {behavSaveType}, runningObjID: {runningObjId}, runningOnObjID: {runningOnObjId}");
171+
Debug.Log($" locals: {string.Join(", ", locals)}");
172+
}
173+
174+
Debug.Log($"[P] Position before cTSRelationshipTable: 0x{reader.Position:X}");
175+
// Read the cTSRelationshipTable
176+
var relationshipTableFlag = reader.ReadInt32();
177+
Debug.Log($" relationshipTableFlag: {relationshipTableFlag}");
178+
if (relationshipTableFlag < 0)
179+
{
180+
var relationShipCount = reader.ReadInt32();
181+
Debug.Log($" relationShipCount: {relationShipCount}");
182+
for (var i = 0; i < relationShipCount; i++)
183+
{
184+
var isPresent = reader.ReadInt32();
185+
if (isPresent == 0)
186+
{
187+
continue;
188+
}
189+
190+
var relationInstanceId = reader.ReadUInt32();
191+
192+
// TODO: read the entry here
193+
var relationEntryCount = reader.ReadInt32();
194+
for (var j = 0; j < relationEntryCount; j++)
195+
{
196+
var entry = reader.ReadUInt32();
197+
}
198+
}
199+
} else if (relationshipTableFlag - 2 <= 1)
200+
{
201+
var relationShipCount = reader.ReadInt32();
202+
Debug.Log($" relationShipCount: {relationShipCount}");
203+
for (var i = 0; i < relationShipCount; i++)
204+
{
205+
var relationEntryId = reader.ReadUInt32();
206+
}
207+
}
208+
125209
// Slots...
126210
var slotsFlag = reader.ReadInt16();
127211
Debug.Log($"slotsFlag: {slotsFlag}");
212+
if (slotsFlag < 0)
213+
{
214+
if (slotsFlag == -100)
215+
{
216+
// Unknown short.
217+
reader.ReadUInt16();
218+
}
128219

129-
var numSlots = reader.ReadInt16();
130-
for (var i = 0; i < numSlots; i++)
220+
var numSlots = reader.ReadInt16();
221+
for (var i = 0; i < numSlots; i++)
222+
{
223+
var slotValue = reader.ReadInt16();
224+
}
225+
Debug.Log($"numSlots: {numSlots}");
226+
}
227+
else
131228
{
132-
var slotValue = reader.ReadInt16();
229+
if (slotsFlag > 0)
230+
{
231+
// Two unknown shorts.
232+
reader.ReadUInt16();
233+
reader.ReadUInt16();
234+
}
235+
236+
for (var i = 0; i < slotsFlag - 1; i++)
237+
{
238+
// Two shorts per flag.
239+
reader.ReadInt16();
240+
reader.ReadInt16();
241+
}
133242
}
134-
Debug.Log($"numSlots: {numSlots}");
135243

136-
var numEffects = reader.ReadInt16();
137-
Debug.Log($"numEffects: {numEffects}");
244+
Debug.Log($"Position before readEffects: 0x{reader.Position:X}");
138245

139-
Debug.Log($"Position after numEffects: 0x{reader.Position:X}");
246+
var effectsFlag = reader.ReadInt16();
247+
Debug.Log($"effectsFlag: {effectsFlag}");
248+
if (effectsFlag != 0)
249+
{
250+
var hasEffects = reader.ReadUInt16() == 1;
251+
Debug.Log($"hasEffects: {hasEffects}");
252+
if (hasEffects)
253+
{
254+
var effectCount = reader.ReadUInt32();
255+
for (var i = 0; i < effectCount; i++)
256+
{
257+
reader.ReadVariableLengthPascalString();
258+
reader.ReadVariableLengthPascalString();
259+
reader.ReadUInt32();
260+
reader.ReadUInt32();
261+
}
262+
}
263+
}
140264

141265
var numbOverrides = reader.ReadInt16();
142266
Debug.Log($"numOverides: {numbOverrides}");
@@ -147,7 +271,7 @@ public override AbstractAsset Deserialize(byte[] bytes, ResourceKey tgi, DBPFFil
147271
var overrideString2 = reader.ReadVariableLengthPascalString();
148272
var overrideString3 = reader.ReadVariableLengthPascalString();
149273
Debug.Log($"{overrideString1} / {overrideString2} / {overrideString3}");
150-
}*/
274+
}
151275

152276
return asset;
153277
}

Assets/Tests/OpenTS2/Files/Formats/DBPF/SimsObjectCodecTest.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public void SetUp()
1919
[Test]
2020
public void TestSuccessfullyLoadsSimsObject()
2121
{
22-
var objectAsset = ContentManager.Instance.GetAsset<SimsObjectAsset>(new ResourceKey(0x158, _groupID, TypeIDs.XOBJ));
22+
// Pancake object.
23+
var objectAsset = ContentManager.Instance.GetAsset<SimsObjectAsset>(
24+
new ResourceKey(163, _groupID, TypeIDs.XOBJ));
2325

2426
Assert.That(objectAsset, Is.Not.Null);
2527
}

TestAssets/Codecs/ObjCodecs.package

538 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)