using System.Globalization; using System.Xml.Linq; namespace XmlTool.Importers { public sealed class PassiveAbilityProfileSummary { public int PassiveTypeCount { get; init; } public int EntryCount { get; init; } public int DuplicateTypeCount { get; init; } public string TypeIds { get; init; } = string.Empty; } public static class PassiveAbilityProfileInspector { public static PassiveAbilityProfileSummary ValidatePackage(string passiveAbilityXmlPath) { if (!File.Exists(passiveAbilityXmlPath)) { throw new FileNotFoundException("PassiveAbility.xml not found.", passiveAbilityXmlPath); } XDocument document = XDocument.Load(passiveAbilityXmlPath); XElement? root = document.Root; if (root == null || root.Name.LocalName != "PassiveAbilityTable") { throw new InvalidOperationException("PassiveAbility.xml root node must be ."); } List typeIds = new(); int entryCount = 0; int rowNumber = 0; foreach (XElement passiveTypeElement in root.Elements("PassiveType")) { rowNumber++; int type = ReadRequiredInt(passiveTypeElement, "Type", $"PassiveType row {rowNumber}"); if (type <= 0) { throw new InvalidOperationException($"PassiveType row {rowNumber} has invalid Type value: '{type}'."); } XElement? entriesElement = passiveTypeElement.Element("Entries"); if (entriesElement == null) { throw new InvalidOperationException($"PassiveType row {rowNumber} is missing the node."); } entryCount += entriesElement.Elements().Count(); typeIds.Add(type); } return new PassiveAbilityProfileSummary { PassiveTypeCount = typeIds.Count, EntryCount = entryCount, DuplicateTypeCount = typeIds.Count - typeIds.Distinct().Count(), TypeIds = string.Join(", ", typeIds.OrderBy(x => x)) }; } 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; } } }