using System; using System.IO; using System.Text; using System.Xml; using System.Collections.Generic; using System.Globalization; using System.Xml.Linq; namespace BinXmlConverter.Classes { public class DigimonData { // ================= BASIC ================= public int Species, Model, DigimonRank; public int EvolutionType, AttributeType; public int Family1, Family2, Family3; public int NatureType, Nature1, Nature2, Nature3; public int BaseLevel; // ================= STRINGS ================= public string Name = ""; public string SoundDirName = ""; public string s_szEvoEffectDir = ""; public string Form = ""; // ================= STATS (BIN = ushort) ================= public ushort s_nBaseStat_HP; public ushort s_nBaseStat_DS; public ushort s_nBaseStat_DE; public ushort s_nBaseStat_EV; public ushort s_nBaseStat_MS; public ushort s_nBaseStat_CR; public ushort s_nBaseStat_AT; public ushort s_nBaseStat_AS; public ushort s_nBaseStat_AR; public ushort s_nBaseStat_HT; // XML logical, BIN ushort public int CharSize; // ================= SKILLS ================= public int Skill1_ID, Skill2_ID, Skill3_ID, Skill4_ID; public int SkillLevel, SkillLevel1, SkillLevel2, SkillLevel3; // ================= MOVE ================= public float s_fSelectScale; public float WakkLen, RunLen, aRunLen; // ================= FLAGS ================= public byte DigimonType; public byte padding; // ===================================================== // SAFE PARSERS (NO OVERFLOW EVER) // ===================================================== private static ushort ParseUShortSafe(string v) { int i = ParseIntSafe(v); if (i < ushort.MinValue) return ushort.MinValue; if (i > ushort.MaxValue) return ushort.MaxValue; return (ushort)i; } private static byte ParseByteSafe(string v) { int i = ParseIntSafe(v); if (i < byte.MinValue) return byte.MinValue; if (i > byte.MaxValue) return byte.MaxValue; return (byte)i; } private static int ParseIntSafe(string v) { if (int.TryParse(v, NumberStyles.Integer, CultureInfo.InvariantCulture, out int invariantValue)) return invariantValue; return int.Parse(v, NumberStyles.Integer, CultureInfo.CurrentCulture); } private static float ParseFloatSafe(string v) { if (float.TryParse(v, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out float invariantValue)) return invariantValue; return float.Parse(v, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.CurrentCulture); } // ===================================================== // XML -> OBJECT // ===================================================== private static void SetProperty(DigimonData d, string n, string v) { switch (n) { case "Species": d.Species = ParseIntSafe(v); break; case "Model": d.Model = ParseIntSafe(v); break; case "DigimonRank": d.DigimonRank = ParseIntSafe(v); break; case "EvolutionType": d.EvolutionType = ParseIntSafe(v); break; case "AttributeType": d.AttributeType = ParseIntSafe(v); break; case "Family1": d.Family1 = ParseIntSafe(v); break; case "Family2": d.Family2 = ParseIntSafe(v); break; case "Family3": d.Family3 = ParseIntSafe(v); break; case "NatureType": d.NatureType = ParseIntSafe(v); break; case "Nature1": d.Nature1 = ParseIntSafe(v); break; case "Nature2": d.Nature2 = ParseIntSafe(v); break; case "Nature3": d.Nature3 = ParseIntSafe(v); break; case "BaseLevel": d.BaseLevel = ParseIntSafe(v); break; //case "Name": d.Name = v; break; //case "DisplayName": d.DisplayName = v; break; case "Name": { d.Name = v; break; } case "DisplayName": { if (string.IsNullOrEmpty(d.Name)) d.Name = v; break; } case "SoundDirName": d.SoundDirName = v; break; case "s_szEvoEffectDir": d.s_szEvoEffectDir = v; break; case "Form": d.Form = v; break; case "s_nBaseStat_HP": d.s_nBaseStat_HP = ParseUShortSafe(v); break; case "s_nBaseStat_DS": d.s_nBaseStat_DS = ParseUShortSafe(v); break; case "s_nBaseStat_DE": d.s_nBaseStat_DE = ParseUShortSafe(v); break; case "s_nBaseStat_EV": d.s_nBaseStat_EV = ParseUShortSafe(v); break; case "s_nBaseStat_MS": d.s_nBaseStat_MS = ParseUShortSafe(v); break; case "s_nBaseStat_CR": d.s_nBaseStat_CR = ParseUShortSafe(v); break; case "s_nBaseStat_AT": d.s_nBaseStat_AT = ParseUShortSafe(v); break; case "s_nBaseStat_AS": d.s_nBaseStat_AS = ParseUShortSafe(v); break; case "s_nBaseStat_AR": d.s_nBaseStat_AR = ParseUShortSafe(v); break; case "s_nBaseStat_HT": d.s_nBaseStat_HT = ParseUShortSafe(v); break; case "CharSize": d.CharSize = ParseUShortSafe(v); break; case "Skill1_ID": d.Skill1_ID = ParseIntSafe(v); break; case "Skill2_ID": d.Skill2_ID = ParseIntSafe(v); break; case "Skill3_ID": d.Skill3_ID = ParseIntSafe(v); break; case "Skill4_ID": d.Skill4_ID = ParseIntSafe(v); break; case "SkillLevel": case "Skill1_Level": d.SkillLevel = ParseIntSafe(v); break; case "SkillLevel1": case "Skill2_Level": d.SkillLevel1 = ParseIntSafe(v); break; case "SkillLevel2": case "Skill3_Level": d.SkillLevel2 = ParseIntSafe(v); break; case "SkillLevel3": case "Skill4_Level": d.SkillLevel3 = ParseIntSafe(v); break; case "s_fSelectScale": d.s_fSelectScale = ParseFloatSafe(v); break; case "WakkLen": d.WakkLen = ParseFloatSafe(v); break; case "RunLen": d.RunLen = ParseFloatSafe(v); break; case "aRunLen": d.aRunLen = ParseFloatSafe(v); break; case "DigimonType": d.DigimonType = ParseByteSafe(v); break; case "Unknow": case "Padding": d.padding = ParseByteSafe(v); break; } } public static DigimonData[] ImportDigimonDataFromXml(string xml) { List list = new(); using XmlReader r = XmlReader.Create(xml); while (r.Read()) { if (r.NodeType == XmlNodeType.Element && r.Name == "Digimon") { DigimonData d = new(); while (r.Read()) { if (r.NodeType == XmlNodeType.Element) { string n = r.Name; r.Read(); SetProperty(d, n, r.Value); } else if (r.NodeType == XmlNodeType.EndElement && r.Name == "Digimon") { list.Add(d); break; } } } } return list.ToArray(); } // ===================================================== // BIN -> OBJECT // ===================================================== public static DigimonData[] DigimonToXml(string bin) { using BinaryReader br = new(File.OpenRead(bin)); int count = br.ReadInt32(); DigimonData[] arr = new DigimonData[count]; for (int i = 0; i < count; i++) { DigimonData d = new(); d.Species = br.ReadInt32(); d.Model = br.ReadInt32(); d.Name = ReadUnicode(br, 64); d.SoundDirName = ReadAscii(br, 64); d.s_fSelectScale = br.ReadSingle(); d.s_szEvoEffectDir = ReadUnicode(br, 64); d.EvolutionType = br.ReadInt32(); d.AttributeType = br.ReadInt32(); d.Family1 = br.ReadInt32(); d.Family2 = br.ReadInt32(); d.Family3 = br.ReadInt32(); d.NatureType = br.ReadInt32(); d.Nature1 = br.ReadInt32(); d.Nature2 = br.ReadInt32(); d.Nature3 = br.ReadInt32(); d.BaseLevel = br.ReadInt32(); d.s_nBaseStat_HP = br.ReadUInt16(); d.s_nBaseStat_DS = br.ReadUInt16(); d.s_nBaseStat_DE = br.ReadUInt16(); d.s_nBaseStat_EV = br.ReadUInt16(); d.s_nBaseStat_MS = br.ReadUInt16(); d.s_nBaseStat_CR = br.ReadUInt16(); d.s_nBaseStat_AT = br.ReadUInt16(); d.s_nBaseStat_AS = br.ReadUInt16(); d.s_nBaseStat_AR = br.ReadUInt16(); d.s_nBaseStat_HT = br.ReadUInt16(); d.DigimonType = br.ReadByte(); d.padding = br.ReadByte(); d.CharSize = br.ReadUInt16(); d.Skill1_ID = br.ReadInt32(); d.SkillLevel = br.ReadInt32(); d.Skill2_ID = br.ReadInt32(); d.SkillLevel1 = br.ReadInt32(); d.Skill3_ID = br.ReadInt32(); d.SkillLevel2 = br.ReadInt32(); d.Skill4_ID = br.ReadInt32(); d.SkillLevel3 = br.ReadInt32(); d.WakkLen = br.ReadSingle(); d.RunLen = br.ReadSingle(); d.aRunLen = br.ReadSingle(); d.Form = ReadUnicode(br, 64); d.DigimonRank = br.ReadInt32(); ApplyNameFallbacks(d); arr[i] = d; } return arr; } // ===================================================== // OBJECT -> XML // ===================================================== public static void ExportDigimon_ListToXml(string xml, DigimonData[] list) { Directory.CreateDirectory(Path.GetDirectoryName(xml) ?? "."); string tempXml = xml + ".tmp"; using (XmlWriter w = XmlWriter.Create(tempXml, new XmlWriterSettings { Indent = true })) { w.WriteStartDocument(); w.WriteStartElement("DigimonList"); foreach (var d in list) { ApplyNameFallbacks(d); w.WriteStartElement("Digimon"); Write(w, "Species", d.Species); Write(w, "Model", d.Model); Write(w, "Name", d.Name); Write(w, "SoundDirName", d.SoundDirName); Write(w, "s_fSelectScale", d.s_fSelectScale); Write(w, "s_szEvoEffectDir", d.s_szEvoEffectDir); Write(w, "EvolutionType", d.EvolutionType); Write(w, "AttributeType", d.AttributeType); Write(w, "Family1", d.Family1); Write(w, "Family2", d.Family2); Write(w, "Family3", d.Family3); Write(w, "NatureType", d.NatureType); Write(w, "Nature1", d.Nature1); Write(w, "Nature2", d.Nature2); Write(w, "Nature3", d.Nature3); Write(w, "BaseLevel", d.BaseLevel); Write(w, "s_nBaseStat_HP", d.s_nBaseStat_HP); Write(w, "s_nBaseStat_DS", d.s_nBaseStat_DS); Write(w, "s_nBaseStat_DE", d.s_nBaseStat_DE); Write(w, "s_nBaseStat_EV", d.s_nBaseStat_EV); Write(w, "s_nBaseStat_MS", d.s_nBaseStat_MS); Write(w, "s_nBaseStat_CR", d.s_nBaseStat_CR); Write(w, "s_nBaseStat_AT", d.s_nBaseStat_AT); Write(w, "s_nBaseStat_AS", d.s_nBaseStat_AS); Write(w, "s_nBaseStat_AR", d.s_nBaseStat_AR); Write(w, "s_nBaseStat_HT", d.s_nBaseStat_HT); Write(w, "DigimonType", d.DigimonType); Write(w, "CharSize", d.CharSize); Write(w, "Skill1_ID", d.Skill1_ID); Write(w, "Skill1_Level", d.SkillLevel); Write(w, "Skill2_ID", d.Skill2_ID); Write(w, "Skill2_Level", d.SkillLevel1); Write(w, "Skill3_ID", d.Skill3_ID); Write(w, "Skill3_Level", d.SkillLevel2); Write(w, "Skill4_ID", d.Skill4_ID); Write(w, "Skill4_Level", d.SkillLevel3); Write(w, "WakkLen", d.WakkLen); Write(w, "RunLen", d.RunLen); Write(w, "aRunLen", d.aRunLen); Write(w, "Form", d.Form); Write(w, "DigimonRank", d.DigimonRank); w.WriteEndElement(); } w.WriteEndElement(); w.WriteEndDocument(); w.Flush(); } File.Copy(tempXml, xml, true); File.Delete(tempXml); } // ===================================================== // OBJECT -> BIN // ===================================================== public static void ExportToBinary(string bin, DigimonData[] list) { using BinaryWriter w = new(File.Open(bin, FileMode.Create)); w.Write(list.Length); foreach (var d in list) { ApplyNameFallbacks(d); d.SoundDirName ??= ""; d.Form ??= ""; w.Write(d.Species); w.Write(d.Model); WriteUnicode(w, d.Name, 64); WriteAscii(w, d.SoundDirName, 64); w.Write(d.s_fSelectScale); WriteUnicode(w, d.s_szEvoEffectDir, 64); w.Write(d.EvolutionType); w.Write(d.AttributeType); w.Write(d.Family1); w.Write(d.Family2); w.Write(d.Family3); w.Write(d.NatureType); w.Write(d.Nature1); w.Write(d.Nature2); w.Write(d.Nature3); w.Write(d.BaseLevel); w.Write(d.s_nBaseStat_HP); w.Write(d.s_nBaseStat_DS); w.Write(d.s_nBaseStat_DE); w.Write(d.s_nBaseStat_EV); w.Write(d.s_nBaseStat_MS); w.Write(d.s_nBaseStat_CR); w.Write(d.s_nBaseStat_AT); w.Write(d.s_nBaseStat_AS); w.Write(d.s_nBaseStat_AR); w.Write(d.s_nBaseStat_HT); w.Write(d.DigimonType); w.Write(d.padding); w.Write((ushort)Math.Clamp(d.CharSize, ushort.MinValue, ushort.MaxValue)); w.Write(d.Skill1_ID); w.Write(d.SkillLevel); w.Write(d.Skill2_ID); w.Write(d.SkillLevel1); w.Write(d.Skill3_ID); w.Write(d.SkillLevel2); w.Write(d.Skill4_ID); w.Write(d.SkillLevel3); w.Write(d.WakkLen); w.Write(d.RunLen); w.Write(d.aRunLen); WriteUnicode(w, d.Form, 64); w.Write(d.DigimonRank); } } // ================= HELPERS ================= private static string ReadUnicode(BinaryReader br, int c) { char[] arr = new char[c]; for (int i = 0; i < c; i++) arr[i] = (char)br.ReadUInt16(); return CleanFixedString(new string(arr)); } private static string ReadAscii(BinaryReader br, int c) { return CleanFixedString(Encoding.ASCII.GetString(br.ReadBytes(c))); } private static void WriteUnicode(BinaryWriter bw, string s, int c) { for (int i = 0; i < c; i++) bw.Write((ushort)(i < s.Length ? s[i] : '\0')); } private static void WriteAscii(BinaryWriter bw, string s, int c) { byte[] b = Encoding.ASCII.GetBytes(s); Array.Resize(ref b, c); bw.Write(b); } private static void Write(XmlWriter w, string n, object v) { w.WriteStartElement(n); if (v is string s) { w.WriteString(SanitizeXmlString(s)); w.WriteFullEndElement(); return; } if (v is IFormattable formattable) { w.WriteString(formattable.ToString(null, CultureInfo.InvariantCulture)); w.WriteFullEndElement(); return; } w.WriteString(v.ToString()); w.WriteFullEndElement(); } private static void ApplyNameFallbacks(DigimonData d) { string fallbackName = $"Digimon_{d.Species}"; if (string.IsNullOrWhiteSpace(d.Name)) d.Name = fallbackName; if (string.IsNullOrWhiteSpace(d.SoundDirName)) d.SoundDirName = d.Name; } private static string CleanFixedString(string value) { int nullIndex = value.IndexOf('\0'); if (nullIndex >= 0) value = value[..nullIndex]; return SanitizeXmlString(value); } private static string SanitizeXmlString(string? value) { if (string.IsNullOrEmpty(value)) return string.Empty; StringBuilder builder = new StringBuilder(value.Length); foreach (char ch in value) { if (XmlConvert.IsXmlChar(ch)) builder.Append(ch); } return builder.ToString(); } } }