diff --git a/src/Bannerlord.LauncherManager.Native/ReaderUtils.cs b/src/Bannerlord.LauncherManager.Native/ReaderUtils.cs index c3d0b2c..4cb6d7f 100644 --- a/src/Bannerlord.LauncherManager.Native/ReaderUtils.cs +++ b/src/Bannerlord.LauncherManager.Native/ReaderUtils.cs @@ -7,7 +7,9 @@ namespace Bannerlord.LauncherManager.Native; public static class ReaderUtils2 { - private static readonly byte[] BOMMarkUtf8 = Encoding.UTF8.GetPreamble(); + private static readonly byte[] BOMMarkUtf8 = [0xEF, 0xBB, 0xBF]; + private static readonly byte[] BOMMarkUtf16BE = [0xFE, 0xFF]; + private static readonly byte[] BOMMarkUtf16LE = [0xFF, 0xFE]; public static XmlDocument Read(ReadOnlySpan xml) { @@ -15,12 +17,19 @@ public static XmlDocument Read(ReadOnlySpan xml) 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); + var utf8BOM = MemoryMarshal.Cast(BOMMarkUtf8.AsSpan()); + var utf16BEBOM = MemoryMarshal.Cast(BOMMarkUtf16BE.AsSpan()); + var utf16LEBOM = MemoryMarshal.Cast(BOMMarkUtf16LE.AsSpan()); + + var bomOffset = xml.StartsWith(utf8BOM) ? utf8BOM.Length : + xml.StartsWith(utf16BEBOM) ? utf16BEBOM.Length : + xml.StartsWith(utf16LEBOM) ? utf16LEBOM.Length : 0; + + xml = xml.Slice(bomOffset > 0 ? bomOffset : 0); + return xml; } } \ No newline at end of file diff --git a/src/Bannerlord.LauncherManager/Utils/ReaderUtils.cs b/src/Bannerlord.LauncherManager/Utils/ReaderUtils.cs index a2905ce..437e008 100644 --- a/src/Bannerlord.LauncherManager/Utils/ReaderUtils.cs +++ b/src/Bannerlord.LauncherManager/Utils/ReaderUtils.cs @@ -1,29 +1,38 @@ using System; using System.IO; -using System.Text; using System.Xml; namespace Bannerlord.LauncherManager.Utils; public static class ReaderUtils { - private static readonly byte[] BOMMarkUtf8 = Encoding.UTF8.GetPreamble(); + private static readonly byte[] BOMMarkUtf8 = [0xEF, 0xBB, 0xBF]; + private static readonly byte[] BOMMarkUtf16BE = [0xFE, 0xFF]; + private static readonly byte[] BOMMarkUtf16LE = [0xFF, 0xFE]; public static XmlDocument Read(byte[] data) { using var ms = new MemoryStream(data); using var sr = new StreamReader(ms); - Span bom = BOMMarkUtf8.AsSpan(); + var utf8BOM = BOMMarkUtf8.AsSpan(); + var utf16BEBOM = BOMMarkUtf16BE.AsSpan(); + var utf16LEBOM = BOMMarkUtf16LE.AsSpan(); + #if NETSTANDARD2_1_OR_GREATER - Span preamblePreallocated = stackalloc byte[BOMMarkUtf8.Length]; + Span preamblePreallocated = stackalloc byte[4]; ms.Read(preamblePreallocated); #else - var preamblePreallocated = new byte[BOMMarkUtf8.Length]; - ms.Read(preamblePreallocated, 0, BOMMarkUtf8.Length); + var preamblePreallocatedArray = new byte[4]; + ms.Read(preamblePreallocatedArray, 0, 4); + var preamblePreallocated = preamblePreallocatedArray.AsSpan(); #endif - - ms.Seek(bom.SequenceEqual(preamblePreallocated) ? BOMMarkUtf8.Length : 0, SeekOrigin.Begin); + + var bomOffset = preamblePreallocated.StartsWith(utf8BOM) ? BOMMarkUtf8.Length : + preamblePreallocated.StartsWith(utf16BEBOM) ? BOMMarkUtf16BE.Length : + preamblePreallocated.StartsWith(utf16LEBOM) ? BOMMarkUtf16LE.Length : 0; + + ms.Seek(bomOffset > 0 ? bomOffset : 0, SeekOrigin.Begin); var doc = new XmlDocument(); doc.Load(new XmlTextReader(sr)); return doc;