Skip to content

Commit

Permalink
Allow know the kbin encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Milkitic committed Mar 26, 2022
1 parent a465bd6 commit c638582
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
48 changes: 42 additions & 6 deletions src/KbinXml.Net/KbinConverter.Readers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ public static partial class KbinConverter
/// <returns>Returns the <see cref="XDocument"/>.</returns>
public static XDocument ReadXmlLinq(Memory<byte> sourceBuffer)
{
var xDocument = (XDocument)Read(sourceBuffer, e => new XDocumentProvider(e));
var xDocument = (XDocument)Read(sourceBuffer, e => new XDocumentProvider(e), out var knownEncoding);
return xDocument;
}
/// <summary>
/// Reads the KBin bytes into an XML <see cref="XDocument"/>.
/// </summary>
/// <param name="sourceBuffer">The KBin bytes to convert.</param>
/// <returns>Returns the <see cref="XDocument"/>.</returns>
public static XDocument ReadXmlLinq(Memory<byte> sourceBuffer, out KnownEncodings knownEncoding)
{
var xDocument = (XDocument)Read(sourceBuffer, e => new XDocumentProvider(e), out knownEncoding);
return xDocument;
}

Expand All @@ -31,7 +41,18 @@ public static XDocument ReadXmlLinq(Memory<byte> sourceBuffer)
/// <returns>Returns the <see cref="T:byte[]"/>.</returns>
public static byte[] ReadXmlBytes(Memory<byte> sourceBuffer)
{
var bytes = (byte[])Read(sourceBuffer, e => new XmlWriterProvider(e));
var bytes = (byte[])Read(sourceBuffer, e => new XmlWriterProvider(e), out var knownEncoding);
return bytes;
}

/// <summary>
/// Reads the KBin bytes into an XML <see cref="T:byte[]"/>.
/// </summary>
/// <param name="sourceBuffer">The KBin bytes convert.</param>
/// <returns>Returns the <see cref="T:byte[]"/>.</returns>
public static byte[] ReadXmlBytes(Memory<byte> sourceBuffer, out KnownEncodings knownEncoding)
{
var bytes = (byte[])Read(sourceBuffer, e => new XmlWriterProvider(e), out knownEncoding);
return bytes;
}

Expand All @@ -42,13 +63,25 @@ public static byte[] ReadXmlBytes(Memory<byte> sourceBuffer)
/// <returns>Returns the <see cref="XmlDocument"/>.</returns>
public static XmlDocument ReadXml(Memory<byte> sourceBuffer)
{
var xmlDocument = (XmlDocument)Read(sourceBuffer, e => new XmlDocumentProvider(e));
var xmlDocument = (XmlDocument)Read(sourceBuffer, e => new XmlDocumentProvider(e), out var knownEncoding);
return xmlDocument;
}

/// <summary>
/// Reads the KBin bytes into an XML <see cref="XmlDocument"/>.
/// </summary>
/// <param name="sourceBuffer">The KBin bytes convert.</param>
/// <returns>Returns the <see cref="XmlDocument"/>.</returns>
public static XmlDocument ReadXml(Memory<byte> sourceBuffer, out KnownEncodings knownEncoding)
{
var xmlDocument = (XmlDocument)Read(sourceBuffer, e => new XmlDocumentProvider(e), out knownEncoding);
return xmlDocument;
}

private static object Read(Memory<byte> sourceBuffer, Func<Encoding, WriterProvider> createWriterProvider)
private static object Read(Memory<byte> sourceBuffer, Func<Encoding, WriterProvider> 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;
Expand Down Expand Up @@ -272,7 +305,7 @@ private static ReadContext GetReadContext(Memory<byte> sourceBuffer, Func<Encodi

var readProvider = createWriterProvider(encoding);

var readContext = new ReadContext(nodeReader, dataReader, readProvider);
var readContext = new ReadContext(nodeReader, dataReader, readProvider, encoding.ToKnownEncoding());
return readContext;
}

Expand Down Expand Up @@ -324,16 +357,19 @@ private static string GetDisplayable(char c)

private class ReadContext : IDisposable
{
public ReadContext(NodeReader nodeReader, DataReader dataReader, WriterProvider writerProvider)
public ReadContext(NodeReader nodeReader, DataReader dataReader, WriterProvider writerProvider,
KnownEncodings knownEncoding)
{
NodeReader = nodeReader;
DataReader = dataReader;
WriterProvider = writerProvider;
KnownEncoding = knownEncoding;
}

public NodeReader NodeReader { get; set; }
public DataReader DataReader { get; set; }
public WriterProvider WriterProvider { get; set; }
public KnownEncodings KnownEncoding { get; }

public void Dispose()
{
Expand Down
14 changes: 14 additions & 0 deletions src/KbinXml.Net/KnownEncodingsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,18 @@ public static Encoding ToEncoding(this KnownEncodings knownEncodings)
_ => 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);
}
}

0 comments on commit c638582

Please sign in to comment.