using System.Globalization; using System.Xml.Linq; using Microsoft.Data.SqlClient; namespace XmlTool.Importers { public sealed class RideProfileSummary { public int RideCount { get; init; } public int DistinctDigimonCount { get; init; } public int DuplicateDigimonIdCount { get; init; } public int OpenInfoCount { get; init; } public int EmptyOpenInfoSlotCount { get; init; } public int InvalidOpenInfoSlotCount { get; init; } public int DistinctOpenItemCount { get; init; } public int MissingDigimonBaseInfoCount { get; init; } public int MissingItemInfoCount { get; init; } public string DigimonIdRange { get; init; } = string.Empty; public string ChangeRideValues { get; init; } = string.Empty; public string RideTypes { get; init; } = string.Empty; public string MoveSpeedRange { get; init; } = string.Empty; public string AnimationRateRange { get; init; } = string.Empty; public string NeedCountRange { get; init; } = string.Empty; } public static class RideProfileInspector { public static RideProfileSummary ValidateXml(string rideXmlPath) { RidePackage package = LoadPackage(rideXmlPath); return BuildSummary(package, missingDigimonBaseInfoCount: 0, missingItemInfoCount: 0); } public static RideProfileSummary AssessDatabase(string connectionString, string rideXmlPath) { RidePackage package = LoadPackage(rideXmlPath); using SqlConnection connection = new(connectionString); connection.Open(); HashSet digimonTypes = ReadUIntSet(connection, "SELECT [Type] FROM [Asset].[DigimonBaseInfo];"); HashSet itemIds = ReadUIntSet(connection, "SELECT [ItemId] FROM [Asset].[ItemInfo];"); int missingDigimonBaseInfoCount = package.Rides .Select(x => x.DigimonId) .Distinct() .Count(x => !digimonTypes.Contains(x)); int missingItemInfoCount = package.OpenInfos .Where(x => x.ItemType > 0) .Select(x => x.ItemType) .Distinct() .Count(x => !itemIds.Contains(x)); return BuildSummary(package, missingDigimonBaseInfoCount, missingItemInfoCount); } private static RideProfileSummary BuildSummary( RidePackage package, int missingDigimonBaseInfoCount, int missingItemInfoCount) { return new RideProfileSummary { RideCount = package.Rides.Count, DistinctDigimonCount = package.Rides.Select(x => x.DigimonId).Distinct().Count(), DuplicateDigimonIdCount = CountDuplicates(package.Rides.Select(x => x.DigimonId)), OpenInfoCount = package.OpenInfos.Count, EmptyOpenInfoSlotCount = package.OpenInfos.Count(x => x.ItemType == 0 && x.NeedCount == 0), InvalidOpenInfoSlotCount = package.OpenInfos.Count(x => (x.ItemType == 0 && x.NeedCount != 0) || (x.ItemType != 0 && x.NeedCount <= 0)), DistinctOpenItemCount = package.OpenInfos.Where(x => x.ItemType > 0).Select(x => x.ItemType).Distinct().Count(), MissingDigimonBaseInfoCount = missingDigimonBaseInfoCount, MissingItemInfoCount = missingItemInfoCount, DigimonIdRange = FormatRange(package.Rides.Select(x => x.DigimonId)), ChangeRideValues = FormatDistinct(package.Rides.Select(x => x.ChangeRide)), RideTypes = FormatDistinct(package.Rides.Select(x => x.RideType)), MoveSpeedRange = FormatRange(package.Rides.Select(x => x.MoveSpeed)), AnimationRateRange = FormatRange(package.Rides.Select(x => x.AnimationRateRun)), NeedCountRange = FormatRange(package.OpenInfos.Where(x => x.ItemType > 0).Select(x => x.NeedCount)) }; } private static RidePackage LoadPackage(string rideXmlPath) { XElement root = LoadRequiredRoot(rideXmlPath, "RideTable"); List rides = new(); List openInfos = new(); int rideIndex = 0; foreach (XElement rideElement in root.Elements("Ride")) { rideIndex++; string rideContext = $"Ride row {rideIndex}"; RideRow ride = new() { DigimonId = ReadRequiredUInt(rideElement, "s_dwDigimonID", rideContext), ChangeRide = ReadRequiredUInt(rideElement, "s_dwChangeRide", rideContext), MoveSpeed = ReadRequiredDecimal(rideElement, "s_fMoveSpeed", rideContext), RideType = ReadRequiredInt(rideElement, "s_nRideType", rideContext), AnimationRateRun = ReadRequiredDecimal(rideElement, "s_fAniRate_Run", rideContext) }; _ = ReadRequiredString(rideElement, "s_szComment", rideContext); if (ride.DigimonId == 0) { throw new InvalidOperationException($"{rideContext} has invalid s_dwDigimonID value: '0'."); } XElement openInfoElement = rideElement.Element("s_OpenInfo") ?? throw new InvalidOperationException($"{rideContext} is missing required node."); int openInfoIndex = 0; foreach (XElement openInfoNode in openInfoElement.Elements("OpenInfo")) { openInfoIndex++; string openInfoContext = $"{rideContext} OpenInfo row {openInfoIndex}"; openInfos.Add(new RideOpenInfoRow { DigimonId = ride.DigimonId, ItemType = ReadRequiredUInt(openInfoNode, "s_nItemType_S", openInfoContext), NeedCount = ReadRequiredInt(openInfoNode, "s_nNeedCount", openInfoContext) }); } rides.Add(ride); } if (rides.Count == 0) { throw new InvalidOperationException("Ride.xml does not contain any rows."); } return new RidePackage(rides, openInfos); } 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 uint ReadRequiredUInt(XElement parent, string elementName, string recordContext) { XElement? element = parent.Element(elementName); if (element == null) { throw new InvalidOperationException($"{recordContext} is missing required field '{elementName}'."); } if (!uint.TryParse(element.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint value)) { throw new InvalidOperationException($"{recordContext} has invalid unsigned integer value in '{elementName}': '{element.Value}'."); } return value; } private static decimal ReadRequiredDecimal(XElement parent, string elementName, string recordContext) { XElement? element = parent.Element(elementName); if (element == null) { throw new InvalidOperationException($"{recordContext} is missing required field '{elementName}'."); } if (!decimal.TryParse(element.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out decimal value)) { throw new InvalidOperationException($"{recordContext} has invalid decimal 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 HashSet ReadUIntSet(SqlConnection connection, string sql) { using SqlCommand command = new(sql, connection); using SqlDataReader reader = command.ExecuteReader(); HashSet values = new(); while (reader.Read()) { values.Add(Convert.ToUInt32(reader.GetValue(0), CultureInfo.InvariantCulture)); } return values; } 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 FormatRange(IEnumerable values) { uint[] valueArray = values.ToArray(); return valueArray.Length == 0 ? "n/a" : $"{valueArray.Min()} to {valueArray.Max()}"; } private static string FormatRange(IEnumerable values) { decimal[] valueArray = values.ToArray(); return valueArray.Length == 0 ? "n/a" : $"{valueArray.Min().ToString("0.###", CultureInfo.InvariantCulture)} to {valueArray.Max().ToString("0.###", CultureInfo.InvariantCulture)}"; } private static string FormatDistinct(IEnumerable values) { int[] distinctValues = values.Distinct().OrderBy(x => x).ToArray(); return distinctValues.Length == 0 ? "n/a" : string.Join(", ", distinctValues); } private static string FormatDistinct(IEnumerable values) { uint[] distinctValues = values.Distinct().OrderBy(x => x).ToArray(); return distinctValues.Length == 0 ? "n/a" : string.Join(", ", distinctValues); } private sealed record RidePackage(List Rides, List OpenInfos); private sealed class RideRow { public uint DigimonId { get; init; } public uint ChangeRide { get; init; } public decimal MoveSpeed { get; init; } public int RideType { get; init; } public decimal AnimationRateRun { get; init; } } private sealed class RideOpenInfoRow { public uint DigimonId { get; init; } public uint ItemType { get; init; } public int NeedCount { get; init; } } } }