using System.Globalization; using System.Xml.Linq; namespace XmlTool.Importers { public sealed class WorldMapProfileSummary { public int WorldCount { get; init; } public int AreaCount { get; init; } public int DuplicateWorldIdCount { get; init; } public int DuplicateAreaMapIdCount { get; init; } public int LongStringCount { get; init; } public int InvalidGaussianBlurCount { get; init; } public string WorldIdRange { get; init; } = string.Empty; public string AreaMapIdRange { get; init; } = string.Empty; public string WorldTypes { get; init; } = string.Empty; public string AreaTypes { get; init; } = string.Empty; public string FieldTypes { get; init; } = string.Empty; public string WorldUiXRange { get; init; } = string.Empty; public string WorldUiYRange { get; init; } = string.Empty; public string AreaUiXRange { get; init; } = string.Empty; public string AreaUiYRange { get; init; } = string.Empty; } public static class WorldMapProfileInspector { private const int NameLength = 48; private const int CommentLength = 256; public static WorldMapProfileSummary ValidatePackage(string worldMapXmlPath) { string packageDirectory = Path.GetDirectoryName(worldMapXmlPath) ?? throw new InvalidOperationException("WorldMap package path does not have a directory."); List worlds = LoadWorldRows(worldMapXmlPath, out int longWorldStringCount); List areas = LoadAreaRows(RequiredPath(packageDirectory, "AreaMap.xml"), out int longAreaStringCount, out int invalidGaussianBlurCount); return new WorldMapProfileSummary { WorldCount = worlds.Count, AreaCount = areas.Count, DuplicateWorldIdCount = CountDuplicates(worlds.Select(x => x.Id)), DuplicateAreaMapIdCount = CountDuplicates(areas.Select(x => x.MapId)), LongStringCount = longWorldStringCount + longAreaStringCount, InvalidGaussianBlurCount = invalidGaussianBlurCount, WorldIdRange = FormatRange(worlds.Select(x => x.Id)), AreaMapIdRange = FormatRange(areas.Select(x => x.MapId)), WorldTypes = FormatDistinct(worlds.Select(x => x.WorldType)), AreaTypes = FormatDistinct(areas.Select(x => x.AreaType)), FieldTypes = FormatDistinct(areas.Select(x => x.FieldType)), WorldUiXRange = FormatRange(worlds.Select(x => x.UiX)), WorldUiYRange = FormatRange(worlds.Select(x => x.UiY)), AreaUiXRange = FormatRange(areas.Select(x => x.UiX)), AreaUiYRange = FormatRange(areas.Select(x => x.UiY)) }; } private static List LoadWorldRows(string xmlPath, out int longStringCount) { XElement root = LoadRequiredRoot(xmlPath, "WorldMapTable"); List rows = new(); longStringCount = 0; int rowIndex = 0; foreach (XElement worldElement in root.Elements("WorldMap")) { rowIndex++; string rowContext = $"WorldMap row {rowIndex}"; string name = ReadRequiredString(worldElement, "s_szName", rowContext); string comment = ReadRequiredString(worldElement, "s_szComment", rowContext); longStringCount += CountTooLong(name, NameLength) + CountTooLong(comment, CommentLength); rows.Add(new WorldRow { Id = ReadRequiredInt(worldElement, "s_nID", rowContext), WorldType = ReadRequiredInt(worldElement, "s_nWorldType", rowContext), UiX = ReadRequiredInt(worldElement, "s_nUI_X", rowContext), UiY = ReadRequiredInt(worldElement, "s_nUI_Y", rowContext) }); } if (rows.Count == 0) { throw new InvalidOperationException("WorldMap.xml does not contain any rows."); } return rows; } private static List LoadAreaRows(string xmlPath, out int longStringCount, out int invalidGaussianBlurCount) { XElement root = LoadRequiredRoot(xmlPath, "AreaMapTable"); List rows = new(); longStringCount = 0; invalidGaussianBlurCount = 0; int rowIndex = 0; foreach (XElement areaElement in root.Elements("AreaMap")) { rowIndex++; string rowContext = $"AreaMap row {rowIndex}"; string name = ReadRequiredString(areaElement, "s_szName", rowContext); string comment = ReadRequiredString(areaElement, "s_szComment", rowContext); longStringCount += CountTooLong(name, NameLength) + CountTooLong(comment, CommentLength); XElement gaussianBlurElement = areaElement.Element("s_fGaussianBlur") ?? throw new InvalidOperationException($"{rowContext} is missing required node."); float[] blurValues = gaussianBlurElement.Elements("Value") .Select((valueElement, valueIndex) => ReadRequiredFloat(valueElement, $"{rowContext} gaussian blur value {valueIndex + 1}")) .ToArray(); if (blurValues.Length != 3) { invalidGaussianBlurCount++; } rows.Add(new AreaRow { MapId = ReadRequiredInt(areaElement, "s_nMapID", rowContext), AreaType = ReadRequiredInt(areaElement, "s_nAreaType", rowContext), FieldType = ReadRequiredInt(areaElement, "s_nFieldType", rowContext), UiX = ReadRequiredInt(areaElement, "s_nUI_X", rowContext), UiY = ReadRequiredInt(areaElement, "s_nUI_Y", rowContext) }); _ = ReadRequiredInt(areaElement, "s_nFTDetail", rowContext); } if (rows.Count == 0) { throw new InvalidOperationException("AreaMap.xml does not contain any rows."); } return rows; } private static string RequiredPath(string packageDirectory, string fileName) { string path = Path.Combine(packageDirectory, fileName); if (!File.Exists(path)) { throw new FileNotFoundException($"{fileName} not found.", path); } return path; } 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 string ReadRequiredString(XElement parent, string elementName, string recordContext) { XElement? element = parent.Element(elementName); if (element == null) { throw new InvalidOperationException($"{recordContext} is missing required field '{elementName}'."); } return element.Value; } private static float ReadRequiredFloat(XElement element, string recordContext) { if (!float.TryParse(element.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out float value)) { throw new InvalidOperationException($"{recordContext} has invalid float value: '{element.Value}'."); } return value; } private static int CountTooLong(string value, int fixedCharLength) { return value.Length > fixedCharLength - 1 ? 1 : 0; } 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 FormatDistinct(IEnumerable values) { int[] distinctValues = values.Distinct().OrderBy(x => x).ToArray(); return distinctValues.Length == 0 ? "n/a" : string.Join(", ", distinctValues); } private sealed class WorldRow { public int Id { get; init; } public int WorldType { get; init; } public int UiX { get; init; } public int UiY { get; init; } } private sealed class AreaRow { public int MapId { get; init; } public int AreaType { get; init; } public int FieldType { get; init; } public int UiX { get; init; } public int UiY { get; init; } } } }