using System.Globalization; using System.Xml.Linq; using Microsoft.Data.SqlClient; namespace XmlTool.Importers { public sealed class MapObjectProfileSummary { public int MapGroupCount { get; init; } public int ObjectBucketCount { get; init; } public int MapObjectCount { get; init; } public int FactorCount { get; init; } public int DistinctMapCount { get; init; } public int DistinctObjectIdCount { get; init; } public int MissingAssetMapCount { get; init; } public string OpenTypes { get; init; } = string.Empty; } public static class MapObjectProfileInspector { public static MapObjectProfileSummary ValidateXml(string xmlPath) { List groups = LoadGroups(xmlPath); return new MapObjectProfileSummary { MapGroupCount = groups.Count, ObjectBucketCount = groups.Sum(x => x.Buckets.Count), MapObjectCount = groups.Sum(x => x.Buckets.Sum(y => y.ObjectCount)), FactorCount = groups.Sum(x => x.Buckets.Sum(y => y.Factors.Count)), DistinctMapCount = groups.Select(x => x.MapId).Distinct().Count(), DistinctObjectIdCount = groups.SelectMany(x => x.Buckets).Select(x => x.ObjectId).Distinct().Count(), OpenTypes = string.Join(", ", groups.SelectMany(x => x.Buckets).SelectMany(x => x.Factors).Select(x => x.OpenType).Distinct().OrderBy(x => x)) }; } public static MapObjectProfileSummary AssessDatabase(string connectionString, string xmlPath) { List groups = LoadGroups(xmlPath); using SqlConnection connection = new(connectionString); connection.Open(); HashSet assetMapIds = ReadIntSet(connection, "SELECT [MapId] FROM [Asset].[Map];"); return new MapObjectProfileSummary { MapGroupCount = groups.Count, ObjectBucketCount = groups.Sum(x => x.Buckets.Count), MapObjectCount = groups.Sum(x => x.Buckets.Sum(y => y.ObjectCount)), FactorCount = groups.Sum(x => x.Buckets.Sum(y => y.Factors.Count)), DistinctMapCount = groups.Select(x => x.MapId).Distinct().Count(), DistinctObjectIdCount = groups.SelectMany(x => x.Buckets).Select(x => x.ObjectId).Distinct().Count(), MissingAssetMapCount = groups.Select(x => x.MapId).Distinct().Count(x => !assetMapIds.Contains(x)), OpenTypes = string.Join(", ", groups.SelectMany(x => x.Buckets).SelectMany(x => x.Factors).Select(x => x.OpenType).Distinct().OrderBy(x => x)) }; } private static List LoadGroups(string xmlPath) { if (!File.Exists(xmlPath)) { throw new FileNotFoundException("MapObject.xml not found.", xmlPath); } XDocument document = XDocument.Load(xmlPath); XElement? root = document.Root; if (root == null || root.Name.LocalName != "MapObjectTable") { throw new InvalidOperationException("MapObject.xml root node must be ."); } List groups = new(); int groupIndex = 0; foreach (XElement groupElement in root.Elements("MapGroup")) { groupIndex++; string groupContext = $"MapObject MapGroup {groupIndex}"; int mapId = ReadRequiredInt(groupElement, "MapId", groupContext); int declaredBucketCount = ReadRequiredInt(groupElement, "ObjectBucketCount", groupContext); List buckets = new(); foreach (XElement bucketElement in groupElement.Elements("ObjectBucket")) { string bucketContext = $"{groupContext} bucket {buckets.Count + 1}"; int objectId = ReadRequiredInt(bucketElement, "ObjectId", bucketContext); int declaredObjectCount = ReadRequiredInt(bucketElement, "ObjectCount", bucketContext); List mapObjectElements = bucketElement.Elements("MapObject").ToList(); if (mapObjectElements.Count != declaredObjectCount) { throw new InvalidOperationException($"{bucketContext} declares ObjectCount={declaredObjectCount}, but contains {mapObjectElements.Count} rows."); } List factors = new(); foreach (XElement mapObjectElement in mapObjectElements) { string objectContext = $"{bucketContext} object {factors.Count + 1}"; _ = ReadRequiredInt(mapObjectElement, "OrderNumber", objectContext); int declaredFactorCount = ReadRequiredInt(mapObjectElement, "FactorCount", objectContext); List factorElements = mapObjectElement.Elements("Factor").ToList(); if (factorElements.Count != declaredFactorCount) { throw new InvalidOperationException($"{objectContext} declares FactorCount={declaredFactorCount}, but contains {factorElements.Count} rows."); } foreach (XElement factorElement in factorElements) { string factorContext = $"{objectContext} factor {factors.Count + 1}"; factors.Add(new MapObjectFactor { OpenType = ReadRequiredInt(factorElement, "OpenType", factorContext), Count = ReadRequiredInt(factorElement, "FactorCount", factorContext), Value = ReadRequiredInt(factorElement, "FactorValue", factorContext) }); } } buckets.Add(new MapObjectBucket { ObjectId = objectId, ObjectCount = mapObjectElements.Count, Factors = factors }); } if (buckets.Count != declaredBucketCount) { throw new InvalidOperationException($"{groupContext} declares ObjectBucketCount={declaredBucketCount}, but contains {buckets.Count} rows."); } groups.Add(new MapObjectGroup { MapId = mapId, Buckets = buckets }); } if (groups.Count == 0) { throw new InvalidOperationException("MapObject.xml does not contain any rows."); } return groups; } 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 HashSet ReadIntSet(SqlConnection connection, string sql) { using SqlCommand command = new(sql, connection); using SqlDataReader reader = command.ExecuteReader(); HashSet values = new(); while (reader.Read()) { values.Add(Convert.ToInt32(reader.GetValue(0), CultureInfo.InvariantCulture)); } return values; } private sealed class MapObjectGroup { public int MapId { get; init; } public List Buckets { get; init; } = new(); } private sealed class MapObjectBucket { public int ObjectId { get; init; } public int ObjectCount { get; init; } public List Factors { get; init; } = new(); } private sealed class MapObjectFactor { public int OpenType { get; init; } public int Count { get; init; } public int Value { get; init; } } } }