Skip to content

Commit

Permalink
BOM was hot handled correctly everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed May 29, 2024
1 parent df4cd7f commit d5fef0d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 5 additions & 7 deletions src/Bannerlord.LauncherManager.Native/Bindings.ModuleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Xml;
using Bannerlord.LauncherManager.Utils;

namespace Bannerlord.LauncherManager.Native;

Expand Down Expand Up @@ -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);

Expand All @@ -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)));
Expand All @@ -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);
Expand All @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions src/Bannerlord.LauncherManager.Native/ReaderUtils.cs
Original file line number Diff line number Diff line change
@@ -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<char> xml)
{
var doc = new XmlDocument();
doc.LoadXml(new string(RemoveBOM2(xml)));
return doc;
}

private static ReadOnlySpan<char> RemoveBOM2(ReadOnlySpan<char> xml)
{
var bom = MemoryMarshal.Cast<byte, char>(BOMMarkUtf8.AsSpan());
if (xml.StartsWith(bom))
xml = xml.Slice(bom.Length);
return xml;
}
}
4 changes: 1 addition & 3 deletions src/Bannerlord.LauncherManager/Utils/ModuleListHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,7 @@ private void ReadNovusPreset(byte[] data, Action<ModuleInfoExtendedWithMetadata[
return;
}

var document = new XmlDocument();
using var stream = new MemoryStream(data);
document.Load(stream);
var document = ReaderUtils.Read(data);

var importedModules = new List<ModuleListEntry>();
foreach (var xmlNode in document.DocumentElement?.SelectNodes("PresetModule")?.OfType<XmlNode>() ?? Enumerable.Empty<XmlNode>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit d5fef0d

Please sign in to comment.