Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
LeftofZen committed Dec 21, 2023
1 parent 921946d commit faf844e
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 33 deletions.
3 changes: 1 addition & 2 deletions OpenLocoTool/DatFileParsing/ByteReader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace OpenLocoTool.DatFileParsing
{

public static class ByteReader
{
public static object ReadT(ReadOnlySpan<byte> data, Type t, int offset, int arrLength = 0)
Expand Down Expand Up @@ -120,7 +119,7 @@ public static ILocoStruct ReadLocoStruct(ReadOnlySpan<byte> data, Type t)
var arrLength = 0;
if (p.PropertyType.IsArray)
{
var arrLengthAttr = AttributeHelper.Get<LocoArrayLengthAttribute>(p) ?? throw new ArgumentOutOfRangeException(nameof(LocoArrayLengthAttribute), $"type {t} with property {p} didn't have LocoArrayLength attribute specified");
var arrLengthAttr = AttributeHelper.Get<LocoArrayLengthAttribute>(p) ?? throw new ArgumentOutOfRangeException(nameof(data), $"type {t} with property {p} didn't have LocoArrayLength attribute specified");
arrLength = arrLengthAttr.Length;
}

Expand Down
9 changes: 0 additions & 9 deletions OpenLocoTool/DatFileParsing/ByteWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,32 @@ public static void WriteT(Span<byte> data, Type t, int offset, object val)
{
ByteWriterT.Write(data, offset, (uint8_t)(dynamic)val);
}

else if (t == typeof(int8_t))
{
ByteWriterT.Write(data, offset, (int8_t)(dynamic)val);
}

else if (t == typeof(uint16_t))
{
ByteWriterT.Write(data, offset, (uint16_t)(dynamic)val);
}

else if (t == typeof(int16_t))
{
ByteWriterT.Write(data, offset, (int16_t)(dynamic)val);
}

else if (t == typeof(uint32_t))
{
ByteWriterT.Write(data, offset, (uint32_t)(dynamic)val);
}

else if (t == typeof(int32_t))
{
ByteWriterT.Write(data, offset, (int32_t)(dynamic)val);
}

else if (t == typeof(string_id))
{
// string ids should always be 0 in the dat file - they're only set when loaded into memory and never saved
val = 0;
ByteWriterT.Write(data, offset, (string_id)(dynamic)val);
}

else if (t.IsArray)
{
var elementType = t.GetElementType() ?? throw new NullReferenceException();
Expand All @@ -53,14 +46,12 @@ public static void WriteT(Span<byte> data, Type t, int offset, object val)
WriteT(data, elementType, offset + (i * size), value);
}
}

else if (t.IsEnum)
{
var underlyingType = t.GetEnumUnderlyingType();
var underlyingValue = Convert.ChangeType(val, underlyingType);
WriteT(data, underlyingType, offset, underlyingValue);
}

else if (t.IsClass)
{
var objectSize = ByteHelpers.GetObjectSize(t);
Expand Down
4 changes: 2 additions & 2 deletions OpenLocoTool/DatFileParsing/ILocoObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface ILocoObject
ObjectHeader ObjectHeader { get; set; }
ILocoStruct Object { get; set; }
StringTable StringTable { get; set; }
G1Header G1Header { get; set; }
List<G1Element32> G1Elements { get; set; }
G1Header? G1Header { get; set; }
List<G1Element32>? G1Elements { get; set; }
}
}
4 changes: 2 additions & 2 deletions OpenLocoTool/DatFileParsing/SawyerStreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace OpenLocoTool.DatFileParsing
{
public class SawyerStreamReader(ILogger logger)
public static class SawyerStreamReader
{
static uint ComputeObjectChecksum(ReadOnlySpan<byte> flagByte, ReadOnlySpan<byte> name, ReadOnlySpan<byte> data)
{
Expand Down Expand Up @@ -107,7 +107,7 @@ public static ILocoObject LoadFull(string filename, ILogger? logger = null, bool
remainingData = locoStructExtra.Load(remainingData);
}

LocoObject newObj = null;
LocoObject? newObj;
try
{
// some objects have graphics data
Expand Down
4 changes: 2 additions & 2 deletions OpenLocoTool/Headers/G1Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public record G1Element32(
) : ILocoStruct
{
public static int StructLength => 0x10;
public byte[] ImageData;
public byte[] ImageData = [];

public ReadOnlySpan<byte> Write()
{
Expand Down Expand Up @@ -67,6 +67,6 @@ public record G1Header(
) : ILocoStruct
{
public static int StructLength => 0x08;
public byte[] ImageData;
public byte[] ImageData = [];
}
}
1 change: 0 additions & 1 deletion OpenLocoTool/Objects/BuildingObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public ReadOnlySpan<byte> Load(ReadOnlySpan<byte> remainingData)
remainingData = remainingData[(S5Header.StructLength * var_A4.Length)..];

// load ??
var ptr_AD = 0;
for (var i = 0; i < var_AD; ++i)
{
var size = BitConverter.ToUInt16(remainingData[..2]);
Expand Down
2 changes: 1 addition & 1 deletion OpenLocoTool/Objects/TrainStationObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public record TrainStationObject(
public const int Var6ELength = 16;

uint8_t[,] CargoOffsetBytes { get; set; }
uint8_t[] var_6E { get; set; }
uint8_t[] var_6E { get; set; } = [];

public ReadOnlySpan<byte> Load(ReadOnlySpan<byte> remainingData)
{
Expand Down
19 changes: 6 additions & 13 deletions OpenLocoToolGui/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@

namespace OpenLocoToolGui
{
// how this program works
//
// 1. open UI, no loading
// 2. user selects a directory
// 3. if no open-loco-tool index file exists, open-loco-tool fully loads all dat files in directory, creates an index and writes it to `objectIndex.json` in that folder. this is SLOW (currently)
// 4. next time that directory is opened, the index is read instead of loading all files. this is FAST

public partial class MainForm : Form
{
readonly MainFormModel model;
Expand Down Expand Up @@ -364,7 +357,7 @@ private void saveChangesToolStripMenuItem_Click(object sender, EventArgs e)
try
{
var exists = File.Exists(filename);
model.SaveFile(filename, obj);
MainFormModel.SaveFile(filename, obj);

if (!exists)
{
Expand Down Expand Up @@ -731,11 +724,11 @@ private void RefreshObjectUI()

flpImageTable.ResumeLayout(true);

pgS5Header.SelectedObject = CurrentUIObject.S5Header;
pgObjHeader.SelectedObject = CurrentUIObject.ObjectHeader;
pgObject.SelectedObject = CurrentUIObject.Object;
ucStringTable.SetDataBinding(CurrentUIObject.StringTable);
//pgS5Header.SelectedObject = CurrentUIObject; // dont above with flpImageTable
pgS5Header.SelectedObject = CurrentUIObject?.S5Header;
pgObjHeader.SelectedObject = CurrentUIObject?.ObjectHeader;
pgObject.SelectedObject = CurrentUIObject?.Object;
ucStringTable.SetDataBinding(CurrentUIObject?.StringTable);
//pgS5Header.SelectedObject = CurrentUIObject; // done above with flpImageTable
}

private void imgContextMenuSave_Click(object sender, EventArgs e)
Expand Down
2 changes: 1 addition & 1 deletion OpenLocoToolGui/MainFormModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void CreateIndex(string[] allFiles, IProgress<float> progress)
logger.Info($"Finished creating index. Time={sw.Elapsed}");
}

public void SaveFile(string path, ILocoObject obj)
public static void SaveFile(string path, ILocoObject obj)
=> SawyerStreamWriter.Save(path, obj);

public bool LoadDataDirectory(string directory)
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# OpenLocoTool
A modern implementation of 'LocoTool' for dat file parsing

# GUI

1. open UI, no loading
2. user selects a directory
3. if no open-loco-tool index file exists, open-loco-tool fully loads all dat files in directory, creates an index and writes it to `objectIndex.json` in that folder. this is SLOW (currently)
4. next time that directory is opened, the index is read instead of loading all files. this is FAST

# Dat Object Layout

|-File-------------------------------|
Expand Down

0 comments on commit faf844e

Please sign in to comment.