diff --git a/src/Bannerlord.LauncherManager.Native/Bindings.LauncherManager.cs b/src/Bannerlord.LauncherManager.Native/Bindings.LauncherManager.cs index be0fced..4a44de9 100644 --- a/src/Bannerlord.LauncherManager.Native/Bindings.LauncherManager.cs +++ b/src/Bannerlord.LauncherManager.Native/Bindings.LauncherManager.cs @@ -111,8 +111,7 @@ public static unsafe partial class Bindings if (p_handle is null || LauncherManagerHandlerNative.FromPointer(p_handle) is not { } handler) return return_value_void.AsError(BUTR.NativeAOT.Shared.Utils.Copy("Handler is null or wrong!", false), false); - var doc = new XmlDocument(); - doc.LoadXml(new string(param_string.ToSpan(p_xml))); + var doc = ReaderUtils2.Read(param_string.ToSpan(p_xml)); BUTRLocalizationManager.LoadLanguage(doc); Logger.LogOutput(); diff --git a/src/Bannerlord.LauncherManager.Native/Bindings.ModuleManager.cs b/src/Bannerlord.LauncherManager.Native/Bindings.ModuleManager.cs index 2673eb9..b2b08fa 100644 --- a/src/Bannerlord.LauncherManager.Native/Bindings.ModuleManager.cs +++ b/src/Bannerlord.LauncherManager.Native/Bindings.ModuleManager.cs @@ -8,6 +8,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Xml; +using Bannerlord.LauncherManager.Utils; namespace Bannerlord.LauncherManager.Native; @@ -369,8 +370,7 @@ public static unsafe partial class Bindings Logger.LogInput(p_xml_content); try { - var doc = new XmlDocument(); - doc.LoadXml(new string(param_string.ToSpan(p_xml_content))); + var doc = ReaderUtils2.Read(param_string.ToSpan(p_xml_content)); var result = ModuleInfoExtended.FromXml(doc); @@ -390,8 +390,7 @@ public static unsafe partial class Bindings Logger.LogInput(p_xml_content, p_path); try { - var doc = new XmlDocument(); - doc.LoadXml(new string(param_string.ToSpan(p_xml_content))); + var doc = ReaderUtils2.Read(param_string.ToSpan(p_xml_content)); var module = ModuleInfoExtended.FromXml(doc); var result = new ModuleInfoExtendedWithPath(module, new string(param_string.ToSpan(p_path))); @@ -411,7 +410,7 @@ public static unsafe partial class Bindings Logger.LogInput(p_xml_content, p_path); try { - var doc = new XmlDocument(); + var doc = ReaderUtils2.Read(param_string.ToSpan(p_xml_content)); doc.LoadXml(new string(param_string.ToSpan(p_xml_content))); var module = ModuleInfoExtended.FromXml(doc); @@ -433,8 +432,7 @@ public static unsafe partial class Bindings Logger.LogInput(p_xml_content); try { - var doc = new XmlDocument(); - doc.LoadXml(new string(param_string.ToSpan(p_xml_content))); + var doc = ReaderUtils2.Read(param_string.ToSpan(p_xml_content)); var result = SubModuleInfoExtended.FromXml(doc); if (result is null) diff --git a/src/Bannerlord.LauncherManager.Native/ReaderUtils.cs b/src/Bannerlord.LauncherManager.Native/ReaderUtils.cs new file mode 100644 index 0000000..c3d0b2c --- /dev/null +++ b/src/Bannerlord.LauncherManager.Native/ReaderUtils.cs @@ -0,0 +1,26 @@ +using System; +using System.Runtime.InteropServices; +using System.Text; +using System.Xml; + +namespace Bannerlord.LauncherManager.Native; + +public static class ReaderUtils2 +{ + private static readonly byte[] BOMMarkUtf8 = Encoding.UTF8.GetPreamble(); + + public static XmlDocument Read(ReadOnlySpan xml) + { + var doc = new XmlDocument(); + doc.LoadXml(new string(RemoveBOM2(xml))); + return doc; + } + + private static ReadOnlySpan RemoveBOM2(ReadOnlySpan xml) + { + var bom = MemoryMarshal.Cast(BOMMarkUtf8.AsSpan()); + if (xml.StartsWith(bom)) + xml = xml.Slice(bom.Length); + return xml; + } +} \ No newline at end of file diff --git a/src/Bannerlord.LauncherManager/Utils/ModuleListHandler.cs b/src/Bannerlord.LauncherManager/Utils/ModuleListHandler.cs index 4d304fe..f16d2e7 100644 --- a/src/Bannerlord.LauncherManager/Utils/ModuleListHandler.cs +++ b/src/Bannerlord.LauncherManager/Utils/ModuleListHandler.cs @@ -246,9 +246,7 @@ private void ReadNovusPreset(byte[] data, Action(); foreach (var xmlNode in document.DocumentElement?.SelectNodes("PresetModule")?.OfType() ?? Enumerable.Empty()) diff --git a/test/Bannerlord.LauncherManager.Native.Tests/ModuleManagerTests.cs b/test/Bannerlord.LauncherManager.Native.Tests/ModuleManagerTests.cs index 59a4911..b645c53 100644 --- a/test/Bannerlord.LauncherManager.Native.Tests/ModuleManagerTests.cs +++ b/test/Bannerlord.LauncherManager.Native.Tests/ModuleManagerTests.cs @@ -36,6 +36,9 @@ public partial class ModuleManagerTests : BaseTests [LibraryImport(DllPath), UnmanagedCallConv(CallConvs = new[] { typeof(CallConvStdcall) })] private static unsafe partial return_value_json* bmm_get_module_info(param_string* p_xml_content); + [LibraryImport(DllPath), UnmanagedCallConv(CallConvs = new[] { typeof(CallConvStdcall) })] + private static unsafe partial return_value_json* bmm_get_module_info_with_metadata(param_string* p_xml_content, param_string* p_module_provider, param_string* p_path); + [LibraryImport(DllPath), UnmanagedCallConv(CallConvs = new[] { typeof(CallConvStdcall) })] private static unsafe partial return_value_json* bmm_get_sub_module_info(param_string* p_xml_content);