From 8e54b89680cc197e766d210d76bc0958ff7aeada Mon Sep 17 00:00:00 2001 From: Benjamin Sutas Date: Wed, 15 Nov 2023 22:16:58 +1100 Subject: [PATCH] use full classes for stringtable --- .../DatFileParsing/SawyerStreamReader.cs | 7 ++--- OpenLocoTool/DatFileParsing/Typedefs.cs | 29 +++++++++++++++++-- OpenLocoToolGui/StringTableUserControl.cs | 3 +- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/OpenLocoTool/DatFileParsing/SawyerStreamReader.cs b/OpenLocoTool/DatFileParsing/SawyerStreamReader.cs index e75cc596..ae25f68e 100644 --- a/OpenLocoTool/DatFileParsing/SawyerStreamReader.cs +++ b/OpenLocoTool/DatFileParsing/SawyerStreamReader.cs @@ -123,13 +123,10 @@ public static ILocoObject LoadFull(string filename, ILogger? logger = null, bool var lang = (LanguageId)data[ptr++]; var ini = ptr; - while (data[ptr++] != '\0') - { - ; - } + while (data[ptr++] != '\0') ; var str = Encoding.ASCII.GetString(data[ini..(ptr - 1)]); // do -1 to exclude the \0 - if (!languageDict.TryAdd(lang, str)) + if (!languageDict.TryAdd(lang, new StringTableEntry { String = str })) { //Logger.Error($"Key {(i, lang)} already exists (this shouldn't happen)"); break; diff --git a/OpenLocoTool/DatFileParsing/Typedefs.cs b/OpenLocoTool/DatFileParsing/Typedefs.cs index ba4f3565..3816609b 100644 --- a/OpenLocoTool/DatFileParsing/Typedefs.cs +++ b/OpenLocoTool/DatFileParsing/Typedefs.cs @@ -9,7 +9,6 @@ global using Speed32 = System.Int32; global using MicroZ = System.Byte; global using SoundObjectId = System.Byte; -global using StringTable = System.Collections.Generic.Dictionary>; using System.ComponentModel; namespace OpenLocoTool.DatFileParsing @@ -23,4 +22,30 @@ public record Pos2( { public static int StructSize => 0x04; } -} \ No newline at end of file + + // this is only required because WinForms' DataGridView cannot handle plain old dictionary, because string type doesn't have the getter+setter it needs + //public class StringTable + //{ + // Dictionary table = new(); + //} + + public class StringTable + { + public Dictionary> table { get; set; } = new(); + + public void Add(string key, Dictionary value) => table.Add(key, value); + + public Dictionary this[string key] + { + get => table[key]; + set => table[key] = value; + } + + public Dictionary>.KeyCollection Keys => table.Keys; + } + + public class StringTableEntry + { + public string String { get; set; } + } +} diff --git a/OpenLocoToolGui/StringTableUserControl.cs b/OpenLocoToolGui/StringTableUserControl.cs index c53f9752..8145c45c 100644 --- a/OpenLocoToolGui/StringTableUserControl.cs +++ b/OpenLocoToolGui/StringTableUserControl.cs @@ -1,4 +1,5 @@ using OpenLocoTool; +using OpenLocoTool.DatFileParsing; using System.ComponentModel; namespace OpenLocoToolGui @@ -9,7 +10,7 @@ public partial class StringTableUserControl : UserControl public StringTableUserControl() => InitializeComponent(); - public void SetDataBinding(Dictionary> data) + public void SetDataBinding(StringTable data) { blKeys.Clear(); foreach (var key in data.Keys)