using System.Globalization; using System.Text; using System.Xml; using System.Xml.Linq; namespace BinXmlConverter.Classes; public class BuffData { private const int NameCharCount = 64; private const int CommentCharCount = 128; private const int EffectFileByteCount = 64; public ushort s_dwID { get; set; } public string s_szName { get; set; } = string.Empty; public string s_szComment { get; set; } = string.Empty; public ushort s_nBuffIcon { get; set; } public ushort s_nBuffType { get; set; } public ushort s_nBuffLifeType { get; set; } public ushort s_nBuffTimeType { get; set; } public ushort s_nMinLv { get; set; } public ushort s_nBuffClass { get; set; } public uint s_dwSkillCode { get; set; } public uint s_dwDigimonSkillCode { get; set; } public bool s_bDelete { get; set; } public string s_szEffectFile { get; set; } = string.Empty; public ushort s_nConditionLv { get; set; } public static BuffData[] BuffToXml(string buffInput) { using BinaryReader reader = new(File.Open(buffInput, FileMode.Open, FileAccess.Read, FileShare.Read)); int count = reader.ReadInt32(); BuffData[] buffs = new BuffData[count]; for (int i = 0; i < count; i++) { BuffData buff = new() { s_dwID = reader.ReadUInt16(), s_szName = ReadFixedUnicodeString(reader, NameCharCount), s_szComment = ReadFixedUnicodeString(reader, CommentCharCount), s_nBuffIcon = reader.ReadUInt16(), s_nBuffType = reader.ReadUInt16(), s_nBuffLifeType = reader.ReadUInt16(), s_nBuffTimeType = reader.ReadUInt16(), s_nMinLv = reader.ReadUInt16(), s_nBuffClass = reader.ReadUInt16() }; reader.ReadUInt16(); buff.s_dwSkillCode = reader.ReadUInt32(); buff.s_dwDigimonSkillCode = reader.ReadUInt32(); buff.s_bDelete = reader.ReadBoolean(); buff.s_szEffectFile = ReadFixedAsciiString(reader, EffectFileByteCount); reader.ReadByte(); buff.s_nConditionLv = reader.ReadUInt16(); buffs[i] = buff; } return buffs; } public static void ExportBuffToXml(BuffData[] buffArray, string filePath) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)!); XmlWriterSettings settings = new() { Indent = true, Encoding = new UTF8Encoding(false) }; using XmlWriter writer = XmlWriter.Create(filePath, settings); writer.WriteStartDocument(); writer.WriteStartElement("BuffTable"); foreach (BuffData buffData in buffArray) { writer.WriteStartElement("Buff"); writer.WriteElementString(nameof(s_dwID), buffData.s_dwID.ToString(CultureInfo.InvariantCulture)); WriteFullStringElement(writer, nameof(s_szName), buffData.s_szName); WriteFullStringElement(writer, nameof(s_szComment), buffData.s_szComment); writer.WriteElementString(nameof(s_nBuffIcon), buffData.s_nBuffIcon.ToString(CultureInfo.InvariantCulture)); writer.WriteElementString(nameof(s_nBuffType), buffData.s_nBuffType.ToString(CultureInfo.InvariantCulture)); writer.WriteElementString(nameof(s_nBuffLifeType), buffData.s_nBuffLifeType.ToString(CultureInfo.InvariantCulture)); writer.WriteElementString(nameof(s_nBuffTimeType), buffData.s_nBuffTimeType.ToString(CultureInfo.InvariantCulture)); writer.WriteElementString(nameof(s_nMinLv), buffData.s_nMinLv.ToString(CultureInfo.InvariantCulture)); writer.WriteElementString(nameof(s_nBuffClass), buffData.s_nBuffClass.ToString(CultureInfo.InvariantCulture)); writer.WriteElementString(nameof(s_dwSkillCode), buffData.s_dwSkillCode.ToString(CultureInfo.InvariantCulture)); writer.WriteElementString(nameof(s_dwDigimonSkillCode), buffData.s_dwDigimonSkillCode.ToString(CultureInfo.InvariantCulture)); writer.WriteElementString(nameof(s_bDelete), buffData.s_bDelete ? "true" : "false"); WriteFullStringElement(writer, nameof(s_szEffectFile), buffData.s_szEffectFile); writer.WriteElementString(nameof(s_nConditionLv), buffData.s_nConditionLv.ToString(CultureInfo.InvariantCulture)); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndDocument(); } public static BuffData[] ImportBuffFromXml(string filePath) { XDocument document = XDocument.Load(filePath); return document.Root? .Elements("Buff") .Select(buffElement => new BuffData { s_dwID = ParseUShort(buffElement, nameof(s_dwID)), s_szName = ReadString(buffElement, nameof(s_szName)), s_szComment = ReadString(buffElement, nameof(s_szComment)), s_nBuffIcon = ParseUShort(buffElement, nameof(s_nBuffIcon)), s_nBuffType = ParseUShort(buffElement, nameof(s_nBuffType)), s_nBuffLifeType = ParseUShort(buffElement, nameof(s_nBuffLifeType)), s_nBuffTimeType = ParseUShort(buffElement, nameof(s_nBuffTimeType)), s_nMinLv = ParseUShort(buffElement, nameof(s_nMinLv)), s_nBuffClass = ParseUShort(buffElement, nameof(s_nBuffClass)), s_dwSkillCode = ParseUInt(buffElement, nameof(s_dwSkillCode)), s_dwDigimonSkillCode = ParseUInt(buffElement, nameof(s_dwDigimonSkillCode)), s_bDelete = ParseBool(buffElement, nameof(s_bDelete)), s_szEffectFile = ReadString(buffElement, nameof(s_szEffectFile)), s_nConditionLv = ParseUShort(buffElement, nameof(s_nConditionLv)) }) .ToArray() ?? Array.Empty(); } public static void ExportBuffToBinary(string outputPath, BuffData[] buffs) { Directory.CreateDirectory(Path.GetDirectoryName(outputPath)!); using BinaryWriter writer = new(File.Open(outputPath, FileMode.Create, FileAccess.Write, FileShare.None)); writer.Write(buffs.Length); foreach (BuffData buff in buffs) { writer.Write(buff.s_dwID); WriteFixedUnicodeString(writer, buff.s_szName, NameCharCount); WriteFixedUnicodeString(writer, buff.s_szComment, CommentCharCount); writer.Write(buff.s_nBuffIcon); writer.Write(buff.s_nBuffType); writer.Write(buff.s_nBuffLifeType); writer.Write(buff.s_nBuffTimeType); writer.Write(buff.s_nMinLv); writer.Write(buff.s_nBuffClass); writer.Write((ushort)0); writer.Write(buff.s_dwSkillCode); writer.Write(buff.s_dwDigimonSkillCode); writer.Write(buff.s_bDelete); WriteFixedAsciiString(writer, buff.s_szEffectFile, EffectFileByteCount); writer.Write((byte)0); writer.Write(buff.s_nConditionLv); } } 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 ReadFixedAsciiString(BinaryReader reader, int byteCount) { byte[] buffer = reader.ReadBytes(byteCount); int nullIndex = Array.IndexOf(buffer, (byte)0); int length = nullIndex >= 0 ? nullIndex : buffer.Length; return Encoding.ASCII.GetString(buffer, 0, length); } 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 void WriteFixedAsciiString(BinaryWriter writer, string value, int byteCount) { byte[] bytes = Encoding.ASCII.GetBytes(value ?? string.Empty); Array.Resize(ref bytes, byteCount); writer.Write(bytes); } private static void WriteFullStringElement(XmlWriter writer, string name, string value) { writer.WriteStartElement(name); writer.WriteString(RemoveInvalidXmlCharacters(value)); writer.WriteFullEndElement(); } private static string RemoveInvalidXmlCharacters(string? value) { if (string.IsNullOrEmpty(value)) { return string.Empty; } return new string(value.Where(XmlConvert.IsXmlChar).ToArray()); } private static string ReadString(XElement element, string name) { return (string?)element.Element(name) ?? string.Empty; } private static ushort ParseUShort(XElement element, string name) { return ushort.Parse(((string?)element.Element(name)) ?? "0", CultureInfo.InvariantCulture); } private static uint ParseUInt(XElement element, string name) { return uint.Parse(((string?)element.Element(name)) ?? "0", CultureInfo.InvariantCulture); } private static bool ParseBool(XElement element, string name) { string value = ((string?)element.Element(name)) ?? "false"; return value.Equals("1", StringComparison.OrdinalIgnoreCase) || value.Equals("true", StringComparison.OrdinalIgnoreCase); } }