using System.Globalization; using System.Xml.Linq; namespace XmlTool.Importers { public sealed class WeatherProfileSummary { public int WeatherGroupCount { get; init; } public int WeatherCount { get; init; } public int WeatherElementCount { get; init; } public int DistinctMapCount { get; init; } public int DuplicateGroupIndexCount { get; init; } public int EmptyGroupCount { get; init; } public int EmptyWeatherElementCount { get; init; } public int ElementCountMismatchCount { get; init; } public int TotalLoopMismatchCount { get; init; } public string MapIdRange { get; init; } = string.Empty; public string RadiusRange { get; init; } = string.Empty; public string TotalLoopRange { get; init; } = string.Empty; public string WeatherTypes { get; init; } = string.Empty; public string WeatherLevelRange { get; init; } = string.Empty; public string ElementTimeRange { get; init; } = string.Empty; public string FatigueRange { get; init; } = string.Empty; } public static class WeatherProfileInspector { public static WeatherProfileSummary ValidateXml(string weatherXmlPath) { XElement root = LoadRequiredRoot(weatherXmlPath, "WeatherTable"); List groups = new(); List weathers = new(); List elements = new(); int groupIndex = 0; int elementCountMismatchCount = 0; int totalLoopMismatchCount = 0; foreach (XElement groupElement in root.Elements("WeatherGroup")) { string groupContext = $"WeatherGroup row {groupIndex + 1}"; int currentGroupIndex = ReadRequiredInt(groupElement, "GroupIndex", groupContext); List weatherElements = groupElement.Elements("Weather").ToList(); groups.Add(new WeatherGroupRow { GroupIndex = currentGroupIndex, WeatherCount = weatherElements.Count }); int weatherIndex = 0; foreach (XElement weatherElement in weatherElements) { weatherIndex++; string weatherContext = $"{groupContext} weather {weatherIndex}"; XElement elementsElement = weatherElement.Element("Elements") ?? throw new InvalidOperationException($"{weatherContext} is missing required node."); List elementNodes = elementsElement.Elements("Element").ToList(); int declaredElementCount = ReadRequiredInt(weatherElement, "s_nElementCount", weatherContext); uint totalLoop = ReadRequiredUInt(weatherElement, "s_nTotalLoopMin", weatherContext); if (declaredElementCount != elementNodes.Count) { elementCountMismatchCount++; } WeatherRow weather = new() { MapId = ReadRequiredUInt(weatherElement, "s_dwMapID", weatherContext), Radius = ReadRequiredInt(weatherElement, "s_nRadius", weatherContext), TotalLoopMin = totalLoop, ElementCount = elementNodes.Count }; _ = ReadRequiredInt(weatherElement, "s_nCenterX", weatherContext); _ = ReadRequiredInt(weatherElement, "s_nCenterY", weatherContext); weathers.Add(weather); int elementIndex = 0; int elementTimeSum = 0; foreach (XElement elementNode in elementNodes) { elementIndex++; string elementContext = $"{weatherContext} element {elementIndex}"; int timeMin = ReadRequiredInt(elementNode, "s_nTime_Min", elementContext); elementTimeSum += timeMin; elements.Add(new WeatherElementRow { Type = ReadRequiredInt(elementNode, "s_Type", elementContext), Level = ReadRequiredInt(elementNode, "s_nLevel", elementContext), TimeMin = timeMin, Fatigue = ReadRequiredInt(elementNode, "s_nFatigue", elementContext) }); } if (elementNodes.Count > 0 && elementTimeSum != totalLoop) { totalLoopMismatchCount++; } } groupIndex++; } if (groups.Count == 0) { throw new InvalidOperationException("Weather.xml does not contain any rows."); } return new WeatherProfileSummary { WeatherGroupCount = groups.Count, WeatherCount = weathers.Count, WeatherElementCount = elements.Count, DistinctMapCount = weathers.Select(x => x.MapId).Distinct().Count(), DuplicateGroupIndexCount = CountDuplicates(groups.Select(x => x.GroupIndex)), EmptyGroupCount = groups.Count(x => x.WeatherCount == 0), EmptyWeatherElementCount = weathers.Count(x => x.ElementCount == 0), ElementCountMismatchCount = elementCountMismatchCount, TotalLoopMismatchCount = totalLoopMismatchCount, MapIdRange = FormatRange(weathers.Select(x => x.MapId)), RadiusRange = FormatRange(weathers.Select(x => x.Radius)), TotalLoopRange = FormatRange(weathers.Select(x => x.TotalLoopMin)), WeatherTypes = FormatDistinct(elements.Select(x => x.Type)), WeatherLevelRange = FormatRange(elements.Select(x => x.Level)), ElementTimeRange = FormatRange(elements.Select(x => x.TimeMin)), FatigueRange = FormatRange(elements.Select(x => x.Fatigue)) }; } private static XElement LoadRequiredRoot(string xmlPath, string expectedRootName) { if (!File.Exists(xmlPath)) { throw new FileNotFoundException($"{Path.GetFileName(xmlPath)} not found.", xmlPath); } XDocument document = XDocument.Load(xmlPath); XElement root = document.Root ?? throw new InvalidOperationException($"{Path.GetFileName(xmlPath)} is missing a root node."); if (root.Name.LocalName != expectedRootName) { throw new InvalidOperationException($"{Path.GetFileName(xmlPath)} root node must be <{expectedRootName}>."); } return root; } private static int ReadRequiredInt(XElement parent, string elementName, string recordContext) { XElement? element = parent.Element(elementName); if (element == null) { throw new InvalidOperationException($"{recordContext} is missing required field '{elementName}'."); } if (!int.TryParse(element.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int value)) { throw new InvalidOperationException($"{recordContext} has invalid integer value in '{elementName}': '{element.Value}'."); } return value; } private static uint ReadRequiredUInt(XElement parent, string elementName, string recordContext) { XElement? element = parent.Element(elementName); if (element == null) { throw new InvalidOperationException($"{recordContext} is missing required field '{elementName}'."); } if (!uint.TryParse(element.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint value)) { throw new InvalidOperationException($"{recordContext} has invalid unsigned integer value in '{elementName}': '{element.Value}'."); } return value; } private static int CountDuplicates(IEnumerable values) { return values.GroupBy(x => x).Where(x => x.Count() > 1).Sum(x => x.Count() - 1); } private static string FormatRange(IEnumerable values) { int[] valueArray = values.ToArray(); return valueArray.Length == 0 ? "n/a" : $"{valueArray.Min()} to {valueArray.Max()}"; } private static string FormatRange(IEnumerable values) { uint[] valueArray = values.ToArray(); return valueArray.Length == 0 ? "n/a" : $"{valueArray.Min()} to {valueArray.Max()}"; } private static string FormatDistinct(IEnumerable values) { int[] distinctValues = values.Distinct().OrderBy(x => x).ToArray(); return distinctValues.Length == 0 ? "n/a" : string.Join(", ", distinctValues); } private sealed class WeatherGroupRow { public int GroupIndex { get; init; } public int WeatherCount { get; init; } } private sealed class WeatherRow { public uint MapId { get; init; } public int Radius { get; init; } public uint TotalLoopMin { get; init; } public int ElementCount { get; init; } } private sealed class WeatherElementRow { public int Type { get; init; } public int Level { get; init; } public int TimeMin { get; init; } public int Fatigue { get; init; } } } }