using System.Globalization; using System.Xml.Linq; namespace XmlTool.Importers { public sealed class UITextProfileSummary { public int UITextCount { get; init; } public int NonEmptyTextCount { get; init; } public int EmptyTextCount { get; init; } public int DuplicateHashKeyCount { get; init; } public int NegativeHashKeyCount { get; init; } public int FormatTextCount { get; init; } public string HashKeyRange { get; init; } = string.Empty; public string TextLengthRange { get; init; } = string.Empty; public int MaxTextLength { get; init; } } public static class UITextProfileInspector { public static UITextProfileSummary ValidateXml(string uiTextXmlPath) { XElement root = LoadRequiredRoot(uiTextXmlPath, "UITexts"); List rows = new(); int rowIndex = 0; foreach (XElement element in root.Elements("UIText")) { rowIndex++; string rowContext = $"UIText row {rowIndex}"; string text = ReadRequiredString(element, "Text", rowContext); rows.Add(new UITextRow { HashKey = ReadRequiredLong(element, "HashKey", rowContext), Text = text }); } if (rows.Count == 0) { throw new InvalidOperationException("UIText.xml does not contain any rows."); } return new UITextProfileSummary { UITextCount = rows.Count, NonEmptyTextCount = rows.Count(x => !string.IsNullOrEmpty(x.Text)), EmptyTextCount = rows.Count(x => string.IsNullOrEmpty(x.Text)), DuplicateHashKeyCount = CountDuplicates(rows.Select(x => x.HashKey)), NegativeHashKeyCount = rows.Count(x => x.HashKey < 0), FormatTextCount = rows.Count(x => ContainsFormatMarker(x.Text)), HashKeyRange = FormatRange(rows.Select(x => x.HashKey)), TextLengthRange = FormatRange(rows.Select(x => x.Text.Length)), MaxTextLength = rows.Max(x => x.Text.Length) }; } 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 long ReadRequiredLong(XElement parent, string elementName, string recordContext) { XElement? element = parent.Element(elementName); if (element == null) { throw new InvalidOperationException($"{recordContext} is missing required field '{elementName}'."); } if (!long.TryParse(element.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out long 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}'."); } return element.Value; } private static bool ContainsFormatMarker(string text) { return text.Contains('%') || text.Contains('#'); } 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) { long[] valueArray = values.ToArray(); return valueArray.Length == 0 ? "n/a" : $"{valueArray.Min()} to {valueArray.Max()}"; } private static string FormatRange(IEnumerable values) { int[] valueArray = values.ToArray(); return valueArray.Length == 0 ? "n/a" : $"{valueArray.Min()} to {valueArray.Max()}"; } private sealed class UITextRow { public long HashKey { get; init; } public string Text { get; init; } = string.Empty; } } }