using System.Globalization; using System.Xml.Linq; namespace XmlTool.Importers { public sealed class TimeChargeProfileSummary { public int ChargeItemCount { get; init; } public int BitCodeCount { get; init; } public int CashCodeCount { get; init; } public int CashNeedInfoCount { get; init; } public int DuplicateChargeItemCodeCount { get; init; } public int DuplicateBitCodeCount { get; init; } public int DuplicateCashCodeCount { get; init; } public int MissingBitCodeReferenceCount { get; init; } public int MissingCashCodeReferenceCount { get; init; } public int EmptyCashCodeGroupCount { get; init; } public string ChargeItemCodeRange { get; init; } = string.Empty; public string BitCodeRange { get; init; } = string.Empty; public string CashCodeRange { get; init; } = string.Empty; public string KeepTimeRange { get; init; } = string.Empty; public string ChargeTimeRange { get; init; } = string.Empty; public string KeepPriceRange { get; init; } = string.Empty; public string CashNeedTimeIndexRange { get; init; } = string.Empty; public string CashNeedCountRange { get; init; } = string.Empty; } public static class TimeChargeProfileInspector { public static TimeChargeProfileSummary ValidatePackage(string chargeItemXmlPath) { string packageDirectory = Path.GetDirectoryName(chargeItemXmlPath) ?? throw new InvalidOperationException("TimeCharge package path does not have a directory."); List chargeItems = LoadChargeItems(chargeItemXmlPath); List bitCodes = LoadBitCodes(RequiredPath(packageDirectory, "BitCode.xml")); List cashCodes = LoadCashCodes(RequiredPath(packageDirectory, "CashCode.xml")); HashSet knownBitCodes = bitCodes.Select(x => x.BitCode).ToHashSet(); HashSet knownCashCodes = cashCodes.Select(x => x.CashCode).ToHashSet(); List cashNeedRows = cashCodes.SelectMany(x => x.NeedInfos).ToList(); return new TimeChargeProfileSummary { ChargeItemCount = chargeItems.Count, BitCodeCount = bitCodes.Count, CashCodeCount = cashCodes.Count, CashNeedInfoCount = cashNeedRows.Count, DuplicateChargeItemCodeCount = CountDuplicates(chargeItems.Select(x => x.ItemCode)), DuplicateBitCodeCount = CountDuplicates(bitCodes.Select(x => x.BitCode)), DuplicateCashCodeCount = CountDuplicates(cashCodes.Select(x => x.CashCode)), MissingBitCodeReferenceCount = chargeItems.Count(x => !knownBitCodes.Contains(x.BitCode)), MissingCashCodeReferenceCount = chargeItems.Count(x => !knownCashCodes.Contains(x.CashCode)), EmptyCashCodeGroupCount = cashCodes.Count(x => x.NeedInfos.Count == 0), ChargeItemCodeRange = FormatRange(chargeItems.Select(x => x.ItemCode)), BitCodeRange = FormatRange(bitCodes.Select(x => x.BitCode)), CashCodeRange = FormatRange(cashCodes.Select(x => x.CashCode)), KeepTimeRange = FormatRange(bitCodes.Select(x => x.KeepTime)), ChargeTimeRange = FormatRange(bitCodes.Select(x => x.ChargeTime)), KeepPriceRange = FormatRange(bitCodes.Select(x => x.KeepPrice)), CashNeedTimeIndexRange = FormatRange(cashNeedRows.Select(x => x.TimeIndex)), CashNeedCountRange = FormatRange(cashNeedRows.Select(x => x.NeedCount)) }; } private static List LoadChargeItems(string xmlPath) { XElement root = LoadRequiredRoot(xmlPath, "ChargeItemTable"); List rows = new(); int rowIndex = 0; foreach (XElement element in root.Elements("ChargeItem")) { rowIndex++; string rowContext = $"ChargeItem row {rowIndex}"; rows.Add(new ChargeItemRow { ItemCode = ReadRequiredUInt(element, "ItemCode", rowContext), BitCode = ReadRequiredInt(element, "nBitCode", rowContext), CashCode = ReadRequiredInt(element, "nCashCode", rowContext) }); } if (rows.Count == 0) { throw new InvalidOperationException("ChargeItem.xml does not contain any rows."); } return rows; } private static List LoadBitCodes(string xmlPath) { XElement root = LoadRequiredRoot(xmlPath, "BitCodeTable"); List rows = new(); int rowIndex = 0; foreach (XElement element in root.Elements("BitCode")) { rowIndex++; string rowContext = $"BitCode row {rowIndex}"; rows.Add(new BitCodeRow { BitCode = ReadRequiredInt(element, "BitCode", rowContext), KeepTime = ReadRequiredInt(element, "nKeepTime", rowContext), ChargeTime = ReadRequiredInt(element, "nChargeTime", rowContext), KeepPrice = ReadRequiredUInt(element, "dwKeepPrice", rowContext) }); } if (rows.Count == 0) { throw new InvalidOperationException("BitCode.xml does not contain any rows."); } return rows; } private static List LoadCashCodes(string xmlPath) { XElement root = LoadRequiredRoot(xmlPath, "CashCodeTable"); List rows = new(); int rowIndex = 0; foreach (XElement element in root.Elements("CashCode")) { rowIndex++; string rowContext = $"CashCode row {rowIndex}"; XElement needInfosElement = element.Element("NeedInfos") ?? throw new InvalidOperationException($"{rowContext} is missing required node."); List needInfos = new(); int needIndex = 0; foreach (XElement needElement in needInfosElement.Elements("NeedInfo")) { needIndex++; string needContext = $"{rowContext} need info {needIndex}"; needInfos.Add(new CashNeedRow { TimeIndex = ReadRequiredInt(needElement, "nTimeIndex", needContext), NeedCount = ReadRequiredInt(needElement, "nNeedCount", needContext) }); } rows.Add(new CashCodeRow { CashCode = ReadRequiredInt(element, "CashCode", rowContext), NeedInfos = needInfos }); } if (rows.Count == 0) { throw new InvalidOperationException("CashCode.xml does not contain any rows."); } return rows; } private static string RequiredPath(string packageDirectory, string fileName) { string path = Path.Combine(packageDirectory, fileName); if (!File.Exists(path)) { throw new FileNotFoundException($"{fileName} not found.", path); } return path; } 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 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 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 int CountDuplicates(IEnumerable values) { return values.GroupBy(x => x).Where(x => x.Count() > 1).Sum(x => x.Count() - 1); } 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) { uint[] 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 ChargeItemRow { public uint ItemCode { get; init; } public int BitCode { get; init; } public int CashCode { get; init; } } private sealed class BitCodeRow { public int BitCode { get; init; } public int KeepTime { get; init; } public int ChargeTime { get; init; } public uint KeepPrice { get; init; } } private sealed class CashCodeRow { public int CashCode { get; init; } public List NeedInfos { get; init; } = new(); } private sealed class CashNeedRow { public int TimeIndex { get; init; } public int NeedCount { get; init; } } } }