Skip to content

Commit 582066b

Browse files
Update to .NET 8 (#11)
* update projects to .net 8.0 * apply many vs refactoring suggestions * more cleanup --------- Co-authored-by: Benjamin Sutas <[email protected]>
1 parent 0ed6942 commit 582066b

39 files changed

+275
-298
lines changed

DatFileRenamer/DatFileRenamer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>

DatFileRenamer/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// read each files S5Header
2121
using var fileStream = new FileStream(datFile, FileMode.Open, FileAccess.Read, FileShare.None, 32, FileOptions.SequentialScan | FileOptions.Asynchronous);
2222
using var reader = new BinaryReader(fileStream);
23-
var data = reader.ReadBytes(0x10);
23+
var data = reader.ReadBytes(0x10).AsSpan();
2424

2525
var flags = BitConverter.ToUInt32(data[0..4]);
2626
var datName = System.Text.Encoding.ASCII.GetString(data[4..12]).Trim();

OpenLocoTool.sln

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenLocoToolGui", "OpenLoco
1212
EndProject
1313
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenLocoToolCommon", "OpenLocoToolCommon\OpenLocoToolCommon.csproj", "{BCD93536-D322-4C14-B193-1F643D03C788}"
1414
EndProject
15-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenLocoToolTests", "OpenLocoToolTests\OpenLocoToolTests.csproj", "{55293DEB-00FA-45AD-814D-CB37383BE0D5}"
15+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenLocoToolTests", "OpenLocoToolTests\OpenLocoToolTests.csproj", "{55293DEB-00FA-45AD-814D-CB37383BE0D5}"
16+
EndProject
17+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DatFileRenamer", "DatFileRenamer\DatFileRenamer.csproj", "{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}"
1618
EndProject
1719
Global
1820
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -72,6 +74,18 @@ Global
7274
{55293DEB-00FA-45AD-814D-CB37383BE0D5}.Release|x64.Build.0 = Release|Any CPU
7375
{55293DEB-00FA-45AD-814D-CB37383BE0D5}.Release|x86.ActiveCfg = Release|Any CPU
7476
{55293DEB-00FA-45AD-814D-CB37383BE0D5}.Release|x86.Build.0 = Release|Any CPU
77+
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
78+
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
79+
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Debug|x64.ActiveCfg = Debug|Any CPU
80+
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Debug|x64.Build.0 = Debug|Any CPU
81+
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Debug|x86.ActiveCfg = Debug|Any CPU
82+
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Debug|x86.Build.0 = Debug|Any CPU
83+
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
84+
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Release|Any CPU.Build.0 = Release|Any CPU
85+
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Release|x64.ActiveCfg = Release|Any CPU
86+
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Release|x64.Build.0 = Release|Any CPU
87+
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Release|x86.ActiveCfg = Release|Any CPU
88+
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Release|x86.Build.0 = Release|Any CPU
7589
EndGlobalSection
7690
GlobalSection(SolutionProperties) = preSolution
7791
HideSolutionNode = FALSE

OpenLocoTool/DatFileParsing/ByteReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public static ILocoStruct ReadLocoStruct(ReadOnlySpan<byte> data, Type t)
136136
args.Add(ReadT(data, p.PropertyType, offsetAttr.Offset, arrLength));
137137
}
138138

139-
return (ILocoStruct?)Activator.CreateInstance(t, args.ToArray()) ?? throw new InvalidDataException("couldn't parse");
139+
return (ILocoStruct?)Activator.CreateInstance(t, [.. args]) ?? throw new InvalidDataException("couldn't parse");
140140
}
141141

142142
public static IList<ILocoStruct> ReadLocoStructArray(ReadOnlySpan<byte> data, Type t, int count, int structSize) // could get struct size from attribute, but easier just to pass in

OpenLocoTool/DatFileParsing/ByteReaderT.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,34 @@ public static int32_t Read_int32t(ReadOnlySpan<byte> data, int offset)
2323
public static T Read<T>(ReadOnlySpan<byte> data, int offset) where T : struct
2424
{
2525
if (typeof(T) == typeof(uint8_t))
26+
{
2627
return (T)(dynamic)Read_uint8t(data, offset);
28+
}
29+
2730
if (typeof(T) == typeof(int8_t))
31+
{
2832
return (T)(dynamic)Read_int8t(data, offset);
33+
}
2934

3035
if (typeof(T) == typeof(uint16_t))
36+
{
3137
return (T)(dynamic)Read_uint16t(data, offset);
38+
}
39+
3240
if (typeof(T) == typeof(int16_t))
41+
{
3342
return (T)(dynamic)Read_int16t(data, offset);
43+
}
3444

3545
if (typeof(T) == typeof(uint32_t))
46+
{
3647
return (T)(dynamic)Read_uint32t(data, offset);
48+
}
49+
3750
if (typeof(T) == typeof(int32_t))
51+
{
3852
return (T)(dynamic)Read_int32t(data, offset);
53+
}
3954

4055
throw new NotImplementedException("");
4156
}

OpenLocoTool/DatFileParsing/ByteWriter.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,7 @@ public static ReadOnlySpan<byte> WriteLocoStruct(ILocoStruct obj)
114114
arrLength = arrLengthAttr.Length;
115115
}
116116

117-
var propVal = p.GetValue(obj);
118-
if (propVal == null)
119-
{
120-
throw new NullReferenceException();
121-
}
122-
117+
var propVal = p.GetValue(obj) ?? throw new NullReferenceException();
123118
WriteT(buf, p.PropertyType, offsetAttr.Offset, propVal);
124119
}
125120

OpenLocoTool/DatFileParsing/ByteWriterT.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,33 @@ public static void Write_int32t(Span<byte> data, int offset, int32_t val)
2323
public static void Write<T>(Span<byte> data, int offset, T val) where T : struct
2424
{
2525
if (typeof(T) == typeof(uint8_t))
26+
{
2627
Write_uint8t(data, offset, (uint8_t)(dynamic)val);
28+
}
2729
else if (typeof(T) == typeof(int8_t))
30+
{
2831
Write_int8t(data, offset, (int8_t)(dynamic)val);
29-
32+
}
3033
else if (typeof(T) == typeof(uint16_t))
34+
{
3135
Write_uint16t(data, offset, (uint16_t)(dynamic)val);
36+
}
3237
else if (typeof(T) == typeof(int16_t))
38+
{
3339
Write_int16t(data, offset, (int16_t)(dynamic)val);
34-
40+
}
3541
else if (typeof(T) == typeof(uint32_t))
42+
{
3643
Write_uint32t(data, offset, (uint32_t)(dynamic)val);
44+
}
3745
else if (typeof(T) == typeof(int32_t))
46+
{
3847
Write_int32t(data, offset, (int32_t)(dynamic)val);
39-
48+
}
4049
else
50+
{
4151
throw new NotImplementedException($"{typeof(T)}");
52+
}
4253
}
4354

4455
public static T[] Write_Array<T>(Span<byte> data, int count, int offset = 0) where T : struct
Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,27 @@
11
namespace OpenLocoTool.DatFileParsing
22
{
33
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false)]
4-
public class LocoArrayLengthAttribute : Attribute
4+
public class LocoArrayLengthAttribute(int length) : Attribute
55
{
6-
public LocoArrayLengthAttribute(int length) => Length = length;
7-
public int Length { get; }
6+
public int Length => length;
87
}
98

109
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false)]
11-
public class LocoStructOffsetAttribute : Attribute
10+
public class LocoStructOffsetAttribute(int offset) : Attribute
1211
{
13-
public LocoStructOffsetAttribute(int offset) => Offset = offset;
14-
15-
public int Offset { get; }
12+
public int Offset => offset;
1613
}
1714

1815
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false)]
19-
public class LocoStructSizeAttribute : Attribute
16+
public class LocoStructSizeAttribute(int size) : Attribute
2017
{
21-
public LocoStructSizeAttribute(int size) => Size = size;
22-
23-
public int Size { get; }
18+
public int Size => size;
2419
}
2520

2621
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false)]
27-
public class LocoStringTableAttribute : Attribute
22+
public class LocoStringTableAttribute(params string[] names) : Attribute
2823
{
29-
public LocoStringTableAttribute(params string[] names)
30-
{
31-
Names = names;
32-
}
33-
34-
public string[] Names { get; }
24+
public string[] Names => names;
3525

3626
public int Count => Names.Length;
3727
}
@@ -40,4 +30,9 @@ public LocoStringTableAttribute(params string[] names)
4030
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false)]
4131
public class LocoStructVariableLoadAttribute : Attribute
4232
{ }
33+
34+
// basically a 'skip' attribute to allow deferred loading for variable data, and writing of this property will be 0
35+
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false)]
36+
public class LocoStructZeroAttribute : Attribute
37+
{ }
4338
}

OpenLocoTool/DatFileParsing/LocoObject.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,10 @@ public interface ILocoObject
8181
}
8282

8383
[TypeConverter(typeof(ExpandableObjectConverter))]
84-
public class G1Dat
84+
public class G1Dat(G1Header g1Header, List<G1Element32> g1Elements)
8585
{
86-
public G1Dat(G1Header g1Header, List<G1Element32> g1Elements)
87-
{
88-
G1Header = g1Header;
89-
G1Elements = g1Elements;
90-
}
91-
92-
public G1Header G1Header { get; set; }
93-
public List<G1Element32> G1Elements { get; set; }
86+
public G1Header G1Header { get; set; } = g1Header;
87+
public List<G1Element32> G1Elements { get; set; } = g1Elements;
9488
}
9589

9690
public static class ObjectTypeFixedSize

OpenLocoTool/DatFileParsing/ObjectAnnotator.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ public static IList<Annotation> Annotate(byte[] bytelist, out byte[] fullData)
3131
runningCount += ObjectHeader.StructLength;
3232

3333
// Decode Loco Struct
34-
fullData = bytelist[..runningCount]
35-
.Concat(SawyerStreamReader.Decode(objectHeader.Encoding, bytelist.AsSpan()[runningCount..(int)(runningCount + objectHeader.DataLength)]))
36-
.ToArray();
34+
fullData =
35+
[
36+
.. bytelist[..runningCount],
37+
.. SawyerStreamReader.Decode(objectHeader.Encoding, bytelist.AsSpan()[runningCount..(int)(runningCount + objectHeader.DataLength)]),
38+
];
3739

3840
var locoStruct = SawyerStreamReader.GetLocoStruct(s5Header.ObjectType, fullData.AsSpan()[runningCount..]);
3941
if (locoStruct == null)
@@ -164,15 +166,14 @@ public static int AnnotateStringTable(byte[] fullData, int runningCount, ILocoSt
164166
while (continuing);
165167

166168
var endIndexOfStringList = index + runningCount;
167-
var nullIndex = 0;
168169
var elementRoot = new Annotation("Element " + i, root, runningCount, index);
169170
annotations.Add(elementRoot);
170171

171172
do
172173
{
173174
annotations.Add(new Annotation(((LanguageId)fullData[runningCount]).ToString(), elementRoot, runningCount, 1));
174175
runningCount++;
175-
nullIndex = Array.IndexOf(fullData[runningCount..], (byte)0);
176+
var nullIndex = Array.IndexOf(fullData[runningCount..], (byte)0);
176177

177178
var stringElement = Encoding.ASCII.GetString(fullData[runningCount..(runningCount + nullIndex)]);
178179

@@ -187,7 +188,7 @@ public static int AnnotateStringTable(byte[] fullData, int runningCount, ILocoSt
187188
return runningCount;
188189
}
189190

190-
static IList<Annotation> AnnotateProperties(object o, int runningCount = 0, Annotation? root = null)
191+
static List<Annotation> AnnotateProperties(object o, int runningCount = 0, Annotation? root = null)
191192
{
192193
var annotations = new List<Annotation>();
193194

0 commit comments

Comments
 (0)