using System.Xml.Linq; namespace TuneBoss.Core.Models; /// /// Determines whether a bike is a two-cylinder model based on /// manufacturer + model code, driven by twoCylModels.xml. /// public sealed class TwoCylinderRegistry { // Manufacturer codes public const byte Yamaha = 1; public const byte Honda = 2; public const byte Kawasaki = 3; public const byte Suzuki = 6; private readonly Dictionary> _models = new(); private TwoCylinderRegistry() { } /// /// Loads from a twoCylModels.xml file path. /// public static TwoCylinderRegistry Load(string xmlPath) { var doc = XDocument.Load(xmlPath); var registry = new TwoCylinderRegistry(); registry.LoadTag(doc, "ym", Yamaha); registry.LoadTag(doc, "hn", Honda); registry.LoadTag(doc, "kw", Kawasaki); registry.LoadTag(doc, "su", Suzuki); return registry; } /// /// Loads from an XML string (useful for embedding or testing). /// public static TwoCylinderRegistry LoadFromString(string xml) { var doc = XDocument.Parse(xml); var registry = new TwoCylinderRegistry(); registry.LoadTag(doc, "ym", Yamaha); registry.LoadTag(doc, "hn", Honda); registry.LoadTag(doc, "kw", Kawasaki); registry.LoadTag(doc, "su", Suzuki); return registry; } public bool IsTwoCylinder(byte manufacturer, uint model) { return _models.TryGetValue(manufacturer, out var set) && set.Contains(model); } private void LoadTag(XDocument doc, string tag, byte manufacturerCode) { var models = doc.Descendants(tag) .Select(e => uint.TryParse(e.Value.Trim(), out var v) ? (uint?)v : null) .Where(v => v.HasValue) .Select(v => v!.Value) .ToHashSet(); _models[manufacturerCode] = models; } }