using System.Globalization; using System.Text; using System.Xml; using System.Xml.Linq; namespace BinXmlConverter.Classes { public static class MapMonsters { private const int MonsterInfoSize = 48; public sealed class FileGroup { public uint FileTableID { get; set; } public List MapGroups { get; } = new(); } public sealed class MapGroup { public uint GroupMapID { get; set; } public List Monsters { get; } = new(); } public sealed class MonsterSpawn { public uint s_dwMapID { get; set; } public uint s_dwMonsterTableID { get; set; } public int s_nCenterX { get; set; } public int s_nCenterY { get; set; } public int s_nRadius { get; set; } public int s_nCount { get; set; } public int s_nResponTime { get; set; } public int s_nKillgen_MonFTID { get; set; } public int s_nKillgen_Count { get; set; } public int s_nKillgen_ViewCnt { get; set; } public int s_nMoveType { get; set; } public bool s_bInsRespon { get; set; } } public static FileGroup[] ReadMapMonstersFromBinary(string filePath) { using BinaryReader reader = new(File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)); int fileGroupCount = reader.ReadInt32(); if (fileGroupCount < 0) { throw new InvalidDataException($"MapMonsterList file-group count is invalid: {fileGroupCount}."); } FileGroup[] fileGroups = new FileGroup[fileGroupCount]; for (int i = 0; i < fileGroupCount; i++) { FileGroup fileGroup = new() { FileTableID = reader.ReadUInt32() }; int mapGroupCount = reader.ReadInt32(); if (mapGroupCount < 0) { throw new InvalidDataException($"FileTableID {fileGroup.FileTableID} has invalid map-group count {mapGroupCount}."); } for (int j = 0; j < mapGroupCount; j++) { MapGroup mapGroup = new() { GroupMapID = reader.ReadUInt32() }; int monsterCount = reader.ReadInt32(); if (monsterCount < 0) { throw new InvalidDataException($"FileTableID {fileGroup.FileTableID}, GroupMapID {mapGroup.GroupMapID} has invalid monster count {monsterCount}."); } for (int k = 0; k < monsterCount; k++) { byte[] monsterBytes = reader.ReadBytes(MonsterInfoSize); if (monsterBytes.Length != MonsterInfoSize) { throw new EndOfStreamException("Unable to read the expected MapMonster info block."); } mapGroup.Monsters.Add(ReadMonster(monsterBytes)); } fileGroup.MapGroups.Add(mapGroup); } fileGroups[i] = fileGroup; } if (reader.BaseStream.Position != reader.BaseStream.Length) { throw new InvalidDataException($"MapMonsterList.bin was not fully consumed. Remaining bytes: {reader.BaseStream.Length - reader.BaseStream.Position}."); } return fileGroups; } public static void ExportMapMonstersToXml(FileGroup[] fileGroups, 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("MapMonsterList"); writer.WriteElementString("FileGroupCount", fileGroups.Length.ToString(CultureInfo.InvariantCulture)); writer.WriteStartElement("FileGroups"); foreach (FileGroup fileGroup in fileGroups) { writer.WriteStartElement("FileGroup"); WriteElement(writer, "FileTableID", fileGroup.FileTableID); writer.WriteElementString("MapGroupCount", fileGroup.MapGroups.Count.ToString(CultureInfo.InvariantCulture)); writer.WriteStartElement("MapGroups"); foreach (MapGroup mapGroup in fileGroup.MapGroups) { writer.WriteStartElement("MapGroup"); WriteElement(writer, "GroupMapID", mapGroup.GroupMapID); writer.WriteElementString("MonsterCount", mapGroup.Monsters.Count.ToString(CultureInfo.InvariantCulture)); writer.WriteStartElement("Monsters"); foreach (MonsterSpawn monster in mapGroup.Monsters) { writer.WriteStartElement("Monster"); WriteElement(writer, "s_dwMapID", monster.s_dwMapID); WriteElement(writer, "s_dwMonsterTableID", monster.s_dwMonsterTableID); WriteElement(writer, "s_nCenterX", monster.s_nCenterX); WriteElement(writer, "s_nCenterY", monster.s_nCenterY); WriteElement(writer, "s_nRadius", monster.s_nRadius); WriteElement(writer, "s_nCount", monster.s_nCount); WriteElement(writer, "s_nResponTime", monster.s_nResponTime); WriteElement(writer, "s_nKillgen_MonFTID", monster.s_nKillgen_MonFTID); WriteElement(writer, "s_nKillgen_Count", monster.s_nKillgen_Count); WriteElement(writer, "s_nKillgen_ViewCnt", monster.s_nKillgen_ViewCnt); WriteElement(writer, "s_nMoveType", monster.s_nMoveType); WriteElement(writer, "s_bInsRespon", monster.s_bInsRespon ? 1 : 0); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); } public static FileGroup[] ImportMapMonstersFromXml(string filePath) { XDocument document = XDocument.Load(filePath); XElement root = document.Root ?? throw new InvalidDataException("MapMonsterList.xml is missing the root element."); XElement fileGroupsElement = root.Element("FileGroups") ?? throw new InvalidDataException("MapMonsterList.xml is missing the FileGroups element."); List fileGroups = new(); foreach (XElement fileGroupElement in fileGroupsElement.Elements("FileGroup")) { FileGroup fileGroup = new() { FileTableID = ReadUInt(fileGroupElement, "FileTableID") }; foreach (XElement mapGroupElement in fileGroupElement.Element("MapGroups")?.Elements("MapGroup") ?? Enumerable.Empty()) { MapGroup mapGroup = new() { GroupMapID = ReadUInt(mapGroupElement, "GroupMapID") }; foreach (XElement monsterElement in mapGroupElement.Element("Monsters")?.Elements("Monster") ?? Enumerable.Empty()) { mapGroup.Monsters.Add(new MonsterSpawn { s_dwMapID = ReadUInt(monsterElement, "s_dwMapID"), s_dwMonsterTableID = ReadUInt(monsterElement, "s_dwMonsterTableID"), s_nCenterX = ReadInt(monsterElement, "s_nCenterX"), s_nCenterY = ReadInt(monsterElement, "s_nCenterY"), s_nRadius = ReadInt(monsterElement, "s_nRadius"), s_nCount = ReadInt(monsterElement, "s_nCount"), s_nResponTime = ReadInt(monsterElement, "s_nResponTime"), s_nKillgen_MonFTID = ReadInt(monsterElement, "s_nKillgen_MonFTID"), s_nKillgen_Count = ReadInt(monsterElement, "s_nKillgen_Count"), s_nKillgen_ViewCnt = ReadInt(monsterElement, "s_nKillgen_ViewCnt"), s_nMoveType = ReadInt(monsterElement, "s_nMoveType"), s_bInsRespon = ReadBool(monsterElement, "s_bInsRespon") }); } fileGroup.MapGroups.Add(mapGroup); } fileGroups.Add(fileGroup); } return fileGroups.ToArray(); } public static void WriteMapMonstersToBinary(FileGroup[] fileGroups, string filePath) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)!); using BinaryWriter writer = new(File.Open(filePath, FileMode.Create, FileAccess.Write, FileShare.None)); writer.Write(fileGroups.Length); foreach (FileGroup fileGroup in fileGroups) { writer.Write(fileGroup.FileTableID); writer.Write(fileGroup.MapGroups.Count); foreach (MapGroup mapGroup in fileGroup.MapGroups) { writer.Write(mapGroup.GroupMapID); writer.Write(mapGroup.Monsters.Count); foreach (MonsterSpawn monster in mapGroup.Monsters) { byte[] monsterBytes = new byte[MonsterInfoSize]; Array.Copy(BitConverter.GetBytes(monster.s_dwMapID), 0, monsterBytes, 0, sizeof(uint)); Array.Copy(BitConverter.GetBytes(monster.s_dwMonsterTableID), 0, monsterBytes, 4, sizeof(uint)); Array.Copy(BitConverter.GetBytes(monster.s_nCenterX), 0, monsterBytes, 8, sizeof(int)); Array.Copy(BitConverter.GetBytes(monster.s_nCenterY), 0, monsterBytes, 12, sizeof(int)); Array.Copy(BitConverter.GetBytes(monster.s_nRadius), 0, monsterBytes, 16, sizeof(int)); Array.Copy(BitConverter.GetBytes(monster.s_nCount), 0, monsterBytes, 20, sizeof(int)); Array.Copy(BitConverter.GetBytes(monster.s_nResponTime), 0, monsterBytes, 24, sizeof(int)); Array.Copy(BitConverter.GetBytes(monster.s_nKillgen_MonFTID), 0, monsterBytes, 28, sizeof(int)); Array.Copy(BitConverter.GetBytes(monster.s_nKillgen_Count), 0, monsterBytes, 32, sizeof(int)); Array.Copy(BitConverter.GetBytes(monster.s_nKillgen_ViewCnt), 0, monsterBytes, 36, sizeof(int)); Array.Copy(BitConverter.GetBytes(monster.s_nMoveType), 0, monsterBytes, 40, sizeof(int)); monsterBytes[44] = (byte)(monster.s_bInsRespon ? 1 : 0); writer.Write(monsterBytes); } } } } private static MonsterSpawn ReadMonster(byte[] monsterBytes) { return new MonsterSpawn { s_dwMapID = BitConverter.ToUInt32(monsterBytes, 0), s_dwMonsterTableID = BitConverter.ToUInt32(monsterBytes, 4), s_nCenterX = BitConverter.ToInt32(monsterBytes, 8), s_nCenterY = BitConverter.ToInt32(monsterBytes, 12), s_nRadius = BitConverter.ToInt32(monsterBytes, 16), s_nCount = BitConverter.ToInt32(monsterBytes, 20), s_nResponTime = BitConverter.ToInt32(monsterBytes, 24), s_nKillgen_MonFTID = BitConverter.ToInt32(monsterBytes, 28), s_nKillgen_Count = BitConverter.ToInt32(monsterBytes, 32), s_nKillgen_ViewCnt = BitConverter.ToInt32(monsterBytes, 36), s_nMoveType = BitConverter.ToInt32(monsterBytes, 40), s_bInsRespon = monsterBytes[44] != 0 }; } 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 bool ReadBool(XElement parent, string name) { string value = parent.Element(name)?.Value ?? "0"; return value == "1" || value.Equals("true", StringComparison.OrdinalIgnoreCase); } } }