diff --git a/Assets/Scripts/OpenTS2/Content/DBPF/SimsObjectAsset.cs b/Assets/Scripts/OpenTS2/Content/DBPF/SimsObjectAsset.cs index cc52cf5..7fd3853 100644 --- a/Assets/Scripts/OpenTS2/Content/DBPF/SimsObjectAsset.cs +++ b/Assets/Scripts/OpenTS2/Content/DBPF/SimsObjectAsset.cs @@ -35,6 +35,7 @@ public struct SimsObjectStackFrame { public ushort ObjectId; public ushort TreeId; + public ushort BhavSaveType; public short[] Params; public short[] Locals; diff --git a/Assets/Scripts/OpenTS2/Engine/Tests/LotObjectSimulationTest.cs b/Assets/Scripts/OpenTS2/Engine/Tests/LotObjectSimulationTest.cs index dc1adf1..4acb1d0 100644 --- a/Assets/Scripts/OpenTS2/Engine/Tests/LotObjectSimulationTest.cs +++ b/Assets/Scripts/OpenTS2/Engine/Tests/LotObjectSimulationTest.cs @@ -1,6 +1,7 @@ using System.Collections; using System.Collections.Generic; using System.IO; +using System.Linq; using OpenTS2.Common; using OpenTS2.Content; using OpenTS2.Content.DBPF; @@ -58,6 +59,21 @@ private void LoadLot(string neighborhoodPrefix, int id) lotPackage.GetAssetByTGI(new ResourceKey(instanceID: 0, GroupIDs.Local, TypeIDs.OBJ_SAVE_TYPE_TABLE)); + var saveTypeToGuid = new Dictionary(); + for (var index = 0; index < saveTable.Selectors.Count; index++) + { + var selector = saveTable.Selectors[index]; + var def = ObjectManager.Instance.GetObjectByGUID(selector.objectGuid); + if (def == null) + { + continue; + } + + saveTypeToGuid[selector.saveType] = selector.objectGuid; + + 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}"); @@ -80,10 +96,23 @@ private void LoadLot(string neighborhoodPrefix, int id) foreach (var frame in objectState.StackFrames) { - var vmFrame = new VMStackFrame(entity.GetBHAV(frame.TreeId), entity.MainThread); - vmFrame.Arguments = frame.Params; - vmFrame.Locals = frame.Locals; + Debug.Log("Frame -----"); + Debug.Log($" TreeId: 0x{frame.TreeId:X}, bhavSaveType: {frame.BhavSaveType}"); + + var bhavObjDef = ObjectManager.Instance.GetObjectByGUID(saveTypeToGuid[frame.BhavSaveType]); + // TODO: add a static method to do this or something. We don't want to make a VMEntity instance just + // to get the BHAV. + var bhav = new VMEntity(bhavObjDef).GetBHAV(frame.TreeId); + + var vmFrame = new VMStackFrame(bhav, entity.MainThread) + { + Arguments = frame.Params, + Locals = frame.Locals + }; entity.MainThread.Frames.Push(vmFrame); + + Debug.Log($" BHAV TGI: {entity.MainThread.Frames.Peek().BHAV.GlobalTGI}"); + Debug.Log($" params: ({string.Join(", ", vmFrame.Arguments)})"); } vm.Tick(); diff --git a/Assets/Scripts/OpenTS2/Files/Formats/DBPF/SimsObjectCodec.cs b/Assets/Scripts/OpenTS2/Files/Formats/DBPF/SimsObjectCodec.cs index 24eec5e..79e108b 100644 --- a/Assets/Scripts/OpenTS2/Files/Formats/DBPF/SimsObjectCodec.cs +++ b/Assets/Scripts/OpenTS2/Files/Formats/DBPF/SimsObjectCodec.cs @@ -60,15 +60,12 @@ private static SimsObjectAsset DeserializeWithVersion(IoBuffer reader, int versi semiAttrs[i] = reader.ReadInt16(); } - Debug.Log($" Data array offset: {reader.Position:X}"); - // 8 unknown shorts called "data". var dataArray = new short[8]; for (var i = 0; i < dataArray.Length; i++) { dataArray[i] = reader.ReadInt16(); } - Debug.Log($"dataArray: [{string.Join(", ", dataArray)}]"); // Next is a number of shorts that depends on the exact version of the file. uint numShorts = version switch @@ -104,7 +101,6 @@ private static SimsObjectAsset DeserializeWithVersion(IoBuffer reader, int versi } Debug.Log($"InventoryToken(tokenGUID={tokenGUID}, tokenFlags={tokenFlags}, numTokenProps={numTokenProperties})"); - // Next is the number of object arrays. Each being a short array itself. var numObjectArrays = reader.ReadInt16(); var shortArrays = new List(numObjectArrays); @@ -184,6 +180,7 @@ private static SimsObjectAsset DeserializeWithVersion(IoBuffer reader, int versi { ObjectId = objectID, TreeId = treeID, + BhavSaveType = behavSaveType, Locals = locals, Params = frameParams }; diff --git a/Assets/Scripts/OpenTS2/SimAntics/VMEntity.cs b/Assets/Scripts/OpenTS2/SimAntics/VMEntity.cs index 390c286..53c102c 100644 --- a/Assets/Scripts/OpenTS2/SimAntics/VMEntity.cs +++ b/Assets/Scripts/OpenTS2/SimAntics/VMEntity.cs @@ -5,6 +5,7 @@ using OpenTS2.Lua.Disassembly.OpCodes; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -33,7 +34,7 @@ public uint SemiGlobalGroupID { var semiGlobal = ObjectDefinition.SemiGlobal; if (semiGlobal == null) - return 0; + throw new FileNotFoundException($"Object {ObjectDefinition.FileName} ({ObjectDefinition.GlobalTGI}) has no semi-global file"); return semiGlobal.SemiGlobalGroupID; } }