using System.Text;
using TuneBoss.Core.Models;
namespace TuneBoss.Core.IO;
///
/// Byte-exact serializer / parser for the table sections inside a .tst map's
/// data string. Replicates the legacy helpers verbatim:
///
/// 2D : label__data__load__rpm__max min mult sign ____
/// AFR : label__data__load__rpm____ (no modifiers)
/// Sub : label__data____ (24 data bytes)
/// 1D : label__<24 wire bytes>____
///
/// Every value is its unsigned decimal followed by a single space; __ separates
/// fields within a section, ____ terminates each section. Subtable RPM axes are
/// not stored — on load they are derived from the parent 2D table's RPM axis (every
/// 4th breakpoint), matching legacy populateSubTable.
///
public static class MapTableFormat
{
public const string SectionSep = "____";
public const string FieldSep = "__";
// ── Value lists ─────────────────────────────────────────────────────────────
private static string Bytes(byte[] a)
{
var sb = new StringBuilder(a.Length * 4);
foreach (var b in a) sb.Append((int)b).Append(' '); // "v v v "
return sb.ToString();
}
/// Parses a space-separated decimal list into bytes (empties skipped).
public static byte[] ParseBytes(string s)
{
var parts = s.Split(' ', StringSplitOptions.RemoveEmptyEntries);
var r = new byte[parts.Length];
for (int i = 0; i < parts.Length; i++)
r[i] = (byte)(int.Parse(parts[i]) & 0xFF);
return r;
}
// ── Serialize ───────────────────────────────────────────────────────────────
public static string Section2D(string label, TuningTable t) =>
label + FieldSep + Bytes(t.Data) + FieldSep + Bytes(t.LoadAxis) + FieldSep + Bytes(t.RpmAxis) +
FieldSep + $"{t.MaxRaw} {t.MinRaw} {t.Mult} {t.Sign} " + SectionSep;
public static string SectionAfr(string label, AfrTable t) =>
label + FieldSep + Bytes(t.Data) + FieldSep + Bytes(t.LoadAxis) + FieldSep + Bytes(t.RpmAxis) + SectionSep;
public static string SectionSub(string label, SubTable t) =>
label + FieldSep + Bytes(t.Data) + SectionSep;
public static string Section1D(string label, OneDimTable t) =>
label + FieldSep + Bytes(t.ToWirePayload()) + SectionSep;
/// Generic named byte block (used for the misc regions). label__<bytes>____.
public static string SectionBytes(string label, byte[] data) =>
label + FieldSep + Bytes(data) + SectionSep;
/// Parses a block back into its raw bytes.
public static byte[] ParseBytesSection(string section)
{
var f = Fields(section);
return f.Length > 1 ? ParseBytes(f[1]) : Array.Empty();
}
// ── Section splitting ───────────────────────────────────────────────────────
/// Splits a data string into its ____-terminated sections (positional).
public static string[] SplitSections(string data) =>
data.Split(SectionSep, StringSplitOptions.None);
private static string[] Fields(string section) =>
section.Split(FieldSep, StringSplitOptions.None);
/// The label of a section (its first field), for sanity checks.
public static string LabelOf(string section) => Fields(section)[0];
// ── Parse ───────────────────────────────────────────────────────────────────
public static TuningTable Parse2D(string section)
{
var f = Fields(section);
var data = Fit(ParseBytes(f[1]), TuningTable.DataBytes);
var load = Fit(ParseBytes(f[2]), TuningTable.Rows);
var rpm = Fit(ParseBytes(f[3]), TuningTable.Cols);
var mod = ParseBytes(f.Length > 4 ? f[4] : "");
return new TuningTable
{
Data = data, LoadAxis = load, RpmAxis = rpm,
MaxRaw = At(mod, 0), MinRaw = At(mod, 1), Mult = At(mod, 2), Sign = At(mod, 3),
};
}
public static AfrTable ParseAfr(string section)
{
var f = Fields(section);
return new AfrTable
{
Data = Fit(ParseBytes(f[1]), AfrTable.DataBytes),
LoadAxis = Fit(ParseBytes(f[2]), AfrTable.Rows),
RpmAxis = Fit(ParseBytes(f[3]), AfrTable.Cols),
};
}
///
/// Parses a subtable section. The 12-byte RPM axis is derived from the parent 2D
/// table's 24-byte RPM axis: 6 (lo,hi) pairs taken from indices 4·x and 4·x+3.
///
public static SubTable ParseSub(string section, byte[] parentRpm)
{
var f = Fields(section);
var sub = new SubTable { Data = Fit(ParseBytes(f[1]), SubTable.DataBytes) };
for (int x = 0; x < SubTable.Cols; x++)
{
sub.RpmAxis[x * 2] = At(parentRpm, x * 4);
sub.RpmAxis[x * 2 + 1] = At(parentRpm, x * 4 + 3);
}
return sub;
}
public static OneDimTable Parse1D(string section)
{
var f = Fields(section);
var wire = Fit(ParseBytes(f[1]), OneDimTable.WireLength);
return OneDimTable.FromWire(wire, 0);
}
// ── Helpers ─────────────────────────────────────────────────────────────────
private static byte At(byte[] a, int i) => i < a.Length ? a[i] : (byte)0;
/// Returns an array of exactly bytes (pad/truncate).
private static byte[] Fit(byte[] src, int n)
{
if (src.Length == n) return src;
var r = new byte[n];
Array.Copy(src, r, Math.Min(src.Length, n));
return r;
}
}