Launcher: the build code (launcher/Logic/Build/) that v0.5.2 was built with

The root .gitignore's build/ also matched launcher/Logic/Build/ on Windows, so BuildRunner.cs, BuildProtocol.cs and BuildTimes.cs were never committed and the source of v0.5.2 did not build the launcher (BuildRunner, BuildResult undefined). The three files byte for byte as TriforceSetup.exe v0.5.2 was built from them, and launcher/.gitignore's exception !Logic/Build/.
This commit is contained in:
ruckus7382sfgsgdf
2026-11-19 20:55:08 -06:00
parent fb26495475
commit b0e46adb48
4 changed files with 652 additions and 0 deletions

2
launcher/.gitignore vendored
View File

@@ -5,3 +5,5 @@ out/
ui/audio/
ui/img/game/
ui/img/save*.jpg
# (11-19) the root .gitignore's build/ also matched Logic/Build/ on Windows: the launcher's build code is source
!Logic/Build/

View File

@@ -0,0 +1,180 @@
// make_game.py's lines (its docstring, nx2/make_game.py:12-25; research/02 3.2, research/12 4.2), as typed events.
// @@stage <id> <text> a step begins @@progress <0..1> how far that step is (at most 4 a second)
// @@plan <changed> <total> files the compile redoes @@file <name> <s> <new|cached|again> one module built
// @@eta <seconds> the compile's time left @@times <total> <compile> <previous|-1> at the end
// @@check <ok|bad> <text> (--check) @@done <folder> finished: THE success signal, not the exit code
// @@error <text> stopped
// and two plain lines: "the shaders were not prepared now (<why>): ..." (NOT_PREPARED: left to the game's first start) and
// "the build's steps: before the game files 0:41, read 1:32, ..." (each stage's time, make_game.py stage_times).
using System.Globalization;
using System.Text.RegularExpressions;
namespace Triforce.Logic;
public abstract record BuildEvent
{
public sealed record Stage(string Id, string Text) : BuildEvent;
public sealed record Progress(double Fraction) : BuildEvent;
public sealed record Plan(int Changed, int Total) : BuildEvent;
public sealed record FileBuilt(string Name, double Seconds, string How) : BuildEvent;
public sealed record Eta(int Seconds) : BuildEvent;
public sealed record Times(int TotalSeconds, int CompileSeconds, int? PreviousSeconds) : BuildEvent;
public sealed record Done(string Folder) : BuildEvent;
public sealed record Error(string Text) : BuildEvent;
public sealed record Check(bool Ok, string Text) : BuildEvent;
/// <summary>The shaders were not prepared now: the game prepares them at its first start (the shader row says so).</summary>
public sealed record NotPrepared(string Line) : BuildEvent;
/// <summary>make_game's own account of each stage's time ("read 1:32, lift 2:10, ...").</summary>
public sealed record StageTimes(IReadOnlyDictionary<string, double> Seconds) : BuildEvent;
/// <summary>Any other line (the details drawer shows it).</summary>
public sealed record Plain(string Line) : BuildEvent;
}
/// <summary>A step of the build, in order, with the words the build screen shows (old Shell.cs Stages; DESIGN 6 Build).</summary>
public sealed record BuildStage(string Id, string Text, double Weight);
public static class BuildStages
{
/// <summary>read, lift, compile, finish (make_game.py:1850, 1953, 2007, 2077) and shaders (the game's own --prepare-shaders,
/// gpu/vk/vk_main.cpp:1153, which make_game runs at its end and passes its @@stage/@@progress on). Weight: a stage's share of a
/// build without the shaders (make_game.py STAGE_SPAN: read 0-5 %, lift 5-25, compile 25-95, finish 95-100).</summary>
public static readonly IReadOnlyList<BuildStage> All = new BuildStage[]
{
new("read", "Reading your game files", 0.05),
new("lift", "Translating the game to PC code", 0.20),
new("compile", "Compiling", 0.70),
new("finish", "Putting the game folder together", 0.05),
new("shaders", "Preparing shaders for your graphics card", 0),
};
public static BuildStage? Find(string id) => All.FirstOrDefault(s => s.Id == id);
public static int IndexOf(string id) => All.ToList().FindIndex(s => s.Id == id);
}
public static class BuildProtocol
{
public const string NotPreparedStart = "the shaders were not prepared now";
const string StepsStart = "the build's steps: ";
static readonly CultureInfo Inv = CultureInfo.InvariantCulture;
/// <summary>One line of make_game.py's output as an event (never null for a line: plain text is Text).</summary>
public static BuildEvent Parse(string line)
{
if (line.StartsWith("@@", StringComparison.Ordinal))
{
var sp = line.IndexOf(' ');
var word = sp < 0 ? line.Substring(2) : line.Substring(2, sp - 2);
var rest = sp < 0 ? "" : line.Substring(sp + 1);
var w = rest.Split(' ', StringSplitOptions.RemoveEmptyEntries);
switch (word)
{
case "stage" when w.Length >= 1:
return new BuildEvent.Stage(w[0], rest.Length > w[0].Length ? rest.Substring(w[0].Length).Trim() : "");
case "progress" when w.Length >= 1 && double.TryParse(w[0], NumberStyles.Float, Inv, out var f):
return new BuildEvent.Progress(Math.Clamp(f, 0, 1));
case "plan" when w.Length == 2 && int.TryParse(w[0], out var c) && int.TryParse(w[1], out var t):
return new BuildEvent.Plan(c, t);
case "file" when w.Length == 3 && double.TryParse(w[1], NumberStyles.Float, Inv, out var s):
return new BuildEvent.FileBuilt(w[0], s, w[2]);
case "eta" when w.Length >= 1 && double.TryParse(w[0], NumberStyles.Float, Inv, out var e):
return new BuildEvent.Eta((int)Math.Round(e));
case "times" when w.Length == 3 && double.TryParse(w[0], NumberStyles.Float, Inv, out var tt) && double.TryParse(w[1], NumberStyles.Float, Inv, out var cc)
&& double.TryParse(w[2], NumberStyles.Float, Inv, out var pp):
return new BuildEvent.Times((int)tt, (int)cc, pp < 0 ? null : (int)pp);
case "done":
return new BuildEvent.Done(rest.Trim());
case "error":
return new BuildEvent.Error(rest);
case "check" when w.Length >= 1 && (w[0] == "ok" || w[0] == "bad"):
return new BuildEvent.Check(w[0] == "ok", rest.Length > w[0].Length ? rest.Substring(w[0].Length + 1) : "");
}
return new BuildEvent.Plain(line);
}
if (line.StartsWith(NotPreparedStart, StringComparison.Ordinal)) return new BuildEvent.NotPrepared(line);
if (line.StartsWith(StepsStart, StringComparison.Ordinal)) return new BuildEvent.StageTimes(ParseSteps(line.Substring(StepsStart.Length)));
return new BuildEvent.Plain(line);
}
/// <summary>"before the game files 0:41, read 1:32, lift 2:10, compile 7:42, finish 1:17" (clock: m:ss or h:mm:ss).</summary>
public static IReadOnlyDictionary<string, double> ParseSteps(string text)
{
var d = new Dictionary<string, double>();
foreach (var part in text.Split(", "))
{
var m = Regex.Match(part.Trim(), @"^(.*\S)\s+(\d+(?::\d{1,2}){1,2})$");
if (!m.Success) continue;
var secs = m.Groups[2].Value.Split(':').Aggregate(0.0, (acc, x) => acc * 60 + int.Parse(x));
d[m.Groups[1].Value] = secs;
}
return d;
}
}
/// <summary>The build's state as its lines come (old Shell.cs Line/Ended; the Tk builder's parser): the stage it is in, that
/// stage's progress, when each stage began, the plan, the time left the compile said, the folder @@done named, the last
/// @@error. Fed line by line by BuildRunner, or by a test from a transcript.</summary>
public sealed class BuildTracker
{
readonly Func<DateTime> now;
public BuildTracker(Func<DateTime>? clock = null) { now = clock ?? (() => DateTime.UtcNow); Started = now(); }
public DateTime Started { get; private set; }
public string? Stage { get; private set; }
public double StageProgress { get; private set; }
public DateTime StageStarted { get; private set; }
/// <summary>Each stage's seconds once the next began (the last one's at the end).</summary>
public Dictionary<string, double> StageSeconds { get; } = new();
public (int Changed, int Total)? Plan { get; private set; }
public int FilesBuilt { get; private set; }
public int? EtaSeconds { get; private set; }
public BuildEvent.Times? Times { get; private set; }
public string? DoneFolder { get; private set; }
public string? Error { get; private set; }
public string? NotPrepared { get; private set; }
public IReadOnlyDictionary<string, double>? ReportedSteps { get; private set; }
public event Action<BuildEvent>? Event;
/// <summary>A stage ended (its id) and the next began.</summary>
public event Action<string?, string>? StageChanged;
public BuildEvent Feed(string line)
{
var e = BuildProtocol.Parse(line);
switch (e)
{
case BuildEvent.Stage s:
var t = now();
if (Stage != null && !ended) StageSeconds[Stage] = StageSeconds.GetValueOrDefault(Stage) + (t - StageStarted).TotalSeconds;
var was = Stage;
Stage = s.Id;
StageStarted = t;
StageProgress = 0;
if (s.Id != "compile") EtaSeconds = null;
StageChanged?.Invoke(was, s.Id);
break;
case BuildEvent.Progress p: StageProgress = p.Fraction; break;
case BuildEvent.Plan p: Plan = (p.Changed, p.Total); break;
case BuildEvent.FileBuilt f when f.How is "new" or "again": FilesBuilt++; break;
case BuildEvent.Eta x: EtaSeconds = x.Seconds; break;
case BuildEvent.Times x: Times = x; break;
case BuildEvent.Done d: DoneFolder = d.Folder; break;
case BuildEvent.Error x: Error = x.Text; break;
case BuildEvent.NotPrepared n: NotPrepared = n.Line; break;
case BuildEvent.StageTimes st: ReportedSteps = st.Seconds; break;
}
Event?.Invoke(e);
return e;
}
/// <summary>The last stage's time, at the build's end.</summary>
public void End()
{
if (Stage != null && !ended) StageSeconds[Stage] = StageSeconds.GetValueOrDefault(Stage) + (now() - StageStarted).TotalSeconds;
ended = true;
}
bool ended;
public TimeSpan Elapsed => now() - Started;
public TimeSpan InStage => Stage == null ? TimeSpan.Zero : now() - StageStarted;
}

View File

@@ -0,0 +1,274 @@
// nx2/make_game.py as a child process (old Setup.cs Runner, Shell.cs Run/Line/Ended; hookshot_builder.py start_make/on_exit):
// <python> -I <tools>\nx2\make_game.py <args> cwd <tools>, stdout+stderr UTF-8, below-normal priority (Windows)
// env: PYTHONIOENCODING=utf-8, HOOKSHOT_BUILDER_EXE (written into the game folder's builder= line), HOOKSHOT_NGX_SDK when
// DLSS is on, EMSDK / RX_CPM / WGPU_NATIVE / HOOKSHOT_MINGW for tool folders that exist (Windows); Linux: DLSS on drops
// HOOKSHOT_VK_PREBUILT (the Vulkan back end is compiled with NGX), DLSS off drops HOOKSHOT_NGX_SDK
// Success is the @@done line (and the game's program in that folder), never the exit code alone. Stop kills the whole tree.
using System.Diagnostics;
using System.Text;
namespace Triforce.Logic;
/// <summary>What to build: a new game folder from a source, or an existing folder again (an update, a mods rebuild).</summary>
public sealed record BuildRequest
{
public GameSource? Source { get; init; }
public string? OutFolder { get; init; }
/// <summary>A language code (Languages); an update keeps the folder's own (make_game reads its language.txt).</summary>
public string Language { get; init; } = "en-US";
public string? UpdateFolder { get; init; }
/// <summary>NVIDIA DLSS: null decides as the old Setup did (the setting, or a folder built with it, Dlss.ForBuild).</summary>
public bool? Dlss { get; init; }
/// <summary>Linux "Prepare shaders now" (unticked: --no-prepare-shaders). Windows always prepares them.</summary>
public bool PrepareShaders { get; init; } = true;
public IReadOnlyList<string> ExtraArgs { get; init; } = Array.Empty<string>();
public static BuildRequest New(GameSource source, string outFolder, string language) => new() { Source = source, OutFolder = outFolder, Language = language };
public static BuildRequest Update(string folder) => new() { UpdateFolder = folder };
public bool IsUpdate => UpdateFolder != null;
}
public sealed class BuildResult
{
public bool Success { get; init; }
public bool Cancelled { get; init; }
/// <summary>The game folder @@done named.</summary>
public string? Folder { get; init; }
/// <summary>Why it stopped, in one sentence: @@error's text, or the runner's own.</summary>
public string? Failure { get; init; }
public int? ExitCode { get; init; }
public TimeSpan Elapsed { get; init; }
/// <summary>The stage it was in when it stopped.</summary>
public string? StoppedAt { get; init; }
/// <summary>"make_game.py &lt;args&gt; (tools &lt;root&gt;)" for the log and the report.</summary>
public string? CommandLine { get; init; }
public string? OutFolder { get; init; }
public BuildEvent.Times? Times { get; init; }
/// <summary>The shaders were left to the game's first start (the line that said so).</summary>
public string? ShadersNotPrepared { get; init; }
public string? LogFile { get; init; }
}
public sealed class BuildRunner
{
readonly TriforceContext ctx;
Process? process;
volatile bool cancelled;
readonly StringBuilder details = new();
readonly object gate = new();
public BuildRunner(TriforceContext ctx) { this.ctx = ctx; }
/// <summary>Every line make_game prints, as it comes (stdout and stderr).</summary>
public event Action<string>? Line;
/// <summary>Every line as a typed event.</summary>
public event Action<BuildEvent>? Event;
/// <summary>A stage began (the one before ended): the App moves its rows and plays the step sound.</summary>
public event Action<string?, string>? StageChanged;
/// <summary>The build's state while it runs (stage, progress, eta...).</summary>
public BuildTracker Tracker { get; private set; } = new();
/// <summary>The time estimate this build started with (BuildEstimator), for BuildClock.Remaining.</summary>
public BuildEstimate? Estimate { get; private set; }
public bool Running => process is { HasExited: false };
/// <summary>All the lines so far but @@progress (the details drawer; the report's "build's last lines"). Cut in half past
/// 4000 lines, as the old details box was.</summary>
public string Details { get { lock (gate) return details.ToString(); } }
/// <summary>Every line of the build, @@progress too, in &lt;work&gt;\launcher-build.log (the one before as launcher-build.old.log):
/// the build's own words kept for a bug report (research/12 4.5 asked for it; the old Setup kept them in memory only).</summary>
public string LogFile => Path.Combine(ctx.WorkFolder, "launcher-build.log");
/// <summary>make_game.py's arguments for a request (old Shell.cs StartBuild/Run): the source, --out, --language; or --update;
/// --no-prepare-shaders (Linux, unticked); --fetch-ngx-sdk --accept-nvidia-license with DLSS; --fsr unless --fsr/--no-fsr is
/// there (Windows: FSR 4 in every build); in a test run --work &lt;root&gt;\build.</summary>
public static List<string> Arguments(TriforceContext ctx, BuildRequest r, bool dlss)
{
var a = new List<string>();
if (r.UpdateFolder != null) a.AddRange(new[] { "--update", r.UpdateFolder });
else
{
if (r.Source == null || r.OutFolder == null) throw new ArgumentException("A new build needs the game files and a folder.");
a.AddRange(r.Source.Args);
a.AddRange(new[] { "--out", r.OutFolder, "--language", Languages.Find(r.Language)?.Code ?? "en-US" });
}
a.AddRange(r.ExtraArgs);
if (!r.PrepareShaders && !ctx.IsWindows && !a.Contains("--no-prepare-shaders")) a.Add("--no-prepare-shaders");
if (dlss && !a.Contains("--fetch-ngx-sdk")) a.AddRange(new[] { "--fetch-ngx-sdk", "--accept-nvidia-license" });
// (11-15: AMD FSR 4 in every build: its code built in and AMD's two signed libraries fetched beside the game: make_game --fsr)
if (ctx.IsWindows && !a.Contains("--fsr") && !a.Contains("--no-fsr")) a.Add("--fsr");
// (a test run works in its own build folder, the one its uninstall removes. Left to itself make_game.py works in the PC's
// real one and begins by stopping whatever build runs there)
if (ctx.TestRoot != null && !a.Contains("--work")) a.AddRange(new[] { "--work", Path.Combine(ctx.TestRoot, "build") });
return a;
}
/// <summary>The process: python -I make_game.py args, in the tools folder, with the build's environment.</summary>
public static ProcessStartInfo StartInfo(TriforceContext ctx, ToolSet tools, IEnumerable<string> args, string? ngxSdk, bool dlss)
{
var si = new ProcessStartInfo(tools.Python)
{
WorkingDirectory = tools.Root, UseShellExecute = false, CreateNoWindow = true,
RedirectStandardOutput = true, RedirectStandardError = true, RedirectStandardInput = true,
StandardOutputEncoding = Encoding.UTF8, StandardErrorEncoding = Encoding.UTF8,
};
si.ArgumentList.Add("-I");
si.ArgumentList.Add(tools.MakeGame);
foreach (var a in args) si.ArgumentList.Add(a);
si.Environment["PYTHONIOENCODING"] = "utf-8";
// (written into the game folder: the game starts the launcher again from here to update, host/hos_update.c)
if (ctx.IsWindows || string.IsNullOrEmpty(si.Environment.TryGetValue("HOOKSHOT_BUILDER_EXE", out var b) ? b : null))
si.Environment["HOOKSHOT_BUILDER_EXE"] = ctx.BuilderExe;
if (dlss && ngxSdk != null) si.Environment["HOOKSHOT_NGX_SDK"] = ngxSdk;
if (ctx.IsWindows)
{
foreach (var (name, folder) in new[] { ("EMSDK", "emsdk"), ("RX_CPM", "cpm"), ("WGPU_NATIVE", "wgpu-native"), ("HOOKSHOT_MINGW", "llvm-mingw") })
{
var path = Path.Combine(tools.Root, folder);
if (Directory.Exists(path)) si.Environment[name] = path;
}
}
else if (dlss) si.Environment.Remove("HOOKSHOT_VK_PREBUILT"); // (build.py compiles hookshot_vk.so with NGX)
else si.Environment.Remove("HOOKSHOT_NGX_SDK"); // (one set in the shell is not passed on when unticked)
return si;
}
/// <summary>Runs the build to its end. Never throws for the build's own failures: the result says what happened.</summary>
public async Task<BuildResult> RunAsync(BuildRequest request, CancellationToken token = default)
{
var tools = new Tools(ctx).Find();
var sw = Stopwatch.StartNew();
if (tools == null) return new BuildResult { Failure = "The Triforce tools are not here: start Triforce again to download them.", Elapsed = sw.Elapsed };
// (a test run builds and updates game folders only inside its own folder)
var target = request.UpdateFolder ?? request.OutFolder;
if (target != null && !ctx.Allowed(target)) return new BuildResult { Failure = ctx.TestOnly, Elapsed = sw.Elapsed, OutFolder = target };
var dlss = request.Dlss ?? new Dlss(ctx).ForBuild(request.UpdateFolder);
var args = Arguments(ctx, request, dlss);
var line = "make_game.py " + string.Join(" ", args) + " (tools " + tools.Root + ")";
ctx.Log.Write("build: " + line);
var history = BuildHistory.Load(ctx.WorkFolder);
Estimate = BuildEstimator.Estimate(history, request.IsUpdate, Environment.ProcessorCount, Gpus.Main()?.Vendor, shaders: ctx.IsWindows || request.PrepareShaders);
Tracker = new BuildTracker();
cancelled = false;
lock (gate) { details.Clear(); detailLines = 0; }
StreamWriter? log = null;
try
{
Directory.CreateDirectory(ctx.WorkFolder);
if (File.Exists(LogFile)) File.Move(LogFile, Path.Combine(ctx.WorkFolder, "launcher-build.old.log"), true);
log = new StreamWriter(LogFile, false, new UTF8Encoding(false)) { AutoFlush = true };
log.WriteLine("# " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + line);
}
catch (Exception) { log = null; }
// (stdout and stderr come on two threads: the state is changed under the lock, the App's handlers run outside it)
void Take(string l)
{
BuildEvent e;
string? was;
lock (gate)
{
try { log?.WriteLine(l); } catch (Exception) { }
was = Tracker.Stage;
e = Tracker.Feed(l);
if (!l.StartsWith("@@progress", StringComparison.Ordinal))
{
details.Append(l).Append('\n');
if (++detailLines > 4000)
{
details.Remove(0, details.Length / 2);
detailLines = CountLines();
}
}
}
if (e is BuildEvent.Stage s) StageChanged?.Invoke(was, s.Id);
Event?.Invoke(e);
Line?.Invoke(l);
}
int? code = null;
string? failure = null;
try
{
var si = StartInfo(ctx, tools, args, ctx.NgxSdkFolder, dlss);
using var p = ctx.Platform.StartTree(si);
process = p;
try { if (ctx.IsWindows) p.PriorityClass = ProcessPriorityClass.BelowNormal; } catch (Exception) { }
try { p.StandardInput.Close(); } catch (Exception) { }
p.ErrorDataReceived += (_, e) => { if (e.Data != null) Take(e.Data); };
p.BeginErrorReadLine();
using (token.Register(Stop))
{
for (string? l; (l = await p.StandardOutput.ReadLineAsync(CancellationToken.None).ConfigureAwait(false)) != null;) Take(l);
await p.WaitForExitAsync(CancellationToken.None).ConfigureAwait(false);
p.WaitForExit(); // (the stderr reader drains)
}
code = p.ExitCode;
}
catch (Exception e) { failure = "The build tools could not start: " + e.Message; }
finally
{
process = null;
try { log?.Dispose(); } catch (Exception) { }
}
Tracker.End();
sw.Stop();
var done = Tracker.DoneFolder;
var ok = failure == null && !cancelled && done is { Length: > 0 } && new Game(ctx).FindExe(done) != null;
if (!ok && failure == null)
failure = cancelled ? "cancelled"
: done is { Length: > 0 } ? "The build said it finished, but there is no game in " + done + "."
: code == 0 ? "The build stopped."
: Tracker.Error ?? "The build tools stopped without saying why. See the details.";
var result = new BuildResult
{
Success = ok, Cancelled = cancelled, Folder = ok ? done : null, Failure = ok ? null : failure, ExitCode = code, Elapsed = sw.Elapsed,
StoppedAt = ok ? null : Tracker.Stage, CommandLine = line, OutFolder = target, Times = Tracker.Times, ShadersNotPrepared = Tracker.NotPrepared,
LogFile = log != null ? LogFile : null,
};
ctx.Log.Write("build ended: " + (ok ? "done, " + done : failure) + " after " + BuildClock.Span(sw.Elapsed));
if (ok) Finished(done!, request);
return result;
}
int detailLines;
int CountLines()
{
var n = 0;
for (var i = 0; i < details.Length; i++) if (details[i] == '\n') n++;
return n;
}
/// <summary>A build that ended well (old Shell.cs Ended): GameFolder = the @@done folder, the mods' code marked as built, the
/// stage times kept, Linux's menu entry written, and a notification (the App shows it when its window is not in front).</summary>
void Finished(string folder, BuildRequest request)
{
ctx.Settings.GameFolder = folder;
try { new ModManager(ctx).MarkBuilt(folder); } catch (Exception e) { ctx.Log.Write("mods: the build's mark could not be kept: " + e.Message); }
var stages = Tracker.StageSeconds.Count > 0 ? (IReadOnlyDictionary<string, double>)Tracker.StageSeconds : Tracker.ReportedSteps;
if (stages != null && stages.Count > 0) BuildHistory.RecordStages(ctx.WorkFolder, request.IsUpdate ? "update" : "build", stages);
if (!ctx.IsWindows)
{
try
{
var crest = new Tools(ctx).Find() is { } t ? Path.Combine(t.Root, "assets", "triforce.png") : null;
new Game(ctx).AddToMenu(folder, crest);
}
catch (Exception e) { ctx.Log.Write("menu entry: " + e.Message); }
}
ctx.Platform.Notify("Your game is ready", request.IsUpdate ? "The update is done. Press Play." : "Triforce built your game. Press Play.");
}
/// <summary>Stop: the whole tree killed (taskkill /T /F; Linux the process group), waiting up to 5 s. The result says "cancelled".
/// What is done is kept: a new build into the same folder reuses the unpacked game files, the translation and the objects.</summary>
public void Stop()
{
cancelled = true;
var p = process;
if (p != null) ctx.Platform.KillTree(p);
}
}

View File

@@ -0,0 +1,196 @@
// How long a build takes on this PC (DESIGN 3 rule 4: "a wait never shows without a time": this PC's own last build, or, with
// none, a stated range). make_game.py keeps <work>\build_times.json (format 1: the last 20 builds' totals and compile times,
// each module's seconds; nx2/make_game.py:203-256): read here, never written (make_game rewrites it whole and drops what it
// does not know). The time of each stage is not in it: the launcher keeps its own record beside it, launcher_stages.json,
// from the @@stage lines' times of its builds that ended well (make_game's own "the build's steps:" line when it said one).
using System.Text.Json;
using System.Text.Json.Nodes;
namespace Triforce.Logic;
public sealed record PastBuild(DateTime When, string Kind, int Modules, int Compiled, double CompileSeconds, double TotalSeconds);
public sealed record PastStages(DateTime When, string Kind, IReadOnlyDictionary<string, double> Seconds);
public sealed record StageEstimate(string Stage, TimeSpan Low, TimeSpan High, bool Measured)
{
public TimeSpan Mid => (Low + High) / 2;
}
public sealed record BuildEstimate(IReadOnlyList<StageEstimate> Stages, bool Measured)
{
public TimeSpan Low => Stages.Aggregate(TimeSpan.Zero, (a, s) => a + s.Low);
public TimeSpan High => Stages.Aggregate(TimeSpan.Zero, (a, s) => a + s.High);
public StageEstimate? Of(string stage) => Stages.FirstOrDefault(s => s.Stage == stage);
/// <summary>"About 45 minutes" (measured) or "40 to 70 minutes" (a range).</summary>
public string Text => BuildClock.About(Low, High);
}
public sealed class BuildHistory
{
public const string TimesFile = "build_times.json", StagesFile = "launcher_stages.json";
const int Keep = 20;
public IReadOnlyList<PastBuild> Builds { get; init; } = Array.Empty<PastBuild>();
public IReadOnlyList<PastStages> Stages { get; init; } = Array.Empty<PastStages>();
/// <summary>Both records of a work folder; empty ones when missing, half written, or of another format.</summary>
public static BuildHistory Load(string work)
{
var builds = new List<PastBuild>();
try
{
var root = JsonNode.Parse(File.ReadAllText(Path.Combine(work, TimesFile)));
if (root?["format"]?.GetValue<int>() == 1 && root["builds"] is JsonArray list)
foreach (var b in list.OfType<JsonObject>())
{
if (b["total_seconds"] is not JsonValue tv || !tv.TryGetValue<double>(out var total)) continue;
DateTime.TryParse(b["when"]?.GetValue<string>(), out var when);
builds.Add(new PastBuild(when, b["kind"]?.GetValue<string>() ?? "build", Int(b["modules"]), Int(b["compiled"]), Dbl(b["compile_seconds"]), total));
}
}
catch (Exception) { }
var stages = new List<PastStages>();
try
{
var root = JsonNode.Parse(File.ReadAllText(Path.Combine(work, StagesFile)));
if (root?["format"]?.GetValue<int>() == 1 && root["builds"] is JsonArray list)
foreach (var b in list.OfType<JsonObject>())
{
if (b["seconds"] is not JsonObject secs) continue;
DateTime.TryParse(b["when"]?.GetValue<string>(), out var when);
stages.Add(new PastStages(when, b["kind"]?.GetValue<string>() ?? "build",
secs.Where(kv => kv.Value is JsonValue).ToDictionary(kv => kv.Key, kv => Dbl(kv.Value))));
}
}
catch (Exception) { }
return new BuildHistory { Builds = builds, Stages = stages };
}
static int Int(JsonNode? n) { try { return n?.GetValue<int>() ?? 0; } catch (Exception) { try { return (int)(n?.GetValue<double>() ?? 0); } catch (Exception) { return 0; } } }
static double Dbl(JsonNode? n) { try { return n?.GetValue<double>() ?? 0; } catch (Exception) { try { return n?.GetValue<int>() ?? 0; } catch (Exception) { return 0; } } }
/// <summary>One build's stage times, kept (the last 20), written whole through a .new file.</summary>
public static void RecordStages(string work, string kind, IReadOnlyDictionary<string, double> seconds)
{
try
{
var file = Path.Combine(work, StagesFile);
JsonArray list = new();
try
{
var old = JsonNode.Parse(File.ReadAllText(file));
if (old?["format"]?.GetValue<int>() == 1 && old["builds"] is JsonArray l) list = (JsonArray)l.DeepClone();
}
catch (Exception) { }
var secs = new JsonObject();
foreach (var (k, v) in seconds) secs[k] = Math.Round(v, 1);
list.Add(new JsonObject { ["when"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ["kind"] = kind, ["seconds"] = secs });
while (list.Count > Keep) list.RemoveAt(0);
Directory.CreateDirectory(work);
File.WriteAllText(file + ".new", new JsonObject { ["format"] = 1, ["builds"] = list }.ToJsonString(new JsonSerializerOptions { WriteIndented = true }) + "\n");
File.Move(file + ".new", file, true);
}
catch (Exception) { }
}
}
public static class BuildEstimator
{
/// <summary>A full build's range (read to finish, without the shaders) by the CPU's threads, for a PC with no build behind it.
/// A STATED RANGE, not a measurement (DESIGN 3 rule 4): from the "about 45 minutes" the old Setup told everyone, widened by the
/// CPU. Tune once players' build_times.json are seen.</summary>
public static (TimeSpan Low, TimeSpan High) FullBuildRange(int threads) => threads switch
{
>= 16 => (TimeSpan.FromMinutes(25), TimeSpan.FromMinutes(45)),
>= 12 => (TimeSpan.FromMinutes(35), TimeSpan.FromMinutes(60)),
>= 8 => (TimeSpan.FromMinutes(45), TimeSpan.FromMinutes(75)),
>= 6 => (TimeSpan.FromMinutes(60), TimeSpan.FromMinutes(100)),
_ => (TimeSpan.FromMinutes(80), TimeSpan.FromMinutes(150)),
};
/// <summary>The shader step by the graphics card, stated (research/02 3.2: "an hour or more on AMD GPUs"; DESIGN 6: "1 to 2 hours
/// on an RX 6800 XT", "About 13 min on an RTX 4070").</summary>
public static (TimeSpan Low, TimeSpan High) ShaderRange(GpuVendor? gpu) => gpu switch
{
GpuVendor.Nvidia => (TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(20)),
GpuVendor.Amd => (TimeSpan.FromMinutes(30), TimeSpan.FromMinutes(120)),
_ => (TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(40)),
};
/// <summary>Each stage's time for a build (update: --update of a folder, mods rebuilds too). This PC's last build of that kind
/// (its stage record; else build_times.json's total and compile, the rest split by the stages' weights), else the stated
/// ranges. shaders: whether the shader step runs (Windows prepares them in every build; Linux unless unticked).</summary>
public static BuildEstimate Estimate(BuildHistory history, bool update, int threads, GpuVendor? gpu, bool shaders = true)
{
var kind = update ? "update" : "build";
var list = new List<StageEstimate>();
var stages = history.Stages.LastOrDefault(s => s.Kind == kind) ?? (update ? null : history.Stages.LastOrDefault());
var past = history.Builds.LastOrDefault(b => b.Kind == kind) ?? (update ? null : history.Builds.LastOrDefault());
var (low, high) = FullBuildRange(threads);
if (update) low = TimeSpan.FromMinutes(5); // (an update compiles what changed; a new translation stamp makes it a whole compile)
var measured = stages != null || past != null;
foreach (var st in BuildStages.All)
{
if (st.Id == "shaders")
{
if (!shaders) continue;
if (stages != null && stages.Seconds.TryGetValue("shaders", out var ss) && ss > 0) { list.Add(Exact(st.Id, ss)); continue; }
var (sl, sh) = ShaderRange(gpu);
list.Add(new StageEstimate(st.Id, sl, sh, false));
continue;
}
if (stages != null && stages.Seconds.TryGetValue(st.Id, out var s)) { list.Add(Exact(st.Id, s)); continue; }
if (past != null)
{
var rest = Math.Max(0, past.TotalSeconds - past.CompileSeconds);
var secs = st.Id == "compile" ? past.CompileSeconds : rest * st.Weight / (1 - BuildStages.Find("compile")!.Weight);
list.Add(Exact(st.Id, secs));
continue;
}
list.Add(new StageEstimate(st.Id, TimeSpan.FromSeconds(Math.Round(low.TotalSeconds * st.Weight)), TimeSpan.FromSeconds(Math.Round(high.TotalSeconds * st.Weight)), false));
}
return new BuildEstimate(list, measured);
}
static StageEstimate Exact(string id, double seconds) => new(id, TimeSpan.FromSeconds(Math.Round(seconds)), TimeSpan.FromSeconds(Math.Round(seconds)), true);
}
/// <summary>Times in words, and the time left while a build runs.</summary>
public static class BuildClock
{
/// <summary>The old Shell.cs Span: "1 h 5 min", "12 min", "40 s".</summary>
public static string Span(TimeSpan t) => t.TotalHours >= 1 ? (int)t.TotalHours + " h " + t.Minutes + " min" : t.TotalMinutes >= 1 ? t.Minutes + " min" : t.Seconds + " s";
/// <summary>"About 45 minutes" for one time, "40 to 70 minutes" (or "1 to 2 hours") for a range.</summary>
public static string About(TimeSpan low, TimeSpan high)
{
static string M(TimeSpan t) => t.TotalMinutes < 90 ? Math.Max(1, (int)Math.Round(t.TotalMinutes)) + "" : Math.Round(t.TotalHours, 1).ToString(System.Globalization.CultureInfo.InvariantCulture);
static string Unit(TimeSpan t) => t.TotalMinutes < 90 ? "minutes" : "hours";
if (Math.Abs((high - low).TotalMinutes) < 2) return "About " + M(high) + " " + Unit(high);
return Unit(low) == Unit(high) ? M(low) + " to " + M(high) + " " + Unit(high) : M(low) + " " + Unit(low) + " to " + M(high) + " " + Unit(high);
}
/// <summary>The time left: the stages after this one by the estimate, and this one by @@eta (compile), else by its progress so
/// far (from 5 %), else its estimate less what it has run. Never below zero; "Almost done" is the App's words under a minute.</summary>
public static TimeSpan Remaining(BuildEstimate estimate, BuildTracker tracker)
{
var at = tracker.Stage is { } s ? BuildStages.IndexOf(s) : -1;
var left = TimeSpan.Zero;
foreach (var st in estimate.Stages)
if (BuildStages.IndexOf(st.Stage) > at) left += st.Mid;
if (at >= 0 && estimate.Of(tracker.Stage!) is { } cur)
{
TimeSpan here;
if (tracker.Stage == "compile" && tracker.EtaSeconds is { } eta) here = TimeSpan.FromSeconds(eta);
else if (tracker.StageProgress >= 0.05) here = tracker.InStage * ((1 - tracker.StageProgress) / tracker.StageProgress);
else here = cur.Mid - tracker.InStage;
left += here < TimeSpan.Zero ? TimeSpan.Zero : here;
}
return left;
}
/// <summary>"About 31 minutes left"; "Almost done" under a minute (DESIGN 6 Build).</summary>
public static string LeftText(TimeSpan left) =>
left.TotalSeconds < 60 ? "Almost done" : left.TotalMinutes < 90 ? "About " + (int)Math.Round(left.TotalMinutes) + " minutes left" : "About " + (int)left.TotalHours + " h " + left.Minutes + " min left";
}