diff --git a/src/KbinXml.Net/KbinConverter.Readers.cs b/src/KbinXml.Net/KbinConverter.Readers.cs index 5bc20f2..5ca12e3 100644 --- a/src/KbinXml.Net/KbinConverter.Readers.cs +++ b/src/KbinXml.Net/KbinConverter.Readers.cs @@ -20,7 +20,17 @@ public static partial class KbinConverter /// Returns the . public static XDocument ReadXmlLinq(Memory sourceBuffer) { - var xDocument = (XDocument)Read(sourceBuffer, e => new XDocumentProvider(e)); + var xDocument = (XDocument)Read(sourceBuffer, e => new XDocumentProvider(e), out var knownEncoding); + return xDocument; + } + /// + /// Reads the KBin bytes into an XML . + /// + /// The KBin bytes to convert. + /// Returns the . + public static XDocument ReadXmlLinq(Memory sourceBuffer, out KnownEncodings knownEncoding) + { + var xDocument = (XDocument)Read(sourceBuffer, e => new XDocumentProvider(e), out knownEncoding); return xDocument; } @@ -31,7 +41,18 @@ public static XDocument ReadXmlLinq(Memory sourceBuffer) /// Returns the . public static byte[] ReadXmlBytes(Memory sourceBuffer) { - var bytes = (byte[])Read(sourceBuffer, e => new XmlWriterProvider(e)); + var bytes = (byte[])Read(sourceBuffer, e => new XmlWriterProvider(e), out var knownEncoding); + return bytes; + } + + /// + /// Reads the KBin bytes into an XML . + /// + /// The KBin bytes convert. + /// Returns the . + public static byte[] ReadXmlBytes(Memory sourceBuffer, out KnownEncodings knownEncoding) + { + var bytes = (byte[])Read(sourceBuffer, e => new XmlWriterProvider(e), out knownEncoding); return bytes; } @@ -42,13 +63,25 @@ public static byte[] ReadXmlBytes(Memory sourceBuffer) /// Returns the . public static XmlDocument ReadXml(Memory sourceBuffer) { - var xmlDocument = (XmlDocument)Read(sourceBuffer, e => new XmlDocumentProvider(e)); + var xmlDocument = (XmlDocument)Read(sourceBuffer, e => new XmlDocumentProvider(e), out var knownEncoding); + return xmlDocument; + } + + /// + /// Reads the KBin bytes into an XML . + /// + /// The KBin bytes convert. + /// Returns the . + public static XmlDocument ReadXml(Memory sourceBuffer, out KnownEncodings knownEncoding) + { + var xmlDocument = (XmlDocument)Read(sourceBuffer, e => new XmlDocumentProvider(e), out knownEncoding); return xmlDocument; } - private static object Read(Memory sourceBuffer, Func createWriterProvider) + private static object Read(Memory sourceBuffer, Func createWriterProvider, out KnownEncodings knownEncoding) { using var readContext = GetReadContext(sourceBuffer, createWriterProvider); + knownEncoding = readContext.KnownEncoding; var writerProvider = readContext.WriterProvider; var nodeReader = readContext.NodeReader; var dataReader = readContext.DataReader; @@ -272,7 +305,7 @@ private static ReadContext GetReadContext(Memory sourceBuffer, Func throw new ArgumentOutOfRangeException(nameof(knownEncodings), knownEncodings, null) }; } + public static KnownEncodings ToKnownEncoding(this Encoding knownEncodings) + { + if (knownEncodings.EncodingName == EncodingDictionary.EncodingMap[0x80].EncodingName) + return KnownEncodings.ShiftJIS; + if (knownEncodings.EncodingName == EncodingDictionary.EncodingMap[0x20].EncodingName) + return KnownEncodings.ASCII; + if (knownEncodings.EncodingName == EncodingDictionary.EncodingMap[0x40].EncodingName) + return KnownEncodings.ISO_8859_1; + if (knownEncodings.EncodingName == EncodingDictionary.EncodingMap[0x60].EncodingName) + return KnownEncodings.EUC_JP; + if (knownEncodings.EncodingName == EncodingDictionary.EncodingMap[0xA0].EncodingName) + return KnownEncodings.UTF8; + throw new ArgumentOutOfRangeException(nameof(knownEncodings), knownEncodings, null); + } } \ No newline at end of file