using System.Globalization; using System.Text; using System.Xml; using System.Xml.Linq; namespace BinXmlConverter.Classes; public static class Digimon_Book { private const int NameCharCount = 64; private const int ExplainCharCount = 512; private const int DeckOptionCount = 3; public sealed class BookInfo { public uint OptionId { get; set; } public string OptionName { get; set; } = string.Empty; public ushort Icon { get; set; } public string OptionExplain { get; set; } = string.Empty; } public sealed class EncyclopediaException { public uint DigimonId { get; set; } public string Name { get; set; } = string.Empty; } public sealed class DeckOption { public ushort GroupIndex { get; set; } public string GroupName { get; set; } = string.Empty; public string Explain { get; set; } = string.Empty; public ushort[] Condition { get; set; } = new ushort[DeckOptionCount]; public ushort[] AttackType { get; set; } = new ushort[DeckOptionCount]; public ushort[] Option { get; set; } = new ushort[DeckOptionCount]; public ushort[] Value { get; set; } = new ushort[DeckOptionCount]; public uint[] Probability { get; set; } = new uint[DeckOptionCount]; public uint[] Time { get; set; } = new uint[DeckOptionCount]; } public sealed class DeckComposition { public ushort GroupIndex { get; set; } public List Digimons { get; } = new(); } public sealed class DeckDigimon { public uint BaseDigimonId { get; set; } public string BaseDigimonName { get; set; } = string.Empty; public ushort EvolutionSlot { get; set; } public uint DestDigimonId { get; set; } public string DestDigimonName { get; set; } = string.Empty; } public static (BookInfo[] books, EncyclopediaException[] exceptions, DeckOption[] deckOptions, DeckComposition[] compositions) ReadDigimonBookFromBinary(string inputFile) { using BinaryReader reader = new(File.Open(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read)); int bookCount = reader.ReadInt32(); BookInfo[] books = new BookInfo[bookCount]; for (int i = 0; i < bookCount; i++) { books[i] = new BookInfo { OptionId = reader.ReadUInt32(), OptionName = ReadFixedUnicodeString(reader, NameCharCount), Icon = reader.ReadUInt16(), OptionExplain = ReadPaddedUnicodeString(reader, ExplainCharCount) }; } int exceptionCount = reader.ReadInt32(); EncyclopediaException[] exceptions = new EncyclopediaException[exceptionCount]; for (int i = 0; i < exceptionCount; i++) { exceptions[i] = new EncyclopediaException { DigimonId = reader.ReadUInt32(), Name = ReadFixedUnicodeString(reader, NameCharCount) }; } int deckCount = reader.ReadInt32(); DeckOption[] deckOptions = new DeckOption[deckCount]; for (int i = 0; i < deckCount; i++) { DeckOption deck = new() { GroupIndex = reader.ReadUInt16(), GroupName = ReadFixedUnicodeString(reader, NameCharCount), Explain = ReadPaddedUnicodeString(reader, ExplainCharCount) }; for (int index = 0; index < DeckOptionCount; index++) deck.Condition[index] = reader.ReadUInt16(); for (int index = 0; index < DeckOptionCount; index++) deck.AttackType[index] = reader.ReadUInt16(); for (int index = 0; index < DeckOptionCount; index++) deck.Option[index] = reader.ReadUInt16(); for (int index = 0; index < DeckOptionCount; index++) deck.Value[index] = reader.ReadUInt16(); for (int index = 0; index < DeckOptionCount; index++) deck.Probability[index] = reader.ReadUInt32(); for (int index = 0; index < DeckOptionCount; index++) deck.Time[index] = reader.ReadUInt32(); deckOptions[i] = deck; } int compositionCount = reader.ReadInt32(); DeckComposition[] compositions = new DeckComposition[compositionCount]; for (int i = 0; i < compositionCount; i++) { DeckComposition composition = new() { GroupIndex = reader.ReadUInt16() }; ushort digimonCount = reader.ReadUInt16(); for (int j = 0; j < digimonCount; j++) { DeckDigimon digimon = new() { BaseDigimonId = reader.ReadUInt32(), BaseDigimonName = ReadFixedUnicodeString(reader, NameCharCount), EvolutionSlot = reader.ReadUInt16(), DestDigimonId = ReadPaddedUInt32(reader), DestDigimonName = ReadFixedUnicodeString(reader, NameCharCount) }; composition.Digimons.Add(digimon); } compositions[i] = composition; } return (books, exceptions, deckOptions, compositions); } public static void ExportBookInfoToXml(BookInfo[] books, string filePath) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)!); using XmlWriter writer = CreateWriter(filePath); writer.WriteStartDocument(); writer.WriteStartElement("BookInfoTable"); foreach (BookInfo book in books) { writer.WriteStartElement("BookInfo"); writer.WriteElementString("OptionId", book.OptionId.ToString(CultureInfo.InvariantCulture)); WriteFullStringElement(writer, "OptionName", book.OptionName); writer.WriteElementString("Icon", book.Icon.ToString(CultureInfo.InvariantCulture)); WriteFullStringElement(writer, "OptionExplain", book.OptionExplain); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndDocument(); } public static void ExportEncyclopediaExceptionToXml(EncyclopediaException[] exceptions, string filePath) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)!); using XmlWriter writer = CreateWriter(filePath); writer.WriteStartDocument(); writer.WriteStartElement("EncyclopediaExceptionTable"); foreach (EncyclopediaException exception in exceptions) { writer.WriteStartElement("EncyclopediaException"); writer.WriteElementString("DigimonId", exception.DigimonId.ToString(CultureInfo.InvariantCulture)); WriteFullStringElement(writer, "Name", exception.Name); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndDocument(); } public static void ExportDeckOptionToXml(DeckOption[] deckOptions, string filePath) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)!); using XmlWriter writer = CreateWriter(filePath); writer.WriteStartDocument(); writer.WriteStartElement("DeckOptionTable"); foreach (DeckOption deck in deckOptions) { writer.WriteStartElement("DeckOption"); writer.WriteElementString("GroupIndex", deck.GroupIndex.ToString(CultureInfo.InvariantCulture)); WriteFullStringElement(writer, "GroupName", deck.GroupName); WriteFullStringElement(writer, "Explain", deck.Explain); WriteArray(writer, "Condition", "Value", deck.Condition.Select(value => value.ToString(CultureInfo.InvariantCulture))); WriteArray(writer, "AttackType", "Value", deck.AttackType.Select(value => value.ToString(CultureInfo.InvariantCulture))); WriteArray(writer, "Option", "Value", deck.Option.Select(value => value.ToString(CultureInfo.InvariantCulture))); WriteArray(writer, "OptionValue", "Value", deck.Value.Select(value => value.ToString(CultureInfo.InvariantCulture))); WriteArray(writer, "Probability", "Value", deck.Probability.Select(value => value.ToString(CultureInfo.InvariantCulture))); WriteArray(writer, "Time", "Value", deck.Time.Select(value => value.ToString(CultureInfo.InvariantCulture))); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndDocument(); } public static void ExportDeckCompositionToXml(DeckComposition[] compositions, string filePath) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)!); using XmlWriter writer = CreateWriter(filePath); writer.WriteStartDocument(); writer.WriteStartElement("DeckCompositionTable"); foreach (DeckComposition composition in compositions) { writer.WriteStartElement("DeckComposition"); writer.WriteElementString("GroupIndex", composition.GroupIndex.ToString(CultureInfo.InvariantCulture)); writer.WriteStartElement("Digimons"); foreach (DeckDigimon digimon in composition.Digimons) { writer.WriteStartElement("DeckDigimon"); writer.WriteElementString("BaseDigimonId", digimon.BaseDigimonId.ToString(CultureInfo.InvariantCulture)); WriteFullStringElement(writer, "BaseDigimonName", digimon.BaseDigimonName); writer.WriteElementString("EvolutionSlot", digimon.EvolutionSlot.ToString(CultureInfo.InvariantCulture)); writer.WriteElementString("DestDigimonId", digimon.DestDigimonId.ToString(CultureInfo.InvariantCulture)); WriteFullStringElement(writer, "DestDigimonName", digimon.DestDigimonName); writer.WriteEndElement(); } writer.WriteFullEndElement(); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndDocument(); } public static BookInfo[] ImportBookInfosFromXml(string filePath) { XDocument document = XDocument.Load(filePath); return document.Root? .Elements("BookInfo") .Select(element => new BookInfo { OptionId = ParseUInt(element, "OptionId"), OptionName = ReadString(element, "OptionName"), Icon = ParseUShort(element, "Icon"), OptionExplain = ReadString(element, "OptionExplain") }) .ToArray() ?? Array.Empty(); } public static EncyclopediaException[] ImportEncyclopediaExceptionsFromXml(string filePath) { XDocument document = XDocument.Load(filePath); return document.Root? .Elements("EncyclopediaException") .Select(element => new EncyclopediaException { DigimonId = ParseUInt(element, "DigimonId"), Name = ReadString(element, "Name") }) .ToArray() ?? Array.Empty(); } public static DeckOption[] ImportDeckOptionsFromXml(string filePath) { XDocument document = XDocument.Load(filePath); return document.Root? .Elements("DeckOption") .Select(element => new DeckOption { GroupIndex = ParseUShort(element, "GroupIndex"), GroupName = ReadString(element, "GroupName"), Explain = ReadString(element, "Explain"), Condition = ReadUShortArray(element, "Condition"), AttackType = ReadUShortArray(element, "AttackType"), Option = ReadUShortArray(element, "Option"), Value = ReadUShortArray(element, "OptionValue"), Probability = ReadUIntArray(element, "Probability"), Time = ReadUIntArray(element, "Time") }) .ToArray() ?? Array.Empty(); } public static DeckComposition[] ImportDeckCompositionsFromXml(string filePath) { XDocument document = XDocument.Load(filePath); return document.Root? .Elements("DeckComposition") .Select(element => { DeckComposition composition = new() { GroupIndex = ParseUShort(element, "GroupIndex") }; foreach (XElement digimonElement in element.Element("Digimons")?.Elements("DeckDigimon") ?? Enumerable.Empty()) { composition.Digimons.Add(new DeckDigimon { BaseDigimonId = ParseUInt(digimonElement, "BaseDigimonId"), BaseDigimonName = ReadString(digimonElement, "BaseDigimonName"), EvolutionSlot = ParseUShort(digimonElement, "EvolutionSlot"), DestDigimonId = ParseUInt(digimonElement, "DestDigimonId"), DestDigimonName = ReadString(digimonElement, "DestDigimonName") }); } return composition; }) .ToArray() ?? Array.Empty(); } public static void WriteDigimon_BookToBinary(string outputFile, BookInfo[] books, EncyclopediaException[] exceptions, DeckOption[] deckOptions, DeckComposition[] compositions) { Directory.CreateDirectory(Path.GetDirectoryName(outputFile)!); using BinaryWriter writer = new(File.Open(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)); writer.Write(books.Length); foreach (BookInfo book in books) { writer.Write(book.OptionId); WriteFixedUnicodeString(writer, book.OptionName, NameCharCount); writer.Write(book.Icon); writer.Write((ushort)0); WriteFixedUnicodeString(writer, book.OptionExplain, ExplainCharCount); } writer.Write(exceptions.Length); foreach (EncyclopediaException exception in exceptions) { writer.Write(exception.DigimonId); WriteFixedUnicodeString(writer, exception.Name, NameCharCount); } writer.Write(deckOptions.Length); foreach (DeckOption deck in deckOptions) { writer.Write(deck.GroupIndex); WriteFixedUnicodeString(writer, deck.GroupName, NameCharCount); writer.Write((ushort)0); WriteFixedUnicodeString(writer, deck.Explain, ExplainCharCount); WriteFixedUShortArray(writer, NormalizeArray(deck.Condition, DeckOptionCount)); WriteFixedUShortArray(writer, NormalizeArray(deck.AttackType, DeckOptionCount)); WriteFixedUShortArray(writer, NormalizeArray(deck.Option, DeckOptionCount)); WriteFixedUShortArray(writer, NormalizeArray(deck.Value, DeckOptionCount)); WriteFixedUIntArray(writer, NormalizeArray(deck.Probability, DeckOptionCount)); WriteFixedUIntArray(writer, NormalizeArray(deck.Time, DeckOptionCount)); } writer.Write(compositions.Length); foreach (DeckComposition composition in compositions) { writer.Write(composition.GroupIndex); writer.Write((ushort)composition.Digimons.Count); foreach (DeckDigimon digimon in composition.Digimons) { writer.Write(digimon.BaseDigimonId); WriteFixedUnicodeString(writer, digimon.BaseDigimonName, NameCharCount); writer.Write(digimon.EvolutionSlot); writer.Write((ushort)0); writer.Write(digimon.DestDigimonId); WriteFixedUnicodeString(writer, digimon.DestDigimonName, NameCharCount); } } } private static ushort[] NormalizeArray(ushort[] values, int count) { ushort[] normalized = new ushort[count]; for (int i = 0; i < count; i++) { normalized[i] = i < values.Length ? values[i] : (ushort)0; } return normalized; } private static uint[] NormalizeArray(uint[] values, int count) { uint[] normalized = new uint[count]; for (int i = 0; i < count; i++) { normalized[i] = i < values.Length ? values[i] : 0U; } return normalized; } private static void WriteFixedUShortArray(BinaryWriter writer, ushort[] values) { foreach (ushort value in values) { writer.Write(value); } } private static void WriteFixedUIntArray(BinaryWriter writer, uint[] values) { foreach (uint value in values) { writer.Write(value); } } private static uint[] ReadUIntArray(XElement element, string containerName) { return element.Element(containerName)?.Elements("Value").Select(value => uint.Parse(value.Value, CultureInfo.InvariantCulture)).ToArray() ?? new uint[DeckOptionCount]; } private static ushort[] ReadUShortArray(XElement element, string containerName) { return element.Element(containerName)?.Elements("Value").Select(value => ushort.Parse(value.Value, CultureInfo.InvariantCulture)).ToArray() ?? new ushort[DeckOptionCount]; } private static string ReadFixedUnicodeString(BinaryReader reader, int charCount) { byte[] buffer = reader.ReadBytes(charCount * 2); string raw = Encoding.Unicode.GetString(buffer); int nullIndex = raw.IndexOf('\0'); return nullIndex >= 0 ? raw[..nullIndex] : raw; } private static string ReadPaddedUnicodeString(BinaryReader reader, int charCount) { reader.ReadUInt16(); return ReadFixedUnicodeString(reader, charCount); } private static uint ReadPaddedUInt32(BinaryReader reader) { reader.ReadUInt16(); return reader.ReadUInt32(); } private static void WriteFixedUnicodeString(BinaryWriter writer, string value, int charCount) { string sanitized = value ?? string.Empty; int limit = Math.Min(sanitized.Length, charCount); for (int i = 0; i < charCount; i++) { char ch = i < limit ? sanitized[i] : '\0'; writer.Write((ushort)ch); } } private static string ReadString(XElement element, string name) { return (string?)element.Element(name) ?? string.Empty; } private static uint ParseUInt(XElement element, string name) { return uint.Parse(((string?)element.Element(name)) ?? "0", CultureInfo.InvariantCulture); } private static ushort ParseUShort(XElement element, string name) { return ushort.Parse(((string?)element.Element(name)) ?? "0", CultureInfo.InvariantCulture); } private static XmlWriter CreateWriter(string filePath) { XmlWriterSettings settings = new() { Indent = true, Encoding = new UTF8Encoding(false) }; return XmlWriter.Create(filePath, settings); } private static void WriteArray(XmlWriter writer, string containerName, string childName, IEnumerable values) { writer.WriteStartElement(containerName); foreach (string value in values) { writer.WriteElementString(childName, value); } writer.WriteEndElement(); } private static void WriteFullStringElement(XmlWriter writer, string name, string value) { writer.WriteStartElement(name); writer.WriteString(value ?? string.Empty); writer.WriteFullEndElement(); } }