// Triforce Setup's window: install -> gather (update check, downloads) -> forge (choose the game, build) -> play. // The pages are on the left; on the right the crest (Stage.cs), whose stones go out when work starts and light as it is done. using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Net.Http; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Interop; using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Threading; using Microsoft.Win32; namespace TriforceSetup; sealed partial class Shell { public readonly Window Window; const long NeededBytes = 60L << 30; // (what the tools take unpacked, until their zip is here to say it exactly: v0.4.0's Triforce-Tools.zip is 284 MB and unpacks // to 1.1 GB in 13,230 files, 3.9 times its size) const int ToolsUnpackedTimes = 5; // (the build's steps, and the stone each one lights when it is done; the last one makes the whole crest gleam) // ((Triforce, 11-18) "shaders": the game's --prepare-shaders after the folder is put together (nx2/make_game.py prepare_shaders; // its "@@stage shaders" and progress, gpu/vk/vk_main.cpp). Without a row it ran under four finished steps with no bar: an // update on an AMD card sat there over an hour and looked stuck) static readonly (string Id, S Text, int Stone)[] Stages = { ("read", S.Read, Art.Emerald), ("lift", S.Lift, Art.Sapphire), ("compile", S.Compile, Art.Ruby), ("finish", S.Finish, -1), ("shaders", S.Shaders, -1) }; // parts of Main.xaml readonly Grid Root, InstallPage, FetchPage, SetupPage, BuildPage, NupRow, AskLayer; readonly TextBox InstallPath, NupPath, RomfsPath, ExefsPath, OutPath, Log; readonly TextBlock InstallSpace, FetchTitle, FetchNote, FetchStep, FetchPercent, FetchNumber, Subtitle, UpdateText, CheckText, SpaceText, BuildTitle, BuildNote, VersionText, AskText; readonly Caps LanguageLabel, GameFilesLabel, SaveToLabel, BuildEyebrow; readonly CheckBox DesktopIcon, StartIcon; readonly Button InstallButton, BrowseInstall, FetchSkip, FetchCancel, FetchRetry, UpdateGameButton, BrowseA, BrowseB, BrowseC, BrowseD, BuildButton, OpenButton, BackButton, CancelButton, PlayButton, AskYes, AskNo; readonly Rectangle CheckMark; readonly ProgressLine FetchBar; readonly Border UpdateBanner; readonly StackPanel FolderRows, StageList, FetchNumberRow; readonly ColumnDefinition StageColumn; readonly RadioButton UseNup, UseFolders; readonly ComboBox LanguageBox; readonly ToggleButton ShowLog; readonly Stage stage; readonly string? updateFolder; readonly bool relaunched; bool merge; Release? latest; readonly string[]? autoBuild; // (testing: --auto-build builds with the installed tools, as the Build button does) Language lang; readonly Dictionary rows = new(); readonly DispatcherTimer clock = new() { Interval = TimeSpan.FromSeconds(1) }; readonly DispatcherTimer checkDelay = new() { Interval = TimeSpan.FromMilliseconds(500) }; readonly Stopwatch elapsed = new(); Runner? runner; string? current; string finished = ""; int checkSerial; bool sourceOk; CancellationTokenSource? fetch; Action? bannerAction, askYes; bool closing; Grid? shown; readonly string? snapshot; string T(S s) => (int)s < lang.Text.Length ? lang.Text[(int)s] : Strings.All[0].Text[(int)s]; T Part(string name) where T : class => (Window.FindName(name) as T) ?? throw new InvalidOperationException("Main.xaml has no " + name); Brush Brush(string key) => (Brush)Window.Resources[key]; public Shell(string[] args) { for (var i = 0; i < args.Length; i++) { if (args[i] == "--update" && i + 1 < args.Length) { updateFolder = args[++i]; merge = true; } else if (args[i] == "--merge") merge = true; else if (args[i] == "--relaunched") relaunched = true; else if (args[i] == "--snapshot" && i + 1 < args.Length) snapshot = args[++i]; else if (args[i] == "--auto-build" && i + 3 < args.Length) autoBuild = new[] { args[++i], args[++i], args[++i] }; } Fx.Still = snapshot != null; Art.Load(); using (var s = Program.Resource("Main.xaml")) Window = (Window)XamlReader.Load(s); Root = Part("Root"); AskLayer = Part("AskLayer"); InstallPage = Part("InstallPage"); FetchPage = Part("FetchPage"); SetupPage = Part("SetupPage"); BuildPage = Part("BuildPage"); NupRow = Part("NupRow"); InstallPath = Part("InstallPath"); NupPath = Part("NupPath"); RomfsPath = Part("RomfsPath"); ExefsPath = Part("ExefsPath"); OutPath = Part("OutPath"); Log = Part("Log"); InstallSpace = Part("InstallSpace"); FetchTitle = Part("FetchTitle"); FetchNote = Part("FetchNote"); FetchStep = Part("FetchStep"); FetchPercent = Part("FetchPercent"); FetchNumber = Part("FetchNumber"); Subtitle = Part("Subtitle"); UpdateText = Part("UpdateText"); CheckText = Part("CheckText"); SpaceText = Part("SpaceText"); BuildTitle = Part("BuildTitle"); BuildNote = Part("BuildNote"); VersionText = Part("VersionText"); AskText = Part("AskText"); LanguageLabel = Part("LanguageLabel"); GameFilesLabel = Part("GameFilesLabel"); SaveToLabel = Part("SaveToLabel"); BuildEyebrow = Part("BuildEyebrow"); DesktopIcon = Part("DesktopIcon"); StartIcon = Part("StartIcon"); InstallButton = Part