using System.Globalization; using System.Xml.Linq; namespace XmlTool.Importers { public sealed class NatureProfileSummary { public int NatureCount { get; init; } public int NatureElementCount { get; init; } public int DuplicateNatureIdCount { get; init; } public int CompareTypeCount { get; init; } public int BaseTypeCount { get; init; } public int PointCount { get; init; } public string NatureIds { get; init; } = string.Empty; public string CompareTypes { get; init; } = string.Empty; public string NatureValueRange { get; init; } = string.Empty; public string BaseElementValueRange { get; init; } = string.Empty; } public static class NatureProfileInspector { public static NatureProfileSummary ValidatePackage(string natureXmlPath) { string packageDirectory = Path.GetDirectoryName(natureXmlPath) ?? throw new InvalidOperationException("Nature package path does not have a directory."); List natureRows = LoadNatureRows(natureXmlPath); List pointRows = LoadBaseElementRows(Path.Combine(packageDirectory, "BaseElement.xml"), out int compareTypeCount, out int baseTypeCount); return new NatureProfileSummary { NatureCount = natureRows.Count, NatureElementCount = natureRows.Sum(x => x.Elements.Count), DuplicateNatureIdCount = natureRows.GroupBy(x => x.Id).Where(x => x.Count() > 1).Sum(x => x.Count() - 1), CompareTypeCount = compareTypeCount, BaseTypeCount = baseTypeCount, PointCount = pointRows.Count, NatureIds = string.Join(", ", natureRows.Select(x => x.Id).OrderBy(x => x)), CompareTypes = string.Join(", ", pointRows.Select(x => x.CompareType).Distinct().OrderBy(x => x)), NatureValueRange = FormatRange(natureRows.SelectMany(x => x.Elements).Select(x => x.Value)), BaseElementValueRange = FormatRange(pointRows.Select(x => x.Value)) }; } private static List LoadNatureRows(string xmlPath) { XElement root = LoadRequiredRoot(xmlPath, "NatureTable"); List rows = new(); int rowIndex = 0; foreach (XElement natureElement in root.Elements("Nature")) { rowIndex++; string rowContext = $"Nature row {rowIndex}"; int id = ReadRequiredInt(natureElement, "Id", rowContext); XElement elementsNode = natureElement.Element("Elements") ?? throw new InvalidOperationException($"{rowContext} is missing required node."); List elements = new(); int elementIndex = 0; foreach (XElement elementNode in elementsNode.Elements("Element")) { elementIndex++; string elementContext = $"{rowContext} element {elementIndex}"; elements.Add(new NatureElementRow { Index = ReadRequiredInt(elementNode, "Index", elementContext), Value = ReadRequiredInt(elementNode, "Value", elementContext) }); } if (elements.Count != 11) { throw new InvalidOperationException($"{rowContext} must contain exactly 11 rows, but contains {elements.Count}."); } string expectedIndexes = string.Join(", ", Enumerable.Range(0, 11)); string actualIndexes = string.Join(", ", elements.Select(x => x.Index).OrderBy(x => x)); if (!elements.Select(x => x.Index).OrderBy(x => x).SequenceEqual(Enumerable.Range(0, 11))) { throw new InvalidOperationException($"{rowContext} has invalid element indexes. Expected [{expectedIndexes}], got [{actualIndexes}]."); } rows.Add(new NatureRow { Id = id, Elements = elements }); } if (rows.Count == 0) { throw new InvalidOperationException("Nature.xml does not contain any rows."); } return rows; } private static List LoadBaseElementRows(string xmlPath, out int compareTypeCount, out int baseTypeCount) { XElement root = LoadRequiredRoot(xmlPath, "BaseElementTable"); List pointRows = new(); compareTypeCount = 0; baseTypeCount = 0; foreach (XElement compareTypeElement in root.Elements("CompareType")) { compareTypeCount++; string compareContext = $"BaseElement CompareType {compareTypeCount}"; int compareType = ReadRequiredInt(compareTypeElement, "Type", compareContext); XElement baseTypesNode = compareTypeElement.Element("BaseTypes") ?? throw new InvalidOperationException($"{compareContext} is missing required node."); List baseTypeElements = baseTypesNode.Elements("BaseType").ToList(); if (baseTypeElements.Count != 5) { throw new InvalidOperationException($"{compareContext} must contain exactly 5 rows, but contains {baseTypeElements.Count}."); } int baseTypeIndex = 0; foreach (XElement baseTypeElement in baseTypeElements) { baseTypeIndex++; baseTypeCount++; string baseTypeContext = $"{compareContext} base type {baseTypeIndex}"; int baseType = ReadRequiredInt(baseTypeElement, "Type", baseTypeContext); XElement pointsNode = baseTypeElement.Element("Points") ?? throw new InvalidOperationException($"{baseTypeContext} is missing required node."); List pointElements = pointsNode.Elements("Point").ToList(); if (pointElements.Count != 5) { throw new InvalidOperationException($"{baseTypeContext} must contain exactly 5 rows, but contains {pointElements.Count}."); } int pointIndex = 0; foreach (XElement pointElement in pointElements) { pointIndex++; string pointContext = $"{baseTypeContext} point {pointIndex}"; pointRows.Add(new BaseElementPointRow { CompareType = compareType, BaseType = baseType, TargetType = ReadRequiredInt(pointElement, "TargetType", pointContext), Value = ReadRequiredInt(pointElement, "Value", pointContext) }); } } } if (compareTypeCount == 0) { throw new InvalidOperationException("BaseElement.xml does not contain any rows."); } return pointRows; } 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 FormatRange(IEnumerable values) { int[] valueArray = values.ToArray(); return valueArray.Length == 0 ? "n/a" : $"{valueArray.Min()} to {valueArray.Max()}"; } private sealed class NatureRow { public int Id { get; init; } public List Elements { get; init; } = new(); } private sealed class NatureElementRow { public int Index { get; init; } public int Value { get; init; } } private sealed class BaseElementPointRow { public int CompareType { get; init; } public int BaseType { get; init; } public int TargetType { get; init; } public int Value { get; init; } } } }