Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename and prepare release #17

Merged
merged 17 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
439 changes: 436 additions & 3 deletions .editorconfig

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions OpenLocoTool/Annotation.cs → Core/Annotation.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace OpenLocoTool
namespace OpenLoco.ObjectEditor
{
public class Annotation
{
private int start = 0;
private int end = 0;
private int length = 0;
private int start;
private int end;
private int length;

public Annotation(string name, Annotation? parent, int start, int length)
{
Expand Down
20 changes: 20 additions & 0 deletions Core/Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AnalysisLevel>latest</AnalysisLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Zenith.Core" Version="1.0.20" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System.Reflection;
using OpenLocoTool.Headers;
using OpenLoco.ObjectEditor.Data;
using System.Reflection;

namespace OpenLocoTool.DatFileParsing
namespace OpenLoco.ObjectEditor.DatFileParsing
{
public static class AttributeHelper
{
public static T? Get<T>(PropertyInfo p) where T : Attribute
{
var attrs = p.GetCustomAttributes(typeof(T), inherit: false);
return attrs.Length == 1 ? attrs[0] as T : null;
var attributes = p.GetCustomAttributes(typeof(T), inherit: false);
return attributes.Length == 1 ? attributes[0] as T : null;
}

public static T? Get<T>(Type t) where T : Attribute
Expand All @@ -26,10 +26,10 @@
public static class ObjectAttributes
{
public static int StructSize<T>() where T : ILocoStruct
=> AttributeHelper.Get<LocoStructSizeAttribute>(typeof(T)).Size;

Check warning on line 29 in Core/DatFileParsing/AttributeHelper.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.

public static ObjectType ObjectType<T>() // where T : ILocoStruct
=> AttributeHelper.Get<LocoStructTypeAttribute>(typeof(T)).ObjectType;

Check warning on line 32 in Core/DatFileParsing/AttributeHelper.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.

public static ObjectType ObjectType(ILocoStruct str) // where T : ILocoStruct
=> AttributeHelper.Get<LocoStructTypeAttribute>(str.GetType()).ObjectType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace OpenLocoTool.DatFileParsing
using Zenith.Core;

namespace OpenLoco.ObjectEditor.DatFileParsing
{
public static class ByteHelpers
{
Expand All @@ -23,10 +25,7 @@ public static int GetObjectSize(Type type)
size = sizeAttr.Size;
}

if (size == 0)
{
throw new ArgumentException("unknown primitive type with no size");
}
Verify.Positive(size, message: $"type {type.Name} has no size data associated with it");

return size;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace OpenLocoTool.DatFileParsing
using Zenith.Core;

namespace OpenLoco.ObjectEditor.DatFileParsing
{
public static class ByteReader
{
Expand Down Expand Up @@ -41,7 +43,7 @@ public static object ReadT(ReadOnlySpan<byte> data, Type t, int offset, int arrL

if (t.IsArray)
{
var elementType = t.GetElementType();
var elementType = t.GetElementType() ?? throw new ArgumentNullException(t.Name);
var size = ByteHelpers.GetObjectSize(elementType);

var arr = Array.CreateInstance(elementType, arrLength);
Expand All @@ -65,7 +67,7 @@ public static object ReadT(ReadOnlySpan<byte> data, Type t, int offset, int arrL

foreach (var enumValue in enumValues)
{
var enumValueInt = Convert.ToInt32(Enum.Parse(t, enumValue.ToString())); // Convert to int
var enumValueInt = Convert.ToInt32(Enum.Parse(t, enumValue.ToString()!)); // Convert to int
if ((enumValueInt & Convert.ToInt32(underlyingValue)) != 0) // Convert to int
{
combinedValue |= enumValueInt;
Expand Down Expand Up @@ -127,13 +129,39 @@ public static ILocoStruct ReadLocoStruct(ReadOnlySpan<byte> data, Type t)
var variableAttr = AttributeHelper.Get<LocoStructVariableLoadAttribute>(p);
if (variableAttr != null)
{
if (p.PropertyType.IsArray && p.PropertyType.GetElementType() == typeof(uint8_t))
if (p.PropertyType.IsArray)
{
args.Add(new uint8_t[arrLength]);
// todo: find a generic way to do this
if (p.PropertyType.GetElementType() == typeof(uint8_t))
{
args.Add(new uint8_t[arrLength]);
}
else if (p.PropertyType.GetElementType() == typeof(int8_t))
{
args.Add(new int8_t[arrLength]);
}
else if (p.PropertyType.GetElementType() == typeof(uint16_t))
{
args.Add(new uint16_t[arrLength]);
}
else if (p.PropertyType.GetElementType() == typeof(int16_t))
{
args.Add(new int16_t[arrLength]);
}
else if (p.PropertyType.GetElementType() == typeof(uint32_t))
{
args.Add(new uint32_t[arrLength]);
}
else if (p.PropertyType.GetElementType() == typeof(int32_t))
{
args.Add(new int32_t[arrLength]);
}
}
else
{
args.Add(Activator.CreateInstance(p.PropertyType));
var newInstance = Activator.CreateInstance(p.PropertyType);
Verify.NotNull(newInstance, paramName: p.PropertyType.Name);
args.Add(newInstance!);
}

continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OpenLocoTool.DatFileParsing
namespace OpenLoco.ObjectEditor.DatFileParsing
{
public static class ByteReaderT
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace OpenLocoTool.DatFileParsing
using Zenith.Core;

namespace OpenLoco.ObjectEditor.DatFileParsing
{
public static class ByteWriter
{
Expand Down Expand Up @@ -36,13 +38,13 @@ public static void WriteT(Span<byte> data, Type t, int offset, object val)
}
else if (t.IsArray)
{
var elementType = t.GetElementType() ?? throw new NullReferenceException();
var elementType = t.GetElementType() ?? throw new ArgumentNullException(t.Name);
var size = ByteHelpers.GetObjectSize(elementType);
var arr = (Array)val;

for (var i = 0; i < arr.Length; i++)
{
var value = arr.GetValue(i) ?? throw new NullReferenceException();
var value = arr.GetValue(i) ?? throw new ArgumentNullException($"{t.Name}[{i}]");
WriteT(data, elementType, offset + (i * size), value);
}
}
Expand Down Expand Up @@ -72,10 +74,7 @@ public static void WriteT(Span<byte> data, Type t, int offset, object val)

public static ReadOnlySpan<byte> WriteLocoStruct(ILocoStruct obj)
{
if (obj == null)
{
throw new NullReferenceException();
}
Verify.NotNull(obj);

var t = obj.GetType();
var objSize = ByteHelpers.GetObjectSize(t);
Expand Down Expand Up @@ -113,7 +112,7 @@ public static ReadOnlySpan<byte> WriteLocoStruct(ILocoStruct obj)
arrLength = arrLengthAttr.Length;
}

var propVal = p.GetValue(obj) ?? throw new NullReferenceException();
var propVal = p.GetValue(obj) ?? throw new ArgumentNullException($"{p.Name} for {obj} was null");
WriteT(buf, p.PropertyType, offsetAttr.Offset, propVal);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OpenLocoTool.DatFileParsing
namespace OpenLoco.ObjectEditor.DatFileParsing
{
public static class ByteWriterT
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using OpenLocoTool.Headers;
using OpenLoco.ObjectEditor.Data;

namespace OpenLocoTool.DatFileParsing
namespace OpenLoco.ObjectEditor.DatFileParsing
{
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false)]
public class LocoArrayLengthAttribute(int length) : Attribute
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System.Diagnostics;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using OpenLocoTool.Headers;
using OpenLoco.ObjectEditor.Data;
using OpenLoco.ObjectEditor.Headers;
using Zenith.Core;

namespace OpenLocoTool.DatFileParsing
namespace OpenLoco.ObjectEditor.DatFileParsing
{
public static class ObjectAnnotator
{
Expand Down Expand Up @@ -38,11 +41,7 @@ .. SawyerStreamReader.Decode(objectHeader.Encoding, bytelist[runningCount..(int)
];

var locoStruct = SawyerStreamReader.GetLocoStruct(s5Header.ObjectType, fullData.AsSpan()[runningCount..]);
if (locoStruct == null)
{
Debugger.Break();
throw new NullReferenceException("loco object was null");
}
Verify.NotNull(locoStruct);

var structSize = AttributeHelper.Get<LocoStructSizeAttribute>(locoStruct.GetType());
var locoStructSize = structSize!.Size;
Expand Down Expand Up @@ -201,11 +200,42 @@ static List<Annotation> AnnotateProperties(object o, int runningCount = 0, Annot
if (offset != null)
{
var location = runningCount + offset!.Offset;
annotations.Add(new Annotation(p.Name, root, location, 1));

var propType = p.PropertyType;

// should probably use recursion
while (propType.IsGenericType)
{
propType = propType.GenericTypeArguments[0];
}

while (propType.IsArray)
{
propType = propType.GetElementType();
}

if (propType.IsEnum)
{
propType = propType.GetEnumUnderlyingType();
}

var locoSize = propType.GetCustomAttribute<LocoStructSizeAttribute>();

var length = locoSize != null
? locoSize.Size
: Marshal.SizeOf(propType);

var lengthAttr = p.GetCustomAttribute<LocoArrayLengthAttribute>();
if (lengthAttr != null)
{
length *= lengthAttr.Length;
}

annotations.Add(new Annotation(p.Name, root, location, length));
}
}

return annotations;
}
}
}
}
Loading
Loading