Skip to content

Commit dab570a

Browse files
committed
SimAntics: Implement Semi-Attributes, fix Attribute count
1 parent 561917e commit dab570a

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

Assets/Scripts/OpenTS2/SimAntics/VMContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public short GetData(VMDataSource source, short dataIndex)
2727
{
2828
VMDataSource.Globals => VM.GetGlobal((ushort)dataIndex),
2929
VMDataSource.MyObjectsAttributes => Entity.Attributes[dataIndex],
30+
VMDataSource.MyObjectsSemiAttributes => Entity.SemiAttributes[dataIndex],
3031
VMDataSource.MyObject => Entity.ObjectData[dataIndex],
3132
VMDataSource.StackObject => StackObjectEntity.ObjectData[dataIndex],
3233
VMDataSource.Literal => dataIndex,
@@ -38,10 +39,12 @@ public short GetData(VMDataSource source, short dataIndex)
3839
VMDataSource.Local => StackFrame.Locals[dataIndex],
3940
VMDataSource.StackObjectsDefinition => (short)StackObjectEntity.ObjectDefinition.Fields[dataIndex],
4041
VMDataSource.StackObjectsAttributes => StackObjectEntity.Attributes[dataIndex],
42+
VMDataSource.StackObjectsSemiAttributes => StackObjectEntity.SemiAttributes[dataIndex],
43+
VMDataSource.StackObjectsSemiAttributeByParam => StackObjectEntity.SemiAttributes[StackFrame.Arguments[dataIndex]],
4144
_ => throw new SimAnticsException($"Attempted to retrieve a variable from an out of range data source ({source}[{dataIndex}]).", StackFrame)
4245
};
4346
}
44-
catch (ArgumentOutOfRangeException)
47+
catch (IndexOutOfRangeException)
4548
{
4649
throw new SimAnticsException($"Attempted to retrieve a variable from an out of range data index ({source}[{dataIndex}]).", StackFrame);
4750
}

Assets/Scripts/OpenTS2/SimAntics/VMEntity.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
using OpenTS2.Content.DBPF;
1+
using OpenTS2.Common;
2+
using OpenTS2.Content;
3+
using OpenTS2.Content.DBPF;
24
using OpenTS2.Files.Formats.DBPF;
5+
using OpenTS2.Lua.Disassembly.OpCodes;
36
using System;
47
using System.Collections.Generic;
58
using System.Linq;
@@ -21,6 +24,7 @@ public class VMEntity
2124
public VM VM;
2225
public ObjectDefinitionAsset ObjectDefinition;
2326
public short[] Attributes;
27+
public short[] SemiAttributes;
2428
public short[] ObjectData = new short[114];
2529
public uint PrivateGroupID => ObjectDefinition.GlobalTGI.GroupID;
2630
public uint SemiGlobalGroupID
@@ -42,7 +46,30 @@ protected VMEntity()
4246
public VMEntity(ObjectDefinitionAsset objectDefinition) : this()
4347
{
4448
ObjectDefinition = objectDefinition;
45-
Attributes = new short[objectDefinition.NumAttributes];
49+
Attributes = new short[GetNumberOfAttributes()];
50+
SemiAttributes = new short[GetNumberOfSemiGlobalAttributes()];
51+
}
52+
53+
// TODO: Verify these two methods below are right. Sometimes objdefs have 0 attributes but do have attribute labels and get/set their attributes.
54+
private int GetNumberOfAttributes()
55+
{
56+
var objDefAttributes = ObjectDefinition.NumAttributes;
57+
var attrLabels = ContentManager.Instance.GetAsset<StringSetAsset>(new ResourceKey(0x100, PrivateGroupID, TypeIDs.STR));
58+
if (attrLabels == null)
59+
return objDefAttributes;
60+
var attrLabelsCount = attrLabels.StringData.Strings[Languages.USEnglish].Count;
61+
return attrLabelsCount > objDefAttributes ? attrLabelsCount : objDefAttributes;
62+
}
63+
64+
private int GetNumberOfSemiGlobalAttributes()
65+
{
66+
var semiGlobal = ObjectDefinition.SemiGlobal;
67+
if (semiGlobal == null)
68+
return 0;
69+
var semiAttributeLabels = ContentManager.Instance.GetAsset<StringSetAsset>(new ResourceKey(0x100, semiGlobal.SemiGlobalGroupID, TypeIDs.STR));
70+
if (semiAttributeLabels == null)
71+
return 0;
72+
return semiAttributeLabels.StringData.Strings[Languages.USEnglish].Count;
4673
}
4774

4875
public short GetObjectData(VMObjectData field)

0 commit comments

Comments
 (0)