Skip to content

Commit

Permalink
use full classes for stringtable
Browse files Browse the repository at this point in the history
  • Loading branch information
LeftofZen committed Nov 15, 2023
1 parent 582066b commit 8e54b89
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
7 changes: 2 additions & 5 deletions OpenLocoTool/DatFileParsing/SawyerStreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 27 additions & 2 deletions OpenLocoTool/DatFileParsing/Typedefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, System.Collections.Generic.Dictionary<OpenLocoTool.LanguageId, string>>;
using System.ComponentModel;

namespace OpenLocoTool.DatFileParsing
Expand All @@ -23,4 +22,30 @@ public record Pos2(
{
public static int StructSize => 0x04;
}
}

// this is only required because WinForms' DataGridView cannot handle plain old dictionary<foo, string>, because string type doesn't have the getter+setter it needs
//public class StringTable
//{
// Dictionary<string, StringTableInner> table = new();
//}

public class StringTable
{
public Dictionary<string, Dictionary<LanguageId, StringTableEntry>> table { get; set; } = new();

public void Add(string key, Dictionary<LanguageId, StringTableEntry> value) => table.Add(key, value);

public Dictionary<LanguageId, StringTableEntry> this[string key]
{
get => table[key];
set => table[key] = value;
}

public Dictionary<string, Dictionary<LanguageId, StringTableEntry>>.KeyCollection Keys => table.Keys;
}

public class StringTableEntry
{
public string String { get; set; }
}
}
3 changes: 2 additions & 1 deletion OpenLocoToolGui/StringTableUserControl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OpenLocoTool;
using OpenLocoTool.DatFileParsing;
using System.ComponentModel;

namespace OpenLocoToolGui
Expand All @@ -9,7 +10,7 @@ public partial class StringTableUserControl : UserControl

public StringTableUserControl() => InitializeComponent();

public void SetDataBinding(Dictionary<string, Dictionary<LanguageId, string>> data)
public void SetDataBinding(StringTable data)
{
blKeys.Clear();
foreach (var key in data.Keys)
Expand Down

0 comments on commit 8e54b89

Please sign in to comment.