diff --git a/AGENTS.md b/AGENTS.md index 554d5dff..21a5f4ae 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -19,7 +19,7 @@ - `docs/path-finding-grids.md` - Pixely.PathFinding.Grids: grid geometry, clearance-based agent footprints, connectivity and the corner rule, overlays, the admissible grid heuristic - `docs/development-packages.md` - Consuming packages from the public development feed - `docs/sdk.md` - Pixely as an MSBuild project SDK: the `` line, what it adds to a project, the browser build and its framework switch, central package management, the PIXELY0002 error for projects without it, the package layout with its two `lib/` folders -- `docs/hosting.md` - Hosting: opting in with `PixelyHosting`, the generated `Main`, `Configure` and `OnException` with its default message box in Release builds, diagnostics, the browser: `dotnet publish -r browser-wasm`, the `net11.0-browser` framework switch and its lateness, multi-targeting with `-f net11.0-browser`, `BrowserHost` and the frame loop, exit codes, the default page and its replacement, Mono AOT on by default (`RunAOTCompilation`), its opt-out and what IL stripping leaves readable, opt-in Brotli downloads with `PixelyBrowserBrotli` through the boot config for hosts without `Content-Encoding: br` such as GitHub Pages, XDL linked into the browser runtime from `XdlSharp.Native.browser-wasm` with the workload and `WasmBuildNative`, the formats XDL's satellites decode, native archives from a URL with `NativeUrlReference`, WebGPU in the browser: device adoption through `BrowserHost.PrepareAsync`, the fallback adapter warning, notice, `pixelygpuadapter` event and `readGpuAdapter()`, device loss and `GpuDeviceLostException`, the default page's loading indicator and stop message, the `pixelyfirstframe` event, teardown, what is unsupported +- `docs/hosting.md` - Hosting: opting in with `PixelyHosting`, the generated `Main`, `Configure` and `OnException` with its default message box in Release builds, diagnostics, the browser: `dotnet publish -r browser-wasm`, the `net11.0-browser` framework switch and its lateness, multi-targeting with `-f net11.0-browser`, `BrowserHost` and the frame loop, exit codes, the default page and its replacement, Mono AOT on by default (`RunAOTCompilation`), its opt-out and what IL stripping leaves readable, opt-in Brotli downloads with `PixelyBrowserBrotli` through the boot config for hosts without `Content-Encoding: br` such as GitHub Pages, an installable app with `PixelyBrowserInstallable`, the generated web app manifest, its icons and PIXELY0016, XDL linked into the browser runtime from `XdlSharp.Native.browser-wasm` with the workload and `WasmBuildNative`, the formats XDL's satellites decode, native archives from a URL with `NativeUrlReference`, WebGPU in the browser: device adoption through `BrowserHost.PrepareAsync`, the fallback adapter warning, notice, `pixelygpuadapter` event and `readGpuAdapter()`, device loss and `GpuDeviceLostException`, the default page's loading indicator and stop message, the `pixelyfirstframe` event, teardown, what is unsupported - `docs/cursors.md` - Mouse cursors through `ICursorService`: system, image and animated cursors, the hotspot, frame durations, setting, resetting and hiding the cursor, cursor lifetime and the main-thread rule, platform behavior of animated cursors, headless mode - `docs/platform-settings.md` - The user's OS or browser settings through `IPlatformSettings`: the main-thread rule, the light or dark theme with `ThemeChanged`, where SDL reads it on each platform, `Unknown` without an XDG portal and in headless mode; preferred locales with `Locale` for games that set `InvariantGlobalization`, why `CultureInfo` cannot tell them there, unvalidated codes, Linux ordering, the 127-byte limit, the `SDL_HINT_PREFERRED_LOCALES` override and its environment variable, skipped empty items, `PreferredLocalesChanged` and the platforms where SDL reports changes - `docs/taskbar-icons.md` - Application-wide taskbar and Dock icons loaded from virtual content diff --git a/DECISIONS.md b/DECISIONS.md index 3b200c24..e1e0e597 100644 --- a/DECISIONS.md +++ b/DECISIONS.md @@ -2,6 +2,15 @@ Design decisions with the constraints that decided them and their known costs, newest first. +## 2026-11-19: An installable browser app gets a generated manifest and no service worker + +`PixelyBrowserInstallable` generates a web app manifest (`docs/hosting.md`). A publish has no service worker, so an installed game still needs the network to start. + +- Chrome installs an app from its menu without a worker. +- WriteLinesToFile turns backslashes into slashes on Unix, and a quote in a path ends a property function's argument, so an inline task resolves the icons and writes the manifest and the page. + +Cost: the inline task compiles with Roslyn on its first use in a build. + ## 2026-11-14: A test app never starts SDL, so tests run in parallel `TestApp.CreateBuilder()` builds an app without SDL (`docs/testing.md`). Its windows are `StubWindow`s without a platform, its display, cursors, clipboard and platform settings are stubs, its text uses `StubFontSystem`, and its sound `StubAudioSystem`. `Window` became platform-neutral, and `NativeWindow` holds the SDL window under `SwapchainWindow` and `OffscreenWindow`. `AudioSystem` became an abstract base with internal `*Core` methods, as `GpuDevice` is, with `SdlAudioSystem` on SDL_mixer. `PixelyAppBuilder.Build()`, `AddGpuServices` and `RegisterAudio` register a default only when nothing has registered the service, and a frame reads the platform through an internal `IEventSource`. diff --git a/docs/hosting.md b/docs/hosting.md index 299ec3e4..56fbbf21 100644 --- a/docs/hosting.md +++ b/docs/hosting.md @@ -439,3 +439,39 @@ content. Leave the property off on a host that sends `Content-Encoding: br` itself. The download is the same size, and Chrome compiles `dotnet.native.wasm` while it downloads instead of decoding it afterwards. + +### Installable app + +`PixelyBrowserInstallable=true` makes the published page a web app that a browser can install, as a +window of its own and an entry in the launcher or on the home screen: + +```xml + + true + Example Game + #1a1a2e + + + + + +``` + +- The SDK generates `manifest.webmanifest` with the name, `"start_url": "./"`, + `"display": "standalone"`, the colour as both theme and background colour, and the icons. + `PixelyBrowserAppName` defaults to `$(Product)`, which defaults to the assembly name. + `PixelyBrowserThemeColor` defaults to `#000`, the default page's background. +- An icon under the project's `wwwroot/` is served from there already, and the manifest names it by + its path there. Any other icon is served at its `TargetPath`, by default its file name. `Sizes` + is the manifest's space-separated `sizes`, and `Purpose`, when set, its `purpose`. +- Chrome installs an app only with a 192x192 and a 512x512 icon, so without both the build fails + with PIXELY0016. +- The package's `index.html` gets `` and + `` before ``. A project's own `wwwroot/index.html` or a page named + by `PixelyBrowserIndexHtml` adds both itself. +- A project's own `wwwroot/manifest.webmanifest` replaces the generated one. Its icons are still + served, and the icon check is then the project's own. `PixelyBrowserDefaultAssets=false` turns off the generated manifest, the + served icons and the tags with the rest of the page, since Pixely then adds no web asset. + +A browser installs only a page from a secure context, which WebGPU needs anyway. An installed app +still needs the network to start. diff --git a/docs/sdk.md b/docs/sdk.md index d8e5ef40..2c2b0c05 100644 --- a/docs/sdk.md +++ b/docs/sdk.md @@ -65,7 +65,7 @@ Sdk/Sdk.props, Sdk/Sdk.targets imported by the SDK resolver Sdk/Pixely.AfterSdk.targets WebAssembly, shader and hosting targets, imported once the base SDK has set its properties Sdk/Pixely.Hosting.targets the entry point generator Sdk/Pixely.Browser.props imports the WebAssembly SDK props on browser-wasm -Sdk/Pixely.Browser.targets the framework switch, browser property overrides, XDL's link settings, native URL references, PIXELY0004 to PIXELY0012, the default web assets, the Brotli boot config extension, the browser file system +Sdk/Pixely.Browser.targets the framework switch, browser property overrides, XDL's link settings, native URL references, PIXELY0004 to PIXELY0012 and PIXELY0016, the default web assets, the web app manifest, the Brotli boot config extension, the browser file system Sdk/Pixely.Native.targets the XdlSharp.Native. reference, PIXELY0014 and PIXELY0015 Sdk/Pixely.Version.props the package version and the XdlSharp version, generated at pack time buildTransitive/Pixely.targets fails a project that reaches Pixely without the SDK diff --git a/packaging/Pixely/Sdk/Pixely.Browser.targets b/packaging/Pixely/Sdk/Pixely.Browser.targets index 96df795f..b5bb53eb 100644 --- a/packaging/Pixely/Sdk/Pixely.Browser.targets +++ b/packaging/Pixely/Sdk/Pixely.Browser.targets @@ -50,7 +50,8 @@ true true true - $(MSBuildThisFileDirectory)..\wwwroot\index.html + <_PixelyBrowserPackageIndexHtml>$(MSBuildThisFileDirectory)..\wwwroot\index.html + $(_PixelyBrowserPackageIndexHtml) $(MSBuildThisFileDirectory)..\wwwroot\main.js <_PixelyBrowserHostJs>$(MSBuildThisFileDirectory)..\wwwroot\pixely-host.js @@ -152,6 +153,148 @@ + + + + + + + + + + + + + + + resolved = new System.Collections.Generic.List(); + foreach (Microsoft.Build.Framework.ITaskItem icon in Icons ?? new Microsoft.Build.Framework.ITaskItem[0]) + { + Microsoft.Build.Utilities.TaskItem item = new Microsoft.Build.Utilities.TaskItem(icon); + string fullPath = System.IO.Path.GetFullPath(icon.GetMetadata("FullPath")); + bool inWwwroot = fullPath.StartsWith(wwwroot, System.StringComparison.Ordinal); + string relativeToWwwroot = inWwwroot ? fullPath.Substring(wwwroot.Length) : ""; + string targetPath = icon.GetMetadata("TargetPath"); + if (targetPath.Length == 0) + { + targetPath = icon.GetMetadata("Filename") + icon.GetMetadata("Extension"); + } + item.SetMetadata("Src", (inWwwroot ? relativeToWwwroot : targetPath).Replace('\\', '/')); + item.SetMetadata("InWwwroot", inWwwroot ? "true" : "false"); + item.SetMetadata("RelativePath", targetPath); + resolved.Add(item); + } + ResolvedIcons = resolved.ToArray(); + + if (!string.IsNullOrEmpty(ManifestFile)) + { + System.Collections.Generic.List icons = new System.Collections.Generic.List(); + foreach (Microsoft.Build.Framework.ITaskItem icon in resolved) + { + string purpose = icon.GetMetadata("Purpose"); + string purposeMember = purpose.Length > 0 ? ", \"purpose\": " + Quote(purpose) : ""; + icons.Add(" { \"src\": " + Quote(icon.GetMetadata("Src")) + ", \"sizes\": " + Quote(icon.GetMetadata("Sizes")) + purposeMember + " }"); + } + WriteIfChanged(ManifestFile, "{\n" + + " \"name\": " + Quote(Name) + ",\n" + + " \"start_url\": \"./\",\n" + + " \"display\": \"standalone\",\n" + + " \"background_color\": " + Quote(ThemeColor) + ",\n" + + " \"theme_color\": " + Quote(ThemeColor) + ",\n" + + " \"icons\": [\n" + string.Join(",\n", icons) + "\n ]\n" + + "}\n"); + } + + // The package's page ends its head with a line of its own, so the two tags go in before it. + if (System.IO.Path.GetFullPath(Page) == System.IO.Path.GetFullPath(PackagePage)) + { + string color = System.Net.WebUtility.HtmlEncode(ThemeColor); + string head = " \n \n"; + WriteIfChanged(PageFile, System.IO.File.ReadAllText(PackagePage).Replace("", head)); + Page = PageFile; + } + ]]> + + + + + + + <_PixelyBrowserGeneratedRoot>$([MSBuild]::NormalizeDirectory('$(MSBuildProjectDirectory)', '$(IntermediateOutputPath)', 'pixely-browser-generated')) + <_PixelyBrowserWwwroot>$([MSBuild]::NormalizeDirectory('$(MSBuildProjectDirectory)', 'wwwroot')) + <_PixelyBrowserAppName>$(PixelyBrowserAppName) + <_PixelyBrowserAppName Condition="'$(PixelyBrowserAppName.Length)' == '0'">$(Product) + <_PixelyBrowserThemeColor>$(PixelyBrowserThemeColor) + <_PixelyBrowserThemeColor Condition="'$(PixelyBrowserThemeColor.Length)' == '0'">#000 + <_PixelyBrowserOwnManifest>false + <_PixelyBrowserOwnManifest Condition="Exists('$(_PixelyBrowserWwwroot)manifest.webmanifest')">true + <_PixelyBrowserManifestFile Condition="'$(_PixelyBrowserOwnManifest)' != 'true'">$(_PixelyBrowserGeneratedRoot)manifest.webmanifest + + + + <_PixelyBrowserAppIconSizes>|@(PixelyBrowserAppIcon->'%(Sizes)', '|')| + <_PixelyBrowserAppIconSizes>$(_PixelyBrowserAppIconSizes.Replace(' ', '|').ToLowerInvariant()) + + + <_PixelyWriteInstallableAssets Icons="@(PixelyBrowserAppIcon)" Wwwroot="$(_PixelyBrowserWwwroot)" Name="$(_PixelyBrowserAppName)" ThemeColor="$(_PixelyBrowserThemeColor)" + ManifestFile="$(_PixelyBrowserManifestFile)" PackagePage="$(_PixelyBrowserPackageIndexHtml)" PageFile="$(_PixelyBrowserGeneratedRoot)index.html" Page="$(PixelyBrowserIndexHtml)"> + + + + + <_PixelyBrowserInstallableAssetSource Include="$(_PixelyBrowserManifestFile)" RelativePath="manifest.webmanifest" Condition="'$(_PixelyBrowserManifestFile.Length)' != '0'" /> + <_PixelyBrowserInstallableAssetSource Include="@(_PixelyBrowserAppIcon->WithMetadataValue('InWwwroot', 'false'))" /> + + + + + DependsOnTargets="_PixelyGenerateBrowserInstallableAssets" Condition="'$(_PixelyBrowser)' == 'true' and '$(PixelyBrowserDefaultAssets)' == 'true'"> <_PixelyBrowserAssetsRoot>$([MSBuild]::NormalizeDirectory('$(MSBuildProjectDirectory)', '$(IntermediateOutputPath)', 'pixely-browser')) + <_PixelyBrowserIndexHtmlSource Condition="'$(_PixelyBrowserIndexHtmlSource.Length)' == '0'">$(PixelyBrowserIndexHtml) - <_PixelyBrowserDefaultAssetSource Include="$(PixelyBrowserIndexHtml)" RelativePath="index.html" /> + <_PixelyBrowserDefaultAssetSource Include="$(_PixelyBrowserIndexHtmlSource)" RelativePath="index.html" /> <_PixelyBrowserDefaultAssetSource Include="$(PixelyBrowserMainJs)" RelativePath="main.js" /> <_PixelyBrowserDefaultAssetSource Include="$(_PixelyBrowserHostJs)" RelativePath="pixely-host.js" /> + <_PixelyBrowserDefaultAssetSource Include="@(_PixelyBrowserInstallableAssetSource)" /> <_PixelyBrowserDefaultAssetSource Remove="@(_PixelyBrowserDefaultAssetSource)" Condition="Exists('$(MSBuildProjectDirectory)/wwwroot/%(RelativePath)')" /> diff --git a/tests/Pixely.Package.Tests/Consumers/BrowserContentConsumer/BrowserContentConsumer.csproj b/tests/Pixely.Package.Tests/Consumers/BrowserContentConsumer/BrowserContentConsumer.csproj index e4dc1902..7202c471 100644 --- a/tests/Pixely.Package.Tests/Consumers/BrowserContentConsumer/BrowserContentConsumer.csproj +++ b/tests/Pixely.Package.Tests/Consumers/BrowserContentConsumer/BrowserContentConsumer.csproj @@ -28,6 +28,16 @@ + + + + + + + + + + diff --git a/tests/Pixely.Package.Tests/Consumers/BrowserContentConsumer/Extras/icon-192.png b/tests/Pixely.Package.Tests/Consumers/BrowserContentConsumer/Extras/icon-192.png new file mode 100644 index 00000000..3d877ad3 --- /dev/null +++ b/tests/Pixely.Package.Tests/Consumers/BrowserContentConsumer/Extras/icon-192.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c2153f77e11087fcb078ae38527fa83bef29791e3700e30cc87fec4405a66d0f +size 70 diff --git a/tests/Pixely.Package.Tests/Consumers/BrowserContentConsumer/Extras/icon-512.png b/tests/Pixely.Package.Tests/Consumers/BrowserContentConsumer/Extras/icon-512.png new file mode 100644 index 00000000..3d877ad3 --- /dev/null +++ b/tests/Pixely.Package.Tests/Consumers/BrowserContentConsumer/Extras/icon-512.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c2153f77e11087fcb078ae38527fa83bef29791e3700e30cc87fec4405a66d0f +size 70 diff --git a/tests/Pixely.Package.Tests/PackageIntegrationTests.cs b/tests/Pixely.Package.Tests/PackageIntegrationTests.cs index 0a6d0c1b..0f70d1df 100644 --- a/tests/Pixely.Package.Tests/PackageIntegrationTests.cs +++ b/tests/Pixely.Package.Tests/PackageIntegrationTests.cs @@ -1153,6 +1153,124 @@ public async Task BrowserAppWithoutContentArchiveNamesTheArchiveInTheError() Assert.That(result, Does.Contain("RESULT error").And.Contain("PixelyBrowserVfsFile with TargetPath Content.pk3").And.Contain("RESULT exit code 1")); } + // One publish covers the manifest with a name that needs escaping, the icons at their target paths and the tags in the package's page. + [Test] + public async Task BrowserPublishMakesAnInstallableApp() + { + string consumerDirectory = GetConsumerDirectory("BrowserContentConsumer"); + DeleteConsumerOutputs("BrowserContentConsumer"); + + await PublishConsumerAsync(consumerDirectory, "browser-wasm", properties: ["PixelyBrowserInstallable=true", "BrowserContentConsumerIcons=true", "PixelyBrowserAppName=Pixely%3B C:\\Games 'Game'", "PixelyBrowserThemeColor=#123456"]); + string wwwroot = GetPublishedWwwroot(consumerDirectory); + using (JsonDocument manifest = JsonDocument.Parse(File.ReadAllText(Path.Combine(wwwroot, "manifest.webmanifest")))) + { + JsonElement root = manifest.RootElement; + JsonElement[] icons = root.GetProperty("icons").EnumerateArray().ToArray(); + Assert.Multiple(() => + { + Assert.That(root.GetProperty("name").GetString(), Is.EqualTo("Pixely; C:\\Games 'Game'")); + Assert.That(root.GetProperty("start_url").GetString(), Is.EqualTo("./")); + Assert.That(root.GetProperty("display").GetString(), Is.EqualTo("standalone")); + Assert.That(root.GetProperty("theme_color").GetString(), Is.EqualTo("#123456")); + Assert.That(root.GetProperty("background_color").GetString(), Is.EqualTo("#123456")); + Assert.That(icons.Select(icon => icon.GetProperty("src").GetString()), Is.EqualTo(new[] { "icon-192.png", "icons/icon-512.png" })); + Assert.That(icons.Select(icon => icon.GetProperty("sizes").GetString()), Is.EqualTo(new[] { "192x192", "512x512" })); + Assert.That(icons[0].TryGetProperty("purpose", out _), Is.False); + Assert.That(icons[1].GetProperty("purpose").GetString(), Is.EqualTo("any maskable")); + }); + } + Assert.Multiple(() => + { + Assert.That(File.Exists(Path.Combine(wwwroot, "icon-192.png")), Is.True); + Assert.That(File.Exists(Path.Combine(wwwroot, "icons", "icon-512.png")), Is.True); + Assert.That(File.ReadAllText(Path.Combine(wwwroot, "index.html")), + Does.Contain("\n \n").And.Contain(" + { + Assert.That(File.ReadAllText(Path.Combine(wwwroot, "manifest.webmanifest")), Does.Contain("\"Own\"")); + Assert.That(File.Exists(Path.Combine(wwwroot, "icon-192.png")), Is.True); + Assert.That(File.Exists(Path.Combine(wwwroot, "icons", "icon-512.png")), Is.True); + Assert.That(File.Exists(Path.Combine(wwwroot, "it's.png")), Is.True); + }); + } + finally + { + DeleteDirectory(consumerWwwroot); + File.Delete(quotedIcon); + } + } + + // An icon under the project's wwwroot is named by its path there, and a page named by PixelyBrowserIndexHtml gets no tags. + [Test] + public async Task BrowserPublishNamesAWwwrootIconByItsPathAndLeavesAnOwnPage() + { + string consumerDirectory = GetConsumerDirectory("BrowserContentConsumer"); + DeleteConsumerOutputs("BrowserContentConsumer"); + string consumerWwwroot = Path.Combine(consumerDirectory, "wwwroot"); + const string ownPage = "\n\n\n\n\n\n"; + string ownPagePath = Path.Combine(_testArtifactsDirectory, "own-index.html"); + File.WriteAllText(ownPagePath, ownPage); + Directory.CreateDirectory(Path.Combine(consumerWwwroot, "images")); + string itemsPath = Path.Combine(_testArtifactsDirectory, "extra-items.props"); + File.WriteAllText(itemsPath, """ + + + + + + + """); + try + { + File.Copy(Path.Combine(consumerDirectory, "Extras", "icon-192.png"), Path.Combine(consumerWwwroot, "images", "icon-192.png")); + + await PublishConsumerAsync(consumerDirectory, "browser-wasm", properties: ["PixelyBrowserInstallable=true", $"BrowserContentConsumerExtraItems={itemsPath}", $"PixelyBrowserIndexHtml={ownPagePath}"]); + string wwwroot = GetPublishedWwwroot(consumerDirectory); + using JsonDocument manifest = JsonDocument.Parse(File.ReadAllText(Path.Combine(wwwroot, "manifest.webmanifest"))); + Assert.Multiple(() => + { + Assert.That(manifest.RootElement.GetProperty("icons").EnumerateArray().Select(icon => icon.GetProperty("src").GetString()), Is.EqualTo(new[] { "images/icon-192.png", "icon-512.png" })); + Assert.That(File.Exists(Path.Combine(wwwroot, "images", "icon-192.png")), Is.True); + Assert.That(File.Exists(Path.Combine(wwwroot, "icon-192.png")), Is.False); + Assert.That(File.ReadAllText(Path.Combine(wwwroot, "index.html")), Is.EqualTo(ownPage)); + }); + } + finally + { + DeleteDirectory(consumerWwwroot); + } + } + + [Test] + public async Task BrowserBuildOfAnInstallableAppWithoutBothIconsFailsWithPixely0016() + { + string consumerDirectory = GetConsumerDirectory("BrowserContentConsumer"); + DeleteConsumerOutputs("BrowserContentConsumer"); + + string output = await BuildConsumerAsync(consumerDirectory, runtimeIdentifier: "browser-wasm", expectSuccess: false, properties: ["PixelyBrowserInstallable=true"]); + Assert.That(output, Does.Contain("error PIXELY0016").And.Contain("192x192 and 512x512")); + } + // On the desktop the item is inert and the loader finds the Content directory through the project tree. [Test] public async Task DesktopBuildIgnoresPixelyBrowserVfsFile()