Initial Commit
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
using System.Text;
|
||||
using TuneBoss.Core.Models;
|
||||
|
||||
namespace TuneBoss.Core.IO;
|
||||
|
||||
/// <summary>
|
||||
/// Byte-exact serializer / parser for the table sections inside a <c>.tst</c> map's
|
||||
/// data string. Replicates the legacy helpers verbatim:
|
||||
///
|
||||
/// 2D : <c>label__data__load__rpm__max min mult sign ____</c>
|
||||
/// AFR : <c>label__data__load__rpm____</c> (no modifiers)
|
||||
/// Sub : <c>label__data____</c> (24 data bytes)
|
||||
/// 1D : <c>label__<24 wire bytes>____</c>
|
||||
///
|
||||
/// Every value is its unsigned decimal followed by a single space; <c>__</c> separates
|
||||
/// fields within a section, <c>____</c> 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 <c>populateSubTable</c>.
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>Parses a space-separated decimal list into bytes (empties skipped).</summary>
|
||||
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;
|
||||
|
||||
/// <summary>Generic named byte block (used for the misc regions). <c>label__<bytes>____</c>.</summary>
|
||||
public static string SectionBytes(string label, byte[] data) =>
|
||||
label + FieldSep + Bytes(data) + SectionSep;
|
||||
|
||||
/// <summary>Parses a <see cref="SectionBytes"/> block back into its raw bytes.</summary>
|
||||
public static byte[] ParseBytesSection(string section)
|
||||
{
|
||||
var f = Fields(section);
|
||||
return f.Length > 1 ? ParseBytes(f[1]) : Array.Empty<byte>();
|
||||
}
|
||||
|
||||
// ── Section splitting ───────────────────────────────────────────────────────
|
||||
/// <summary>Splits a data string into its <c>____</c>-terminated sections (positional).</summary>
|
||||
public static string[] SplitSections(string data) =>
|
||||
data.Split(SectionSep, StringSplitOptions.None);
|
||||
|
||||
private static string[] Fields(string section) =>
|
||||
section.Split(FieldSep, StringSplitOptions.None);
|
||||
|
||||
/// <summary>The label of a section (its first field), for sanity checks.</summary>
|
||||
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),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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;
|
||||
|
||||
/// <summary>Returns an array of exactly <paramref name="n"/> bytes (pad/truncate).</summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user