From 5b006ab7d3cdc10f3dd7d05ea1777938060faa44 Mon Sep 17 00:00:00 2001 From: Atony <75191600+AtonyGit@users.noreply.github.com> Date: Tue, 25 Aug 2026 01:04:11 -0500 Subject: [PATCH] Fix xml loading --- MiraAPI/Translation/MiraLocaleManager.cs | 67 +++++++++++++++--------- 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/MiraAPI/Translation/MiraLocaleManager.cs b/MiraAPI/Translation/MiraLocaleManager.cs index 5914c07..ee6b1d8 100644 --- a/MiraAPI/Translation/MiraLocaleManager.cs +++ b/MiraAPI/Translation/MiraLocaleManager.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; -using System.Xml.Linq; +using System.Xml; using UnityEngine; namespace MiraAPI.Translation; @@ -180,12 +180,18 @@ public static class MiraLocaleManager private static void LoadInternalStrings(Assembly assembly, string modGuid, string dir) { Directory.CreateDirectory(dir); + + if (!Locale.ContainsKey(modGuid)) + { + Locale[modGuid] = []; + } + var resourcePrefix = $"{assembly.GetName().Name}.Resources.Locale."; foreach (var locale in LangList) { using var resourceStream = - assembly.GetManifestResourceStream(resourcePrefix + locale.Value); + assembly.GetManifestResourceStream(resourcePrefix + locale.Value + ".xml"); if (resourceStream == null) { // Silently skipped @@ -202,7 +208,7 @@ public static class MiraLocaleManager } 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 ParseXmlFile(string filePath) + private static Dictionary ParseXmlFile(string xmlContent) { var dict = new Dictionary(); - var doc = XDocument.Load(filePath); - - if (doc.Root == null) return dict; - - foreach (var element in doc.Root.Elements("string")) + XmlDocument xmlDoc = new(); + try { - var name = element.Attribute("name")?.Value; - var value = element.Value; + xmlDoc.LoadXml(xmlContent); + XmlNodeList? stringNodes = xmlDoc.SelectNodes("/resources/string"); - if (string.IsNullOrEmpty(name)) continue; - - if (value.Contains('[')) + if (stringNodes != null) { - 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("", "\n").Replace("", "&"); + + dict[name] = value; + } + } } - - if (value.Contains(']')) - { - value = value.Replace("]", ">"); - } - - value = value.Replace("", "\n").Replace("", "&"); - - dict[name] = value; + } + catch (XmlException ex) + { + Error($"XML parsing error: {ex.Message}"); } return dict;