using System.Globalization; using System.Text; using System.Xml; using System.Xml.Linq; namespace BinXmlConverter.Classes; public static class DigimonEvo { private const int MaxEvolution = 9; private const int MaxEffectLength = 32; private const int MaxJointTacticsLength = 3; private const int BattleEvoElementCount = 6; private const int BattleEvoTypeCount = 5; public sealed class DigimonEvolutionPackage { public List Digimons { get; } = new(); public List BattleInfos { get; } = new(); } public sealed class DigimonEvolutionInfo { public uint DigimonId { get; set; } public uint BattleEvoType { get; set; } public List Evolutions { get; } = new(); } public sealed class EvolutionObject { public uint DigimonId { get; set; } public ushort EvoSlot { get; set; } public List EvolutionLinks { get; } = new(); public IntPoint IconPos { get; set; } = new(); public ushort EnableSlot { get; set; } public ushort OpenQualification { get; set; } public ushort OpenLevel { get; set; } public ushort OpenQuest { get; set; } public ushort OpenItemTypeS { get; set; } public ushort OpenItemNum { get; set; } public ushort UseItem { get; set; } public ushort UseItemNum { get; set; } public ushort Intimacy { get; set; } public ushort OpenCrest { get; set; } public ushort EvoCard1 { get; set; } public ushort EvoCard2 { get; set; } public ushort EvoCard3 { get; set; } public ushort EvoDigimental { get; set; } public ushort EvoTamerDS { get; set; } public ushort Dummy { get; set; } public int Render { get; set; } public IntPoint TargetStartPos { get; set; } = new(); public int StartHeight { get; set; } public int StartRot { get; set; } public IntPoint TargetEndPos { get; set; } = new(); public int EndHeight { get; set; } public int EndRot { get; set; } public int Speed { get; set; } public uint AnimationId { get; set; } public double StartTime { get; set; } public double EndTime { get; set; } public int R { get; set; } public int G { get; set; } public int B { get; set; } public string LevelEffect { get; set; } = string.Empty; public string EnchantEffect { get; set; } = string.Empty; public string SizeEffect { get; set; } = string.Empty; public int EvolutionTree { get; set; } public uint JoGressQuestCheck { get; set; } public ushort ChipsetType { get; set; } public ushort ChipsetTypeC { get; set; } public ushort ChipsetNum { get; set; } public ushort ChipsetTypeP { get; set; } public ushort JoGressesNum { get; set; } public int[] JoGressTacticses { get; } = new int[MaxJointTacticsLength]; } public sealed class EvolutionLink { public ushort Slot { get; set; } public ushort Type { get; set; } public uint DigimonId { get; set; } } public sealed class BattleEvolutionInfo { public int TypeIndex { get; set; } public ushort MaxEP { get; set; } public List Elements { get; } = new(); } public sealed class BattleEvolutionElement { public int ElementIndex { get; set; } public bool IsEnabled { get; set; } public ushort EvolStep { get; set; } public ushort RequiredEvoEP { get; set; } public ushort DecreaseEP { get; set; } } public sealed class IntPoint { public int X { get; set; } public int Y { get; set; } } public static DigimonEvolutionPackage ReadFromBinary(string inputPath) { var package = new DigimonEvolutionPackage(); using BinaryReader reader = new(File.OpenRead(inputPath), Encoding.ASCII, leaveOpen: false); int digimonCount = reader.ReadInt32(); for (int i = 0; i < digimonCount; i++) { var digimon = new DigimonEvolutionInfo { DigimonId = reader.ReadUInt32(), BattleEvoType = reader.ReadUInt32() }; int evolutionCount = reader.ReadInt32(); for (int e = 0; e < evolutionCount; e++) { digimon.Evolutions.Add(ReadEvolutionObject(reader)); } package.Digimons.Add(digimon); } int battleInfoCount = reader.ReadInt32(); for (int i = 0; i < battleInfoCount; i++) { package.BattleInfos.Add(ReadBattleEvolutionInfo(reader, i + 1)); } if (reader.BaseStream.Position != reader.BaseStream.Length) { throw new InvalidDataException($"DigimonEvo.bin was not fully consumed. Remaining bytes: {reader.BaseStream.Length - reader.BaseStream.Position}."); } return package; } public static void ExportToXml(string digimonOutputPath, string battleOutputPath, DigimonEvolutionPackage package) { Directory.CreateDirectory(Path.GetDirectoryName(digimonOutputPath)!); Directory.CreateDirectory(Path.GetDirectoryName(battleOutputPath)!); SaveXDocument(BuildDigimonDocument(package), digimonOutputPath); SaveXDocument(BuildBattleDocument(package), battleOutputPath); } public static DigimonEvolutionPackage ImportFromXml(string digimonInputPath, string battleInputPath) { var package = new DigimonEvolutionPackage(); XDocument digimonDoc = XDocument.Load(digimonInputPath); foreach (XElement digimonElement in digimonDoc.Root?.Elements("Digimon") ?? Enumerable.Empty()) { var digimon = new DigimonEvolutionInfo { DigimonId = ParseUIntElement(digimonElement, "DigimonId"), BattleEvoType = ParseUIntElement(digimonElement, "BattleEvoType") }; foreach (XElement evolutionElement in digimonElement.Elements("Evolution")) { var evolution = new EvolutionObject { DigimonId = ParseUIntElement(evolutionElement, "DigimonId"), EvoSlot = ParseUShortElement(evolutionElement, "EvoSlot"), IconPos = ReadPoint(evolutionElement.Element("IconPos")), EnableSlot = ParseUShortElement(evolutionElement, "EnableSlot"), OpenQualification = ParseUShortElement(evolutionElement, "OpenQualification"), OpenLevel = ParseUShortElement(evolutionElement, "OpenLevel"), OpenQuest = ParseUShortElement(evolutionElement, "OpenQuest"), OpenItemTypeS = ParseUShortElement(evolutionElement, "OpenItemTypeS"), OpenItemNum = ParseUShortElement(evolutionElement, "OpenItemNum"), UseItem = ParseUShortElement(evolutionElement, "UseItem"), UseItemNum = ParseUShortElement(evolutionElement, "UseItemNum"), Intimacy = ParseUShortElement(evolutionElement, "Intimacy"), OpenCrest = ParseUShortElement(evolutionElement, "OpenCrest"), EvoCard1 = ParseUShortElement(evolutionElement, "EvoCard1"), EvoCard2 = ParseUShortElement(evolutionElement, "EvoCard2"), EvoCard3 = ParseUShortElement(evolutionElement, "EvoCard3"), EvoDigimental = ParseUShortElement(evolutionElement, "EvoDigimental"), EvoTamerDS = ParseUShortElement(evolutionElement, "EvoTamerDS"), Dummy = ParseUShortElement(evolutionElement, "Dummy"), Render = ParseIntElement(evolutionElement, "Render"), TargetStartPos = ReadPoint(evolutionElement.Element("TargetStartPos")), StartHeight = ParseIntElement(evolutionElement, "StartHeight"), StartRot = ParseIntElement(evolutionElement, "StartRot"), TargetEndPos = ReadPoint(evolutionElement.Element("TargetEndPos")), EndHeight = ParseIntElement(evolutionElement, "EndHeight"), EndRot = ParseIntElement(evolutionElement, "EndRot"), Speed = ParseIntElement(evolutionElement, "Speed"), AnimationId = ParseUIntElement(evolutionElement, "AnimationId"), StartTime = ParseDoubleElement(evolutionElement, "StartTime"), EndTime = ParseDoubleElement(evolutionElement, "EndTime"), R = ParseIntElement(evolutionElement, "R"), G = ParseIntElement(evolutionElement, "G"), B = ParseIntElement(evolutionElement, "B"), LevelEffect = GetElementValue(evolutionElement, "LevelEffect"), EnchantEffect = GetElementValue(evolutionElement, "EnchantEffect"), SizeEffect = GetElementValue(evolutionElement, "SizeEffect"), EvolutionTree = ParseIntElement(evolutionElement, "EvolutionTree"), JoGressQuestCheck = ParseUIntElement(evolutionElement, "JoGressQuestCheck"), ChipsetType = ParseUShortElement(evolutionElement, "ChipsetType"), ChipsetTypeC = ParseUShortElement(evolutionElement, "ChipsetTypeC"), ChipsetNum = ParseUShortElement(evolutionElement, "ChipsetNum"), ChipsetTypeP = ParseUShortElement(evolutionElement, "ChipsetTypeP"), JoGressesNum = ParseUShortElement(evolutionElement, "JoGressesNum") }; foreach (XElement linkElement in evolutionElement.Elements("EvolutionLink")) { evolution.EvolutionLinks.Add(new EvolutionLink { Slot = ParseUShortElement(linkElement, "Slot"), Type = ParseUShortElement(linkElement, "Type"), DigimonId = ParseUIntElement(linkElement, "DigimonId") }); } while (evolution.EvolutionLinks.Count < MaxEvolution) { evolution.EvolutionLinks.Add(new EvolutionLink()); } XElement? tacticsElement = evolutionElement.Element("JoGressTacticses"); if (tacticsElement != null) { foreach (XElement tacticElement in tacticsElement.Elements("Tactic")) { int index = ParseIntAttribute(tacticElement, "Index"); if (index >= 1 && index <= MaxJointTacticsLength) { evolution.JoGressTacticses[index - 1] = ParseIntAttribute(tacticElement, "Value"); } } } digimon.Evolutions.Add(evolution); } package.Digimons.Add(digimon); } if (File.Exists(battleInputPath)) { XDocument battleDoc = XDocument.Load(battleInputPath); foreach (XElement battleElement in battleDoc.Root?.Elements("BattleEvolution") ?? Enumerable.Empty()) { var battleInfo = new BattleEvolutionInfo { TypeIndex = ParseIntElement(battleElement, "TypeIndex"), MaxEP = ParseUShortElement(battleElement, "MaxEP") }; foreach (XElement element in battleElement.Elements("Element")) { battleInfo.Elements.Add(new BattleEvolutionElement { ElementIndex = ParseIntElement(element, "ElementIndex"), IsEnabled = ParseBoolElement(element, "IsEnabled"), EvolStep = ParseUShortElement(element, "EvolStep"), RequiredEvoEP = ParseUShortElement(element, "RequiredEvoEP"), DecreaseEP = ParseUShortElement(element, "DecreaseEP") }); } while (battleInfo.Elements.Count < BattleEvoElementCount) { battleInfo.Elements.Add(new BattleEvolutionElement { ElementIndex = battleInfo.Elements.Count + 1 }); } package.BattleInfos.Add(battleInfo); } } while (package.BattleInfos.Count < BattleEvoTypeCount) { package.BattleInfos.Add(CreateDefaultBattleEvolutionInfo(package.BattleInfos.Count + 1)); } return package; } public static void WriteToBinary(string outputPath, DigimonEvolutionPackage package) { Directory.CreateDirectory(Path.GetDirectoryName(outputPath)!); using BinaryWriter writer = new(File.Open(outputPath, FileMode.Create, FileAccess.Write, FileShare.None), Encoding.ASCII, leaveOpen: false); writer.Write(package.Digimons.Count); foreach (DigimonEvolutionInfo digimon in package.Digimons) { writer.Write(digimon.DigimonId); writer.Write(digimon.BattleEvoType); writer.Write(digimon.Evolutions.Count); foreach (EvolutionObject evolution in digimon.Evolutions) { WriteEvolutionObject(writer, evolution); } } List battleInfos = package.BattleInfos .OrderBy(info => info.TypeIndex) .Take(BattleEvoTypeCount) .ToList(); while (battleInfos.Count < BattleEvoTypeCount) { battleInfos.Add(CreateDefaultBattleEvolutionInfo(battleInfos.Count + 1)); } writer.Write(battleInfos.Count); foreach (BattleEvolutionInfo battleInfo in battleInfos) { WriteBattleEvolutionInfo(writer, battleInfo); } } private static EvolutionObject ReadEvolutionObject(BinaryReader reader) { var evolution = new EvolutionObject { DigimonId = reader.ReadUInt32(), EvoSlot = reader.ReadUInt16() }; reader.ReadUInt16(); for (int i = 0; i < MaxEvolution; i++) { evolution.EvolutionLinks.Add(new EvolutionLink { Slot = reader.ReadUInt16(), Type = reader.ReadUInt16(), DigimonId = reader.ReadUInt32() }); } evolution.IconPos = ReadPoint(reader); evolution.EnableSlot = reader.ReadUInt16(); evolution.OpenQualification = reader.ReadUInt16(); evolution.OpenLevel = reader.ReadUInt16(); evolution.OpenQuest = reader.ReadUInt16(); evolution.OpenItemTypeS = reader.ReadUInt16(); evolution.OpenItemNum = reader.ReadUInt16(); evolution.UseItem = reader.ReadUInt16(); evolution.UseItemNum = reader.ReadUInt16(); evolution.Intimacy = reader.ReadUInt16(); evolution.OpenCrest = reader.ReadUInt16(); evolution.EvoCard1 = reader.ReadUInt16(); evolution.EvoCard2 = reader.ReadUInt16(); evolution.EvoCard3 = reader.ReadUInt16(); evolution.EvoDigimental = reader.ReadUInt16(); evolution.EvoTamerDS = reader.ReadUInt16(); evolution.Dummy = reader.ReadUInt16(); evolution.Render = reader.ReadInt32(); evolution.TargetStartPos = ReadPoint(reader); evolution.StartHeight = reader.ReadInt32(); evolution.StartRot = reader.ReadInt32(); evolution.TargetEndPos = ReadPoint(reader); evolution.EndHeight = reader.ReadInt32(); evolution.EndRot = reader.ReadInt32(); evolution.Speed = reader.ReadInt32(); evolution.AnimationId = reader.ReadUInt32(); reader.ReadUInt32(); evolution.StartTime = reader.ReadDouble(); evolution.EndTime = reader.ReadDouble(); evolution.R = reader.ReadInt32(); evolution.G = reader.ReadInt32(); evolution.B = reader.ReadInt32(); evolution.LevelEffect = ReadFixedAscii(reader, MaxEffectLength); evolution.EnchantEffect = ReadFixedAscii(reader, MaxEffectLength); evolution.SizeEffect = ReadFixedAscii(reader, MaxEffectLength); evolution.EvolutionTree = reader.ReadInt32(); evolution.JoGressQuestCheck = reader.ReadUInt32(); evolution.ChipsetType = reader.ReadUInt16(); evolution.ChipsetTypeC = reader.ReadUInt16(); evolution.ChipsetNum = reader.ReadUInt16(); evolution.ChipsetTypeP = reader.ReadUInt16(); evolution.JoGressesNum = reader.ReadUInt16(); reader.ReadUInt16(); for (int i = 0; i < MaxJointTacticsLength; i++) { evolution.JoGressTacticses[i] = reader.ReadInt32(); } reader.ReadUInt32(); // tail padding to keep sizeof(CDigimonEvolveObj) aligned to 8 bytes return evolution; } private static void WriteEvolutionObject(BinaryWriter writer, EvolutionObject evolution) { writer.Write(evolution.DigimonId); writer.Write(evolution.EvoSlot); writer.Write((ushort)0); for (int i = 0; i < MaxEvolution; i++) { EvolutionLink link = i < evolution.EvolutionLinks.Count ? evolution.EvolutionLinks[i] : new EvolutionLink(); writer.Write(link.Slot); writer.Write(link.Type); writer.Write(link.DigimonId); } WritePoint(writer, evolution.IconPos); writer.Write(evolution.EnableSlot); writer.Write(evolution.OpenQualification); writer.Write(evolution.OpenLevel); writer.Write(evolution.OpenQuest); writer.Write(evolution.OpenItemTypeS); writer.Write(evolution.OpenItemNum); writer.Write(evolution.UseItem); writer.Write(evolution.UseItemNum); writer.Write(evolution.Intimacy); writer.Write(evolution.OpenCrest); writer.Write(evolution.EvoCard1); writer.Write(evolution.EvoCard2); writer.Write(evolution.EvoCard3); writer.Write(evolution.EvoDigimental); writer.Write(evolution.EvoTamerDS); writer.Write(evolution.Dummy); writer.Write(evolution.Render); WritePoint(writer, evolution.TargetStartPos); writer.Write(evolution.StartHeight); writer.Write(evolution.StartRot); WritePoint(writer, evolution.TargetEndPos); writer.Write(evolution.EndHeight); writer.Write(evolution.EndRot); writer.Write(evolution.Speed); writer.Write(evolution.AnimationId); writer.Write(0); writer.Write(evolution.StartTime); writer.Write(evolution.EndTime); writer.Write(evolution.R); writer.Write(evolution.G); writer.Write(evolution.B); WriteFixedAscii(writer, evolution.LevelEffect, MaxEffectLength); WriteFixedAscii(writer, evolution.EnchantEffect, MaxEffectLength); WriteFixedAscii(writer, evolution.SizeEffect, MaxEffectLength); writer.Write(evolution.EvolutionTree); writer.Write(evolution.JoGressQuestCheck); writer.Write(evolution.ChipsetType); writer.Write(evolution.ChipsetTypeC); writer.Write(evolution.ChipsetNum); writer.Write(evolution.ChipsetTypeP); writer.Write(evolution.JoGressesNum); writer.Write((ushort)0); for (int i = 0; i < MaxJointTacticsLength; i++) { writer.Write(i < evolution.JoGressTacticses.Length ? evolution.JoGressTacticses[i] : 0); } writer.Write(0); // tail padding to keep sizeof(CDigimonEvolveObj) aligned to 8 bytes } private static BattleEvolutionInfo ReadBattleEvolutionInfo(BinaryReader reader, int typeIndex) { var battleInfo = new BattleEvolutionInfo { TypeIndex = typeIndex, MaxEP = reader.ReadUInt16() }; for (int i = 0; i < BattleEvoElementCount; i++) { battleInfo.Elements.Add(new BattleEvolutionElement { ElementIndex = i + 1, IsEnabled = reader.ReadBoolean(), }); reader.ReadByte(); // padding after bool to keep sELEMENT aligned to 2 bytes BattleEvolutionElement currentElement = battleInfo.Elements[^1]; currentElement.EvolStep = reader.ReadUInt16(); currentElement.RequiredEvoEP = reader.ReadUInt16(); currentElement.DecreaseEP = reader.ReadUInt16(); } return battleInfo; } private static void WriteBattleEvolutionInfo(BinaryWriter writer, BattleEvolutionInfo battleInfo) { writer.Write(battleInfo.MaxEP); for (int i = 0; i < BattleEvoElementCount; i++) { BattleEvolutionElement element = i < battleInfo.Elements.Count ? battleInfo.Elements[i] : new BattleEvolutionElement { ElementIndex = i + 1 }; writer.Write(element.IsEnabled); writer.Write((byte)0); // padding after bool to keep sELEMENT aligned to 2 bytes writer.Write(element.EvolStep); writer.Write(element.RequiredEvoEP); writer.Write(element.DecreaseEP); } } private static IntPoint ReadPoint(BinaryReader reader) { return new IntPoint { X = reader.ReadInt32(), Y = reader.ReadInt32() }; } private static void WritePoint(BinaryWriter writer, IntPoint point) { writer.Write(point.X); writer.Write(point.Y); } private static string ReadFixedAscii(BinaryReader reader, int length) { byte[] bytes = reader.ReadBytes(length); int terminatorIndex = Array.IndexOf(bytes, (byte)0); int count = terminatorIndex >= 0 ? terminatorIndex : bytes.Length; string value = Encoding.ASCII.GetString(bytes, 0, count); return RemoveInvalidXmlCharacters(value); } private static void WriteFixedAscii(BinaryWriter writer, string value, int length) { byte[] buffer = new byte[length]; Encoding.ASCII.GetBytes((value ?? string.Empty).AsSpan(0, Math.Min(value?.Length ?? 0, length - 1)), buffer); writer.Write(buffer); } private static string RemoveInvalidXmlCharacters(string value) { if (string.IsNullOrEmpty(value)) { return string.Empty; } var builder = new StringBuilder(value.Length); foreach (char c in value) { if (XmlConvert.IsXmlChar(c)) { builder.Append(c); } } return builder.ToString(); } private static XDocument BuildDigimonDocument(DigimonEvolutionPackage package) { var root = new XElement("DigimonEvolutionTable"); foreach (DigimonEvolutionInfo digimon in package.Digimons) { var digimonElement = new XElement("Digimon", new XElement("DigimonId", digimon.DigimonId), new XElement("BattleEvoType", digimon.BattleEvoType)); foreach (EvolutionObject evolution in digimon.Evolutions) { var evolutionElement = new XElement("Evolution", new XElement("DigimonId", evolution.DigimonId), new XElement("EvoSlot", evolution.EvoSlot), WritePointElement("IconPos", evolution.IconPos), new XElement("EnableSlot", evolution.EnableSlot), new XElement("OpenQualification", evolution.OpenQualification), new XElement("OpenLevel", evolution.OpenLevel), new XElement("OpenQuest", evolution.OpenQuest), new XElement("OpenItemTypeS", evolution.OpenItemTypeS), new XElement("OpenItemNum", evolution.OpenItemNum), new XElement("UseItem", evolution.UseItem), new XElement("UseItemNum", evolution.UseItemNum), new XElement("Intimacy", evolution.Intimacy), new XElement("OpenCrest", evolution.OpenCrest), new XElement("EvoCard1", evolution.EvoCard1), new XElement("EvoCard2", evolution.EvoCard2), new XElement("EvoCard3", evolution.EvoCard3), new XElement("EvoDigimental", evolution.EvoDigimental), new XElement("EvoTamerDS", evolution.EvoTamerDS), new XElement("Dummy", evolution.Dummy), new XElement("Render", evolution.Render), WritePointElement("TargetStartPos", evolution.TargetStartPos), new XElement("StartHeight", evolution.StartHeight), new XElement("StartRot", evolution.StartRot), WritePointElement("TargetEndPos", evolution.TargetEndPos), new XElement("EndHeight", evolution.EndHeight), new XElement("EndRot", evolution.EndRot), new XElement("Speed", evolution.Speed), new XElement("AnimationId", evolution.AnimationId), new XElement("StartTime", evolution.StartTime.ToString("R", CultureInfo.InvariantCulture)), new XElement("EndTime", evolution.EndTime.ToString("R", CultureInfo.InvariantCulture)), new XElement("R", evolution.R), new XElement("G", evolution.G), new XElement("B", evolution.B), new XElement("LevelEffect", evolution.LevelEffect ?? string.Empty), new XElement("EnchantEffect", evolution.EnchantEffect ?? string.Empty), new XElement("SizeEffect", evolution.SizeEffect ?? string.Empty), new XElement("EvolutionTree", evolution.EvolutionTree), new XElement("JoGressQuestCheck", evolution.JoGressQuestCheck), new XElement("ChipsetType", evolution.ChipsetType), new XElement("ChipsetTypeC", evolution.ChipsetTypeC), new XElement("ChipsetNum", evolution.ChipsetNum), new XElement("ChipsetTypeP", evolution.ChipsetTypeP), new XElement("JoGressesNum", evolution.JoGressesNum)); foreach (EvolutionLink link in evolution.EvolutionLinks.Take(MaxEvolution)) { evolutionElement.Add( new XElement("EvolutionLink", new XElement("Slot", link.Slot), new XElement("Type", link.Type), new XElement("DigimonId", link.DigimonId))); } var tacticsElement = new XElement("JoGressTacticses"); for (int index = 0; index < MaxJointTacticsLength; index++) { tacticsElement.Add( new XElement("Tactic", new XAttribute("Index", index + 1), new XAttribute("Value", evolution.JoGressTacticses[index]))); } evolutionElement.Add(tacticsElement); digimonElement.Add(evolutionElement); } root.Add(digimonElement); } return new XDocument(new XDeclaration("1.0", "utf-8", null), root); } private static XDocument BuildBattleDocument(DigimonEvolutionPackage package) { IEnumerable battleInfos = package.BattleInfos.Any() ? package.BattleInfos.OrderBy(info => info.TypeIndex) : Enumerable.Range(1, BattleEvoTypeCount).Select(CreateDefaultBattleEvolutionInfo); var root = new XElement("BattleEvolutionTable"); foreach (BattleEvolutionInfo info in battleInfos) { var battleElement = new XElement("BattleEvolution", new XElement("TypeIndex", info.TypeIndex), new XElement("MaxEP", info.MaxEP)); foreach (BattleEvolutionElement element in info.Elements.Take(BattleEvoElementCount)) { battleElement.Add( new XElement("Element", new XElement("ElementIndex", element.ElementIndex), new XElement("IsEnabled", element.IsEnabled), new XElement("EvolStep", element.EvolStep), new XElement("RequiredEvoEP", element.RequiredEvoEP), new XElement("DecreaseEP", element.DecreaseEP))); } root.Add(battleElement); } return new XDocument(new XDeclaration("1.0", "utf-8", null), root); } private static XElement WritePointElement(string name, IntPoint point) { return new XElement(name, new XElement("X", point.X), new XElement("Y", point.Y)); } private static IntPoint ReadPoint(XElement? pointElement) { if (pointElement == null) { return new IntPoint(); } return new IntPoint { X = ParseIntElement(pointElement, "X"), Y = ParseIntElement(pointElement, "Y") }; } private static int ParseIntElement(XElement element, string name) { return int.Parse(element.Element(name)?.Value ?? "0", CultureInfo.InvariantCulture); } private static uint ParseUIntElement(XElement element, string name) { return uint.Parse(element.Element(name)?.Value ?? "0", CultureInfo.InvariantCulture); } private static ushort ParseUShortElement(XElement element, string name) { return ushort.Parse(element.Element(name)?.Value ?? "0", CultureInfo.InvariantCulture); } private static double ParseDoubleElement(XElement element, string name) { return double.Parse(element.Element(name)?.Value ?? "0", CultureInfo.InvariantCulture); } private static bool ParseBoolElement(XElement element, string name) { return bool.Parse(element.Element(name)?.Value ?? "False"); } private static int ParseIntAttribute(XElement element, string name) { return int.Parse(element.Attribute(name)?.Value ?? "0", CultureInfo.InvariantCulture); } private static string GetElementValue(XElement element, string name) { return element.Element(name)?.Value ?? string.Empty; } private static void SaveXDocument(XDocument document, string outputPath) { string tempPath = outputPath + ".tmp"; var settings = new XmlWriterSettings { Indent = true, Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false) }; using (XmlWriter writer = XmlWriter.Create(tempPath, settings)) { document.Save(writer); } if (File.Exists(outputPath)) { File.Delete(outputPath); } File.Move(tempPath, outputPath); } private static BattleEvolutionInfo CreateDefaultBattleEvolutionInfo(int typeIndex) { var info = new BattleEvolutionInfo { TypeIndex = typeIndex }; for (int i = 0; i < BattleEvoElementCount; i++) { info.Elements.Add(new BattleEvolutionElement { ElementIndex = i + 1 }); } return info; } }