diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fddb21c..946f8e9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -36,8 +36,13 @@ jobs: with: dotnet-version: '10.0.x' - - name: Publish self-contained build - run: dotnet publish VISaveManager.csproj -c Release -r ${{ matrix.rid }} --self-contained true -o dist/publish + - name: Install the Native AOT toolchain + if: runner.os == 'Linux' + run: sudo apt-get update && sudo apt-get install -y clang zlib1g-dev + + # Linux: Native AOT. Windows: single-file, self-contained, ReadyToRun (set in the .csproj). + - name: Publish release build + run: dotnet publish VISaveManager.csproj -c Release -r ${{ matrix.rid }} -o dist/publish - name: Verify published files shell: pwsh diff --git a/Program.cs b/Program.cs index f7ce058..804849a 100644 --- a/Program.cs +++ b/Program.cs @@ -4,6 +4,7 @@ using System.Text; using System.Text.Json; using System.Text.Json.Nodes; +using System.Text.Json.Serialization; using System.Text.RegularExpressions; namespace VISM; @@ -62,7 +63,6 @@ interface IDesktopHost sealed class SaveManager(IDesktopHost host, bool test, string? saveDirectory) { - static readonly JsonSerializerOptions Json = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; const string TitleId = SaveLocations.TitleId; readonly SaveLocations locations = new(test, saveDirectory); public string ProfileDirectory => locations.ProfileDirectory; @@ -92,7 +92,7 @@ async Task CheckUpdateAsync() var url = (string?)j?["html_url"] ?? $"https://github.com/{Repo}/releases/latest"; var notes = ((string?)j?["body"] ?? "").Trim(); if (notes.Length > 400) notes = notes[..400] + "…"; - Send(new { type = "update", version = latest.ToString(), url, notes }); + Send(new UpdateMessage("update", latest.ToString(), url, notes)); } catch { } } @@ -103,7 +103,9 @@ static void OpenRelease(string? url) try { Process.Start(new ProcessStartInfo(url) { UseShellExecute = true }); } catch { } } - void Send(object value) => host.Post(JsonSerializer.Serialize(value, Json)); + // messages to the page: types known at build time, so Native AOT needs no reflection + void Send(UpdateMessage value) => host.Post(JsonSerializer.Serialize(value, VismJson.Default.UpdateMessage)); + void Send(SavesMessage value) => host.Post(JsonSerializer.Serialize(value, VismJson.Default.SavesMessage)); public void OnMessage(string raw) { @@ -225,18 +227,16 @@ static bool IsVictory(string value) void SendSaves(string? note = null, bool ok = true) { var dir = SavesDir(); - object Item(string d) => new { name = Path.GetFileName(d), size = DirSize(d), gameTime = DirNewest(d).ToString("yyyy-MM-dd HH:mm"), kept = KeptTime(d).ToString("yyyy-MM-dd HH:mm"), note = Note(d), auto = Path.GetFileName(d).StartsWith("auto ") }; + SaveItem Item(string d) => new(Path.GetFileName(d), DirSize(d), DirNewest(d).ToString("yyyy-MM-dd HH:mm"), KeptTime(d).ToString("yyyy-MM-dd HH:mm"), Note(d), Path.GetFileName(d).StartsWith("auto ")); var spots = (Directory.Exists(SnapsRoot) ? Directory.GetDirectories(SnapsRoot) : Array.Empty()).OrderByDescending(KeptTime).Select(Item).ToList(); var team = (Directory.Exists(TeamRoot) ? Directory.GetDirectories(TeamRoot) : Array.Empty()).OrderBy(d => d).Select(Item).ToList(); - Send(new - { - type = "saves", note, ok, test, canChooseSaveFolder = OperatingSystem.IsLinux() && !test, - saveDirectory = OperatingSystem.IsLinux() ? dir : null, - found = dir != null && DirSize(dir) > 0, - size = dir != null ? DirSize(dir) : 0, - newest = dir != null ? DirNewest(dir).ToString("yyyy-MM-dd HH:mm") : "", - running = GameRunning(), spots, team, - }); + Send(new SavesMessage( + "saves", note, ok, test, OperatingSystem.IsLinux() && !test, + OperatingSystem.IsLinux() ? dir : null, + dir != null && DirSize(dir) > 0, + dir != null ? DirSize(dir) : 0, + dir != null ? DirNewest(dir).ToString("yyyy-MM-dd HH:mm") : "", + GameRunning(), spots, team)); } static DateTime KeptTime(string directory) => OperatingSystem.IsWindows() @@ -362,9 +362,9 @@ void Share(string? name, string kind) if (filename == null) { SendSaves(); return; } using (var zip = new ZipArchive(File.Create(filename), ZipArchiveMode.Create)) { - var info = new { name = label, note = Note(src), game = "GTA VI (Xbox Series), title " + TitleId, savedAt = DirNewest(src).ToString("yyyy-MM-dd HH:mm"), madeWith = "VI Save Manager" }; + var info = new SaveInfo(label, Note(src), "GTA VI (Xbox Series), title " + TitleId, DirNewest(src).ToString("yyyy-MM-dd HH:mm"), "VI Save Manager"); var e = zip.CreateEntry("save-info.json"); - using (var w = new StreamWriter(e.Open(), new UTF8Encoding(false))) w.Write(JsonSerializer.Serialize(info, new JsonSerializerOptions { WriteIndented = true })); + using (var w = new StreamWriter(e.Open(), new UTF8Encoding(false))) w.Write(JsonSerializer.Serialize(info, VismJsonIndented.Default.SaveInfo)); foreach (var f in Directory.EnumerateFiles(src, "*", SaveTree)) { if (Path.GetFileName(f) == "about.txt") continue; @@ -418,3 +418,20 @@ void Import() SendSaves(msg.Trim(), failed.Count == 0); } } + +// ---- the JSON this app writes, generated at build time (Native AOT has no reflection) ---- +record UpdateMessage(string Type, string Version, string Url, string Notes); +record SaveItem(string Name, long Size, string GameTime, string Kept, string Note, bool Auto); +record SavesMessage(string Type, string? Note, bool Ok, bool Test, bool CanChooseSaveFolder, string? SaveDirectory, + bool Found, long Size, string Newest, bool Running, List Spots, List Team); +// save-info.json inside a shared zip +record SaveInfo(string Name, string Note, string Game, string SavedAt, string MadeWith); + +[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)] +[JsonSerializable(typeof(UpdateMessage))] +[JsonSerializable(typeof(SavesMessage))] +partial class VismJson : JsonSerializerContext { } + +[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, WriteIndented = true)] +[JsonSerializable(typeof(SaveInfo))] +partial class VismJsonIndented : JsonSerializerContext { } diff --git a/README.md b/README.md index 2abee0d..45d7d83 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ sudo dnf install gtk3 webkit2gtk4.1 # Nobara / Fedora sudo apt install libgtk-3-0 libwebkit2gtk-4.1-0 # Debian / Ubuntu ``` -Build with the .NET 8 SDK or newer, then launch from the output directory. The default build uses the installed .NET runtime. For a shareable archive that includes .NET, use `./build.sh --release`. Extract that archive into its own folder and run `./VISaveManager`. +Build with the .NET 8 SDK or newer, then launch from the output directory. The default build uses the installed .NET runtime. For a shareable archive, use `./build.sh --release`: it compiles with **Native AOT** into one native `VISaveManager` executable that needs no .NET install (needs `clang` and the zlib headers: `sudo dnf install clang zlib-devel` or `sudo apt install clang zlib1g-dev`). Extract that archive into its own folder and run `./VISaveManager`. ```sh ./build.sh @@ -51,8 +51,8 @@ powershell -ExecutionPolicy Bypass -File release.ps1 # make the zip to share `dotnet build -c Release` defaults to the host OS. You can also target Linux or Windows explicitly with the same checkout. Cross publishing requires the matching SDK/runtime packs, which `dotnet publish` restores from NuGet: ```sh -dotnet publish VISaveManager.csproj -c Release -r linux-x64 --self-contained true -o dist/linux-x64-self-contained -dotnet publish VISaveManager.csproj -c Release -r win-x64 --self-contained true -o dist/win-x64-self-contained +dotnet publish VISaveManager.csproj -c Release -r linux-x64 -o dist/linux-x64 # Native AOT +dotnet publish VISaveManager.csproj -c Release -r win-x64 -o dist/win-x64 # single-file, self-contained, ReadyToRun ``` ## Automated builds @@ -63,3 +63,8 @@ The **Build VISM** workflow compiles both platforms on pull requests into `main` - `VISaveManager-win-x64` contains `VISaveManager-win-x64.zip`. Extract it to a folder and run `VISaveManager.exe`. The WebView2 Runtime is required. Both archives include .NET, the application interface, and the bundled team saves. + +Release builds: Linux is compiled with Native AOT (one native executable, no .NET needed). Windows +can't use Native AOT because WinForms and WebView2 don't support it, so it ships as one +self-contained, ReadyToRun-compiled `VISaveManager.exe` instead (no .NET needed either). Both keep +the `ui` and `team-saves` folders next to the executable. diff --git a/VISaveManager.csproj b/VISaveManager.csproj index 98d3976..30f15e8 100644 --- a/VISaveManager.csproj +++ b/VISaveManager.csproj @@ -18,6 +18,22 @@ vism.ico PerMonitorV2 + + + true + true + true + + + true + true + true + true + true + diff --git a/build.sh b/build.sh index 6964a37..22f98b0 100755 --- a/build.sh +++ b/build.sh @@ -10,11 +10,12 @@ esac if [[ $# == 0 ]]; then output="dist/$rid" - dotnet publish VISaveManager.csproj -c Release -r "$rid" --self-contained false -o "$output" + dotnet publish VISaveManager.csproj -c Release -r "$rid" --self-contained false -p:PublishAot=false -o "$output" printf 'Launch with ./%s/VISaveManager\n' "$output" elif [[ $# == 1 && $1 == --release ]]; then - output="dist/$rid-self-contained" - dotnet publish VISaveManager.csproj -c Release -r "$rid" --self-contained true -o "$output" + # Native AOT: needs clang and zlib headers (Fedora: clang zlib-devel, Debian/Ubuntu: clang zlib1g-dev) + output="dist/$rid-native" + dotnet publish VISaveManager.csproj -c Release -r "$rid" -o "$output" archive="dist/VISaveManager-$rid.tar.gz" tar -czf "$archive" -C "$output" . printf 'Built %s\n' "$archive" diff --git a/release.ps1 b/release.ps1 index 773e009..3293035 100644 --- a/release.ps1 +++ b/release.ps1 @@ -1,4 +1,4 @@ -# The zip to send to the team: a self-contained build (no .NET install needed on their PC), +# The zip to send to the team: one self-contained, ReadyToRun-compiled VISaveManager.exe (no .NET install needed), # saved as dist\VISaveManager-.zip. Unzip anywhere and run VISaveManager.exe. # Run: powershell -ExecutionPolicy Bypass -File release.ps1 $ErrorActionPreference = 'Stop' @@ -6,7 +6,7 @@ Set-Location -LiteralPath $PSScriptRoot $version = ([xml](Get-Content VISaveManager.csproj)).Project.PropertyGroup.Version | Where-Object { $_ } | Select-Object -First 1 $out = Join-Path $PSScriptRoot 'dist\VISaveManager' if (Test-Path $out) { Remove-Item $out -Recurse -Force } -dotnet publish -c Release -r win-x64 --self-contained true -p:PublishSingleFile=false -o $out +dotnet publish -c Release -r win-x64 -o $out # single-file, self-contained, ReadyToRun (see the .csproj) if ($LASTEXITCODE -ne 0) { throw "Build failed (exit code $LASTEXITCODE)" } $zip = Join-Path $PSScriptRoot "dist\VISaveManager-$version.zip" if (Test-Path $zip) { Remove-Item $zip -Force }