2026-11-28 22:22:16 +00:00
<#
Updates the Steam Workshop item from the command line , through the running Steam client : no password ,
no game window , no dev mode . RimWorld may stay open .
. \ Tools \ workshop-upload . ps1 dry run : builds , stages a clean copy , checks the item and
that this Steam account owns it . Uploads nothing .
. \ Tools \ workshop-upload . ps1 -Publish uploads the staged copy . The change note is the CHANGELOG . md
entry for the version in About . xml .
-DescriptionFile docs \ workshop-description . txt also replaces the Workshop description ( BBCode )
2026-11-29 10:48:43 +00:00
-DescriptionOnly with -DescriptionFile: replaces only the description . No build ,
no content upload , no change note .
Every [ img ] in the description must load with an Access-Control -Allow -Origin header : Steam loads
description images as cross-origin requests and shows a broken image otherwise . -Publish refuses
when one doesn ' t .
2026-11-28 22:22:16 +00:00
-Publish refuses unless the shipped files are committed and pushed , the top CHANGELOG . md entry matches
About . xml ' s modVersion , and the last Tests \ run-tests . ps1 run passed after the last build .
-SkipChecks skips those checks . The in-game button ( Tools \ workshop-stage . ps1 ) still works as a fallback .
#>
param (
[ string ] $RimWorldDir = 'E:\SteamLibrary\steamapps\common\RimWorld' ,
[ switch ] $Publish ,
[ string ] $DescriptionFile ,
2026-11-29 10:48:43 +00:00
[ switch ] $DescriptionOnly ,
2026-11-28 22:22:16 +00:00
[ string ] $TestResults = ( Join-Path $env:TEMP 'BlaxSlavesTest\BlaxSlavesTestResults.txt' ) ,
[ switch ] $SkipChecks
)
$ErrorActionPreference = 'Stop'
$repo = Split-Path -Parent $PSScriptRoot
$ship = 'About' , '1.7' , 'Defs' , 'Patches' , 'Languages' , 'Textures' , 'Sounds'
$toolProj = Join-Path $PSScriptRoot 'WorkshopUpload\WorkshopUpload.csproj'
$toolExe = Join-Path $PSScriptRoot 'WorkshopUpload\bin\Release\WorkshopUpload.exe'
2026-11-29 10:48:43 +00:00
if ( $DescriptionOnly -and -not $DescriptionFile ) { throw '-DescriptionOnly needs -DescriptionFile.' }
# Steam adds crossorigin to description images, so a host without Access-Control-Allow-Origin shows a broken image.
function Test-DescriptionImages([string]$file ) {
[ Net.ServicePointManager ] :: SecurityProtocol = [ Net.SecurityProtocolType ] :: Tls12
$found = @ ( )
$text = [ IO.File ] :: ReadAllText ( ( Resolve-Path $file ) . Path , [ Text.Encoding ] :: UTF8 )
foreach ( $m in [ regex ] :: Matches ( $text , '\[img\](.*?)\[/img\]' ) ) {
$url = $m . Groups [ 1 ] . Value . Trim ( )
try {
$r = Invoke-WebRequest -Uri $url -UseBasicParsing -Headers @ { Origin = 'https://steamcommunity.com' }
$type = " $( $r . Headers [ 'Content-Type' ] ) "
$acao = " $( $r . Headers [ 'Access-Control-Allow-Origin' ] ) "
if ( $type -notlike 'image/*' ) { $found + = " $url is not an image ( $type ). " }
elseif ( $acao -ne '*' -and $acao -ne 'https://steamcommunity.com' ) { $found + = " $url sends no Access-Control-Allow-Origin header; Steam would show it broken. " }
}
catch { $found + = " $url does not load: $( $_ . Exception . Message ) " }
}
$found
}
2026-11-28 22:22:16 +00:00
$itemId = ( Get-Content -LiteralPath ( Join-Path $repo 'About\PublishedFileId.txt' ) -Raw ) . Trim ( )
$about = [ xml ] ( Get-Content -LiteralPath ( Join-Path $repo 'About\About.xml' ) -Raw -Encoding UTF8 )
$packageId = $about . ModMetaData . packageId . Trim ( )
$modVersion = " $( $about . ModMetaData . modVersion ) " . Trim ( )
# Same tags the in-game uploader sets: "Mod" plus Major.Minor of every supported version.
$tags = @ ( 'Mod' ) + @ ( $about . ModMetaData . supportedVersions . li | ForEach-Object { $v = [ version ] " $_ " . Trim ( ) ; " $( $v . Major ) . $( $v . Minor ) " } )
# Change note: every CHANGELOG.md entry newer than the last version published from here
# (Tools\workshop-published.txt, written after each successful -Publish).
$publishedFile = Join-Path $PSScriptRoot 'workshop-published.txt'
$lastPublished = if ( Test-Path $publishedFile ) { [ version ] ( Get-Content -LiteralPath $publishedFile -Raw ) . Trim ( ) } else { [ version ] '0.0' }
$sections = [ ordered ] @ { }
$current = $null
foreach ( $line in Get-Content -LiteralPath ( Join-Path $repo 'CHANGELOG.md' ) -Encoding UTF8 ) {
if ( $line -match '^##\s+(.+)$' ) { $current = $Matches [ 1 ] . Trim ( ) ; $sections [ $current ] = New-Object System . Collections . ArrayList ; continue }
if ( $current -and $line . Trim ( ) ) { [ void ] $sections [ $current ] . Add ( $line . TrimEnd ( ) ) }
}
if ( $sections . Count -eq 0 ) { throw 'CHANGELOG.md has no "## <version>" entry.' }
$topVersion = @ ( $sections . Keys ) [ 0 ]
$noteLines = @ ( )
foreach ( $v in $sections . Keys ) {
if ( [ version ] $v -le $lastPublished ) { break }
if ( $noteLines ) { $noteLines + = '' }
$noteLines + = @ ( " $( $about . ModMetaData . name ) $v " ) + $sections [ $v ]
}
if ( -not $noteLines ) { $noteLines = @ ( " $( $about . ModMetaData . name ) $topVersion " ) + $sections [ $topVersion ] }
$note = ( $noteLines -join " `n " ) . Trim ( )
2026-11-29 10:48:43 +00:00
if ( -not $DescriptionOnly ) {
foreach ( $proj in Get-ChildItem ( Join-Path $repo 'Source\*\*.csproj' ) ) {
& dotnet build $proj . FullName -c Release -nologo -v q " -p:RimWorldDir= $RimWorldDir "
if ( $LASTEXITCODE -ne 0 ) { throw " build failed: $( $proj . Name ) " }
}
2026-11-28 22:22:16 +00:00
}
& dotnet build $toolProj -c Release -nologo -v q " -p:RimWorldDir= $RimWorldDir "
if ( $LASTEXITCODE -ne 0 ) { throw 'build failed: WorkshopUpload' }
2026-11-29 10:48:43 +00:00
$imageProblems = @ ( )
if ( $DescriptionFile ) {
$imageProblems = @ ( Test-DescriptionImages $DescriptionFile )
foreach ( $p in $imageProblems ) { Write-Warning $p }
}
2026-11-28 22:22:16 +00:00
if ( $Publish -and -not $SkipChecks ) {
2026-11-29 10:48:43 +00:00
$problems = @ ( ) + $imageProblems
2026-11-28 22:22:16 +00:00
$ahead = & git -C $repo rev-list - -count '@{u}..HEAD' 2 > $null
if ( $LASTEXITCODE -ne 0 ) { $problems + = 'The branch has no upstream to compare with.' }
2026-11-29 10:48:43 +00:00
elseif ( [ int ] $ahead -gt 0 ) { $problems + = " $ahead commit(s) not pushed yet, so the Workshop would show what the repo doesn't. " }
if ( $DescriptionOnly ) {
$dirty = & git -C $repo status - -porcelain - - ( Resolve-Path $DescriptionFile ) . Path
if ( $dirty ) { $problems + = " The description file has uncommitted changes: $dirty " }
}
2026-11-28 22:22:16 +00:00
else {
2026-11-29 10:48:43 +00:00
if ( $topVersion -ne $modVersion ) { $problems + = " CHANGELOG.md's top entry is $topVersion , About.xml says $modVersion . " }
$dirty = & git -C $repo status - -porcelain - - $ship
if ( $dirty ) { $problems + = " Uncommitted changes in shipped files: `n $( $dirty -join " `n " ) " }
$dll = Get-ChildItem ( Join-Path $repo '1.7\Assemblies\*.dll' ) | Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ( -not ( Test-Path $TestResults ) ) { $problems + = " No test results at $TestResults . Run .\Tests\run-tests.ps1 first. " }
else {
$res = Get-Item $TestResults
$lines = Get-Content -LiteralPath $TestResults -Encoding UTF8
if ( -not ( $lines -match '^END$' ) -or ( $lines -match '^FAIL ' ) ) { $problems + = 'The last test run failed or did not finish.' }
elseif ( $dll -and $res . LastWriteTime -lt $dll . LastWriteTime ) { $problems + = " The last test run ( $( $res . LastWriteTime ) ) is older than the build ( $( $dll . LastWriteTime ) ). Run .\Tests\run-tests.ps1 again. " }
}
2026-11-28 22:22:16 +00:00
}
if ( $problems ) { throw ( " Not publishing: `n - " + ( $problems -join " `n - " ) ) }
}
2026-11-29 10:48:43 +00:00
if ( $DescriptionOnly ) {
$toolArgs = @ ( '--item' , $itemId , '--description-file' , ( Resolve-Path $DescriptionFile ) . Path )
2026-11-28 22:22:16 +00:00
}
2026-11-29 10:48:43 +00:00
else {
# Clean copy outside the repo: only what players need, no PDBs. TEMP has no spaces in its path.
$root = Join-Path $env:TEMP 'BlaxWorkshopUpload'
$stage = Join-Path $root $packageId
if ( Test-Path $stage ) { Remove-Item $stage -Recurse -Force }
New-Item -ItemType Directory $stage -Force | Out-Null
foreach ( $name in $ship ) {
$src = Join-Path $repo $name
if ( Test-Path $src ) { Copy-Item $src ( Join-Path $stage $name ) -Recurse }
}
Get-ChildItem $stage -Recurse -File -Filter * . pdb | Remove-Item
$noteFile = Join-Path $root " $packageId .note.txt "
[ IO.File ] :: WriteAllText ( $noteFile , $note , ( New-Object Text . UTF8Encoding $false ) )
2026-11-28 22:22:16 +00:00
2026-11-29 10:48:43 +00:00
$toolArgs = @ ( '--item' , $itemId , '--content' , $stage , '--note-file' , $noteFile , '--tags' , ( $tags -join ',' ) )
if ( $DescriptionFile ) { $toolArgs + = @ ( '--description-file' , ( Resolve-Path $DescriptionFile ) . Path ) }
}
2026-11-28 22:22:16 +00:00
if ( $Publish ) { $toolArgs + = '--publish' }
# steam_api writes status lines to stderr; under 'Stop' PowerShell 5.1 would turn them into errors.
$ErrorActionPreference = 'Continue'
Push-Location ( Split-Path $toolExe )
try {
& $toolExe @toolArgs 2 > & 1 | ForEach-Object { " $_ " } | Where-Object { $_ -notmatch '^(Setting breakpad|SteamInternal_)' }
$code = $LASTEXITCODE
}
finally { Pop-Location }
2026-11-29 10:48:43 +00:00
if ( $Publish -and $code -eq 0 -and -not $DescriptionOnly ) {
2026-11-28 22:22:16 +00:00
[ IO.File ] :: WriteAllText ( $publishedFile , " $topVersion `n " )
Write-Host " Recorded $topVersion in Tools\workshop-published.txt. Commit it. "
}
exit $code