[CmdletBinding()] param( [string]$OutDir = "dist", [string]$Tag = "", [switch]$Publish, [string]$ForgeUrl = "https://eureka.gamecult.org/forge", [string]$Repo = "GameCult/RustToDust", [string]$Token = "", [string]$GodotExe = "", [string]$YmirRoot = "", [string]$CultLibRoot = "" ) Set-StrictMode -Version Latest $ErrorActionPreference = "Stop" try { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 } catch { # Ignore on runtimes where SecurityProtocol is managed by OS } $ExpectedYmirDllBytes = 776192L function Resolve-RepoPath { param([string]$PathValue, [string]$BaseDir) if ([System.IO.Path]::IsPathRooted($PathValue)) { return [System.IO.Path]::GetFullPath($PathValue) } return [System.IO.Path]::GetFullPath((Join-Path $BaseDir $PathValue)) } function Resolve-DependencyRoot { param( [string]$Explicit, [string]$EnvName, [string]$SiblingName, [string]$FallbackPath, [string]$RepoRoot ) if (-not [string]::IsNullOrWhiteSpace($Explicit) -and (Test-Path $Explicit)) { return (Resolve-Path $Explicit).Path } $envVal = [Environment]::GetEnvironmentVariable($EnvName) if (-not [string]::IsNullOrWhiteSpace($envVal) -and (Test-Path $envVal)) { return (Resolve-Path $envVal).Path } $sibling = Join-Path (Split-Path $RepoRoot -Parent) $SiblingName if (Test-Path $sibling) { return (Resolve-Path $sibling).Path } if (Test-Path $FallbackPath) { return (Resolve-Path $FallbackPath).Path } throw "Could not locate $SiblingName root. Set -$($SiblingName)Root or `$env:$EnvName." } function Resolve-GodotConsoleExe { param([string]$Explicit) if (-not [string]::IsNullOrWhiteSpace($Explicit)) { if (-not (Test-Path $Explicit)) { throw "Godot console executable not found at '$Explicit'." } return (Resolve-Path $Explicit).Path } $candidates = @( $env:GODOT_CONSOLE_EXE, "F:\Applications\Godot_v4.7.2-stable_mono_win64\Godot_v4.7.2-stable_mono_win64_console.exe", "C:\Program Files\Godot\Godot_v4.7.2-stable_mono_win64_console.exe" ) foreach ($c in $candidates) { if (-not [string]::IsNullOrWhiteSpace($c) -and (Test-Path $c)) { return (Resolve-Path $c).Path } } $cmd = Get-Command "Godot_v4.7.2-stable_mono_win64_console.exe" -ErrorAction SilentlyContinue if ($cmd) { return $cmd.Source } throw "Could not locate Godot_v4.7.2-stable_mono_win64_console.exe. Pass -GodotExe or set GODOT_CONSOLE_EXE." } function Resolve-ForgejoToken { param([string]$Explicit, [string]$ForgeBaseUrl) if (-not [string]::IsNullOrWhiteSpace($Explicit)) { return $Explicit.Trim() } if (-not [string]::IsNullOrWhiteSpace($env:FORGEJO_TOKEN)) { return $env:FORGEJO_TOKEN.Trim() } $eurekaTokenPath = Join-Path $env:USERPROFILE ".config\forgejo\eureka-pr-token" if (Test-Path $eurekaTokenPath) { $tok = (Get-Content $eurekaTokenPath -Raw).Trim() if (-not [string]::IsNullOrWhiteSpace($tok)) { return $tok } } $teaPaths = @( (Join-Path $env:USERPROFILE ".config\tea\config.yml"), (Join-Path $env:APPDATA "tea\config.yml") ) $forgeHost = ([Uri]$ForgeBaseUrl).Host foreach ($teaPath in $teaPaths) { if (Test-Path $teaPath) { $blockUrl = "" $blockToken = "" foreach ($line in (Get-Content $teaPath)) { if ($line -match "^\s*-\s+") { if ($blockUrl -match [regex]::Escape($forgeHost) -and -not [string]::IsNullOrWhiteSpace($blockToken)) { return $blockToken } $blockUrl = "" $blockToken = "" } if ($line -match "url:\s*(.+)") { $blockUrl = $Matches[1].Trim().Trim('"').Trim("'") } elseif ($line -match "token:\s*(.+)") { $blockToken = $Matches[1].Trim().Trim('"').Trim("'") } } if ($blockUrl -match [regex]::Escape($forgeHost) -and -not [string]::IsNullOrWhiteSpace($blockToken)) { return $blockToken } } } $gitCredPath = Join-Path $env:USERPROFILE ".git-credentials" if (Test-Path $gitCredPath) { foreach ($line in Get-Content $gitCredPath) { if ($line -match "^https?://[^:]+:([^@]+)@$([regex]::Escape($forgeHost))") { return [Uri]::UnescapeDataString($Matches[1]) } } } throw "Forgejo token not found. Pass -Token, set `$env:FORGEJO_TOKEN, or store a token in ~/.config/forgejo/eureka-pr-token or ~/.config/tea/config.yml." } function Copy-FilteredTree { param( [string]$SourceDir, [string]$DestDir, [string[]]$IncludeExtensions ) if (-not (Test-Path $SourceDir)) { throw "Required asset source directory missing: '$SourceDir'" } $srcFull = (Resolve-Path $SourceDir).Path.TrimEnd('\', '/') New-Item -ItemType Directory -Force -Path $DestDir | Out-Null $files = Get-ChildItem -Path $srcFull -Recurse -File $count = 0 foreach ($file in $files) { if ($IncludeExtensions -notcontains $file.Extension.ToLowerInvariant()) { continue } $rel = $file.FullName.Substring($srcFull.Length + 1) $target = Join-Path $DestDir $rel $targetParent = Split-Path $target -Parent if (-not (Test-Path $targetParent)) { New-Item -ItemType Directory -Force -Path $targetParent | Out-Null } Copy-Item -Path $file.FullName -Destination $target -Force $count++ } return $count } function Invoke-ForgeApiJson { param( [string]$Method, [string]$Url, [string]$Token, [object]$Body = $null ) $headers = @{ "Authorization" = "token $Token" "Accept" = "application/json" } $params = @{ Method = $Method Uri = $Url Headers = $headers ErrorAction = "Stop" } if ($null -ne $Body) { $params["ContentType"] = "application/json" $params["Body"] = ($Body | ConvertTo-Json -Depth 6) } return Invoke-RestMethod @params } $RepoRoot = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path $ClientDir = Join-Path $RepoRoot "client" $AssetsSrcDir = Join-Path $RepoRoot "assets" $fullSha = (& git -C $RepoRoot rev-parse HEAD).Trim() if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($fullSha)) { throw "Failed to resolve git HEAD SHA." } if ([string]::IsNullOrWhiteSpace($Tag)) { $shortSha = (& git -C $RepoRoot rev-parse --short HEAD).Trim() if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($shortSha)) { throw "Failed to resolve git short SHA for default tag." } $dateStamp = (Get-Date).ToUniversalTime().ToString("yyyyMMdd") $Tag = "rtd-$dateStamp-$shortSha" } $ResolvedOutDir = Resolve-RepoPath -PathValue $OutDir -BaseDir $RepoRoot $StageDir = Join-Path $ResolvedOutDir "stage-$Tag" $ZipName = "RustToDust-win64-$Tag.zip" $ZipPath = Join-Path $ResolvedOutDir $ZipName $ShaPath = Join-Path $ResolvedOutDir "SHA256SUMS.txt" $YmirRoot = Resolve-DependencyRoot -Explicit $YmirRoot -EnvName "YMIR_ROOT" -SiblingName "Ymir" -FallbackPath "F:\Projects\Ymir" -RepoRoot $RepoRoot $CultLibRoot = Resolve-DependencyRoot -Explicit $CultLibRoot -EnvName "CULTLIB_ROOT" -SiblingName "CultLib" -FallbackPath "F:\Projects\CultLib" -RepoRoot $RepoRoot $GodotConsole = Resolve-GodotConsoleExe -Explicit $GodotExe $TemplateDir = Join-Path $env:APPDATA "Godot\export_templates\4.7.2.stable.mono" if (-not (Test-Path $TemplateDir)) { throw "Godot 4.7.2.stable.mono export templates missing at '$TemplateDir'." } $env:YMIR_ROOT = $YmirRoot $env:CULTLIB_ROOT = $CultLibRoot $env:MSBUILDDISABLENODEREUSE = "1" $env:DOTNET_CLI_DO_NOT_USE_MSBUILD_SERVER = "1" $env:UseSharedCompilation = "false" Write-Host "=== Rust to Dust win64 Release Build ===" Write-Host "RepoRoot : $RepoRoot" Write-Host "Tag : $Tag" Write-Host "YmirRoot : $YmirRoot" Write-Host "CultLibRoot: $CultLibRoot" Write-Host "Godot : $GodotConsole" Write-Host "OutDir : $ResolvedOutDir" # Step 1: Build Release ymir_box3d.dll and verify exact Release binary size Write-Host "`n[1/5] Building Release ymir_box3d.dll..." $ymirProj = Join-Path $YmirRoot "src\Ymir.Box3D\Ymir.Box3D.csproj" & dotnet build $ymirProj -c Release -p:YmirNativeConfiguration=Release --disable-build-servers -p:UseSharedCompilation=false -nr:false if ($LASTEXITCODE -ne 0) { throw "dotnet build Ymir.Box3D failed with exit code $LASTEXITCODE." } $ymirDllPath = Join-Path $YmirRoot "artifacts\box3d-native\production\Release\bin\Release\ymir_box3d.dll" if (-not (Test-Path $ymirDllPath)) { throw "Release ymir_box3d.dll not found at '$ymirDllPath'." } $ymirDllLen = (Get-Item $ymirDllPath).Length if ($ymirDllLen -ne $ExpectedYmirDllBytes) { throw "ymir_box3d.dll size mismatch: expected $ExpectedYmirDllBytes bytes (Release), got $ymirDllLen bytes at '$ymirDllPath'." } Write-Host "Verified Release ymir_box3d.dll ($ymirDllLen B)." # Step 2: Export Windows Desktop Release via Godot Write-Host "`n[2/5] Exporting Windows Desktop Release..." if (Test-Path $StageDir) { Remove-Item -Recurse -Force $StageDir } New-Item -ItemType Directory -Force -Path $StageDir | Out-Null $exportExePath = Join-Path $StageDir "RustToDust.exe" $clientSlnPath = Join-Path $ClientDir "RustToDust.Client.sln" $createdTempSln = $false try { if (-not (Test-Path $clientSlnPath)) { Write-Host "Creating temporary client/RustToDust.Client.sln for Godot export..." & dotnet new sln -n "RustToDust.Client" -o $ClientDir --format sln | Out-Null if ($LASTEXITCODE -ne 0) { throw "dotnet new sln failed with exit code $LASTEXITCODE." } $createdTempSln = $true & dotnet sln $clientSlnPath add (Join-Path $ClientDir "RustToDust.Client.csproj") | Out-Null if ($LASTEXITCODE -ne 0) { throw "dotnet sln add failed with exit code $LASTEXITCODE." } } if (-not (Test-Path (Join-Path $ClientDir ".godot"))) { Write-Host "Bootstrapping client/.godot import cache (headless)..." & $GodotConsole --headless --audio-driver Dummy --path $ClientDir --import --quit if ($LASTEXITCODE -ne 0) { throw "Godot headless --import failed with exit code $LASTEXITCODE." } } & $GodotConsole --headless --audio-driver Dummy --path $ClientDir --quit --export-release "Windows Desktop" $exportExePath if ($LASTEXITCODE -ne 0) { throw "Godot --export-release failed with exit code $LASTEXITCODE." } } finally { if ($createdTempSln -and (Test-Path $clientSlnPath)) { Remove-Item -Force $clientSlnPath -ErrorAction SilentlyContinue } } if (-not (Test-Path $exportExePath)) { throw "Expected exported executable missing: '$exportExePath'." } $dataDirCandidates = @( (Join-Path $StageDir "data_RustToDust_windows_x86_64"), (Join-Path $StageDir "data_RustToDust.Client_windows_x86_64") ) $stagedDataDir = $dataDirCandidates | Where-Object { Test-Path $_ } | Select-Object -First 1 if (-not $stagedDataDir) { throw "Expected Godot Mono data directory missing under '$StageDir'." } # Remove PDBs if any slipped through Get-ChildItem -Path $StageDir -Recurse -Filter "*.pdb" -File | Remove-Item -Force # Step 3: Stage Release ymir_box3d.dll & runtime assets/ Write-Host "`n[3/5] Staging Release ymir_box3d.dll and runtime assets/..." Copy-Item -Path $ymirDllPath -Destination (Join-Path $StageDir "ymir_box3d.dll") -Force Copy-Item -Path $ymirDllPath -Destination (Join-Path $stagedDataDir "ymir_box3d.dll") -Force $stagedAssetsDir = Join-Path $StageDir "assets" $vCount = Copy-FilteredTree -SourceDir (Join-Path $AssetsSrcDir "vehicles") -DestDir (Join-Path $stagedAssetsDir "vehicles") -IncludeExtensions @(".glb", ".cc", ".json") $gCount = Copy-FilteredTree -SourceDir (Join-Path $AssetsSrcDir "gear") -DestDir (Join-Path $stagedAssetsDir "gear") -IncludeExtensions @(".glb") $dCount = Copy-FilteredTree -SourceDir (Join-Path $AssetsSrcDir "decals") -DestDir (Join-Path $stagedAssetsDir "decals") -IncludeExtensions @(".png") $aCount = Copy-FilteredTree -SourceDir (Join-Path $AssetsSrcDir "audio") -DestDir (Join-Path $stagedAssetsDir "audio") -IncludeExtensions @(".wav", ".md") $mCount = Copy-FilteredTree -SourceDir (Join-Path $AssetsSrcDir "maps\sion") -DestDir (Join-Path $stagedAssetsDir "maps\sion") -IncludeExtensions @(".json", ".f32", ".png", ".bin", ".glb") Write-Host "Staged runtime assets: vehicles=$vCount gear=$gCount decals=$dCount audio=$aCount sion=$mCount" # Step 4: Create ZIP and SHA256SUMS.txt Write-Host "`n[4/5] Packaging $ZipName..." New-Item -ItemType Directory -Force -Path $ResolvedOutDir | Out-Null if (Test-Path $ZipPath) { Remove-Item -Force $ZipPath } Add-Type -AssemblyName System.IO.Compression.FileSystem [System.IO.Compression.ZipFile]::CreateFromDirectory($StageDir, $ZipPath, [System.IO.Compression.CompressionLevel]::Optimal, $false) $zipInfo = Get-Item $ZipPath $zipHash = (Get-FileHash -Path $ZipPath -Algorithm SHA256).Hash.ToLowerInvariant() "$zipHash $ZipName" | Set-Content -Path $ShaPath -Encoding ASCII Write-Host "Created $ZipPath ($($zipInfo.Length) B, sha256=$zipHash)" # Remove intermediate stage directory so dist/ holds clean release outputs Remove-Item -Recurse -Force $StageDir # Step 5: Optional Forgejo Release publish if ($Publish) { Write-Host "`n[5/5] Publishing release '$Tag' to $ForgeUrl ($Repo)..." $resolvedToken = Resolve-ForgejoToken -Explicit $Token -ForgeBaseUrl $ForgeUrl $apiBase = "$($ForgeUrl.TrimEnd('/'))/api/v1/repos/$Repo" $release = $null try { $release = Invoke-ForgeApiJson -Method "GET" -Url "$apiBase/releases/tags/$Tag" -Token $resolvedToken Write-Host "Found existing release id=$($release.id) for tag '$Tag'." } catch { $statusCode = $null $ex = $_.Exception if ($ex -is [System.Net.WebException] -and $ex.Response) { $statusCode = [int]$ex.Response.StatusCode } elseif ($ex.PSObject.Properties['Response'] -and $ex.Response -and $ex.Response.PSObject.Properties['StatusCode']) { $statusCode = [int]$ex.Response.StatusCode } if ($statusCode -ne 404) { throw } } $releaseNotes = "Standalone Windows x64 build ($Tag). Unzip and run RustToDust.exe." if ($null -eq $release) { $release = Invoke-ForgeApiJson -Method "POST" -Url "$apiBase/releases" -Token $resolvedToken -Body @{ tag_name = $Tag target_commitish = $fullSha name = $Tag body = $releaseNotes draft = $false prerelease = $false } Write-Host "Created release id=$($release.id) for tag '$Tag' ($fullSha)." } else { $release = Invoke-ForgeApiJson -Method "PATCH" -Url "$apiBase/releases/$($release.id)" -Token $resolvedToken -Body @{ name = $Tag body = $releaseNotes draft = $false prerelease = $false } } foreach ($assetPath in @($ZipPath, $ShaPath)) { $assetName = [System.IO.Path]::GetFileName($assetPath) if ($release.assets) { foreach ($existing in $release.assets) { if ($existing.name -eq $assetName) { Write-Host "Removing existing release asset '$assetName' (id=$($existing.id))..." Invoke-ForgeApiJson -Method "DELETE" -Url "$apiBase/releases/$($release.id)/assets/$($existing.id)" -Token $resolvedToken | Out-Null } } } Write-Host "Uploading '$assetName'..." $uploadUrl = "$apiBase/releases/$($release.id)/assets?name=$([Uri]::EscapeDataString($assetName))" & curl.exe -fsS -X POST -H "Authorization: token $resolvedToken" -F "attachment=@$assetPath" $uploadUrl | Out-Null if ($LASTEXITCODE -ne 0) { throw "curl.exe failed uploading '$assetName' (exit $LASTEXITCODE)." } } Write-Host "Published: $($ForgeUrl.TrimEnd('/'))/$Repo/releases/tag/$Tag" } else { Write-Host "`n[5/5] Skipping Forgejo publish (-Publish not set)." }