using System.Globalization; using System.Xml; using System.Xml.Linq; namespace BinXmlConverter.Classes { public static class MapNpc { public sealed class MapNPCs { public uint s_dwNpcID { get; set; } public uint s_dwMapID { get; set; } public int s_nInitPosX { get; set; } public int s_nInitPosY { get; set; } public float s_fRotation { get; set; } } public static MapNPCs[] ReadMapNPCFromBinary(string filePath) { using BinaryReader reader = new(File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)); int count = reader.ReadInt32(); if (count < 0) { throw new InvalidDataException($"MapNpc count is invalid: {count}."); } MapNPCs[] npcs = new MapNPCs[count]; for (int i = 0; i < count; i++) { npcs[i] = new MapNPCs { s_dwNpcID = reader.ReadUInt32(), s_dwMapID = reader.ReadUInt32(), s_nInitPosX = reader.ReadInt32(), s_nInitPosY = reader.ReadInt32(), s_fRotation = reader.ReadSingle() }; } if (reader.BaseStream.Position != reader.BaseStream.Length) { throw new InvalidDataException($"MapNpc.bin was not fully consumed. Remaining bytes: {reader.BaseStream.Length - reader.BaseStream.Position}."); } return npcs; } public static void ExportMapNPCsToXml(MapNPCs[] mapNPCs, string filePath) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)!); XmlWriterSettings settings = new() { Indent = true, Encoding = new System.Text.UTF8Encoding(false) }; using XmlWriter writer = XmlWriter.Create(filePath, settings); writer.WriteStartDocument(); writer.WriteStartElement("MapNpcTable"); writer.WriteElementString("Count", mapNPCs.Length.ToString(CultureInfo.InvariantCulture)); writer.WriteStartElement("Npcs"); foreach (MapNPCs npc in mapNPCs) { writer.WriteStartElement("MapNpc"); WriteElement(writer, "s_dwNpcID", npc.s_dwNpcID); WriteElement(writer, "s_dwMapID", npc.s_dwMapID); WriteElement(writer, "s_nInitPosX", npc.s_nInitPosX); WriteElement(writer, "s_nInitPosY", npc.s_nInitPosY); WriteElement(writer, "s_fRotation", npc.s_fRotation); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); } public static MapNPCs[] ImportMapNPCsFromXml(string filePath) { XDocument document = XDocument.Load(filePath); XElement root = document.Root ?? throw new InvalidDataException("MapNpc.xml is missing the root element."); XElement npcsElement = root.Element("Npcs") ?? throw new InvalidDataException("MapNpc.xml is missing the Npcs element."); return npcsElement.Elements("MapNpc") .Select(element => new MapNPCs { s_dwNpcID = ReadUInt(element, "s_dwNpcID"), s_dwMapID = ReadUInt(element, "s_dwMapID"), s_nInitPosX = ReadInt(element, "s_nInitPosX"), s_nInitPosY = ReadInt(element, "s_nInitPosY"), s_fRotation = ReadFloat(element, "s_fRotation") }) .ToArray(); } public static void WriteMapNPCToBinary(string filePath, MapNPCs[] mapNPCs) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)!); using BinaryWriter writer = new(File.Open(filePath, FileMode.Create, FileAccess.Write, FileShare.None)); writer.Write(mapNPCs.Length); foreach (MapNPCs npc in mapNPCs) { writer.Write(npc.s_dwNpcID); writer.Write(npc.s_dwMapID); writer.Write(npc.s_nInitPosX); writer.Write(npc.s_nInitPosY); writer.Write(npc.s_fRotation); } } private static void WriteElement(XmlWriter writer, string name, object value) { writer.WriteElementString(name, Convert.ToString(value, CultureInfo.InvariantCulture) ?? string.Empty); } private static int ReadInt(XElement parent, string name) { return int.Parse(parent.Element(name)?.Value ?? "0", CultureInfo.InvariantCulture); } private static uint ReadUInt(XElement parent, string name) { return uint.Parse(parent.Element(name)?.Value ?? "0", CultureInfo.InvariantCulture); } private static float ReadFloat(XElement parent, string name) { return float.Parse(parent.Element(name)?.Value ?? "0", CultureInfo.InvariantCulture); } } }