Skip to content

Commit

Permalink
Try to fix which object state we load
Browse files Browse the repository at this point in the history
  • Loading branch information
ammaraskar committed Oct 20, 2024
1 parent 265662f commit 4e6596a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Assets/Scenes/Tests/LotObjectSimulationTest.unity

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Assets/Scripts/OpenTS2/Content/DBPF/ObjectModuleAsset.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
using System.Collections.Generic;

namespace OpenTS2.Content.DBPF
{
/// <summary>
/// Called cEdithObjectModule in game.
/// </summary>
public class ObjectModuleAsset : AbstractAsset
{
public ObjectModuleAsset(int version)
public ObjectModuleAsset(int version, Dictionary<int, int> objectIdToSaveType)
{
Version = version;
ObjectIdToSaveType = objectIdToSaveType;
}

public int Version { get; }

public Dictionary<int, int> ObjectIdToSaveType { get; }
}
}
26 changes: 17 additions & 9 deletions Assets/Scripts/OpenTS2/Engine/Tests/LotObjectSimulationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ private void LoadLot(string neighborhoodPrefix, int id)
}

var lotPackage = contentManager.AddPackage(lotFullPath);

// Get the version of objects and their IDs from the Edith Object module file OBJM.
var objectModule =
lotPackage.GetAssetByTGI<ObjectModuleAsset>(new ResourceKey(instanceID: 1, groupID: GroupIDs.Local,
TypeIDs.OBJM));
foreach (var key in objectModule.ObjectIdToSaveType.Keys)
{
Debug.Log($"item id: {key}");
}
Debug.Log($"ItemIndex: {ItemIndex}");
var objectToLoadSaveType = objectModule.ObjectIdToSaveType[ItemIndex];

// Get the lot's OBJT / object save type table.
var saveTable =
lotPackage.GetAssetByTGI<ObjectSaveTypeTableAsset>(new ResourceKey(instanceID: 0, GroupIDs.Local,
Expand All @@ -70,24 +82,20 @@ private void LoadLot(string neighborhoodPrefix, int id)
}

saveTypeToGuid[selector.saveType] = selector.objectGuid;

Debug.Log($"{index}: saveType: {selector.saveType} resource name: {selector.catalogResourceName}, Obj name: {def.FileName}");
//Debug.Log($"{index}: saveType: {selector.saveType} resource name: {selector.catalogResourceName}, Obj name: {def.FileName}");
}

var objectToLoad = saveTable.Selectors[ItemIndex];
Debug.Log($"Loading object {objectToLoad.catalogResourceName} with guid {objectToLoad.objectGuid:X}");
Debug.Log($"ItemIndex: {ItemIndex}, saveType: {objectToLoadSaveType}");
var objectToLoad = saveTable.Selectors[objectToLoadSaveType];

var objectDefinition = ObjectManager.Instance.GetObjectByGUID(objectToLoad.objectGuid);
Debug.Assert(objectDefinition != null, "Could not find objd.");

// Get the version of objects from the Edith Object module file OBJM.
var objectModule =
lotPackage.GetAssetByTGI<ObjectModuleAsset>(new ResourceKey(instanceID: 1, groupID: GroupIDs.Local,
TypeIDs.OBJM));
Debug.Log($"Loading object {objectToLoad.catalogResourceName} with guid {objectToLoad.objectGuid:X} group: {objectDefinition.GlobalTGI.GroupID:X}");

// Now load the state of the object.
var objectStateBytes = lotPackage.GetBytesByTGI(
new ResourceKey(instanceID: (uint)objectToLoad.saveType, GroupIDs.Local, TypeIDs.XOBJ));
new ResourceKey(instanceID: (uint)ItemIndex, GroupIDs.Local, TypeIDs.XOBJ));
var objectState = SimsObjectCodec.DeserializeFromBytesAndVersion(objectStateBytes, objectModule.Version);

// Create an entity for the object.
Expand Down
16 changes: 8 additions & 8 deletions Assets/Scripts/OpenTS2/Files/Formats/DBPF/ObjectModuleCodec.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using OpenTS2.Common;
using OpenTS2.Content;
Expand Down Expand Up @@ -35,19 +36,18 @@ public override AbstractAsset Deserialize(byte[] bytes, ResourceKey tgi, DBPFFil
throw new NotImplementedException("ObjM file does not have `ObjM` magic bytes");
}

var objectIdToSaveType = new Dictionary<int, int>();
// Next is the number of objects.
int numObjects = reader.ReadInt32();
Debug.Log($"numObjects: {numObjects}");
var numObjects = reader.ReadInt32();
for (var i = 0; i < numObjects; i++)
{
int selectorSaveType = reader.ReadInt32();
int missingObjectSaveType = reader.ReadInt32();

Debug.Log($"selectorSaveType: {selectorSaveType}, missingObjectSaveType: {missingObjectSaveType}");
break;
// Data attribute 0x13, might be object id.
int dataAttr = reader.ReadInt32();
int objectSaveType = reader.ReadInt32();
objectIdToSaveType[dataAttr] = objectSaveType;
}

return new ObjectModuleAsset(version: version);
return new ObjectModuleAsset(version: version, objectIdToSaveType: objectIdToSaveType);
}
}
}

0 comments on commit 4e6596a

Please sign in to comment.