Add an init method for UI text content & method to read text tables

This commit is contained in:
Thorsten Sommer 2025-03-22 14:24:45 +01:00
parent 5eafb20e07
commit 568a4270ae
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -468,6 +468,54 @@ public abstract class PluginBase
message = string.Empty; message = string.Empty;
return true; return true;
} }
/// <summary>
/// Tries to initialize the UI text content of the plugin.
/// </summary>
/// <param name="message">The error message, when the UI text content could not be read.</param>
/// <param name="pluginContent">The read UI text content.</param>
/// <returns>True, when the UI text content could be read successfully.</returns>
protected bool TryInitUITextContent(out string message, out Dictionary<string, string> pluginContent)
{
if (!this.state.Environment["UI_TEXT_CONTENT"].TryRead<LuaTable>(out var textTable))
{
message = "The UI_TEXT_CONTENT table does not exist or is not a valid table.";
pluginContent = [];
return false;
}
this.ReadTextTable("root", textTable, out pluginContent);
message = string.Empty;
return true;
}
/// <summary>
/// Reads a flat or hierarchical text table.
/// </summary>
/// <param name="parent">The parent key(s).</param>
/// <param name="table">The table to read.</param>
/// <param name="tableContent">The read table content.</param>
protected void ReadTextTable(string parent, LuaTable table, out Dictionary<string, string> tableContent)
{
tableContent = [];
var lastKey = LuaValue.Nil;
while (table.TryGetNext(lastKey, out var pair))
{
var keyText = pair.Key.ToString();
if (pair.Value.TryRead<string>(out var value))
tableContent[$"{parent}::{keyText}"] = value;
else if (pair.Value.TryRead<LuaTable>(out var t))
{
this.ReadTextTable($"{parent}::{keyText}", t, out var subContent);
foreach (var (k, v) in subContent)
tableContent[k] = v;
}
lastKey = pair.Key;
}
}
#endregion #endregion
} }