Skip to content

Commit 2580f6b

Browse files
committed
name strings in string table
1 parent e5b42cb commit 2580f6b

39 files changed

+72
-135
lines changed

OpenLocoTool/DatFileParsing/LocoAttributes.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ public class LocoStructSizeAttribute : Attribute
2424
}
2525

2626
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false)]
27-
public class LocoStringCountAttribute : Attribute
27+
public class LocoStringTableAttribute : Attribute
2828
{
29-
public LocoStringCountAttribute(int count) => Count = count;
29+
public LocoStringTableAttribute(params string[] names)
30+
{
31+
Names = names;
32+
}
3033

31-
public int Count { get; }
34+
public string[] Names { get; }
35+
36+
public int Count => Names.Length;
3237
}
3338

3439
// basically a 'skip' attribute to allow deferred loading for variable data

OpenLocoTool/DatFileParsing/ObjectAnnotator.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ public static IList<Annotation> AnnotateG1Data(byte[] fullData, int runningCount
129129
}
130130

131131
annotations.Add(new Annotation("Image " + (i + 1), g1ImageDataAnnotation, imageStart, imageSize));
132-
//runningCount = imageDataStart + (int)g32elements[i].Offset;
133132
}
134133
}
135134

@@ -141,8 +140,8 @@ public static int AnnotateStringTable(byte[] fullData, int runningCount, ILocoSt
141140
var root = new Annotation("String Table", runningCount, 1);
142141
annotations.Add(root);
143142

144-
var stringAttr = locoStruct.GetType().GetCustomAttribute(typeof(LocoStringCountAttribute), inherit: false) as LocoStringCountAttribute;
145-
var stringsInTable = stringAttr?.Count ?? 1;
143+
var stringAttr = locoStruct.GetType().GetCustomAttribute(typeof(LocoStringTableAttribute), inherit: false) as LocoStringTableAttribute;
144+
var stringsInTable = stringAttr?.Count ?? 0;
146145

147146
for (var i = 0; i < stringsInTable; i++)
148147
{
@@ -160,9 +159,9 @@ public static int AnnotateStringTable(byte[] fullData, int runningCount, ILocoSt
160159
else
161160
{
162161
continuing = false;
163-
}
162+
}
164163
}
165-
while (continuing);
164+
while (continuing);
166165

167166
var endIndexOfStringList = index + runningCount;
168167
var nullIndex = 0;

OpenLocoTool/DatFileParsing/SawyerStreamReader.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ public G1Dat LoadG1(string filename)
4848
// load file
4949
public ILocoObject LoadFull(string filename, bool loadExtra = true)
5050
{
51-
//LoadFullExperimental(filename);
52-
5351
ReadOnlySpan<byte> fullData = LoadBytesFromFile(filename);
5452

5553
// make openlocotool useful objects
@@ -111,18 +109,17 @@ public ILocoObject LoadFull(string filename, bool loadExtra = true)
111109

112110
static (StringTable table, int bytesRead) LoadStringTable(ReadOnlySpan<byte> data, ILocoStruct locoStruct)
113111
{
114-
var stringAttr = locoStruct.GetType().GetCustomAttribute(typeof(LocoStringCountAttribute), inherit: false) as LocoStringCountAttribute;
115-
var stringsInTable = stringAttr?.Count ?? throw new ArgumentException($"Struct {locoStruct.GetType().Name} had no string count attribute");
116-
var strings = new StringTable();
112+
var stringTableAttr = locoStruct.GetType().GetCustomAttribute(typeof(LocoStringTableAttribute), inherit: false) as LocoStringTableAttribute;
113+
var stringTable = new StringTable();
117114

118-
if (data.Length == 0 || stringsInTable == 0)
115+
if (data.Length == 0 || stringTableAttr == null || stringTableAttr.Count == 0)
119116
{
120-
return (strings, 0);
117+
return (stringTable, 0);
121118
}
122119

123120
var ptr = 0;
124121

125-
for (var i = 0; i < stringsInTable; ++i)
122+
for (var i = 0; i < stringTableAttr.Count; ++i)
126123
{
127124
for (; ptr < data.Length && data[ptr] != 0xFF;)
128125
{
@@ -132,22 +129,22 @@ public ILocoObject LoadFull(string filename, bool loadExtra = true)
132129
while (data[ptr++] != '\0') ;
133130

134131
var str = Encoding.ASCII.GetString(data[ini..(ptr - 1)]); // do -1 to exclude the \0
135-
136-
if (strings.ContainsKey((i, lang)))
132+
var stringName = stringTableAttr.Names[i];
133+
if (stringTable.ContainsKey((stringName, lang)))
137134
{
138135
//Logger.Error($"Key {(i, lang)} already exists (this shouldn't happen)");
139136
break;
140137
}
141138
else
142139
{
143-
strings.Add((i, lang), str);
140+
stringTable.Add((stringName, lang), str);
144141
}
145142
}
146143

147144
ptr++; // add one because we skipped the 0xFF byte at the end
148145
}
149146

150-
return (strings, ptr);
147+
return (stringTable, ptr);
151148
}
152149

153150
static (G1Header header, List<G1Element32> table, int bytesRead) LoadImageTable(ReadOnlySpan<byte> data)

OpenLocoTool/DatFileParsing/Typedefs.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
global using Speed32 = System.Int32;
1010
global using MicroZ = System.Byte;
1111
global using SoundObjectId = System.Byte;
12-
global using StringTable = System.Collections.Generic.Dictionary<(int, OpenLocoTool.LanguageId), string>;
12+
global using StringTable = System.Collections.Generic.Dictionary<(string, OpenLocoTool.LanguageId), string>;
1313
using System.ComponentModel;
1414

1515
namespace OpenLocoTool.DatFileParsing
1616
{
1717
[TypeConverter(typeof(ExpandableObjectConverter))]
1818
[LocoStructSize(0x04)]
19-
[LocoStringCount(0)]
2019
public record Pos2(
2120
[property: LocoStructOffset(0x00)] int16_t X,
2221
[property: LocoStructOffset(0x02)] int16_t Y

OpenLocoTool/Objects/AirportObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public record MovementEdge(
4747

4848
[TypeConverter(typeof(ExpandableObjectConverter))]
4949
[LocoStructSize(0xBA)]
50-
[LocoStringCount(1)]
50+
[LocoStringTable("Name")]
5151
public record AirportObject(
5252
//[property: LocoStructOffset(0x00)] string_id Name,
5353
[property: LocoStructOffset(0x02)] int16_t BuildCostFactor,

OpenLocoTool/Objects/BridgeObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace OpenLocoTool.Objects
88
// it works just the same but has more code.
99
[TypeConverter(typeof(ExpandableObjectConverter))]
1010
[LocoStructSize(0x2C)]
11-
[LocoStringCount(1)]
11+
[LocoStringTable("Name")]
1212
public class BridgeObject : ILocoStruct, ILocoStructVariableData
1313
{
1414
public const ObjectType ObjType = ObjectType.Bridge;

OpenLocoTool/Objects/BuildingObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public enum BuildingObjectFlags : uint8_t
1616

1717
[TypeConverter(typeof(ExpandableObjectConverter))]
1818
[LocoStructSize(0xBE)]
19-
[LocoStringCount(1)]
19+
[LocoStringTable("Name")]
2020
public record BuildingObject(
2121
//[property: LocoStructOffset(0x00)] string_id Name,
2222
//[property: LocoStructOffset(0x02)] uint32_t Image,

OpenLocoTool/Objects/CargoObject.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public enum CargoObjectFlags : uint8_t
1515

1616
[TypeConverter(typeof(ExpandableObjectConverter))]
1717
[LocoStructSize(0x1F)]
18-
[LocoStringCount(4)]
18+
[LocoStringTable("Name", "UnitsAndCargoName", "UnitNameSingular", "UnitNamePlural")]
1919
public record CargoObject(
2020
//[property: LocoStructOffset(0x00)] string_id Name,
2121
[property: LocoStructOffset(0x02)] uint16_t var_02,
@@ -40,19 +40,19 @@ public record CargoObject(
4040
public static ObjectType ObjectType => ObjectType.Cargo;
4141
public static int StructSize => 0x1F;
4242

43-
public string Name { get; set; }
44-
public string UnitsAndCargoName { get; set; }
45-
46-
public string UnitNameSingular { get; set; }
47-
48-
public string UnitNamePlural { get; set; }
43+
//public string Name { get; set; }
44+
//public string UnitsAndCargoName { get; set; }
45+
//
46+
//public string UnitNameSingular { get; set; }
47+
//
48+
//public string UnitNamePlural { get; set; }
4949

5050
public void LoadPostStringTable(StringTable stringTable)
5151
{
52-
Name = stringTable[(0, (LanguageId)0)];
53-
UnitsAndCargoName = stringTable[(1, (LanguageId)0)];
54-
UnitNameSingular = stringTable[(2, (LanguageId)0)];
55-
UnitNamePlural = stringTable[(3, (LanguageId)0)];
52+
// Name = stringTable[(0, (LanguageId)0)];
53+
// UnitsAndCargoName = stringTable[(1, (LanguageId)0)];
54+
// UnitNameSingular = stringTable[(2, (LanguageId)0)];
55+
// UnitNamePlural = stringTable[(3, (LanguageId)0)];
5656
}
5757
}
5858
}

OpenLocoTool/Objects/CliffEdgeObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace OpenLocoTool.Objects
66
{
77
[TypeConverter(typeof(ExpandableObjectConverter))]
88
[LocoStructSize(0x06)]
9-
[LocoStringCount(1)]
9+
[LocoStringTable("Name")]
1010
public class CliffEdgeObject : ILocoStruct
1111
{
1212
public const ObjectType ObjType = ObjectType.CliffEdge;

OpenLocoTool/Objects/ClimateObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace OpenLocoTool.Objects
66
{
77
[TypeConverter(typeof(ExpandableObjectConverter))]
88
[LocoStructSize(0x0A)]
9-
[LocoStringCount(1)]
9+
[LocoStringTable("Name")]
1010
public record ClimateObject(
1111
//[property: LocoStructOffset(0x00)] string_id Name,
1212
[property: LocoStructOffset(0x02)] uint8_t FirstSeason,

0 commit comments

Comments
 (0)