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

Objects v2 #13

Merged
merged 23 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
18 changes: 18 additions & 0 deletions OpenLocoTool/DatFileParsing/AttributeHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using OpenLocoTool.Headers;

namespace OpenLocoTool.DatFileParsing
{
Expand All @@ -15,8 +16,25 @@

public static bool Has<T>(PropertyInfo p) where T : Attribute
=> p.GetCustomAttribute(typeof(T), inherit: false) is T;
public static bool Has<T>(Type t) where T : Attribute
=> t.GetCustomAttribute(typeof(T), inherit: false) is T;

public static IEnumerable<PropertyInfo> GetAllPropertiesWithAttribute<T>(Type t) where T : Attribute
=> t.GetProperties().Where(Has<T>);
}

public static class ObjectAttributes
{
public static int StructSize<T>() where T : ILocoStruct
=> AttributeHelper.Get<LocoStructSizeAttribute>(typeof(T)).Size;

Check warning on line 29 in OpenLocoTool/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 OpenLocoTool/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;

Check warning on line 35 in OpenLocoTool/DatFileParsing/AttributeHelper.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.

public static string[] StringTableNames<T>() // where T : ILocoStruct
=> AttributeHelper.Get<LocoStringTableAttribute>(typeof(T)).Strings;

Check warning on line 38 in OpenLocoTool/DatFileParsing/AttributeHelper.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.
}
}
6 changes: 3 additions & 3 deletions OpenLocoTool/DatFileParsing/ByteReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
if (t.IsArray)
{
var elementType = t.GetElementType();
var size = ByteHelpers.GetObjectSize(elementType);

Check warning on line 45 in OpenLocoTool/DatFileParsing/ByteReader.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Possible null reference argument for parameter 'type' in 'int ByteHelpers.GetObjectSize(Type type)'.

var arr = Array.CreateInstance(elementType, arrLength);
for (var i = 0; i < arrLength; i++)
Expand All @@ -65,7 +65,7 @@

foreach (var enumValue in enumValues)
{
var enumValueInt = Convert.ToInt32(Enum.Parse(t, enumValue.ToString())); // Convert to int

Check warning on line 68 in OpenLocoTool/DatFileParsing/ByteReader.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Possible null reference argument for parameter 'value' in 'object Enum.Parse(Type enumType, string value)'.
if ((enumValueInt & Convert.ToInt32(underlyingValue)) != 0) // Convert to int
{
combinedValue |= enumValueInt;
Expand All @@ -91,8 +91,8 @@
throw new NotImplementedException(t.ToString());
}

public static ILocoStruct ReadLocoStruct<T>(ReadOnlySpan<byte> data) where T : class
=> ReadLocoStruct(data, typeof(T));
public static T ReadLocoStruct<T>(ReadOnlySpan<byte> data) where T : class
=> (T)ReadLocoStruct(data, typeof(T));

public static ILocoStruct ReadLocoStruct(ReadOnlySpan<byte> data, Type t)
{
Expand All @@ -110,7 +110,7 @@

// ignore skipped properties (usually image ids and string ids which are only used in loco itself, not this tool
var skip = AttributeHelper.Get<LocoStructSkipReadAttribute>(p);
if (offsetAttr == null)
if (skip != null)
{
continue;
}
Expand All @@ -133,7 +133,7 @@
}
else
{
args.Add(Activator.CreateInstance(p.PropertyType));

Check warning on line 136 in OpenLocoTool/DatFileParsing/ByteReader.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Possible null reference argument for parameter 'item' in 'void List<object>.Add(object item)'.
}

continue;
Expand Down
23 changes: 17 additions & 6 deletions OpenLocoTool/DatFileParsing/ByteReaderT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ public static class ByteReaderT
public static uint8_t Read_uint8t(ReadOnlySpan<byte> data, int offset)
=> data[offset];

public static MicroZ Read_MicroZ(ReadOnlySpan<byte> data, int offset)
=> data[offset];

public static int8_t Read_int8t(ReadOnlySpan<byte> data, int offset)
=> (sbyte)data[offset];

Expand All @@ -14,6 +17,9 @@ public static uint16_t Read_uint16t(ReadOnlySpan<byte> data, int offset)
public static int16_t Read_int16t(ReadOnlySpan<byte> data, int offset)
=> BitConverter.ToInt16(data[offset..(offset + 2)]);

public static Speed16 Read_Speed16(ReadOnlySpan<byte> data, int offset)
=> BitConverter.ToInt16(data[offset..(offset + 2)]);

public static uint32_t Read_uint32t(ReadOnlySpan<byte> data, int offset)
=> BitConverter.ToUInt32(data[offset..(offset + 4)]);

Expand All @@ -22,39 +28,44 @@ 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))
var type = typeof(T);

if (type == typeof(uint8_t) || type == typeof(MicroZ) || type == typeof(SoundObjectId))
{
return (T)(dynamic)Read_uint8t(data, offset);
}

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

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

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

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

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

throw new NotImplementedException("");
}

// todo:
// static Dictionary cachedTypes

public static T[] Read_Array<T>(ReadOnlySpan<byte> data, int count, int offset = 0) where T : struct
{
var arr = new T[count];
Expand Down
7 changes: 7 additions & 0 deletions OpenLocoTool/DatFileParsing/ByteWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ public static ReadOnlySpan<byte> WriteLocoStruct(ILocoStruct obj)
continue;
}

// skip variable struct loading as they'll be set in special save
var variable = AttributeHelper.Get<LocoStructVariableLoadAttribute>(p);
if (variable != null)
{
continue;
}

// special array handling
var arrLength = 0;
if (p.PropertyType.IsArray)
Expand Down
12 changes: 12 additions & 0 deletions OpenLocoTool/DatFileParsing/DatFileInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.ComponentModel;
using OpenLocoTool.Headers;

namespace OpenLocoTool.DatFileParsing
{
[TypeConverter(typeof(ExpandableObjectConverter))]
public class DatFileInfo(S5Header s5Header, ObjectHeader objectHeader)
{
public S5Header S5Header { get; set; } = s5Header;
public ObjectHeader ObjectHeader { get; set; } = objectHeader;
}
}
3 changes: 0 additions & 3 deletions OpenLocoTool/DatFileParsing/ILocoObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ namespace OpenLocoTool.DatFileParsing
[TypeConverter(typeof(ExpandableObjectConverter))]
public interface ILocoObject
{
S5Header S5Header { get; set; }
ObjectHeader ObjectHeader { get; set; }
ILocoStruct Object { get; set; }
StringTable StringTable { get; set; }
G1Header? G1Header { get; set; }
List<G1Element32>? G1Elements { get; set; }
}
}
49 changes: 1 addition & 48 deletions OpenLocoTool/DatFileParsing/ILocoStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,7 @@

namespace OpenLocoTool.DatFileParsing
{
/*
=== Dat File Format ===
|-File-------------------------------|
|-S5Header-|-DatHeader--|-ObjectData-|

==============================================================================================================

|-S5Header----------------|
|-Flags-|-Name-|-Checksum-|

|-DatHeader-------------|
|-Encoding-|-Datalength-|

|-ObjectData-----------------------------------------|
|-Object-|-StringTable-|-VariableData-|-GraphicsData-|

==============================================================================================================

|-Object-|
-- per-object

|-StringTable-|
|-String{n}---|

|-VariableData-|
-- per-object

|-GraphicsData------------------------------|
|-G1Header-|-G1Element32{n}-|-ImageBytes{n}-|

==============================================================================================================

|-String-----------------|
|-Language-|-StringBytes-|

|-G1Header---------------|
|-NumEntries-|-TotalSize-|

|-G1Element32------------------------------------------------------|
|-Offset-|-Width-|-Height-|-xOffset-|-yOffset-|-Flags-|-ZoomOffset-|

|-ImageBytes-|
-- offset by G1Element32.Offset
*/

[TypeConverter(typeof(ExpandableObjectConverter))]
public interface ILocoStruct
{
static int StructSize { get; }
}
{ }
}
10 changes: 0 additions & 10 deletions OpenLocoTool/DatFileParsing/ILocoStructStringTablePostLoad.cs

This file was deleted.

22 changes: 21 additions & 1 deletion OpenLocoTool/DatFileParsing/LocoAttributes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace OpenLocoTool.DatFileParsing
using OpenLocoTool.Headers;

namespace OpenLocoTool.DatFileParsing
{
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false)]
public class LocoArrayLengthAttribute(int length) : Attribute
Expand All @@ -18,6 +20,12 @@ public class LocoStructSizeAttribute(int size) : Attribute
public int Size => size;
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false)]
public class LocoStructTypeAttribute(ObjectType objectType) : Attribute
{
public ObjectType ObjectType => objectType;
}

// basically a 'skip' attribute to allow deferred loading for variable data
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false)]
public class LocoStructVariableLoadAttribute : Attribute
Expand All @@ -32,4 +40,16 @@ public class LocoStructSkipReadAttribute : Attribute
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false)]
public class LocoStringAttribute : Attribute
{ }

// to mark properties that seemingly have no purpose or use
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false)]
public class LocoPropertyMaybeUnused : Attribute
{ }

// basically a 'skip' attribute to allow deferred loading for variable data, and writing of this property will be 0
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false)]
public class LocoStringTableAttribute(params string[] strings) : Attribute
{
public string[] Strings => strings;
}
}
14 changes: 3 additions & 11 deletions OpenLocoTool/DatFileParsing/LocoObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,25 @@

namespace OpenLocoTool.DatFileParsing
{

[TypeConverter(typeof(ExpandableObjectConverter))]
public class LocoObject : ILocoObject
{
public LocoObject(S5Header s5Hdr, ObjectHeader objHdr, ILocoStruct obj, StringTable stringTable, G1Header? g1Header, List<G1Element32>? g1Elements)
public LocoObject(ILocoStruct obj, StringTable stringTable, List<G1Element32>? g1Elements)
{
S5Header = s5Hdr;
ObjectHeader = objHdr;
Object = obj;
StringTable = stringTable;
G1Header = g1Header;
G1Elements = g1Elements;
}
public LocoObject(S5Header s5Hdr, ObjectHeader objHdr, ILocoStruct obj, StringTable stringTable)
public LocoObject(ILocoStruct obj, StringTable stringTable)
{
S5Header = s5Hdr;
ObjectHeader = objHdr;
Object = obj;
StringTable = stringTable;
G1Header = null;
G1Elements = null;
}

public S5Header S5Header { get; set; }
public ObjectHeader ObjectHeader { get; set; }
public ILocoStruct Object { get; set; }
public StringTable StringTable { get; set; }
public G1Header? G1Header { get; set; }
public List<G1Element32>? G1Elements { get; set; }
}
}
10 changes: 6 additions & 4 deletions OpenLocoTool/DatFileParsing/ObjectAnnotator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static IList<Annotation> AnnotateG1Data(byte[] fullData, int runningCount

for (var i = 0; i < g1Header.NumEntries; i++)
{
var g32Element = (G1Element32)ByteReader.ReadLocoStruct<G1Element32>(fullData.AsSpan()[runningCount..]);
var g32Element = ByteReader.ReadLocoStruct<G1Element32>(fullData.AsSpan()[runningCount..]);
var g32ElementAnnotation = new Annotation("Header " + (i + 1), gHeadersAnnotation, runningCount, G1Element32.StructLength);

annotations.Add(g32ElementAnnotation);
Expand Down Expand Up @@ -142,11 +142,13 @@ public static int AnnotateStringTable(byte[] fullData, int runningCount, ILocoSt
var root = new Annotation("String Table", runningCount, 1);
annotations.Add(root);

var stringAttributes = AttributeHelper.GetAllPropertiesWithAttribute<LocoStringAttribute>(locoStruct.GetType());
//var stringsInTable = stringAttributes?.Count ?? 0;
var locoStructType = locoStruct.GetType();
var stringTableStrings = AttributeHelper.Has<LocoStringTableAttribute>(locoStructType)
? AttributeHelper.Get<LocoStringTableAttribute>(locoStructType)!.Strings
: AttributeHelper.GetAllPropertiesWithAttribute<LocoStringAttribute>(locoStructType).Select(s => s.Name).ToArray();

var i = 0;
foreach (var locoString in stringAttributes)
foreach (var locoString in stringTableStrings)
{
var index = 0;
var continuing = true;
Expand Down
Loading
Loading