mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
Fix xml loading
This commit is contained in:
1 parent
47582897a8
commit
5b006ab7d3
1 file changed
+43
-24
@@ -3,7 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Xml.Linq;
|
using System.Xml;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace MiraAPI.Translation;
|
namespace MiraAPI.Translation;
|
||||||
@@ -180,12 +180,18 @@ public static class MiraLocaleManager
|
|||||||
private static void LoadInternalStrings(Assembly assembly, string modGuid, string dir)
|
private static void LoadInternalStrings(Assembly assembly, string modGuid, string dir)
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(dir);
|
Directory.CreateDirectory(dir);
|
||||||
|
|
||||||
|
if (!Locale.ContainsKey(modGuid))
|
||||||
|
{
|
||||||
|
Locale[modGuid] = [];
|
||||||
|
}
|
||||||
|
|
||||||
var resourcePrefix = $"{assembly.GetName().Name}.Resources.Locale.";
|
var resourcePrefix = $"{assembly.GetName().Name}.Resources.Locale.";
|
||||||
|
|
||||||
foreach (var locale in LangList)
|
foreach (var locale in LangList)
|
||||||
{
|
{
|
||||||
using var resourceStream =
|
using var resourceStream =
|
||||||
assembly.GetManifestResourceStream(resourcePrefix + locale.Value);
|
assembly.GetManifestResourceStream(resourcePrefix + locale.Value + ".xml");
|
||||||
if (resourceStream == null)
|
if (resourceStream == null)
|
||||||
{
|
{
|
||||||
// Silently skipped
|
// Silently skipped
|
||||||
@@ -202,7 +208,7 @@ public static class MiraLocaleManager
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Error($"Failed to load translation {resourcePrefix}.{locale.Value}: {e.Message}");
|
Error($"Failed to load translation {resourcePrefix}{locale.Value}: {e.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,33 +239,46 @@ public static class MiraLocaleManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Dictionary<string, string> ParseXmlFile(string filePath)
|
private static Dictionary<string, string> ParseXmlFile(string xmlContent)
|
||||||
{
|
{
|
||||||
var dict = new Dictionary<string, string>();
|
var dict = new Dictionary<string, string>();
|
||||||
var doc = XDocument.Load(filePath);
|
XmlDocument xmlDoc = new();
|
||||||
|
try
|
||||||
if (doc.Root == null) return dict;
|
|
||||||
|
|
||||||
foreach (var element in doc.Root.Elements("string"))
|
|
||||||
{
|
{
|
||||||
var name = element.Attribute("name")?.Value;
|
xmlDoc.LoadXml(xmlContent);
|
||||||
var value = element.Value;
|
XmlNodeList? stringNodes = xmlDoc.SelectNodes("/resources/string");
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(name)) continue;
|
if (stringNodes != null)
|
||||||
|
|
||||||
if (value.Contains('['))
|
|
||||||
{
|
{
|
||||||
value = value.Replace("[", "<");
|
foreach (XmlNode node in stringNodes)
|
||||||
|
{
|
||||||
|
if (node.Attributes?["name"] != null)
|
||||||
|
{
|
||||||
|
string name = node.Attributes["name"]!.Value;
|
||||||
|
string value = node.InnerText;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(name)) continue;
|
||||||
|
|
||||||
|
if (value.Contains('['))
|
||||||
|
{
|
||||||
|
value = value.Replace("[", "<");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value.Contains(']'))
|
||||||
|
{
|
||||||
|
value = value.Replace("]", ">");
|
||||||
|
}
|
||||||
|
|
||||||
|
value = value.Replace("<nl>", "\n").Replace("<and>", "&");
|
||||||
|
|
||||||
|
dict[name] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (value.Contains(']'))
|
catch (XmlException ex)
|
||||||
{
|
{
|
||||||
value = value.Replace("]", ">");
|
Error($"XML parsing error: {ex.Message}");
|
||||||
}
|
|
||||||
|
|
||||||
value = value.Replace("<nl>", "\n").Replace("<and>", "&");
|
|
||||||
|
|
||||||
dict[name] = value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return dict;
|
return dict;
|
||||||
|
|||||||
Reference in new issue
Block a user