Skip to content

Commit 94ccb46

Browse files
committed
code cleanup
1 parent 921946d commit 94ccb46

File tree

13 files changed

+28
-41
lines changed

13 files changed

+28
-41
lines changed

OpenLocoTool/DatFileParsing/ByteReader.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace OpenLocoTool.DatFileParsing
22
{
3-
43
public static class ByteReader
54
{
65
public static object ReadT(ReadOnlySpan<byte> data, Type t, int offset, int arrLength = 0)
@@ -120,7 +119,7 @@ public static ILocoStruct ReadLocoStruct(ReadOnlySpan<byte> data, Type t)
120119
var arrLength = 0;
121120
if (p.PropertyType.IsArray)
122121
{
123-
var arrLengthAttr = AttributeHelper.Get<LocoArrayLengthAttribute>(p) ?? throw new ArgumentOutOfRangeException(nameof(LocoArrayLengthAttribute), $"type {t} with property {p} didn't have LocoArrayLength attribute specified");
122+
var arrLengthAttr = AttributeHelper.Get<LocoArrayLengthAttribute>(p) ?? throw new ArgumentOutOfRangeException(nameof(data), $"type {t} with property {p} didn't have LocoArrayLength attribute specified");
124123
arrLength = arrLengthAttr.Length;
125124
}
126125

OpenLocoTool/DatFileParsing/ByteWriter.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,32 @@ public static void WriteT(Span<byte> data, Type t, int offset, object val)
88
{
99
ByteWriterT.Write(data, offset, (uint8_t)(dynamic)val);
1010
}
11-
1211
else if (t == typeof(int8_t))
1312
{
1413
ByteWriterT.Write(data, offset, (int8_t)(dynamic)val);
1514
}
16-
1715
else if (t == typeof(uint16_t))
1816
{
1917
ByteWriterT.Write(data, offset, (uint16_t)(dynamic)val);
2018
}
21-
2219
else if (t == typeof(int16_t))
2320
{
2421
ByteWriterT.Write(data, offset, (int16_t)(dynamic)val);
2522
}
26-
2723
else if (t == typeof(uint32_t))
2824
{
2925
ByteWriterT.Write(data, offset, (uint32_t)(dynamic)val);
3026
}
31-
3227
else if (t == typeof(int32_t))
3328
{
3429
ByteWriterT.Write(data, offset, (int32_t)(dynamic)val);
3530
}
36-
3731
else if (t == typeof(string_id))
3832
{
3933
// string ids should always be 0 in the dat file - they're only set when loaded into memory and never saved
4034
val = 0;
4135
ByteWriterT.Write(data, offset, (string_id)(dynamic)val);
4236
}
43-
4437
else if (t.IsArray)
4538
{
4639
var elementType = t.GetElementType() ?? throw new NullReferenceException();
@@ -53,14 +46,12 @@ public static void WriteT(Span<byte> data, Type t, int offset, object val)
5346
WriteT(data, elementType, offset + (i * size), value);
5447
}
5548
}
56-
5749
else if (t.IsEnum)
5850
{
5951
var underlyingType = t.GetEnumUnderlyingType();
6052
var underlyingValue = Convert.ChangeType(val, underlyingType);
6153
WriteT(data, underlyingType, offset, underlyingValue);
6254
}
63-
6455
else if (t.IsClass)
6556
{
6657
var objectSize = ByteHelpers.GetObjectSize(t);
@@ -89,7 +80,6 @@ public static ReadOnlySpan<byte> WriteLocoStruct(ILocoStruct obj)
8980
var t = obj.GetType();
9081
var objSize = ByteHelpers.GetObjectSize(t);
9182
var buf = new byte[objSize];
92-
var span = buf.AsSpan();
9383

9484
foreach (var p in t.GetProperties())
9585
{
@@ -112,7 +102,7 @@ public static ReadOnlySpan<byte> WriteLocoStruct(ILocoStruct obj)
112102
var arrLength = 0;
113103
if (p.PropertyType.IsArray)
114104
{
115-
var arrLengthAttr = AttributeHelper.Get<LocoArrayLengthAttribute>(p) ?? throw new ArgumentOutOfRangeException(nameof(LocoArrayLengthAttribute), $"type {t} with property {p} didn't have LocoArrayLength attribute specified");
105+
var arrLengthAttr = AttributeHelper.Get<LocoArrayLengthAttribute>(p) ?? throw new ArgumentOutOfRangeException(nameof(obj), $"type {t} with property {p} didn't have LocoArrayLength attribute specified");
116106
arrLength = arrLengthAttr.Length;
117107
}
118108

OpenLocoTool/DatFileParsing/ILocoObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public interface ILocoObject
1010
ObjectHeader ObjectHeader { get; set; }
1111
ILocoStruct Object { get; set; }
1212
StringTable StringTable { get; set; }
13-
G1Header G1Header { get; set; }
14-
List<G1Element32> G1Elements { get; set; }
13+
G1Header? G1Header { get; set; }
14+
List<G1Element32>? G1Elements { get; set; }
1515
}
1616
}

OpenLocoTool/DatFileParsing/SawyerStreamReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace OpenLocoTool.DatFileParsing
99
{
10-
public class SawyerStreamReader(ILogger logger)
10+
public static class SawyerStreamReader
1111
{
1212
static uint ComputeObjectChecksum(ReadOnlySpan<byte> flagByte, ReadOnlySpan<byte> name, ReadOnlySpan<byte> data)
1313
{
@@ -107,7 +107,7 @@ public static ILocoObject LoadFull(string filename, ILogger? logger = null, bool
107107
remainingData = locoStructExtra.Load(remainingData);
108108
}
109109

110-
LocoObject newObj = null;
110+
LocoObject? newObj;
111111
try
112112
{
113113
// some objects have graphics data

OpenLocoTool/DatFileParsing/SawyerStreamWriter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static ReadOnlySpan<byte> WriteLocoObject(ILocoObject obj)
4141
ms.Write(strBytes, 0, strBytes.Length);
4242
ms.WriteByte((byte)'\0');
4343
}
44+
4445
ms.WriteByte(0xff);
4546
}
4647

@@ -58,7 +59,7 @@ public static ReadOnlySpan<byte> WriteLocoObject(ILocoObject obj)
5859
ms.Write(BitConverter.GetBytes(obj.G1Header.NumEntries));
5960
ms.Write(BitConverter.GetBytes(obj.G1Elements.Sum(x => G1Element32.StructLength + x.ImageData.Length)));
6061

61-
int idx = 0;
62+
var idx = 0;
6263
// write G1Elements
6364
foreach (var g1Element in obj.G1Elements)
6465
{

OpenLocoTool/DatFileParsing/Typedefs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public record Pos2(
2525

2626
public class StringTable
2727
{
28-
public Dictionary<string, Dictionary<LanguageId, string>> table { get; set; } = new();
28+
public Dictionary<string, Dictionary<LanguageId, string>> table { get; set; } = [];
2929

3030
public void Add(string key, Dictionary<LanguageId, string> value) => table.Add(key, value);
3131

OpenLocoTool/Headers/G1Header.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public record G1Element32(
3030
) : ILocoStruct
3131
{
3232
public static int StructLength => 0x10;
33-
public byte[] ImageData;
33+
public byte[] ImageData = [];
3434

3535
public ReadOnlySpan<byte> Write()
3636
{
@@ -67,6 +67,6 @@ public record G1Header(
6767
) : ILocoStruct
6868
{
6969
public static int StructLength => 0x08;
70-
public byte[] ImageData;
70+
public byte[] ImageData = [];
7171
}
7272
}

OpenLocoTool/Objects/BuildingObject.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public ReadOnlySpan<byte> Load(ReadOnlySpan<byte> remainingData)
7777
remainingData = remainingData[(S5Header.StructLength * var_A4.Length)..];
7878

7979
// load ??
80-
var ptr_AD = 0;
8180
for (var i = 0; i < var_AD; ++i)
8281
{
8382
var size = BitConverter.ToUInt16(remainingData[..2]);

OpenLocoTool/Objects/SoundObject.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public ReadOnlySpan<byte> Load(ReadOnlySpan<byte> remainingData)
5555
remainingData = remainingData[4..];
5656

5757
// pcm data length
58-
var pcmDataLength = BitConverter.ToUInt32(remainingData[0..4]);
58+
//var pcmDataLength = BitConverter.ToUInt32(remainingData[0..4]); // unused
5959
remainingData = remainingData[4..];
6060

6161
remainingData = remainingData[(int)(numUnkStructs * 16)..];
@@ -66,9 +66,7 @@ public ReadOnlySpan<byte> Load(ReadOnlySpan<byte> remainingData)
6666

6767
RawPcmData = remainingData.ToArray();
6868

69-
remainingData = remainingData[remainingData.Length..];
70-
71-
return remainingData;
69+
return remainingData[remainingData.Length..];
7270
}
7371

7472
public ReadOnlySpan<byte> Save() => throw new NotImplementedException();

OpenLocoTool/Objects/TrainStationObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public record TrainStationObject(
4242
public const int Var6ELength = 16;
4343

4444
uint8_t[,] CargoOffsetBytes { get; set; }
45-
uint8_t[] var_6E { get; set; }
45+
uint8_t[] var_6E { get; set; } = [];
4646

4747
public ReadOnlySpan<byte> Load(ReadOnlySpan<byte> remainingData)
4848
{

0 commit comments

Comments
 (0)