115 lines
4.0 KiB
C#
115 lines
4.0 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Xml.Linq;
|
|
|
|
namespace TuneBoss.Core.Models;
|
|
|
|
/// <summary>
|
|
/// Which bike models have VVA (variable valve actuation), so the RPM Settings page can
|
|
/// gate the VVA RPM Trigger. Mirrors the legacy <c>vvaModels.xml</c> / <c>checkVvaModels</c>.
|
|
///
|
|
/// The list is loaded from an EDITABLE external file at
|
|
/// <c>%APPDATA%\TuneBoss\vvaModels.xml</c> so it can be changed without recompiling.
|
|
/// The file is auto-created (seeded with the defaults below) on first use if missing.
|
|
/// If it can't be read, the built-in defaults are used.
|
|
///
|
|
/// XML schema (same as legacy): <vvaModels><ym>14</ym>…<hn>…</hn>
|
|
/// <su>…</su><kw>…</kw></vvaModels>.
|
|
/// Element → manufacturer code: ym=1 Yamaha, hn=2 Honda, kw=3 Kawasaki, su=6 Suzuki.
|
|
/// </summary>
|
|
public static class VvaModelRegistry
|
|
{
|
|
private static readonly object _lock = new();
|
|
private static Dictionary<byte, HashSet<byte>>? _map;
|
|
|
|
/// <summary>Editable list file. Sits beside settings.json in %APPDATA%\TuneBoss.</summary>
|
|
public static string FilePath => System.IO.Path.Combine(
|
|
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData),
|
|
"TuneBoss", "vvaModels.xml");
|
|
|
|
// Element name → manufacturer code.
|
|
private static readonly (string Elem, byte Mfr)[] Makes =
|
|
{
|
|
("ym", 1), ("hn", 2), ("kw", 3), ("su", 6),
|
|
};
|
|
|
|
// Built-in defaults (legacy vvaModels.xml): only these Yamaha models carry VVA.
|
|
private static Dictionary<byte, HashSet<byte>> Defaults() => new()
|
|
{
|
|
[1] = new HashSet<byte> { 14, 17, 21, 22 },
|
|
};
|
|
|
|
/// <summary>True when the given make/model is known to have VVA.</summary>
|
|
public static bool IsVvaModel(byte manufacturer, byte model)
|
|
{
|
|
var map = EnsureLoaded();
|
|
return map.TryGetValue(manufacturer, out var models) && models.Contains(model);
|
|
}
|
|
|
|
/// <summary>Force a re-read of the file on the next lookup (call after editing it).</summary>
|
|
public static void Reload()
|
|
{
|
|
lock (_lock) { _map = null; }
|
|
}
|
|
|
|
private static Dictionary<byte, HashSet<byte>> EnsureLoaded()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
if (_map != null) return _map;
|
|
|
|
try
|
|
{
|
|
if (System.IO.File.Exists(FilePath))
|
|
{
|
|
_map = Parse(XDocument.Load(FilePath));
|
|
return _map;
|
|
}
|
|
|
|
// First run — seed the editable file with the defaults, then use them.
|
|
_map = Defaults();
|
|
TrySeedFile();
|
|
}
|
|
catch
|
|
{
|
|
// Missing / malformed / unreadable → fall back to the built-in list.
|
|
_map = Defaults();
|
|
}
|
|
|
|
return _map;
|
|
}
|
|
}
|
|
|
|
private static Dictionary<byte, HashSet<byte>> Parse(XDocument doc)
|
|
{
|
|
var map = new Dictionary<byte, HashSet<byte>>();
|
|
foreach (var (elem, mfr) in Makes)
|
|
{
|
|
var set = new HashSet<byte>();
|
|
foreach (var node in doc.Descendants(elem))
|
|
{
|
|
if (byte.TryParse(node.Value.Trim(), out byte m))
|
|
set.Add(m); // a 999 placeholder simply never matches a real model
|
|
}
|
|
if (set.Count > 0) map[mfr] = set;
|
|
}
|
|
return map.Count > 0 ? map : Defaults();
|
|
}
|
|
|
|
private static void TrySeedFile()
|
|
{
|
|
try
|
|
{
|
|
var dir = System.IO.Path.GetDirectoryName(FilePath);
|
|
if (!string.IsNullOrEmpty(dir)) System.IO.Directory.CreateDirectory(dir);
|
|
|
|
var doc = new XDocument(new XElement("vvaModels",
|
|
new XElement("ym", 14), new XElement("ym", 17),
|
|
new XElement("ym", 21), new XElement("ym", 22),
|
|
new XElement("hn", 999), new XElement("su", 999), new XElement("kw", 999)));
|
|
doc.Save(FilePath);
|
|
}
|
|
catch { /* seeding is best-effort; defaults still apply in memory */ }
|
|
}
|
|
}
|