Files
Triforce-Source/setup/Setup.cs
ruckus7382sfgsgdf fb26495475 Triforce v0.5.2
The new launcher (Windows and Linux: the game's own look, music and settings, saves and mods pages, smaller updates in parts), benchmark mode (Find my best settings: the game tours four places by itself and writes its results), Quit and Restart in the pause menu under Mods, Squadala! in the Mods menu, battle music above 60 fps, menu text, the controller in the menus with the game in the background, the whole GameBanana list, RX 580 and RX 5000/6000 shader fixes, the pipeline cache kept through an update, async present black flashes, and faster builds.
2026-11-19 10:52:50 -06:00

887 lines
49 KiB
C#

// Triforce Setup: the one file players download. It installs itself (per user, no admin), keeps itself and the Triforce tools up to
// date from the releases at triforceproject.com (triforce/downloads on its Forgejo), turns the player's own game files into a game folder (nx2/make_game.py from
// the tools) and starts the game. The game's own update offer (host/hos_update.c) starts it again with --update <folder>.
//
// Built for .NET Framework 4.8 (part of Windows 10 and 11): nothing to install, one small exe. setup/build.ps1 compiles it.
// Release assets it looks for: TriforceSetup.exe (this app) and Triforce-Tools.zip (a "tools" folder: nx2, host, gpu, python, ...).
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Microsoft.Win32;
namespace TriforceSetup;
static class Program
{
// (11-13: Triforce's own home. Releases are published in triforce/downloads on its Forgejo; the list is asked on the main
// domain, which passes /api/v1 on, so the address outlives whatever answers behind it. Forgejo's list has GitHub's
// names (tag_name, assets[].name / browser_download_url / size) and takes limit= where GitHub took per_page=.
// Up to v0.3.5 this was api.github.com/repos/ruckus7382sfgsgdf/CBeta0.2: those builds ask there and nowhere else)
public const string Home = "https://triforceproject.com";
public const string ReleaseApi = Home + "/api/v1/repos/triforce/downloads/releases?limit=1";
public const string SetupAsset = "TriforceSetup.exe";
public const string ToolsAsset = "Triforce-Tools.zip";
// ((Triforce, 11-18) the tools in two parts (setup/pack_tools.ps1): the base, which does not change from one release to the
// next (the compiler, Python, the libraries: 95 % of the zip), and the code, with the base's list (Tools.BaseList). An update
// fetches the code and keeps the base it has: 12 MB instead of 284. Triforce-Tools.zip stays for older Setups and a first install)
public const string ToolsCodeAsset = "Triforce-Tools-code.zip";
public const string ToolsBaseAsset = "Triforce-Tools-base.zip";
public const string AppName = "Triforce";
[STAThread]
static int Main(string[] args)
{
// ((Triforce) Long paths. The manifest's longPathAware (build.ps1) is not enough for .NET Framework's own path checks: an
// exe that csc builds has no TargetFramework attribute, is taken as written for 4.0, and keeps the legacy path handling
// that refuses 260 characters and more (PathTooLongException, in a test with Windows' LongPathsEnabled on). These two
// switches are what 4.6.2 and newer default to; they must be set before any path is looked at (Install's fields below:
// System.IO keeps the first value it reads). TRIFORCE_LEGACY_PATHS=1: as before)
if (Environment.GetEnvironmentVariable("TRIFORCE_LEGACY_PATHS") != "1")
{
AppContext.SetSwitch("Switch.System.IO.UseLegacyPathHandling", false);
AppContext.SetSwitch("Switch.System.IO.BlockLongPaths", false);
}
try { ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; } catch (Exception) { }
Install.NoWrites = args.Contains("--snapshot");
// (one Setup at a time for an install, Guard.cs Instance: the folder Setup works in names the lock)
var home = Install.InstalledDir ?? Install.SelfDir;
if (args.Contains("--uninstall"))
{
// (not under a Setup that is open: its tools and its build's work folder would go while it uses them)
if (!Instance.Take(home, Install.TestRoot))
{
Log("not uninstalled: Triforce Setup is open for " + home);
Instance.CallForward(home, Install.TestRoot);
if (!args.Contains("--quiet"))
MessageBox.Show("Triforce Setup is open. Close it, then uninstall again.", "Uninstall Triforce", MessageBoxButton.OK, MessageBoxImage.Information);
return 1;
}
return Install.Uninstall(args.Contains("--quiet")) ? 0 : 1;
}
// (silent install: --install-to <folder> [--no-desktop] [--no-startmenu]; the installed Setup then starts as usual)
var to = Array.IndexOf(args, "--install-to");
if (to >= 0 && to + 1 < args.Length)
{
var exe = Install.Run(Path.GetFullPath(args[to + 1]), !args.Contains("--no-desktop"), !args.Contains("--no-startmenu"));
Log("installed " + Version + " to " + Path.GetDirectoryName(exe) + " (silent)");
Process.Start(new ProcessStartInfo(exe) { UseShellExecute = false, WorkingDirectory = Path.GetDirectoryName(exe)! });
return 0;
}
if (args.Contains("--where")) { Log(Shell.Where()); return 0; }
// (testing: --release-test asks GitHub (or TRIFORCE_RELEASE_API) what Setup would, and writes what it found to setup.log)
if (args.Contains("--release-test"))
{
try
{
using var http = new HttpClient();
http.DefaultRequestHeaders.UserAgent.ParseAdd("TriforceSetup/" + Version);
var rel = Release.Latest(http, CancellationToken.None).Result;
Log("release-test: tag " + rel.Tag + ", setup " + (rel.SetupUrl ?? "MISSING") + " (" + rel.SetupSize + "), tools " + (rel.ToolsUrl ?? "MISSING") + " (" + rel.ToolsSize + ")");
return rel.SetupUrl != null && rel.ToolsUrl != null ? 0 : 1;
}
catch (Exception e) { Log("release-test failed: " + (e.InnerException ?? e).Message); return 2; }
}
// (testing: --convert-saves <from> <to> copies one save folder over another as the Convert saves page does)
var cv = Array.IndexOf(args, "--convert-saves");
if (cv >= 0 && cv + 2 < args.Length)
{
if (!Instance.Take(home, Install.TestRoot)) { Log("convert-saves: not done, Triforce Setup is open for " + home); return 1; }
var b = Saves.Copy(args[cv + 1], args[cv + 2], Path.Combine(Install.SelfDir, "save-backups"), "test");
Log("convert-saves " + args[cv + 1] + " -> " + args[cv + 2] + ", backup " + (b ?? "none"));
return 0;
}
// (testing: --find-saves writes every save of the game the Convert saves page would list to setup.log; --find-saves <folder>
// the save Choose a folder would take from that folder)
var fs = Array.IndexOf(args, "--find-saves");
if (fs >= 0)
{
if (fs + 1 < args.Length && !args[fs + 1].StartsWith("--"))
{
var set = Saves.FindIn(args[fs + 1]);
Log("find-saves " + args[fs + 1] + ": " + (set ?? "none"));
return set != null ? 0 : 1;
}
var sets = Saves.FindEmulatorSaves();
foreach (var set in sets) Log("find-saves: " + set.Name + ", " + set.Slots + " slot(s), " + set.Folder);
Log("find-saves: " + sets.Count + " found");
return 0;
}
// (testing: --check-game <.nup | exefs folder> writes the version check's answer to setup.log)
var cg = Array.IndexOf(args, "--check-game");
if (cg >= 0 && cg + 1 < args.Length)
{
var tools = Tools.Find();
if (tools == null) { Log("check-game: no tools"); return 2; }
var path = args[cg + 1];
var (ok, text) = Runner.Check(tools, new[] { "--check", path.EndsWith(".nup", StringComparison.OrdinalIgnoreCase) ? "--nup" : "--exefs", path }).Result;
Log("check-game " + path + ": " + (ok ? "OK " : "BAD ") + text + " (tools " + tools.Root + ", python " + tools.Python + ")");
return ok ? 0 : 1;
}
// (testing without a screen, --snapshot or TRIFORCE_UI_SCRIPT: a failure is written down, never shown in a message box)
var shots = Array.IndexOf(args, "--snapshot");
var unattended = shots >= 0 && shots + 1 < args.Length ? args[shots + 1]
: Environment.GetEnvironmentVariable("TRIFORCE_UI_SCRIPT") is { Length: > 0 } ? Environment.GetEnvironmentVariable("TRIFORCE_UI_SHOTS") ?? Install.SelfDir : null;
void Report(Exception e)
{
try { Directory.CreateDirectory(unattended!); File.AppendAllText(Path.Combine(unattended!, "error.txt"), e + Environment.NewLine); } catch (Exception) { }
}
// (a second start for the same install must not fetch, build, update or convert saves beside the first: it asks the
// first one's window to the front and ends. --snapshot only draws the pages, reading and writing nothing of an install)
if (shots < 0)
for (var tries = 0; !Instance.Take(home, Install.TestRoot); tries++)
{
if (Instance.CallForward(home, Install.TestRoot))
{
Log("Triforce Setup is already open for " + home + ": its window was asked to the front, and this start ends");
return 0;
}
// (nobody to ask: the first one is only just starting or closing. In a moment it can be asked, or the folder is free)
if (tries >= 30)
{
Log("Triforce Setup is already running for " + home + " and does not answer: this start ends");
if (unattended == null)
MessageBox.Show("Triforce Setup is already running, but its window could not be brought to the front.\n\nClose it (in Task Manager: TriforceSetup.exe) and start Triforce again.",
"Triforce Setup", MessageBoxButton.OK, MessageBoxImage.Information);
return 1;
}
Thread.Sleep(100);
}
var app = new Application { ShutdownMode = ShutdownMode.OnMainWindowClose };
app.DispatcherUnhandledException += (_, e) =>
{
e.Handled = true;
if (unattended != null) { Report(e.Exception); Environment.Exit(3); }
MessageBox.Show("Triforce Setup ran into a problem:\n\n" + e.Exception.Message, "Triforce Setup", MessageBoxButton.OK, MessageBoxImage.Error);
};
Shell w;
try { w = new Shell(args); }
catch (Exception e) when (unattended != null) { Report(e); return 3; }
Instance.Listen(() => w.Window.Dispatcher.BeginInvoke(new Action(w.ComeForward)));
w.Window.Closed += (_, _) => Instance.Release(); // (a start just after closing finds the folder free)
return app.Run(w.Window);
}
/// This build's release tag (setup/build.ps1 -Version), "dev" for a local build
public static string Version =>
typeof(Program).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "dev";
/// setup.log beside the installed Setup: what it checked, downloaded and ran (for bug reports)
public static void Log(string line)
{
if (Install.NoWrites) return;
try
{
var file = Path.Combine(Install.InstalledDir ?? Install.SelfDir, "setup.log");
if (File.Exists(file) && new FileInfo(file).Length > (2 << 20)) File.Delete(file);
File.AppendAllText(file, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + line + Environment.NewLine);
}
catch (Exception) { }
}
public static Stream Resource(string name) => typeof(Program).Assembly.GetManifestResourceStream(name)
?? throw new FileNotFoundException("missing resource " + name);
public static BitmapImage Image(string name)
{
var b = new BitmapImage();
b.BeginInit();
b.StreamSource = Resource(name);
b.CacheOption = BitmapCacheOption.OnLoad;
b.EndInit();
b.Freeze();
return b;
}
}
/// Where Triforce lives on this PC, and the pieces of a per-user install (shortcuts, the Apps list entry).
static class Install
{
const string UninstallKey = @"Software\Microsoft\Windows\CurrentVersion\Uninstall\Triforce";
const string SettingsKey = @"Software\Triforce";
/// (testing: TRIFORCE_TEST_ROOT=<folder> keeps a whole install inside that folder. What would go to the registry is a text
/// file there, the shortcuts go to its Desktop and Start Menu folders, the build's work folder is its build folder and emulator
/// saves are looked for in its Roaming folder. The PC's real install is neither read nor touched.)
public static readonly string? TestRoot = Environment.GetEnvironmentVariable("TRIFORCE_TEST_ROOT") is { Length: > 0 } t ? Path.GetFullPath(t) : null;
static string TestStore => Path.Combine(TestRoot!, "registry.txt");
static string? Read(string key, string name)
{
if (TestRoot == null)
{
using var k = Registry.CurrentUser.OpenSubKey(key);
return k?.GetValue(name) as string;
}
var start = key + "\\" + name + "=";
return File.Exists(TestStore) ? File.ReadAllLines(TestStore).LastOrDefault(l => l.StartsWith(start))?.Substring(start.Length) : null;
}
static void Write(string key, string name, object value)
{
if (NoWrites) return;
if (TestRoot == null)
{
using var k = Registry.CurrentUser.CreateSubKey(key);
if (value is int n) k.SetValue(name, n, RegistryValueKind.DWord); else k.SetValue(name, value);
return;
}
Directory.CreateDirectory(TestRoot);
var start = key + "\\" + name + "=";
var lines = File.Exists(TestStore) ? File.ReadAllLines(TestStore).Where(l => !l.StartsWith(start)).ToList() : new List<string>();
lines.Add(start + value);
File.WriteAllLines(TestStore, lines);
}
static void Remove(string key)
{
if (TestRoot == null) { try { Registry.CurrentUser.DeleteSubKeyTree(key, false); } catch (Exception) { } return; }
if (File.Exists(TestStore)) File.WriteAllLines(TestStore, File.ReadAllLines(TestStore).Where(l => !l.StartsWith(key + "\\")).ToList());
}
/// May this run write there? Always, except in a test run (TRIFORCE_TEST_ROOT), which installs, builds, copies saves and
/// starts games only inside its own folder, whatever folder is typed or picked: so a look at Setup cannot change the PC's
/// real install, real game or real saves
public static bool Allowed(string path)
{
if (TestRoot == null) return true;
try { return (Path.GetFullPath(path).TrimEnd('\\') + "\\").StartsWith(TestRoot.TrimEnd('\\') + "\\", StringComparison.OrdinalIgnoreCase); }
catch (Exception) { return false; }
}
public static string TestOnly => "This is a test run of Setup: it only writes inside its own folder, " + TestRoot + ".";
static string LocalData => TestRoot ?? Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
public static string DefaultDir => Path.Combine(LocalData, "Programs", "Triforce");
public static string Self => Assembly.GetExecutingAssembly().Location;
public static string SelfDir => Path.GetDirectoryName(Self)!;
/// The install folder from the Apps list entry, when Triforce is installed
public static string? InstalledDir
{
get
{
var d = Read(UninstallKey, "InstallLocation");
return d != null && File.Exists(Path.Combine(d, Program.SetupAsset)) ? d : null;
}
}
public static bool RunningInstalled =>
InstalledDir is { } d && string.Equals(Path.GetFullPath(d).TrimEnd('\\'), Path.GetFullPath(SelfDir).TrimEnd('\\'), StringComparison.OrdinalIgnoreCase);
// ---- small settings (HKCU\Software\Triforce) ----
public static string? Get(string name) => Read(SettingsKey, name);
/// (--snapshot: pictures of the pages only; nothing is written to the registry or to setup.log)
public static bool NoWrites;
public static void Set(string name, string value) => Write(SettingsKey, name, value);
/// Copies this exe into the folder, registers it in the Apps list and makes the shortcuts. Returns the installed exe.
public static string Run(string dir, bool desktop, bool startMenu)
{
if (!Allowed(dir)) throw new InvalidOperationException(TestOnly);
Directory.CreateDirectory(dir);
var exe = Path.Combine(dir, Program.SetupAsset);
// ((Triforce) through <exe>.partial, renamed into place once complete: a copy cut off in the middle left a Setup that does
// not start under the name every shortcut starts. Guard.cs Partial)
if (!string.Equals(Path.GetFullPath(exe), Path.GetFullPath(Self), StringComparison.OrdinalIgnoreCase))
Partial.CopyFile(Self, exe);
Write(UninstallKey, "DisplayName", Program.AppName);
Write(UninstallKey, "DisplayIcon", exe + ",0");
Write(UninstallKey, "DisplayVersion", Program.Version);
Write(UninstallKey, "Publisher", "Triforce");
Write(UninstallKey, "InstallLocation", dir);
Write(UninstallKey, "UninstallString", "\"" + exe + "\" --uninstall");
Write(UninstallKey, "QuietUninstallString", "\"" + exe + "\" --uninstall --quiet");
Write(UninstallKey, "URLInfoAbout", Program.Home);
Write(UninstallKey, "NoModify", 1);
Write(UninstallKey, "NoRepair", 1);
Set("Desktop", desktop ? "1" : "0");
Set("StartMenu", startMenu ? "1" : "0");
// ("Triforce" opens Setup, not the game: every start goes through the update question first, then Play starts the game)
try
{
if (startMenu) Shortcut(Path.Combine(StartFolder, "Triforce.lnk"), exe, "", dir, exe, "Play Triforce");
if (desktop) Shortcut(DesktopLink, exe, "", dir, exe, "Play Triforce");
}
catch (Exception e) { Program.Log("shortcuts: " + e.Message); }
return exe;
}
static string StartFolder => Path.Combine(TestRoot != null ? Path.Combine(TestRoot, "Start Menu") : Environment.GetFolderPath(Environment.SpecialFolder.Programs), "Triforce");
static string DesktopLink => Path.Combine(TestRoot != null ? Path.Combine(TestRoot, "Desktop") : Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "Triforce.lnk");
static void Shortcut(string link, string target, string args, string workDir, string icon, string description)
{
Directory.CreateDirectory(Path.GetDirectoryName(link)!);
var type = Type.GetTypeFromProgID("WScript.Shell")!;
var shell = Activator.CreateInstance(type)!;
try
{
var lnk = type.InvokeMember("CreateShortcut", BindingFlags.InvokeMethod, null, shell, new object[] { link })!;
var t = lnk.GetType();
t.InvokeMember("TargetPath", BindingFlags.SetProperty, null, lnk, new object[] { target });
t.InvokeMember("Arguments", BindingFlags.SetProperty, null, lnk, new object[] { args });
t.InvokeMember("WorkingDirectory", BindingFlags.SetProperty, null, lnk, new object[] { workDir });
t.InvokeMember("IconLocation", BindingFlags.SetProperty, null, lnk, new object[] { icon + ",0" });
t.InvokeMember("Description", BindingFlags.SetProperty, null, lnk, new object[] { description });
t.InvokeMember("Save", BindingFlags.InvokeMethod, null, lnk, null);
Marshal.FinalReleaseComObject(lnk);
}
finally { Marshal.FinalReleaseComObject(shell); }
}
/// Removes Setup, its tools and the shortcuts. Game folders and saves are the player's and stay.
public static bool Uninstall(bool quiet)
{
var dir = InstalledDir ?? SelfDir;
if (!quiet && MessageBox.Show("Remove Triforce Setup and its tools from this PC?\n\nYour game folders and saves are not touched.",
"Uninstall Triforce", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes) return false;
foreach (var f in new[] { DesktopLink })
try { if (File.Exists(f)) File.Delete(f); } catch (Exception) { }
try { if (Directory.Exists(StartFolder)) Directory.Delete(StartFolder, true); } catch (Exception) { }
// (the build's work folder and compile cache, nx2/make_game.py --work: several GB, made again by a build)
var work = TestRoot != null ? Path.Combine(TestRoot, "build") : Path.Combine(LocalData, "Triforce", "build");
try { if (Directory.Exists(work)) Directory.Delete(work, true); } catch (Exception) { }
Remove(UninstallKey);
Remove(SettingsKey);
// (this exe is running from the folder: a short-lived cmd removes it once we are gone)
if (File.Exists(Path.Combine(dir, Program.SetupAsset)))
Process.Start(new ProcessStartInfo("cmd.exe", "/c timeout /t 2 /nobreak >nul & rmdir /s /q \"" + dir + "\"")
{ CreateNoWindow = true, UseShellExecute = false, WorkingDirectory = Path.GetTempPath() });
if (!quiet) MessageBox.Show("Triforce has been removed.", "Uninstall Triforce", MessageBoxButton.OK, MessageBoxImage.Information);
return true;
}
}
/// What a GitHub release offers
sealed class Release
{
public string Tag = "";
public string? SetupUrl, ToolsUrl;
public long SetupSize, ToolsSize;
public string? ToolsCodeUrl, ToolsBaseUrl; // ((Triforce) the tools in parts, when the release has them: Tools.UnpackParts)
public long ToolsCodeSize, ToolsBaseSize;
/// The latest release (TRIFORCE_RELEASE_API overrides the address, for testing against a local copy)
public static async Task<Release> Latest(HttpClient http, CancellationToken token)
{
var url = Environment.GetEnvironmentVariable("TRIFORCE_RELEASE_API") is { Length: > 0 } o ? o
: Program.ReleaseApi; // (the newest, pre-releases too)
using var ask = new CancellationTokenSource(TimeSpan.FromSeconds(20));
using var both = CancellationTokenSource.CreateLinkedTokenSource(token, ask.Token);
using var response = await http.GetAsync(url, both.Token);
if (response.StatusCode == HttpStatusCode.NotFound) throw new InvalidOperationException("There is no Triforce release yet.");
response.EnsureSuccessStatusCode();
var r = Parse(await response.Content.ReadAsStringAsync());
if (r.Tag.Length == 0) throw new InvalidOperationException("There is no Triforce release yet."); // (an empty list: nothing published)
return r;
}
/// The release list's answer (Forgejo's, GitHub's before: the same names), read with a real JSON parser: a list of releases (the newest first) or one release. Each asset's
/// object holds a nested "uploader" object, so nothing simpler than a parser reads it right (v0.1.0 found no assets)
public static Release Parse(string json)
{
var parsed = new System.Web.Script.Serialization.JavaScriptSerializer { MaxJsonLength = int.MaxValue }.DeserializeObject(json);
var release = parsed is object[] list ? (list.Length > 0 ? list[0] as Dictionary<string, object> : null) : parsed as Dictionary<string, object>;
var r = new Release();
if (release == null) return r;
r.Tag = release.TryGetValue("tag_name", out var tag) ? tag as string ?? "" : "";
if (release.TryGetValue("assets", out var assets) && assets is object[] files)
foreach (var f in files.OfType<Dictionary<string, object>>())
{
var name = f.TryGetValue("name", out var n) ? n as string : null;
var link = f.TryGetValue("browser_download_url", out var l) ? l as string : null;
var size = f.TryGetValue("size", out var s) && s != null ? Convert.ToInt64(s) : 0;
if (string.Equals(name, Program.SetupAsset, StringComparison.OrdinalIgnoreCase)) { r.SetupUrl = link; r.SetupSize = size; }
if (string.Equals(name, Program.ToolsAsset, StringComparison.OrdinalIgnoreCase)) { r.ToolsUrl = link; r.ToolsSize = size; }
if (string.Equals(name, Program.ToolsCodeAsset, StringComparison.OrdinalIgnoreCase)) { r.ToolsCodeUrl = link; r.ToolsCodeSize = size; }
if (string.Equals(name, Program.ToolsBaseAsset, StringComparison.OrdinalIgnoreCase)) { r.ToolsBaseUrl = link; r.ToolsBaseSize = size; }
}
return r;
}
// (its files are fetched by Guard.cs's Download: through a .part file a cut download goes on from, checked against the size listed here)
}
/// The Triforce tools beside the installed Setup: nx2/make_game.py and its own Python.
sealed class Tools
{
public readonly string Root, Python;
Tools(string root, string python) { Root = root; Python = python; }
public static string Folder => Path.Combine(Install.InstalledDir ?? Install.SelfDir, "tools");
/// The release these tools came from (tools/version.txt), null when there are none
public static string? InstalledVersion
{
get
{
var v = Path.Combine(Folder, "version.txt");
return File.Exists(v) && File.Exists(Path.Combine(Folder, "nx2", "make_game.py")) ? File.ReadAllText(v).Trim() : null;
}
}
public static Tools? Find()
{
var bundled = Folder;
if (File.Exists(Path.Combine(bundled, "nx2", "make_game.py")))
{
var py = Path.Combine(bundled, "python", "python.exe");
return new Tools(bundled, File.Exists(py) ? py : "python");
}
// (a development copy: Setup run from inside a checkout uses its sources and the PC's Python. Never in a test run,
// which has its own tools or none)
if (Install.TestRoot != null) return null;
for (var d = new DirectoryInfo(Install.SelfDir); d != null; d = d.Parent)
if (File.Exists(Path.Combine(d.FullName, "nx2", "make_game.py")))
return new Tools(d.FullName, Environment.GetEnvironmentVariable("TRIFORCE_PYTHON") ?? "python");
return null;
}
/// What an unpacking that was cut off left behind (Setup closed in the middle of it): a tools.part of up to a gigabyte that is
/// never used and stood in the way of the next try's room, and a tools.old whose removal failed
public static void Sweep()
{
try { if (Directory.Exists(Folder + ".part")) Directory.Delete(Folder + ".part", true); } catch (Exception) { }
// ((Triforce) Setup closed between the two moves of Unpack: the tools that were there are the rollback point, and go back)
try { if (!Directory.Exists(Folder) && Directory.Exists(Folder + ".old")) Directory.Move(Folder + ".old", Folder); } catch (Exception) { }
try { if (Directory.Exists(Folder) && Directory.Exists(Folder + ".old")) Directory.Delete(Folder + ".old", true); } catch (Exception) { }
}
/// Unpacks Triforce-Tools.zip as the tools folder (the old one replaced only once the new one is complete)
public static void Unpack(string zip, string tag, Action<double> progress)
{
var target = Folder;
var part = target + ".part";
if (Directory.Exists(part)) Directory.Delete(part, true);
try
{
using (var z = ZipFile.OpenRead(zip))
{
// (room for all of it, asked before the first file: the zip says what each one unpacks to, and 4 KB are counted for
// each on top, since a file takes whole clusters. The tools there now stay until the new ones are complete, so they
// make no room. Guard.cs Space)
var bytes = z.Entries.Sum(e => e.Length) + z.Entries.Count * 4096L;
if (Space.Problem("the Triforce tools", bytes, Space.Free(part), Space.Disk(part)) is { } none) throw new NoRoomException(none);
Directory.CreateDirectory(part);
// (the zip holds tools/... or the tools' contents at its root)
// (and zips packed on Windows may say tools\... instead)
var prefix = z.Entries.All(e => e.FullName.Replace('\\', '/').StartsWith("tools/", StringComparison.OrdinalIgnoreCase)) ? "tools/" : "";
// ((Triforce) every name is checked before the first file is written, as a mod's archive's are (Mods.Unsafe): a
// name Windows keeps for a device (NUL, COM1, con.txt), a colon (a hidden stream, a drive's letter), a name that
// ends in a dot or a space (Windows drops them: the file lands under another name), a path out of the folder.
// Such a zip is refused whole: a name was only skipped when it left the folder, and the others were written)
foreach (var e in z.Entries)
{
var name = e.FullName.Replace('\\', '/').Substring(prefix.Length);
if (name.Length > 0 && Mods.Unsafe(name, true) is { } why)
throw new InvalidDataException("The tools' zip holds " + why + " (\"" + (name.Length > 120 ? name.Substring(0, 117) + "..." : name) + "\"): it was not unpacked.");
}
var full = Path.GetFullPath(part) + Path.DirectorySeparatorChar;
for (var i = 0; i < z.Entries.Count; i++)
{
var e = z.Entries[i];
var name = e.FullName.Replace('\\', '/').Substring(prefix.Length);
if (name.Length == 0) continue;
var dest = Path.GetFullPath(Path.Combine(part, name));
if (!dest.StartsWith(full, StringComparison.OrdinalIgnoreCase)) continue; // (no paths out of the folder)
if (name.EndsWith("/")) { Directory.CreateDirectory(dest); continue; }
Directory.CreateDirectory(Path.GetDirectoryName(dest)!);
e.ExtractToFile(dest, true);
if ((i & 15) == 0) progress((double)i / z.Entries.Count);
}
}
File.WriteAllText(Path.Combine(part, "version.txt"), tag + "\n");
// ((Triforce) a whole zip's base is the one its list names: the next update keeps it without reading it again)
if (ReadBaseList(Path.Combine(part, BaseList)) is { } listed) File.WriteAllText(Path.Combine(part, BaseHave), listed.Id + "\n");
}
catch (Exception)
{
// (half a tools folder, a gigabyte of it, is of no use and stood in the way of the next try's room)
try { if (Directory.Exists(part)) Directory.Delete(part, true); } catch (Exception) { }
throw;
}
var old = target + ".old";
if (Directory.Exists(old)) Directory.Delete(old, true);
if (Directory.Exists(target)) Directory.Move(target, old);
try { Directory.Move(part, target); }
catch (Exception)
{
// ((Triforce) the tools that were there go back: a move that failed (a virus scanner holding the new folder) left no
// tools folder at all, and the next start found no tools)
try { if (!Directory.Exists(target) && Directory.Exists(old)) Directory.Move(old, target); } catch (Exception) { }
throw;
}
try { if (Directory.Exists(old)) Directory.Delete(old, true); } catch (Exception) { }
progress(1);
}
// ---- (Triforce, 11-18) the tools in two parts: the base kept from one release to the next ----
// setup/pack_tools.ps1 lists the base (the compiler, Python, the libraries: the folders that do not change between releases,
// 95 % of Triforce-Tools.zip) in tools/base.list, which the code part and the whole zip carry. An update of tools that hold
// that base fetches the code part alone (12 MB, not 284; at a release the download server's line was full and each player got
// 75 KB/s) and moves the base folders over from the tools there now. The base is the list's when every file it names is there
// with its size and SHA-256 (read once: base.have then remembers the list's id, and later updates check the sizes alone).
// Anything else (no tools, another base, a file changed or gone) fetches the base part as well.
public const string BaseList = "base.list", BaseHave = "base.have";
public sealed class Base
{
public string Id = ""; // (the SHA-256 of the list's dir and file lines)
public readonly System.Collections.Generic.List<string> Dirs = new();
public readonly System.Collections.Generic.List<(string Path, long Size, string Sha)> Files = new();
}
/// A base list's text: "triforce-tools-base 1", "id <sha256>", then "dir <name>" and "file <size> <sha256> <path>"
/// lines (tab-separated, paths with /). Null when it is not one
public static Base? ParseBaseList(string text)
{
var lines = text.Replace("\r", "").Split('\n');
if (lines.Length < 2 || lines[0].Trim() != "triforce-tools-base\t1") return null;
var b = new Base();
foreach (var line in lines.Skip(1))
{
var f = line.Split('\t');
if (f.Length == 2 && f[0] == "id") b.Id = f[1].Trim().ToLowerInvariant();
else if (f.Length == 2 && f[0] == "dir" && f[1].Length > 0 && Mods.Unsafe(f[1], true) == null && !f[1].Contains('/')) b.Dirs.Add(f[1]);
else if (f.Length == 4 && f[0] == "file" && long.TryParse(f[1], out var size) && f[2].Length == 64 && Mods.Unsafe(f[3], true) == null)
b.Files.Add((f[3], size, f[2].ToLowerInvariant()));
else if (line.Length > 0) return null; // (a line it does not know: another format, not guessed at)
}
return b.Id.Length == 64 && b.Dirs.Count > 0 && b.Files.Count > 0 ? b : null;
}
public static Base? ReadBaseList(string path)
{
try { return File.Exists(path) ? ParseBaseList(File.ReadAllText(path)) : null; } catch (Exception) { return null; }
}
/// The base list in a code (or whole) zip, null when it has none
public static Base? BaseListIn(string zip)
{
using var z = ZipFile.OpenRead(zip);
var e = z.Entries.FirstOrDefault(x => x.FullName.Replace('\\', '/') is var n && (n == "tools/" + BaseList || n == BaseList));
if (e == null) return null;
using var r = new StreamReader(e.Open());
return ParseBaseList(r.ReadToEnd());
}
/// Whether the tools in folder hold base b whole (progress: 0..1 while it reads the files). Remembers a match in base.have
public static bool HasBase(Base b, string folder, Action<double> progress)
{
foreach (var d in b.Dirs)
if (!Directory.Exists(Path.Combine(folder, d))) return false;
foreach (var f in b.Files) // (every file there, at its size: quick, and enough once base.have says the rest was read)
{
var fi = new FileInfo(Path.Combine(folder, f.Path));
if (!fi.Exists || fi.Length != f.Size) return false;
}
string? had = null;
try { had = File.Exists(Path.Combine(folder, BaseHave)) ? File.ReadAllText(Path.Combine(folder, BaseHave)).Trim() : null; } catch (Exception) { }
if (had == b.Id) return true;
long total = Math.Max(1, b.Files.Sum(f => f.Size)), done = 0;
using (var sha = System.Security.Cryptography.SHA256.Create())
foreach (var f in b.Files)
{
string got;
using (var s = File.OpenRead(Path.Combine(folder, f.Path))) got = string.Concat(sha.ComputeHash(s).Select(x => x.ToString("x2")));
if (got != f.Sha) return false;
done += f.Size;
progress((double)done / total);
}
try { File.WriteAllText(Path.Combine(folder, BaseHave), b.Id + "\n"); } catch (Exception) { }
return true;
}
/// The tools folder made again from a code zip and either a base zip (baseZip) or the base folders of the tools there now
/// (baseZip null: HasBase said they are b's). The old folder is replaced only once the new one is complete; what fails on
/// the way puts the base folders back where they were
public static void UnpackParts(string codeZip, string? baseZip, Base b, string tag, Action<double> progress)
{
var target = Folder;
var part = target + ".part";
if (Directory.Exists(part)) Directory.Delete(part, true);
var moved = new System.Collections.Generic.List<string>();
void PutBack()
{
foreach (var d in moved)
try { if (Directory.Exists(target) && !Directory.Exists(Path.Combine(target, d))) Directory.Move(Path.Combine(part, d), Path.Combine(target, d)); }
catch (Exception) { }
}
try
{
Directory.CreateDirectory(part);
Extract(codeZip, part, f => progress(baseZip == null ? f * 0.9 : f * 0.1));
if (baseZip != null) Extract(baseZip, part, f => progress(0.1 + f * 0.8));
else
foreach (var d in b.Dirs)
{
var into = Path.Combine(part, d);
if (Directory.Exists(into)) Directory.Delete(into, true); // (a code zip has none of them; never mixed)
Directory.Move(Path.Combine(target, d), into);
moved.Add(d);
}
foreach (var f in b.Files) // (the base whole in the new folder, whichever way it came)
{
var fi = new FileInfo(Path.Combine(part, f.Path));
if (!fi.Exists || fi.Length != f.Size) throw new InvalidDataException("The tools' base lacks " + f.Path + " (or it has another size): they were not changed.");
}
File.WriteAllText(Path.Combine(part, "version.txt"), tag + "\n");
File.WriteAllText(Path.Combine(part, BaseHave), b.Id + "\n");
}
catch (Exception)
{
PutBack();
try { if (Directory.Exists(part)) Directory.Delete(part, true); } catch (Exception) { }
throw;
}
var old = target + ".old";
if (Directory.Exists(old)) Directory.Delete(old, true);
if (Directory.Exists(target)) Directory.Move(target, old);
try { Directory.Move(part, target); }
catch (Exception)
{
try
{
if (!Directory.Exists(target) && Directory.Exists(old)) Directory.Move(old, target);
PutBack();
}
catch (Exception) { }
throw;
}
try { if (Directory.Exists(old)) Directory.Delete(old, true); } catch (Exception) { }
progress(1);
}
/// A tools zip's files into folder, every name checked before the first is written (as Unpack does: Mods.Unsafe)
static void Extract(string zip, string folder, Action<double> progress)
{
using var z = ZipFile.OpenRead(zip);
var bytes = z.Entries.Sum(e => e.Length) + z.Entries.Count * 4096L;
if (Space.Problem("the Triforce tools", bytes, Space.Free(folder), Space.Disk(folder)) is { } none) throw new NoRoomException(none);
var prefix = z.Entries.All(e => e.FullName.Replace('\\', '/').StartsWith("tools/", StringComparison.OrdinalIgnoreCase)) ? "tools/" : "";
foreach (var e in z.Entries)
{
var name = e.FullName.Replace('\\', '/').Substring(prefix.Length);
if (name.Length > 0 && Mods.Unsafe(name, true) is { } why)
throw new InvalidDataException("The tools' zip holds " + why + " (\"" + (name.Length > 120 ? name.Substring(0, 117) + "..." : name) + "\"): it was not unpacked.");
}
var full = Path.GetFullPath(folder) + Path.DirectorySeparatorChar;
for (var i = 0; i < z.Entries.Count; i++)
{
var e = z.Entries[i];
var name = e.FullName.Replace('\\', '/').Substring(prefix.Length);
if (name.Length == 0) continue;
var dest = Path.GetFullPath(Path.Combine(folder, name));
if (!dest.StartsWith(full, StringComparison.OrdinalIgnoreCase)) continue;
if (name.EndsWith("/")) { Directory.CreateDirectory(dest); continue; }
Directory.CreateDirectory(Path.GetDirectoryName(dest)!);
e.ExtractToFile(dest, true);
if ((i & 15) == 0) progress((double)i / z.Entries.Count);
}
}
}
/// nx2/make_game.py as a child process: its lines as they come, and how it ended.
sealed class Runner
{
readonly Tools tools;
readonly List<string> args;
Process? process;
string? error;
bool cancelled;
public event Action<string>? Line;
public event Action<string?>? Finished;
readonly string? ngxSdk; // (NVIDIA DLSS chosen, its license accepted: the folder NVIDIA's SDK is fetched into and built from)
public Runner(Tools tools, List<string> args, string? ngxSdk = null) { this.tools = tools; this.args = args; this.ngxSdk = ngxSdk; }
static string Quote(string a)
{
if (a.Length > 0 && a.IndexOfAny(new[] { ' ', '\t', '"' }) < 0) return a;
var sb = new StringBuilder("\"");
for (var i = 0; i < a.Length; i++)
{
var slashes = 0;
while (i < a.Length && a[i] == '\\') { slashes++; i++; }
if (i == a.Length) { sb.Append('\\', slashes * 2); break; }
if (a[i] == '"') sb.Append('\\', slashes * 2 + 1).Append('"');
else sb.Append('\\', slashes).Append(a[i]);
}
return sb.Append('"').ToString();
}
static ProcessStartInfo Info(Tools tools, IEnumerable<string> args, string? ngxSdk = null)
{
var all = new List<string> { "-I", Path.Combine(tools.Root, "nx2", "make_game.py") };
all.AddRange(args);
var si = new ProcessStartInfo(tools.Python, string.Join(" ", all.Select(Quote)))
{
WorkingDirectory = tools.Root,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8,
};
si.EnvironmentVariables["PYTHONIOENCODING"] = "utf-8";
if (ngxSdk != null) si.EnvironmentVariables["HOOKSHOT_NGX_SDK"] = ngxSdk; // (nx2/make_game.py --fetch-ngx-sdk, nx2/build.py)
// (written into the game folder: the game starts Setup again from here to update, host/hos_update.c)
var installed = Install.InstalledDir is { } d ? Path.Combine(d, Program.SetupAsset) : Install.Self;
si.EnvironmentVariables["HOOKSHOT_BUILDER_EXE"] = installed;
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.EnvironmentVariables[name] = path;
}
return si;
}
public void Start()
{
Task.Run(() =>
{
try
{
// (a test run builds and updates game folders only inside its own folder)
foreach (var name in new[] { "--out", "--update" })
{
var i = args.IndexOf(name);
if (i >= 0 && i + 1 < args.Count && !Install.Allowed(args[i + 1])) { Finished?.Invoke(Install.TestOnly); return; }
}
// (and it works in its own build folder, the one its uninstall removes. Left to itself make_game.py works in the PC's
// real one, %LOCALAPPDATA%\Triforce\build, and begins by stopping whatever build runs there)
if (Install.TestRoot != null && !args.Contains("--work")) args.AddRange(new[] { "--work", Path.Combine(Install.TestRoot, "build") });
using var p = Process.Start(Info(tools, args, ngxSdk))!;
process = p;
p.ErrorDataReceived += (_, e) => { if (e.Data != null) Line?.Invoke(e.Data); };
p.BeginErrorReadLine();
try { p.PriorityClass = ProcessPriorityClass.BelowNormal; } catch (Exception) { }
for (string? line; (line = p.StandardOutput.ReadLine()) != null;)
{
if (line.StartsWith("@@error ")) error = line.Substring(8);
Line?.Invoke(line);
}
p.WaitForExit();
Finished?.Invoke(cancelled ? "cancelled" : p.ExitCode == 0 ? null : error ?? "The build tools stopped without saying why. See the details.");
}
catch (Exception e)
{
Finished?.Invoke("The build tools could not start: " + e.Message);
}
});
}
public void Cancel()
{
cancelled = true;
try
{
if (process != null && !process.HasExited)
Process.Start(new ProcessStartInfo("taskkill", "/PID " + process.Id + " /T /F") { CreateNoWindow = true, UseShellExecute = false })?.WaitForExit(5000);
}
catch (Exception) { }
}
/// Is this the supported game and version? The answer in plain words.
public static Task<(bool Ok, string Text)> Check(Tools tools, IEnumerable<string> args) => Task.Run(() =>
{
try
{
using var p = Process.Start(Info(tools, args))!;
p.ErrorDataReceived += (_, _) => { };
p.BeginErrorReadLine();
var result = (false, "The game files could not be read.");
for (string? line; (line = p.StandardOutput.ReadLine()) != null;)
{
if (line.StartsWith("@@check ok ")) result = (true, line.Substring(11));
else if (line.StartsWith("@@check bad ")) result = (false, line.Substring(12));
else if (line.StartsWith("@@error ")) result = (false, line.Substring(8));
}
p.WaitForExit();
return result;
}
catch (Exception)
{
if (Directory.Exists(Path.Combine(tools.Root, "python")) && !File.Exists(Path.Combine(tools.Root, "python", "python.exe")))
return (false, "Triforce's own Python (tools\\python\\python.exe) is missing. Your antivirus probably removed it: restore it from " +
"quarantine, or start Triforce Setup again to download the tools once more.");
return (false, "Python was not found. Start Triforce Setup again to download the Triforce tools.");
}
});
}
/// Folder picker (the modern Explorer one, via IFileOpenDialog)
static class FolderPicker
{
[ComImport, Guid("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7")] class FileOpenDialogRCW { }
[ComImport, Guid("42f85136-db7e-439c-85f1-e4075d135fc8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IFileOpenDialog
{
[PreserveSig] int Show(IntPtr parent);
void SetFileTypes(); void SetFileTypeIndex(); void GetFileTypeIndex(); void Advise(); void Unadvise();
void SetOptions(uint fos); void GetOptions(out uint fos);
void SetDefaultFolder(IShellItem psi); void SetFolder(IShellItem psi);
void GetFolder(); void GetCurrentSelection();
void SetFileName([MarshalAs(UnmanagedType.LPWStr)] string name); void GetFileName();
void SetTitle([MarshalAs(UnmanagedType.LPWStr)] string title);
void SetOkButtonLabel(); void SetFileNameLabel();
void GetResult(out IShellItem item);
}
[ComImport, Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IShellItem
{
void BindToHandler(); void GetParent();
void GetDisplayName(uint sigdn, [MarshalAs(UnmanagedType.LPWStr)] out string name);
}
[DllImport("shell32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
static extern void SHCreateItemFromParsingName(string path, IntPtr bc, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, out IShellItem item);
/// (testing, TRIFORCE_UI_SCRIPT: the folders the next picks answer with, no dialog shown)
public static readonly Queue<string> Answers = new();
public static string? Pick(Window owner, string title, string? start)
{
if (Answers.Count > 0) return Answers.Dequeue();
var d = (IFileOpenDialog)new FileOpenDialogRCW();
try
{
d.SetOptions(0x20 | 0x40 | 0x800); // (PICKFOLDERS | FORCEFILESYSTEM | PATHMUSTEXIST)
d.SetTitle(title);
if (start != null)
{
var s = start;
while (s.Length > 3 && !Directory.Exists(s)) s = Path.GetDirectoryName(s) ?? "";
if (Directory.Exists(s))
try { SHCreateItemFromParsingName(s, IntPtr.Zero, typeof(IShellItem).GUID, out var item); d.SetFolder(item); } catch (Exception) { }
}
if (d.Show(new System.Windows.Interop.WindowInteropHelper(owner).Handle) != 0) return null;
d.GetResult(out var result);
result.GetDisplayName(0x80058000, out var path); // (SIGDN_FILESYSPATH)
return path;
}
finally { Marshal.FinalReleaseComObject(d); }
}
}