using System.Globalization; using System.Text; using System.Xml; using System.Xml.Linq; namespace BinXmlConverter.Classes; public static class PassiveAbilityPackage { private const int PassiveStart = 1; private const int PassiveMax = 2; public sealed class PassiveTypeGroup { public int Type { get; set; } public List Entries { get; } = new(); } public sealed class PassiveAbilityEntry { public uint DigimonId { get; set; } public short EvolutionNumber { get; set; } public int Type { get; set; } public int Value1 { get; set; } } public static List ReadFromBinary(string filePath) { using BinaryReader reader = new(File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)); List groups = new(); for (int passiveType = PassiveStart; passiveType < PassiveMax; passiveType++) { int count = reader.ReadInt32(); if (count < 0) { throw new InvalidDataException($"Passive ability count is invalid for type {passiveType}: {count}."); } PassiveTypeGroup group = new() { Type = passiveType }; for (int i = 0; i < count; i++) { PassiveAbilityEntry entry = new() { DigimonId = reader.ReadUInt32(), EvolutionNumber = reader.ReadInt16() }; reader.ReadUInt16(); entry.Type = reader.ReadInt32(); entry.Value1 = reader.ReadInt32(); group.Entries.Add(entry); } groups.Add(group); } if (reader.BaseStream.Position != reader.BaseStream.Length) { throw new InvalidDataException($"Passive_Ability.bin was not fully consumed. Remaining bytes: {reader.BaseStream.Length - reader.BaseStream.Position}."); } return groups; } public static void WriteToBinary(string filePath, List groups) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)!); Dictionary groupMap = groups.ToDictionary(group => group.Type); using BinaryWriter writer = new(File.Open(filePath, FileMode.Create, FileAccess.Write, FileShare.None)); for (int passiveType = PassiveStart; passiveType < PassiveMax; passiveType++) { PassiveTypeGroup group = groupMap.GetValueOrDefault(passiveType) ?? new PassiveTypeGroup { Type = passiveType }; writer.Write(group.Entries.Count); foreach (PassiveAbilityEntry entry in group.Entries) { writer.Write(entry.DigimonId); writer.Write(entry.EvolutionNumber); writer.Write((ushort)0); writer.Write(entry.Type); writer.Write(entry.Value1); } } } public static void ExportToXml(string filePath, List groups) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)!); XElement root = new("PassiveAbilityTable", groups.Select(group => new XElement("PassiveType", new XElement("Type", group.Type), new XElement("Entries", group.Entries.Select(entry => new XElement("PassiveAbility", new XElement("DigimonId", entry.DigimonId), new XElement("EvolutionNumber", entry.EvolutionNumber), new XElement("Type", entry.Type), new XElement("Value1", entry.Value1))))))); SaveXml(filePath, root); } public static List ImportFromXml(string filePath) { XElement root = XElement.Load(filePath); List groups = new(); foreach (XElement groupElement in root.Elements("PassiveType")) { PassiveTypeGroup group = new() { Type = int.Parse(groupElement.Element("Type")?.Value ?? "0", CultureInfo.InvariantCulture) }; XElement? entries = groupElement.Element("Entries"); if (entries != null) { foreach (XElement entryElement in entries.Elements("PassiveAbility")) { group.Entries.Add(new PassiveAbilityEntry { DigimonId = uint.Parse(entryElement.Element("DigimonId")?.Value ?? "0", CultureInfo.InvariantCulture), EvolutionNumber = short.Parse(entryElement.Element("EvolutionNumber")?.Value ?? "0", CultureInfo.InvariantCulture), Type = int.Parse(entryElement.Element("Type")?.Value ?? "0", CultureInfo.InvariantCulture), Value1 = int.Parse(entryElement.Element("Value1")?.Value ?? "0", CultureInfo.InvariantCulture) }); } } groups.Add(group); } return groups; } private static void SaveXml(string path, XElement root) { XmlWriterSettings settings = new() { Indent = true, Encoding = new UTF8Encoding(false) }; using XmlWriter writer = XmlWriter.Create(path, settings); root.Save(writer); } }