# Cyclotron for Windows: https://cyclotron.videogame.ai # # irm https://cyclotron.videogame.ai/install.ps1 | iex # # Opens Cyclotron with a starter racing game in %USERPROFILE%\Cyclotron\my-race. Run it again to reopen that game. # It uses the Node.js on PATH when that is version 24 or later for x64; otherwise it downloads Node.js 24 from # nodejs.org into %USERPROFILE%\.volter\node, checks it against nodejs.org's published SHA-256, and changes nothing # outside your home folder. npm runs through its .cmd files: PowerShell's default policy refuses npm's .ps1 ones # ("running scripts is disabled on this system"). # One script block, so nothing is left defined in your PowerShell, and a failure returns to the prompt instead of # closing the window. & { $ErrorActionPreference = 'Stop' # Windows PowerShell's progress bar slows Invoke-WebRequest's downloads many times over. $ProgressPreference = 'SilentlyContinue' try { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 } catch {} # Cyclotron's editor for Windows is built for x64; on Windows on Arm, x64 Node.js runs it under emulation. function Test-Node([string]$Exe) { try { $kind = & $Exe -p "process.versions.node.split('.')[0] + ' ' + process.arch" 2>$null } catch { return $false } if ("$kind" -notmatch '^(\d+) (\S+)$') { return $false } return ([int]$Matches[1] -ge 24) -and ($Matches[2] -eq 'x64') } function Install-Node([string]$Target) { $base = 'https://nodejs.org/dist/latest-v24.x' $sums = (Invoke-WebRequest -UseBasicParsing "$base/SHASUMS256.txt").Content if ($sums -is [byte[]]) { $sums = [Text.Encoding]::ASCII.GetString($sums) } $line = @($sums -split "`n" | Where-Object { $_.Trim() -match '^[0-9a-f]{64} node-v24\.\d+\.\d+-win-x64\.zip$' })[0] if (-not $line) { throw 'nodejs.org lists no Node.js 24 for Windows x64.' } $sum, $file = $line.Trim() -split ' ' $work = Join-Path ([IO.Path]::GetTempPath()) ('cyclotron-' + [guid]::NewGuid()) New-Item -ItemType Directory $work | Out-Null try { $zip = Join-Path $work $file Write-Host "Downloading Node.js for Cyclotron ($file) from nodejs.org..." Invoke-WebRequest -UseBasicParsing "$base/$file" -OutFile $zip if ((Get-FileHash $zip -Algorithm SHA256).Hash -ne $sum) { throw "The Node.js download doesn't match nodejs.org's checksum." } $unpacked = Join-Path $work 'unpacked' New-Item -ItemType Directory $unpacked | Out-Null $tar = Join-Path $env:SystemRoot 'System32\tar.exe' if (Test-Path $tar) { & $tar -xf $zip -C $unpacked if ($LASTEXITCODE -ne 0) { throw "Couldn't unpack Node.js." } } else { Expand-Archive $zip $unpacked } if (Test-Path $Target) { Remove-Item $Target -Recurse -Force } New-Item -ItemType Directory (Split-Path $Target) -Force | Out-Null Move-Item (Join-Path $unpacked ($file -replace '\.zip$', '')) $Target } finally { Remove-Item $work -Recurse -Force -ErrorAction SilentlyContinue } } # AN INTERRUPTED FIRST DOWNLOAD (Ctrl+C, a dropped connection) can leave npx a cache entry that holds Cyclotron but # not the package.json npx writes last. npx then finds Cyclotron there, installs nothing, and fails on every run # ("Could not read package.json ... _npx"). With the entry gone npx downloads again. Only such entries are removed: # one without Cyclotron in it is another package's, or one npx fills on its next run. function Repair-NpxCache([string]$Npm) { # npm can print a warning on stderr (an unknown config key), which this block's Stop would turn into an error; # in here (this function's scope only) a warning is just a warning. $ErrorActionPreference = 'Continue' # A path npm prints that can't be read (a drive that isn't there) is no repair, not an error of its own. try { $cache = "$(& $Npm config get cache 2>$null)".Trim() if (-not $cache) { return $false } $broken = @(Get-ChildItem -LiteralPath ([IO.Path]::Combine($cache, '_npx')) -Directory -ErrorAction SilentlyContinue | Where-Object { -not (Test-Path -LiteralPath (Join-Path $_.FullName 'package.json')) -and (Test-Path -LiteralPath (Join-Path $_.FullName 'node_modules\@volter\cyclotron')) }) } catch { return $false } # Not Remove-Item -Recurse, which in Windows PowerShell 5.1 can follow a junction out of the folder. $removed = 0 foreach ($entry in $broken) { try { [IO.Directory]::Delete($entry.FullName, $true); $removed++ } catch {} } return $removed -gt 0 } # A GAME KEEPS THE CYCLOTRON IT WAS MADE WITH, so pasting this line again opens that one, fixes or not. When # npm has a newer Cyclotron, say so and how to move the game to it; nothing here changes the game. $PathLine is # the line the advice needs when this script put its own Node.js on PATH (only this window has it). npm's # commands are named .cmd: PowerShell's default policy refuses their .ps1 files. function Show-NewerRelease([string]$Node, [string]$PathLine) { $ErrorActionPreference = 'Continue' try { # npm view's --fetch-timeout starts only once a connection is made; Node asks the registry itself, cut off at 5 s. $js = 'const a = require(`./node_modules/@volter/cyclotron/package.json`).version, n = v => /^\d+\.\d+\.\d+$/.test(v) && v.split(`.`).map(Number); fetch((process.env.npm_config_registry || `https://registry.npmjs.org/`).replace(/\/*$/, `/@volter%2fcyclotron`), { headers: { accept: `application/vnd.npm.install-v1+json` }, signal: AbortSignal.timeout(5000) }).then(r => r.json()).then(p => { const b = p[`dist-tags`].latest, x = n(a), y = n(b); if (x && y && (y[0] - x[0] || y[1] - x[1] || y[2] - x[2]) > 0) console.log(b + ` ` + a) }, () => {}).finally(() => setTimeout(() => process.exit(), 100).unref())' $out = "$(& $Node -e $js 2>$null)".Trim() if ($out -notmatch '^(\d+\.\d+\.\d+) (\d+\.\d+\.\d+)$') { return } Write-Host "Cyclotron $($Matches[1]) is out; this game is on $($Matches[2]). To move it to $($Matches[1]), close Cyclotron, then run: cd '$((Get-Location).ProviderPath)'; ${PathLine}npx.cmd @volter/cyclotron@latest upgrade; npm.cmd install" } catch {} } # The home folder Node.js, and so Cyclotron, uses. $homeDir = if ($env:USERPROFILE) { $env:USERPROFILE } else { $HOME } # npm's own notices (a newer npm, funding, audit) aren't for someone opening Cyclotron. $env:npm_config_update_notifier = 'false'; $env:npm_config_fund = 'false'; $env:npm_config_audit = 'false' $nodeHome = Join-Path $homeDir '.volter\node' $games = Join-Path $homeDir 'Cyclotron' $game = Join-Path $games 'my-race' try { $pathLine = '' $node = @(Get-Command node.exe -CommandType Application -ErrorAction SilentlyContinue)[0].Source if (-not $node -or -not (Test-Node $node)) { $node = Join-Path $nodeHome 'node.exe' if (-not (Test-Path $node) -or -not (Test-Node $node)) { Install-Node $nodeHome } $env:Path = "$nodeHome;$env:Path" $pathLine = "`$env:Path = '$nodeHome;' + `$env:Path; " } $bin = Split-Path $node $npm = Join-Path $bin 'npm.cmd' $npx = Join-Path $bin 'npx.cmd' if (-not (Test-Path $npx)) { $npm = @(Get-Command npm.cmd -CommandType Application -ErrorAction SilentlyContinue)[0].Source $npx = @(Get-Command npx.cmd -CommandType Application -ErrorAction SilentlyContinue)[0].Source if (-not $npx) { throw "Node.js at $bin has no npx." } } if (Test-Path (Join-Path $game 'volter.project.json')) { Push-Location $game try { # A first run that stopped during its install left the game without its packages. if (-not (Test-Path 'node_modules\@volter\cyclotron')) { Write-Host "Installing your game's packages..." & $npm install if ($LASTEXITCODE -ne 0) { throw "npm couldn't install your game's packages." } } Show-NewerRelease $node $pathLine Write-Host "Opening your game at $game..." & $npx --no-install cyclotron edit . if ($LASTEXITCODE -ne 0) { throw "Cyclotron didn't open; the lines above say why." } } finally { Pop-Location } } elseif (Test-Path $game) { throw "$game is already there and isn't a Cyclotron game. Move it, then run this again." } else { New-Item -ItemType Directory $games -Force | Out-Null Push-Location $games try { # npx says nothing while it downloads Cyclotron, about half a minute on a first run. Write-Host "Downloading Cyclotron, then making a starter racing game at $game..." & $npx --yes '@volter/cyclotron@latest' create my-race --template playable # Kept, because npm config (in Repair-NpxCache) resets $LASTEXITCODE. $code = $LASTEXITCODE if ($code -ne 0 -and -not (Test-Path $game) -and (Repair-NpxCache $npm)) { Write-Host 'An earlier download of Cyclotron was cut short; downloading it again...' & $npx --yes '@volter/cyclotron@latest' create my-race --template playable $code = $LASTEXITCODE } if ($code -ne 0) { throw "Cyclotron didn't open; the lines above say why." } } finally { Pop-Location } } } catch { Write-Host "Cyclotron: $($_.Exception.Message)" -ForegroundColor Red } }