using System.Globalization; using System.Xml.Linq; namespace XmlTool.Importers { public sealed class MasterCardProfileSummary { public int AttributeCount { get; init; } public int DigimonImagePathCount { get; init; } public int ElementalCount { get; init; } public int LeaderAbilityCount { get; init; } public int LeaderAbilityTermCount { get; init; } public int LeaderCount { get; init; } public int MasterCardCount { get; init; } public int MasterCardGradeCount { get; init; } public int MasterFilterCount { get; init; } public int PlatePathCount { get; init; } public int DuplicateCardIdCount { get; init; } public int DuplicateLeaderIdCount { get; init; } public int DistinctSealItemCount { get; init; } } public static class MasterCardProfileInspector { public static MasterCardProfileSummary ValidatePackage(string masterCardsXmlPath) { string packageDirectory = Path.GetDirectoryName(masterCardsXmlPath) ?? throw new InvalidOperationException("MasterCard package path does not have a directory."); List cardIds = ValidateMasterCards(masterCardsXmlPath, out int gradeCount); List leaderIds = ValidateLeaders(RequiredPath(packageDirectory, "Leaders.xml")); int leaderAbilityTermCount = ValidateLeaderAbilities(RequiredPath(packageDirectory, "LeaderAbilities.xml"), out int leaderAbilityCount); List sealItemIds = ValidateMasterFilters(RequiredPath(packageDirectory, "MasterFilters.xml")); return new MasterCardProfileSummary { AttributeCount = ValidateTypedPathTable(RequiredPath(packageDirectory, "Attributes.xml"), "Attributes", "Attribute"), DigimonImagePathCount = ValidateDigimonImagePaths(RequiredPath(packageDirectory, "DigimonImgPaths.xml")), ElementalCount = ValidateTypedPathTable(RequiredPath(packageDirectory, "Elementals.xml"), "Elementals", "Elemental"), LeaderAbilityCount = leaderAbilityCount, LeaderAbilityTermCount = leaderAbilityTermCount, LeaderCount = leaderIds.Count, MasterCardCount = cardIds.Count, MasterCardGradeCount = gradeCount, MasterFilterCount = sealItemIds.Count, PlatePathCount = ValidatePlatePaths(RequiredPath(packageDirectory, "PlatePaths.xml")), DuplicateCardIdCount = CountDuplicates(cardIds), DuplicateLeaderIdCount = CountDuplicates(leaderIds), DistinctSealItemCount = sealItemIds.Distinct().Count() }; } private static List ValidateMasterCards(string xmlPath, out int gradeCount) { XElement root = LoadRequiredRoot(xmlPath, "MasterCards"); List cardIds = new(); gradeCount = 0; int rowIndex = 0; foreach (XElement cardElement in root.Elements("MasterCard")) { rowIndex++; string rowContext = $"MasterCards row {rowIndex}"; int id = ReadRequiredInt(cardElement, "Id", rowContext); _ = ReadRequiredString(cardElement, "Name", rowContext); _ = ReadRequiredInt(cardElement, "DigimonId", rowContext); _ = ReadRequiredInt(cardElement, "Icon", rowContext); _ = ReadRequiredInt(cardElement, "Leader", rowContext); _ = ReadRequiredInt(cardElement, "Scale", rowContext); XElement gradeInfosElement = cardElement.Element("GradeInfos") ?? throw new InvalidOperationException($"{rowContext} is missing required node."); List gradeInfoElements = gradeInfosElement.Elements("GradeInfo").ToList(); if (gradeInfoElements.Count != 6) { throw new InvalidOperationException($"{rowContext} must contain exactly 6 rows, but contains {gradeInfoElements.Count}."); } int gradeIndex = 0; foreach (XElement gradeElement in gradeInfoElements) { gradeIndex++; string gradeContext = $"{rowContext} grade {gradeIndex}"; _ = ReadRequiredInt(gradeElement, "ItemId", gradeContext); _ = ReadRequiredInt(gradeElement, "Icon", gradeContext); _ = ReadRequiredInt(gradeElement, "Max", gradeContext); _ = ReadRequiredInt(gradeElement, "IdentityQuantity", gradeContext); _ = ReadRequiredInt(gradeElement, "Effect1", gradeContext); _ = ReadRequiredInt(gradeElement, "Effect1Value", gradeContext); _ = ReadRequiredInt(gradeElement, "Effect2", gradeContext); _ = ReadRequiredInt(gradeElement, "Effect2Value", gradeContext); } cardIds.Add(id); gradeCount += gradeInfoElements.Count; } if (cardIds.Count == 0) { throw new InvalidOperationException("MasterCards.xml does not contain any rows."); } return cardIds; } private static List ValidateLeaders(string xmlPath) { XElement root = LoadRequiredRoot(xmlPath, "Leaders"); List leaderIds = new(); int rowIndex = 0; foreach (XElement leaderElement in root.Elements("Leader")) { rowIndex++; string rowContext = $"Leaders row {rowIndex}"; int id = ReadRequiredInt(leaderElement, "Id", rowContext); _ = ReadRequiredInt(leaderElement, "DigimonId", rowContext); _ = ReadRequiredInt(leaderElement, "PetId", rowContext); _ = ReadRequiredInt(leaderElement, "Ani1", rowContext); _ = ReadRequiredInt(leaderElement, "Ani2", rowContext); _ = ReadRequiredInt(leaderElement, "Special1", rowContext); _ = ReadRequiredInt(leaderElement, "Special2", rowContext); _ = ReadRequiredInt(leaderElement, "Ability1", rowContext); _ = ReadRequiredInt(leaderElement, "Ability2", rowContext); _ = ReadRequiredInt(leaderElement, "Ability3", rowContext); _ = ReadRequiredInt(leaderElement, "Ability4", rowContext); leaderIds.Add(id); } if (leaderIds.Count == 0) { throw new InvalidOperationException("Leaders.xml does not contain any rows."); } return leaderIds; } private static int ValidateLeaderAbilities(string xmlPath, out int leaderAbilityCount) { XElement root = LoadRequiredRoot(xmlPath, "LeaderAbilities"); int termCount = 0; leaderAbilityCount = 0; foreach (XElement abilityElement in root.Elements("LeaderAbility")) { leaderAbilityCount++; string abilityContext = $"LeaderAbilities row {leaderAbilityCount}"; _ = ReadRequiredInt(abilityElement, "Id", abilityContext); XElement termsElement = abilityElement.Element("Terms") ?? throw new InvalidOperationException($"{abilityContext} is missing required node."); List termElements = termsElement.Elements("Term").ToList(); if (termElements.Count != 3) { throw new InvalidOperationException($"{abilityContext} must contain exactly 3 rows, but contains {termElements.Count}."); } int termIndex = 0; foreach (XElement termElement in termElements) { termIndex++; string termContext = $"{abilityContext} term {termIndex}"; _ = ReadRequiredInt(termElement, "TermId", termContext); _ = ReadRequiredInt(termElement, "TermValue", termContext); _ = ReadRequiredInt(termElement, "Effect", termContext); } termCount += termElements.Count; } if (leaderAbilityCount == 0) { throw new InvalidOperationException("LeaderAbilities.xml does not contain any rows."); } return termCount; } private static List ValidateMasterFilters(string xmlPath) { XElement root = LoadRequiredRoot(xmlPath, "MasterFilters"); List sealItemIds = new(); int rowIndex = 0; foreach (XElement filterElement in root.Elements("MasterFilter")) { rowIndex++; string rowContext = $"MasterFilters row {rowIndex}"; _ = ReadRequiredInt(filterElement, "MapGroupId", rowContext); _ = ReadRequiredInt(filterElement, "MapAreaId", rowContext); sealItemIds.Add(ReadRequiredInt(filterElement, "SealItemId", rowContext)); } if (sealItemIds.Count == 0) { throw new InvalidOperationException("MasterFilters.xml does not contain any rows."); } return sealItemIds; } private static int ValidateDigimonImagePaths(string xmlPath) { XElement root = LoadRequiredRoot(xmlPath, "DigimonImgPaths"); int rowIndex = 0; foreach (XElement pathElement in root.Elements("DigimonImgPath")) { rowIndex++; string rowContext = $"DigimonImgPaths row {rowIndex}"; _ = ReadRequiredInt(pathElement, "DigimonId", rowContext); _ = ReadRequiredString(pathElement, "ImagePath", rowContext); } if (rowIndex == 0) { throw new InvalidOperationException("DigimonImgPaths.xml does not contain any rows."); } return rowIndex; } private static int ValidatePlatePaths(string xmlPath) { XElement root = LoadRequiredRoot(xmlPath, "PlatePaths"); int rowIndex = 0; foreach (XElement pathElement in root.Elements("PlatePath")) { rowIndex++; string rowContext = $"PlatePaths row {rowIndex}"; _ = ReadRequiredInt(pathElement, "Id", rowContext); _ = ReadRequiredString(pathElement, "Name", rowContext); _ = ReadRequiredString(pathElement, "NifFilePath", rowContext); _ = ReadRequiredString(pathElement, "GradeBackImagePath", rowContext); } if (rowIndex == 0) { throw new InvalidOperationException("PlatePaths.xml does not contain any rows."); } return rowIndex; } private static int ValidateTypedPathTable(string xmlPath, string rootName, string rowName) { XElement root = LoadRequiredRoot(xmlPath, rootName); int rowIndex = 0; foreach (XElement rowElement in root.Elements(rowName)) { rowIndex++; string rowContext = $"{rootName} row {rowIndex}"; _ = ReadRequiredInt(rowElement, "Type", rowContext); _ = ReadRequiredString(rowElement, "FilePath", rowContext); } if (rowIndex == 0) { throw new InvalidOperationException($"{Path.GetFileName(xmlPath)} does not contain any <{rowName}> rows."); } return rowIndex; } 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 string RequiredPath(string packageDirectory, string fileName) { string path = Path.Combine(packageDirectory, fileName); if (!File.Exists(path)) { throw new FileNotFoundException($"{fileName} not found in MasterCard package.", path); } return path; } 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); } } }