Skip to content

Commit

Permalink
Handle empty keys and values
Browse files Browse the repository at this point in the history
  • Loading branch information
burninrubber0 committed Dec 7, 2023
1 parent 97bbbde commit c8d7811
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
22 changes: 16 additions & 6 deletions LangEditor/LangEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,32 @@ public void UpdateDisplay()
MessageBoxButtons.OK);
}

public void RebuildLanguage()
public int RebuildLanguage()
{
if (_ignoreChanges)
return;
return 1;

Dictionary<uint, string> data = new Dictionary<uint, string>();

for (int i = 0; i < dgvMain.Rows.Count - 1; ++i)
{
string idString = (string)dgvMain.Rows[i].Cells[0].Value;

if (idString == null)
{
MessageBox.Show(this, "ID cannot be left blank", "Warning",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return 2;
}

uint id;
if (idString.StartsWith("0x") && idString.Length == 10) // Hash
{
if (!uint.TryParse(idString[2..], NumberStyles.AllowHexSpecifier, CultureInfo.CurrentCulture, out id))
{
MessageBox.Show(this, "Failed to parse ID \"" + idString + "\"", "Warning",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
return 3;
}
}
else // String
Expand All @@ -144,14 +151,16 @@ public void RebuildLanguage()
{
MessageBox.Show(this, "ID " + idString + " already in use", "Warning",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
return 4;
}
data.Add(id, value);
}

_lang.mpEntries = data;

Changed?.Invoke();

return 0;
}

private void importToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -232,9 +241,10 @@ private void exportToolStripMenuItem_Click(object sender, EventArgs e)

private void applyChangesToolStripMenuItem_Click(object sender, EventArgs e)
{
RebuildLanguage();
int result = RebuildLanguage();

MessageBox.Show(this, "Done!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (result == 0)
MessageBox.Show(this, "Done!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void findToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down
3 changes: 2 additions & 1 deletion LangEditor/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public bool Write(BundleEntry entry)
bw.Write((uint)ms.Length); // String pointer
lastPos = ms.Position;
ms.Position = ms.Length;
bw.Write(langEntry.Value.ToCharArray()); // String data
if (langEntry.Value != null)
bw.Write(langEntry.Value.ToCharArray()); // String data
bw.Write((byte)0); // Null terminator
ms.Position = lastPos;
}
Expand Down

0 comments on commit c8d7811

Please sign in to comment.