drAIML / webgpu /installer /webgpu-enable.ps1
Gregory-L's picture
Professor Codephreak
payout.py review; sign-in URI and expiry checks; installer undo keeps your own Firefox pref
1464612 verified
Raw History Blame Contribute Delete
7.98 kB
# SPDX-License-Identifier: MIT
# drAIML WebGPU installer (Windows). WebGPU is part of the browser; nothing is downloaded. This switches it on where the
# browser has it turned off, exactly as chrome://flags or about:config would:
#
# Chrome, Chromium, Brave, Edge, Vivaldi, Opera: #enable-unsafe-webgpu (+ #ignore-gpu-blocklist with -Blocklist)
# in each browser's "Local State" (where chrome://flags keeps them)
# Firefox (-Firefox): user_pref("dom.webgpu.enabled", true) in each profile's user.js
#
# Close the browser first. Usage (from the folder holding this file):
# powershell -ExecutionPolicy Bypass -File webgpu-enable.ps1 [-Firefox] [-Blocklist] [-DryRun] [-Undo] [-Dir PATH]
param([switch]$Firefox, [switch]$Blocklist, [switch]$DryRun, [switch]$Undo, [string]$Dir = "")
$ErrorActionPreference = "Stop"
$flags = @("enable-unsafe-webgpu")
if ($Blocklist) { $flags += "ignore-gpu-blocklist" }
$L = $env:LOCALAPPDATA; $R = $env:APPDATA
$chromium = @(
@{ dir = "$L\Google\Chrome\User Data"; exe = "chrome" }, @{ dir = "$L\Google\Chrome Beta\User Data"; exe = "chrome" },
@{ dir = "$L\Chromium\User Data"; exe = "chrome" }, @{ dir = "$L\BraveSoftware\Brave-Browser\User Data"; exe = "brave" },
@{ dir = "$L\Microsoft\Edge\User Data"; exe = "msedge" }, @{ dir = "$L\Vivaldi\User Data"; exe = "vivaldi" },
@{ dir = "$R\Opera Software\Opera Stable"; exe = "opera" })
$changed = 0; $skipped = 0
function Test-Running($exe, $dir) {
# a running Chromium holds "lockfile" open exclusively; fall back to the process name
$lock = Join-Path $dir "lockfile"
if (Test-Path $lock) {
try { $s = [IO.File]::Open($lock, 'Open', 'ReadWrite', 'None'); $s.Close(); return $false } catch { return $true }
}
return [bool](Get-Process -Name $exe -ErrorAction SilentlyContinue)
}
# Lossless JSON: System.Text.Json on PowerShell 7, JavaScriptSerializer on Windows PowerShell 5.1 (both keep keys that
# differ only in case, which ConvertFrom-Json refuses)
function Read-Labs($path) {
$text = [IO.File]::ReadAllText($path)
if ($PSVersionTable.PSVersion.Major -ge 7) {
$doc = [System.Text.Json.Nodes.JsonNode]::Parse($text)
if ($null -eq $doc["browser"]) { $doc["browser"] = [System.Text.Json.Nodes.JsonObject]::new() }
if ($null -eq $doc["browser"]["enabled_labs_experiments"]) { $doc["browser"]["enabled_labs_experiments"] = [System.Text.Json.Nodes.JsonArray]::new() }
$labs = @($doc["browser"]["enabled_labs_experiments"] | ForEach-Object { $_.ToString() }) # works on 7.0+
} else {
Add-Type -AssemblyName System.Web.Extensions
$js = New-Object System.Web.Script.Serialization.JavaScriptSerializer; $js.MaxJsonLength = [int]::MaxValue
$doc = $js.DeserializeObject($text)
if (-not $doc.ContainsKey("browser")) { $doc["browser"] = New-Object 'System.Collections.Generic.Dictionary[string,object]' }
if (-not $doc["browser"].ContainsKey("enabled_labs_experiments")) { $doc["browser"]["enabled_labs_experiments"] = @() }
$labs = @($doc["browser"]["enabled_labs_experiments"])
}
return @{ doc = $doc; labs = $labs }
}
function Write-Labs($path, $doc, [string[]]$labs) {
if ($PSVersionTable.PSVersion.Major -ge 7) {
$arr = [System.Text.Json.Nodes.JsonArray]::new(); foreach ($x in $labs) { $arr.Add([System.Text.Json.Nodes.JsonValue]::Create($x)) }
$doc["browser"]["enabled_labs_experiments"] = $arr; $text = $doc.ToJsonString()
} else {
$doc["browser"]["enabled_labs_experiments"] = [object[]]$labs
$js = New-Object System.Web.Script.Serialization.JavaScriptSerializer; $js.MaxJsonLength = [int]::MaxValue
$text = $js.Serialize($doc)
}
$tmp = "$path.tmp-webgpu"; [IO.File]::WriteAllText($tmp, $text, (New-Object Text.UTF8Encoding $false))
Move-Item -Force $tmp $path
}
function Set-Chromium($b) {
$state = Join-Path $b.dir "Local State"
if (-not (Test-Path $state)) { return }
if (Test-Running $b.exe $b.dir) { Write-Host "* $($b.dir) - browser is running: close it and run this again"; $script:skipped++; return }
$side = "$state.webgpu-helper.json"
$j = Read-Labs $state; $labs = $j.labs
if ($Undo) {
$mine = @(if (Test-Path $side) { (Get-Content $side -Raw | ConvertFrom-Json).added }) # @() keeps a one-item list a list
$what = @($labs | Where-Object { $mine -contains $_ }); $new = @($labs | Where-Object { $mine -notcontains $_ })
} else {
$names = @($labs | ForEach-Object { ($_ -split "@")[0] })
$what = @($flags | Where-Object { $names -notcontains (($_ -split "@")[0]) }); $new = @($labs) + $what
}
if ($what.Count -eq 0) { Write-Host "* $($b.dir) - unchanged"; return }
$verb = if ($Undo) { "remove" } else { "add" }
Write-Host "* $($b.dir) - $(if ($DryRun) { 'would ' })$verb $($what -join ' ')"
if ($DryRun) { return }
Copy-Item $state "$state.bak-webgpu-$(Get-Date -Format yyyyMMdd-HHmmss)"
Write-Labs $state $j.doc $new
if ($Undo) { Remove-Item -ErrorAction SilentlyContinue $side }
else {
$prev = @(if (Test-Path $side) { (Get-Content $side -Raw | ConvertFrom-Json).added })
@{ added = @(($prev + $what) | Sort-Object -Unique); by = "drAIML WebGPU helper" } | ConvertTo-Json | Set-Content -Encoding UTF8 $side
}
$script:changed++
}
$A = "// >>> drAIML WebGPU helper"; $B = "// <<< drAIML WebGPU helper"
$utf8 = New-Object Text.UTF8Encoding $false # no BOM: Windows PowerShell 5.1's -Encoding UTF8 would add one
function Set-Firefox($prof) {
if (-not (Test-Path (Join-Path $prof "prefs.js"))) { return }
$uj = Join-Path $prof "user.js"
if (Get-Process -Name firefox -ErrorAction SilentlyContinue) { Write-Host "* $prof - Firefox is running: close it and run this again"; $script:skipped++; return }
$has = (Test-Path $uj) -and ((Get-Content $uj -Raw) -like "*$A*")
if ($Undo) {
if (-not $has) { Write-Host "* $prof - unchanged"; return }
Write-Host "* $prof - $(if ($DryRun) { 'would ' })remove dom.webgpu.enabled"; if ($DryRun) { return }
$had = (Get-Content $uj) -contains "// had-pref: yes"
$keep = $true; $out = foreach ($ln in Get-Content $uj) { if ($ln -eq $A) { $keep = $false } elseif ($ln -eq $B) { $keep = $true } elseif ($keep) { $ln } }
[IO.File]::WriteAllLines($uj, [string[]]@($out), $utf8)
$p = Join-Path $prof "prefs.js"
if (-not $had) { [IO.File]::WriteAllLines($p, [string[]]@(Get-Content $p | Where-Object { $_ -notmatch '"dom\.webgpu\.enabled"' }), $utf8) }
} else {
if ($has) { Write-Host "* $prof - unchanged"; return }
Write-Host "* $prof - $(if ($DryRun) { 'would ' })add dom.webgpu.enabled = true"; if ($DryRun) { return }
$had = if ((Get-Content (Join-Path $prof "prefs.js") -Raw) -match '"dom\.webgpu\.enabled"') { "yes" } else { "no" }
[IO.File]::AppendAllText($uj, "$A`n// had-pref: $had`nuser_pref(`"dom.webgpu.enabled`", true);`n$B`n", $utf8)
}
$script:changed++
}
Write-Host "drAIML WebGPU installer - $(if ($Undo) { 'undo' } else { "flags: $($flags -join ' ')" })$(if ($DryRun) { ' (dry run)' })"
if ($Dir) {
if (Test-Path (Join-Path $Dir "Local State")) { Set-Chromium @{ dir = $Dir; exe = "none" } }
elseif (Test-Path (Join-Path $Dir "prefs.js")) { Set-Firefox $Dir }
else { Write-Error "$Dir is neither a Chromium user-data directory nor a Firefox profile" }
} else {
foreach ($b in $chromium) { Set-Chromium $b }
if ($Firefox -and (Test-Path "$R\Mozilla\Firefox\Profiles")) { Get-ChildItem -Directory "$R\Mozilla\Firefox\Profiles" | ForEach-Object { Set-Firefox $_.FullName } }
}
Write-Host ""
if ($changed -gt 0 -and -not $DryRun) {
Write-Host "Done: $changed changed. Start the browser and run the WebGPU helper's check again."
if ($skipped -gt 0) { Write-Host "$skipped running browser(s) were skipped: close them and run this again for those." }
}
elseif ($skipped -gt 0) { Write-Host "Nothing changed for $skipped running browser(s). Close them and run again."; exit 3 }
else { Write-Host "Nothing to change." }