Skip to content

Commit

Permalink
Update to .NET 8 (#11)
Browse files Browse the repository at this point in the history
* update projects to .net 8.0

* apply many vs refactoring suggestions

* more cleanup

---------

Co-authored-by: Benjamin Sutas <[email protected]>
  • Loading branch information
LeftofZen and BenjaminSutas authored Nov 15, 2023
1 parent 0ed6942 commit 582066b
Show file tree
Hide file tree
Showing 39 changed files with 275 additions and 298 deletions.
2 changes: 1 addition & 1 deletion DatFileRenamer/DatFileRenamer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion DatFileRenamer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// read each files S5Header
using var fileStream = new FileStream(datFile, FileMode.Open, FileAccess.Read, FileShare.None, 32, FileOptions.SequentialScan | FileOptions.Asynchronous);
using var reader = new BinaryReader(fileStream);
var data = reader.ReadBytes(0x10);
var data = reader.ReadBytes(0x10).AsSpan();

var flags = BitConverter.ToUInt32(data[0..4]);
var datName = System.Text.Encoding.ASCII.GetString(data[4..12]).Trim();
Expand Down
16 changes: 15 additions & 1 deletion OpenLocoTool.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenLocoToolGui", "OpenLoco
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenLocoToolCommon", "OpenLocoToolCommon\OpenLocoToolCommon.csproj", "{BCD93536-D322-4C14-B193-1F643D03C788}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenLocoToolTests", "OpenLocoToolTests\OpenLocoToolTests.csproj", "{55293DEB-00FA-45AD-814D-CB37383BE0D5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenLocoToolTests", "OpenLocoToolTests\OpenLocoToolTests.csproj", "{55293DEB-00FA-45AD-814D-CB37383BE0D5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DatFileRenamer", "DatFileRenamer\DatFileRenamer.csproj", "{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -72,6 +74,18 @@ Global
{55293DEB-00FA-45AD-814D-CB37383BE0D5}.Release|x64.Build.0 = Release|Any CPU
{55293DEB-00FA-45AD-814D-CB37383BE0D5}.Release|x86.ActiveCfg = Release|Any CPU
{55293DEB-00FA-45AD-814D-CB37383BE0D5}.Release|x86.Build.0 = Release|Any CPU
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Debug|x64.ActiveCfg = Debug|Any CPU
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Debug|x64.Build.0 = Debug|Any CPU
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Debug|x86.ActiveCfg = Debug|Any CPU
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Debug|x86.Build.0 = Debug|Any CPU
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Release|Any CPU.Build.0 = Release|Any CPU
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Release|x64.ActiveCfg = Release|Any CPU
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Release|x64.Build.0 = Release|Any CPU
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Release|x86.ActiveCfg = Release|Any CPU
{AD079FD2-EC1C-459C-BDE6-8D0C527767AB}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion OpenLocoTool/DatFileParsing/ByteReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static ILocoStruct ReadLocoStruct(ReadOnlySpan<byte> data, Type t)
args.Add(ReadT(data, p.PropertyType, offsetAttr.Offset, arrLength));
}

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

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
Expand Down
15 changes: 15 additions & 0 deletions OpenLocoTool/DatFileParsing/ByteReaderT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,34 @@ public static int32_t Read_int32t(ReadOnlySpan<byte> data, int offset)
public static T Read<T>(ReadOnlySpan<byte> data, int offset) where T : struct
{
if (typeof(T) == typeof(uint8_t))
{
return (T)(dynamic)Read_uint8t(data, offset);
}

if (typeof(T) == typeof(int8_t))
{
return (T)(dynamic)Read_int8t(data, offset);
}

if (typeof(T) == typeof(uint16_t))
{
return (T)(dynamic)Read_uint16t(data, offset);
}

if (typeof(T) == typeof(int16_t))
{
return (T)(dynamic)Read_int16t(data, offset);
}

if (typeof(T) == typeof(uint32_t))
{
return (T)(dynamic)Read_uint32t(data, offset);
}

if (typeof(T) == typeof(int32_t))
{
return (T)(dynamic)Read_int32t(data, offset);
}

throw new NotImplementedException("");
}
Expand Down
7 changes: 1 addition & 6 deletions OpenLocoTool/DatFileParsing/ByteWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,7 @@ public static ReadOnlySpan<byte> WriteLocoStruct(ILocoStruct obj)
arrLength = arrLengthAttr.Length;
}

var propVal = p.GetValue(obj);
if (propVal == null)
{
throw new NullReferenceException();
}

var propVal = p.GetValue(obj) ?? throw new NullReferenceException();
WriteT(buf, p.PropertyType, offsetAttr.Offset, propVal);
}

Expand Down
17 changes: 14 additions & 3 deletions OpenLocoTool/DatFileParsing/ByteWriterT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,33 @@ public static void Write_int32t(Span<byte> data, int offset, int32_t val)
public static void Write<T>(Span<byte> data, int offset, T val) where T : struct
{
if (typeof(T) == typeof(uint8_t))
{
Write_uint8t(data, offset, (uint8_t)(dynamic)val);
}
else if (typeof(T) == typeof(int8_t))
{
Write_int8t(data, offset, (int8_t)(dynamic)val);

}
else if (typeof(T) == typeof(uint16_t))
{
Write_uint16t(data, offset, (uint16_t)(dynamic)val);
}
else if (typeof(T) == typeof(int16_t))
{
Write_int16t(data, offset, (int16_t)(dynamic)val);

}
else if (typeof(T) == typeof(uint32_t))
{
Write_uint32t(data, offset, (uint32_t)(dynamic)val);
}
else if (typeof(T) == typeof(int32_t))
{
Write_int32t(data, offset, (int32_t)(dynamic)val);

}
else
{
throw new NotImplementedException($"{typeof(T)}");
}
}

public static T[] Write_Array<T>(Span<byte> data, int count, int offset = 0) where T : struct
Expand Down
31 changes: 13 additions & 18 deletions OpenLocoTool/DatFileParsing/LocoAttributes.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,27 @@
namespace OpenLocoTool.DatFileParsing
{
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false)]
public class LocoArrayLengthAttribute : Attribute
public class LocoArrayLengthAttribute(int length) : Attribute
{
public LocoArrayLengthAttribute(int length) => Length = length;
public int Length { get; }
public int Length => length;
}

[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false)]
public class LocoStructOffsetAttribute : Attribute
public class LocoStructOffsetAttribute(int offset) : Attribute
{
public LocoStructOffsetAttribute(int offset) => Offset = offset;

public int Offset { get; }
public int Offset => offset;
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false)]
public class LocoStructSizeAttribute : Attribute
public class LocoStructSizeAttribute(int size) : Attribute
{
public LocoStructSizeAttribute(int size) => Size = size;

public int Size { get; }
public int Size => size;
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false)]
public class LocoStringTableAttribute : Attribute
public class LocoStringTableAttribute(params string[] names) : Attribute
{
public LocoStringTableAttribute(params string[] names)
{
Names = names;
}

public string[] Names { get; }
public string[] Names => names;

public int Count => Names.Length;
}
Expand All @@ -40,4 +30,9 @@ public LocoStringTableAttribute(params string[] names)
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false)]
public class LocoStructVariableLoadAttribute : Attribute
{ }

// basically a 'skip' attribute to allow deferred loading for variable data, and writing of this property will be 0
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false)]
public class LocoStructZeroAttribute : Attribute
{ }
}
12 changes: 3 additions & 9 deletions OpenLocoTool/DatFileParsing/LocoObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,10 @@ public interface ILocoObject
}

[TypeConverter(typeof(ExpandableObjectConverter))]
public class G1Dat
public class G1Dat(G1Header g1Header, List<G1Element32> g1Elements)
{
public G1Dat(G1Header g1Header, List<G1Element32> g1Elements)
{
G1Header = g1Header;
G1Elements = g1Elements;
}

public G1Header G1Header { get; set; }
public List<G1Element32> G1Elements { get; set; }
public G1Header G1Header { get; set; } = g1Header;
public List<G1Element32> G1Elements { get; set; } = g1Elements;
}

public static class ObjectTypeFixedSize
Expand Down
13 changes: 7 additions & 6 deletions OpenLocoTool/DatFileParsing/ObjectAnnotator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ public static IList<Annotation> Annotate(byte[] bytelist, out byte[] fullData)
runningCount += ObjectHeader.StructLength;

// Decode Loco Struct
fullData = bytelist[..runningCount]
.Concat(SawyerStreamReader.Decode(objectHeader.Encoding, bytelist.AsSpan()[runningCount..(int)(runningCount + objectHeader.DataLength)]))
.ToArray();
fullData =
[
.. bytelist[..runningCount],
.. SawyerStreamReader.Decode(objectHeader.Encoding, bytelist.AsSpan()[runningCount..(int)(runningCount + objectHeader.DataLength)]),
];

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

var endIndexOfStringList = index + runningCount;
var nullIndex = 0;
var elementRoot = new Annotation("Element " + i, root, runningCount, index);
annotations.Add(elementRoot);

do
{
annotations.Add(new Annotation(((LanguageId)fullData[runningCount]).ToString(), elementRoot, runningCount, 1));
runningCount++;
nullIndex = Array.IndexOf(fullData[runningCount..], (byte)0);
var nullIndex = Array.IndexOf(fullData[runningCount..], (byte)0);

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

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

static IList<Annotation> AnnotateProperties(object o, int runningCount = 0, Annotation? root = null)
static List<Annotation> AnnotateProperties(object o, int runningCount = 0, Annotation? root = null)
{
var annotations = new List<Annotation>();

Expand Down
Loading

0 comments on commit 582066b

Please sign in to comment.