using System.Globalization; using System.Xml.Linq; namespace XmlTool.Importers { public sealed class NewElementProfileSummary { public int AttributeTypeUiCount { get; init; } public int NatureTypeUiCount { get; init; } public int DuplicateAttributeIdCount { get; init; } public int DuplicateNatureIdCount { get; init; } public string AttributeIds { get; init; } = string.Empty; public string NatureIds { get; init; } = string.Empty; } public static class NewElementProfileInspector { public static NewElementProfileSummary ValidatePackage(string attributeTypeUiXmlPath) { string packageDirectory = Path.GetDirectoryName(attributeTypeUiXmlPath) ?? throw new InvalidOperationException("New_Element package path does not have a directory."); List attributeIds = LoadAttributeTypeUiIds(attributeTypeUiXmlPath); List natureIds = LoadNatureTypeUiIds(Path.Combine(packageDirectory, "NatureTypeUI.xml")); return new NewElementProfileSummary { AttributeTypeUiCount = attributeIds.Count, NatureTypeUiCount = natureIds.Count, DuplicateAttributeIdCount = CountDuplicates(attributeIds), DuplicateNatureIdCount = CountDuplicates(natureIds), AttributeIds = string.Join(", ", attributeIds.OrderBy(x => x)), NatureIds = string.Join(", ", natureIds.OrderBy(x => x)) }; } private static List LoadAttributeTypeUiIds(string xmlPath) { XElement root = LoadRequiredRoot(xmlPath, "AttributeTypeUITable"); List ids = new(); int rowIndex = 0; foreach (XElement rowElement in root.Elements("AttributeTypeUI")) { rowIndex++; string rowContext = $"AttributeTypeUI row {rowIndex}"; ids.Add(ReadRequiredInt(rowElement, "AttributeId", rowContext)); _ = ReadRequiredString(rowElement, "Path", rowContext); _ = ReadRequiredInt(rowElement, "X", rowContext); _ = ReadRequiredInt(rowElement, "Y", rowContext); _ = ReadRequiredInt(rowElement, "BarX", rowContext); _ = ReadRequiredInt(rowElement, "BarY", rowContext); } if (ids.Count != 5) { throw new InvalidOperationException($"AttributeTypeUI.xml must contain exactly 5 rows, but contains {ids.Count}."); } return ids; } private static List LoadNatureTypeUiIds(string xmlPath) { XElement root = LoadRequiredRoot(xmlPath, "NatureTypeUITable"); List ids = new(); int rowIndex = 0; foreach (XElement rowElement in root.Elements("NatureTypeUI")) { rowIndex++; string rowContext = $"NatureTypeUI row {rowIndex}"; ids.Add(ReadRequiredInt(rowElement, "NatureId", rowContext)); _ = ReadRequiredString(rowElement, "Path", rowContext); _ = ReadRequiredInt(rowElement, "X", rowContext); _ = ReadRequiredInt(rowElement, "Y", rowContext); _ = ReadRequiredInt(rowElement, "GaugeX", rowContext); _ = ReadRequiredInt(rowElement, "GaugeY", rowContext); _ = ReadRequiredInt(rowElement, "Angle", rowContext); } if (ids.Count != 11) { throw new InvalidOperationException($"NatureTypeUI.xml must contain exactly 11 rows, but contains {ids.Count}."); } return ids; } 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}'."); } string value = element.Value; if (value.Length == 0) { throw new InvalidOperationException($"{recordContext} has empty required value in '{elementName}'."); } return value; } private static int CountDuplicates(IEnumerable values) { return values.GroupBy(x => x).Where(x => x.Count() > 1).Sum(x => x.Count() - 1); } } }