Skip to content

Commit

Permalink
Merge branch 'diff_map_strings'
Browse files Browse the repository at this point in the history
  • Loading branch information
audinowho committed May 11, 2024
2 parents 40b5248 + 8531612 commit 6d11bca
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private void populateStringTable(IList<MapString> strings, bool skipTopMod)
Dictionary<string, Dictionary<string, (string val, string comment)>> rawStrings = new Dictionary<string, Dictionary<string, (string, string)>>();


string FMTStr = String.Format("{0}{1}.{2}", Name, "{0}", ScriptStrings.STRINGS_FILE_EXT);
string FMTStr = String.Format("{0}{1}{2}", Name, "{0}", Text.STRINGS_FILE_EXT);
foreach (string code in Text.SupportedLangs)
{
string fname = String.Format(FMTStr, code == "en" ? "" : ("." + code));//special case for english, which is default
Expand Down Expand Up @@ -151,7 +151,7 @@ public void SaveStrings()
{
string stringsdir = "Strings/";

string FMTStr = String.Format("{0}{1}.{2}", Name, "{0}", Script.ScriptStrings.STRINGS_FILE_EXT);
string FMTStr = String.Format("{0}{1}{2}", Name, "{0}", Text.STRINGS_FILE_EXT);
foreach (string code in Text.SupportedLangs)
{
string fname = String.Format(FMTStr, code == "en" ? "" : ("." + code));//special case for english, which is default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void LoadStrings()
//Clear old strings
Dictionary<string, Dictionary<string, (string val, string comment)>> rawStrings = new Dictionary<string, Dictionary<string, (string, string)>>();

string FMTStr = String.Format("{0}{1}.{2}", ScriptStrings.STRINGS_FILE_NAME, "{0}", ScriptStrings.STRINGS_FILE_EXT);
string FMTStr = String.Format("{0}{1}{2}", ScriptStrings.STRINGS_FILE_NAME, "{0}", Text.STRINGS_FILE_EXT);
foreach (string code in Text.SupportedLangs)
{
string fname = String.Format(FMTStr, code == "en" ? "" : ("." + code));//special case for english, which is default
Expand Down Expand Up @@ -115,7 +115,7 @@ public void SaveStrings()
{
string stringsdir = LuaEngine.MakeGroundMapScriptPath(true, ZoneManager.Instance.CurrentGround.AssetName, "");

string FMTStr = String.Format("{0}{1}.{2}", Script.ScriptStrings.STRINGS_FILE_NAME, "{0}", Script.ScriptStrings.STRINGS_FILE_EXT);
string FMTStr = String.Format("{0}{1}{2}", Script.ScriptStrings.STRINGS_FILE_NAME, "{0}", Text.STRINGS_FILE_EXT);
foreach (string code in Text.SupportedLangs)
{
string fname = String.Format(FMTStr, code == "en" ? "" : ("." + code));//special case for english, which is default
Expand Down
4 changes: 2 additions & 2 deletions RogueEssence/Lua/ScriptServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public string CurrentScriptDir()
{
//We can get the path 3 ways.
if (ZoneManager.Instance.CurrentGround != null)
return Path.GetDirectoryName(LuaEngine.MakeGroundMapScriptPath(false, ZoneManager.Instance.CurrentGround.AssetName, "/init.lua"));
return Path.Join(LuaEngine.SCRIPT_PATH, string.Format(LuaEngine.MAP_SCRIPT_PATTERN, ZoneManager.Instance.CurrentGround.AssetName).Replace('.', '/'), "/");
else if (ZoneManager.Instance.CurrentMap != null)
return Path.GetDirectoryName(LuaEngine.MakeDungeonMapScriptPath(false, ZoneManager.Instance.CurrentMap.AssetName, "/init.lua"));
return Path.Join(LuaEngine.SCRIPT_PATH, string.Format(LuaEngine.DUNGEON_MAP_SCRIPT_PATTERN, ZoneManager.Instance.CurrentMap.AssetName).Replace('.', '/'), "/");
else
throw new Exception("ScriptServices.CurrentScriptDir(): No map lua package currently loaded! And no map currently loaded either! Cannot assemble the current package path!");
}
Expand Down
55 changes: 43 additions & 12 deletions RogueEssence/Lua/ScriptStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,58 @@ namespace RogueEssence.Script
public class ScriptStrings : ILuaEngineComponent
{
public const string STRINGS_FILE_NAME = "strings";
public const string STRINGS_FILE_EXT = "resx";

public LuaTable MakePackageStringTable(string packagefilepath)
{
LuaTable tbl = loadXmlDoc(packagefilepath + "/" + STRINGS_FILE_NAME + "." + LocaleCode() + "." + STRINGS_FILE_EXT);
LuaTable defaulttbl = loadXmlDoc(packagefilepath + "/" + STRINGS_FILE_NAME + "." + STRINGS_FILE_EXT);
LuaFunction addmeta = LuaEngine.Instance.RunString("return function(tbl1, tbl2) setmetatable(tbl1, { __index = tbl2 }) end").First() as LuaFunction;
addmeta.Call(tbl, defaulttbl);

return tbl;
}

private LuaTable loadXmlDoc(string path)
{
try
{
Dictionary<string, string> xmlDict = new Dictionary<string, string>();

string code = LocaleCode();
//order of string fallbacks:
//first go through all mods of the original language
foreach (string path in PathMod.FallbackPaths(packagefilepath + "/" + STRINGS_FILE_NAME + "." + code + ".resx"))
{
Dictionary<string, string> dict = Text.LoadStringResx(path);
foreach (string key in dict.Keys)
{
if (!xmlDict.ContainsKey(key))
xmlDict.Add(key, dict[key]);
}
}

//then go through all mods of the official fallbacks
if (Text.LangNames.ContainsKey(code))
{
foreach (string fallback in Text.LangNames[code].Fallbacks)
{
foreach (string path in PathMod.FallbackPaths(packagefilepath + "/" + STRINGS_FILE_NAME + "." + fallback + ".resx"))
{
Dictionary<string, string> dict = Text.LoadStringResx(path);
foreach (string key in dict.Keys)
{
if (!xmlDict.ContainsKey(key))
xmlDict.Add(key, dict[key]);
}
}
}
}
//then go through all mods of the default language
foreach (string path in PathMod.FallbackPaths(packagefilepath + "/" + STRINGS_FILE_NAME + ".resx"))
{
Dictionary<string, string> dict = Text.LoadStringResx(path);
foreach (string key in dict.Keys)
{
if (!xmlDict.ContainsKey(key))
xmlDict.Add(key, dict[key]);
}
}


//Build a lua table as we go and return it
LuaTable tbl = LuaEngine.Instance.RunString("return {}").First() as LuaTable;
LuaFunction addfn = LuaEngine.Instance.RunString("return function(tbl, key, str) tbl[key] = str end").First() as LuaFunction;

Dictionary<string, string> xmlDict = Text.LoadStringResx(path);
foreach (string name in xmlDict.Keys)
addfn.Call(tbl, name, xmlDict[name]);

Expand Down
8 changes: 5 additions & 3 deletions RogueEssence/Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public LanguageSetting(string name, List<string> fallbacks)
public static class Text
{
public const string DIVIDER_STR = "\n";
public const string STRINGS_FILE_EXT = ".resx";

public static Dictionary<string, string> Strings;
public static Dictionary<string, string> StringsEx;
public static CultureInfo Culture;
Expand Down Expand Up @@ -622,7 +624,7 @@ private static void loadCulture(Dictionary<string, string> strings, string code,
strings.Clear();
//order of string fallbacks:
//first go through all mods of the original language
foreach (string path in PathMod.FallbackPaths("Strings/" + fileName + "." + code + ".resx"))
foreach (string path in PathMod.FallbackPaths("Strings/" + fileName + "." + code + STRINGS_FILE_EXT))
{
Dictionary<string, string> dict = LoadStringResx(path);
foreach (string key in dict.Keys)
Expand All @@ -637,7 +639,7 @@ private static void loadCulture(Dictionary<string, string> strings, string code,
{
foreach (string fallback in LangNames[code].Fallbacks)
{
foreach (string path in PathMod.FallbackPaths("Strings/" + fileName + "." + fallback + ".resx"))
foreach (string path in PathMod.FallbackPaths("Strings/" + fileName + "." + fallback + STRINGS_FILE_EXT))
{
Dictionary<string, string> dict = LoadStringResx(path);
foreach (string key in dict.Keys)
Expand All @@ -649,7 +651,7 @@ private static void loadCulture(Dictionary<string, string> strings, string code,
}
}
//then go through all mods of the default language
foreach (string path in PathMod.FallbackPaths("Strings/" + fileName + ".resx"))
foreach (string path in PathMod.FallbackPaths("Strings/" + fileName + STRINGS_FILE_EXT))
{
Dictionary<string, string> dict = LoadStringResx(path);
foreach (string key in dict.Keys)
Expand Down

0 comments on commit 6d11bca

Please sign in to comment.