Files
TuneBossPC/TuneBoss.Core/Models/TwoCylinderRegistry.cs
T
2026-09-28 16:55:31 +08:00

69 lines
1.9 KiB
C#

using System.Xml.Linq;
namespace TuneBoss.Core.Models;
/// <summary>
/// Determines whether a bike is a two-cylinder model based on
/// manufacturer + model code, driven by twoCylModels.xml.
/// </summary>
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<byte, HashSet<uint>> _models = new();
private TwoCylinderRegistry() { }
/// <summary>
/// Loads from a twoCylModels.xml file path.
/// </summary>
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;
}
/// <summary>
/// Loads from an XML string (useful for embedding or testing).
/// </summary>
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;
}
}