using System.Globalization; using System.Xml; using System.Xml.Linq; namespace BinXmlConverter.Classes; public static class WeatherPackage { public sealed class WeatherElement { public int s_nTime_Min { get; set; } public int s_nFatigue { get; set; } public byte s_Type { get; set; } public byte s_nLevel { get; set; } } public sealed class WeatherEntry { public uint s_dwMapID { get; set; } public int s_nCenterX { get; set; } public int s_nCenterY { get; set; } public int s_nRadius { get; set; } public int s_nElementCount { get; set; } public uint s_nTotalLoopMin { get; set; } public WeatherElement[] Elements { get; set; } = Array.Empty(); } public sealed class WeatherGroup { public int GroupIndex { get; set; } public WeatherEntry[] Weathers { get; set; } = Array.Empty(); } public static WeatherGroup[] ReadFromBinary(string inputPath) { using BinaryReader reader = new(File.Open(inputPath, FileMode.Open, FileAccess.Read, FileShare.Read)); int groupCount = reader.ReadInt32(); if (groupCount < 0) { throw new InvalidDataException("Weather group count cannot be negative."); } WeatherGroup[] groups = new WeatherGroup[groupCount]; for (int g = 0; g < groupCount; g++) { int weatherCount = reader.ReadInt32(); if (weatherCount < 0) { throw new InvalidDataException($"Weather group {g} has a negative weather count."); } WeatherEntry[] weathers = new WeatherEntry[weatherCount]; for (int w = 0; w < weatherCount; w++) { WeatherEntry entry = new() { s_dwMapID = reader.ReadUInt32(), s_nCenterX = reader.ReadInt32(), s_nCenterY = reader.ReadInt32(), s_nRadius = reader.ReadInt32(), s_nElementCount = reader.ReadInt32(), s_nTotalLoopMin = reader.ReadUInt32() }; if (entry.s_nElementCount < 0) { throw new InvalidDataException($"Weather group {g}, weather {w} has a negative element count."); } WeatherElement[] elements = new WeatherElement[entry.s_nElementCount]; for (int e = 0; e < entry.s_nElementCount; e++) { elements[e] = new WeatherElement { s_nTime_Min = reader.ReadInt32(), s_nFatigue = reader.ReadInt32(), s_Type = reader.ReadByte(), s_nLevel = reader.ReadByte() }; reader.ReadUInt16(); } entry.Elements = elements; weathers[w] = entry; } groups[g] = new WeatherGroup { GroupIndex = g, Weathers = weathers }; } if (reader.BaseStream.Position != reader.BaseStream.Length) { throw new InvalidDataException($"Weather.bin was not fully consumed. Remaining bytes: {reader.BaseStream.Length - reader.BaseStream.Position}."); } return groups; } public static void ExportToXml(string outputPath, WeatherGroup[] groups) { Directory.CreateDirectory(Path.GetDirectoryName(outputPath)!); XElement root = new("WeatherTable", groups.Select(group => new XElement("WeatherGroup", new XElement(nameof(WeatherGroup.GroupIndex), group.GroupIndex.ToString(CultureInfo.InvariantCulture)), group.Weathers.Select(weather => new XElement("Weather", new XElement(nameof(WeatherEntry.s_dwMapID), weather.s_dwMapID.ToString(CultureInfo.InvariantCulture)), new XElement(nameof(WeatherEntry.s_nCenterX), weather.s_nCenterX.ToString(CultureInfo.InvariantCulture)), new XElement(nameof(WeatherEntry.s_nCenterY), weather.s_nCenterY.ToString(CultureInfo.InvariantCulture)), new XElement(nameof(WeatherEntry.s_nRadius), weather.s_nRadius.ToString(CultureInfo.InvariantCulture)), new XElement(nameof(WeatherEntry.s_nElementCount), weather.Elements.Length.ToString(CultureInfo.InvariantCulture)), new XElement(nameof(WeatherEntry.s_nTotalLoopMin), weather.s_nTotalLoopMin.ToString(CultureInfo.InvariantCulture)), new XElement("Elements", weather.Elements.Select(element => new XElement("Element", new XElement(nameof(WeatherElement.s_Type), element.s_Type.ToString(CultureInfo.InvariantCulture)), new XElement(nameof(WeatherElement.s_nLevel), element.s_nLevel.ToString(CultureInfo.InvariantCulture)), new XElement(nameof(WeatherElement.s_nTime_Min), element.s_nTime_Min.ToString(CultureInfo.InvariantCulture)), new XElement(nameof(WeatherElement.s_nFatigue), element.s_nFatigue.ToString(CultureInfo.InvariantCulture)))))))))); SaveXml(outputPath, root); } public static WeatherGroup[] ImportFromXml(string inputPath) { XElement root = XElement.Load(inputPath); return root.Elements("WeatherGroup") .Select((groupElement, groupIndex) => new WeatherGroup { GroupIndex = ReadOptionalInt(groupElement, nameof(WeatherGroup.GroupIndex), groupIndex), Weathers = groupElement.Elements("Weather") .Select(weatherElement => { WeatherElement[] elements = weatherElement.Element("Elements")?.Elements("Element") .Select(element => new WeatherElement { s_Type = ReadByte(element, nameof(WeatherElement.s_Type)), s_nLevel = ReadByte(element, nameof(WeatherElement.s_nLevel)), s_nTime_Min = ReadInt(element, nameof(WeatherElement.s_nTime_Min)), s_nFatigue = ReadInt(element, nameof(WeatherElement.s_nFatigue)) }) .ToArray() ?? Array.Empty(); return new WeatherEntry { s_dwMapID = ReadUInt(weatherElement, nameof(WeatherEntry.s_dwMapID)), s_nCenterX = ReadInt(weatherElement, nameof(WeatherEntry.s_nCenterX)), s_nCenterY = ReadInt(weatherElement, nameof(WeatherEntry.s_nCenterY)), s_nRadius = ReadInt(weatherElement, nameof(WeatherEntry.s_nRadius)), s_nElementCount = elements.Length, s_nTotalLoopMin = ReadUInt(weatherElement, nameof(WeatherEntry.s_nTotalLoopMin)), Elements = elements }; }) .ToArray() }) .OrderBy(group => group.GroupIndex) .ToArray(); } public static void WriteToBinary(string outputPath, WeatherGroup[] groups) { Directory.CreateDirectory(Path.GetDirectoryName(outputPath)!); WeatherGroup[] orderedGroups = groups.OrderBy(group => group.GroupIndex).ToArray(); using BinaryWriter writer = new(File.Open(outputPath, FileMode.Create, FileAccess.Write, FileShare.None)); writer.Write(orderedGroups.Length); foreach (WeatherGroup group in orderedGroups) { writer.Write(group.Weathers.Length); foreach (WeatherEntry weather in group.Weathers) { writer.Write(weather.s_dwMapID); writer.Write(weather.s_nCenterX); writer.Write(weather.s_nCenterY); writer.Write(weather.s_nRadius); writer.Write(weather.Elements.Length); writer.Write(weather.s_nTotalLoopMin); foreach (WeatherElement element in weather.Elements) { writer.Write(element.s_nTime_Min); writer.Write(element.s_nFatigue); writer.Write(element.s_Type); writer.Write(element.s_nLevel); writer.Write((ushort)0); } } } } private static void SaveXml(string path, XElement root) { XmlWriterSettings settings = new() { Indent = true, Encoding = new System.Text.UTF8Encoding(false) }; using XmlWriter writer = XmlWriter.Create(path, settings); root.Save(writer); } private static int ReadOptionalInt(XElement parent, string name, int fallback) { XElement? element = parent.Element(name); if (element == null || string.IsNullOrWhiteSpace(element.Value)) { return fallback; } return int.Parse(element.Value, CultureInfo.InvariantCulture); } private static int ReadInt(XElement parent, string name) { XElement element = parent.Element(name) ?? throw new InvalidOperationException($"Missing '{name}' element."); return int.Parse(element.Value, CultureInfo.InvariantCulture); } private static uint ReadUInt(XElement parent, string name) { XElement element = parent.Element(name) ?? throw new InvalidOperationException($"Missing '{name}' element."); return uint.Parse(element.Value, CultureInfo.InvariantCulture); } private static byte ReadByte(XElement parent, string name) { XElement element = parent.Element(name) ?? throw new InvalidOperationException($"Missing '{name}' element."); return byte.Parse(element.Value, CultureInfo.InvariantCulture); } }