229 lines
14 KiB
PowerShell
229 lines
14 KiB
PowerShell
<#
|
|
Runs the in-game test suite. RimWorld 1.7 starts headless (no window, no focus change) with its
|
|
own save data folder, so your mod list, saves and settings stay untouched. Needs Steam running and
|
|
the Harmony mod from the Workshop. Exit code: 0 all passed, 1 failures, 2 the run didn't finish.
|
|
|
|
.\Tests\run-tests.ps1 Harmony, the installed DLCs, Blax Zones
|
|
.\Tests\run-tests.ps1 -Compat also Blax Castes, Blax Slaves and the installed mods Zones is tested next to:
|
|
Hospitality, Pick Up And Haul, Haul to Stack, RimFridge, Weapon Racks, Prison Labor,
|
|
Prisoner Realism, Replace Stuff, VEF with VFE Core and VFE Medical, Dubs Skylights,
|
|
Vanilla Events Expanded, and Blax Defaults once it is installed
|
|
.\Tests\run-tests.ps1 -NoDlc core only, no DLC (tests that need a DLC skip with "needs <DLC>")
|
|
.\Tests\run-tests.ps1 -Dlc Royalty,Biotech only these installed DLCs
|
|
.\Tests\run-tests.ps1 -NoBuild skip dotnet build
|
|
.\Tests\run-tests.ps1 -AllowWhileRunning run next to an open RimWorld (the test game runs at below-normal priority)
|
|
.\Tests\run-tests.ps1 -Worktree run from a copy of the repo (a work package built in parallel): Mods\BlaxZones
|
|
points at this copy for the run and back at its old target afterwards
|
|
.\Tests\run-tests.ps1 -WaitSeconds 1800 wait up to this long for another run's lock instead of failing at once
|
|
|
|
Mods that change TradeDropSpot or AllLaunchableThingsForTrade stay out of -Compat, so they can't hide Zones' own path.
|
|
Mods\BlaxZones must be a directory junction to this repo; it is created if missing and left in place. With -Worktree
|
|
it may point at another copy: it is retargeted for the run and restored afterwards, under the lock.
|
|
While the suite runs, the test mod is linked into the game's Mods folder; that link is removed afterwards.
|
|
Only one run at a time: a lock file in the data folder holds the running script's process id. Copies that test in
|
|
parallel keep the default -DataDir, so they share that lock.
|
|
Every run starts from default mod settings: the Blax Zones, Blax Castes and Blax Slaves settings files in the data folder are deleted.
|
|
#>
|
|
param(
|
|
[string]$RimWorldDir = 'E:\SteamLibrary\steamapps\common\RimWorld',
|
|
[string]$DataDir = (Join-Path $env:TEMP 'BlaxZonesTest'),
|
|
[switch]$Compat,
|
|
[switch]$NoDlc,
|
|
[string[]]$Dlc,
|
|
[switch]$NoBuild,
|
|
[switch]$AllowWhileRunning,
|
|
[switch]$Worktree,
|
|
[int]$WaitSeconds = 0,
|
|
[int]$TimeoutSeconds = 900
|
|
)
|
|
$ErrorActionPreference = 'Stop'
|
|
# A wrong path (for example a switch passed as a positional value) would put the Mods junction somewhere odd.
|
|
if (-not (Test-Path -LiteralPath (Join-Path $RimWorldDir 'Assembly-CSharp.dll'))) { throw "No RimWorld 1.7 install at '$RimWorldDir'." }
|
|
$repo = Split-Path -Parent $PSScriptRoot
|
|
$testMod = Join-Path $PSScriptRoot 'BlaxZonesTests'
|
|
$modLink = Join-Path $RimWorldDir 'Mods\BlaxZones'
|
|
$link = Join-Path $RimWorldDir 'Mods\BlaxZonesTests'
|
|
$workshop = Join-Path $RimWorldDir '..\..\workshop\content\294100'
|
|
$lockFile = Join-Path $DataDir 'run.lock'
|
|
|
|
if ((Get-Process -Name RimWorld -ErrorAction SilentlyContinue) -and -not $AllowWhileRunning) {
|
|
throw 'RimWorld is running. Close it first, or pass -AllowWhileRunning.'
|
|
}
|
|
|
|
function Get-FullPath([string]$path) { [IO.Path]::GetFullPath($path).TrimEnd('\') }
|
|
|
|
# One run at a time. A lock whose process is gone is stale and taken over; a live one is waited for up to -WaitSeconds.
|
|
New-Item -ItemType Directory -Force $DataDir | Out-Null
|
|
function Enter-Lock {
|
|
$deadline = [DateTime]::UtcNow.AddSeconds([Math]::Max(0, $WaitSeconds))
|
|
$stale = 0
|
|
$waiting = $false
|
|
while ($true) {
|
|
try {
|
|
$stream = [IO.File]::Open($lockFile, [IO.FileMode]::CreateNew, [IO.FileAccess]::Write, [IO.FileShare]::Read)
|
|
$bytes = [Text.Encoding]::ASCII.GetBytes([string]$PID)
|
|
$stream.Write($bytes, 0, $bytes.Length)
|
|
$stream.Close()
|
|
return
|
|
}
|
|
catch [IO.IOException] {
|
|
$owner = (Get-Content -LiteralPath $lockFile -Raw -ErrorAction SilentlyContinue)
|
|
$ownerPid = 0
|
|
if ($owner -and [int]::TryParse($owner.Trim(), [ref]$ownerPid) -and $ownerPid -ne $PID -and (Get-Process -Id $ownerPid -ErrorAction SilentlyContinue)) {
|
|
if ([DateTime]::UtcNow -ge $deadline) { throw "Another test run (process $ownerPid) is in progress. Lock: $lockFile" }
|
|
if (-not $waiting) { Write-Host "Waiting for test run $ownerPid to finish..."; $waiting = $true }
|
|
Start-Sleep -Seconds 5
|
|
continue
|
|
}
|
|
if (++$stale -gt 3) { throw "Could not take the lock $lockFile" }
|
|
Write-Warning "removing stale lock from process $owner"
|
|
Remove-Item -LiteralPath $lockFile -Force -ErrorAction SilentlyContinue
|
|
}
|
|
}
|
|
}
|
|
Enter-Lock
|
|
|
|
$restoreTarget = $null
|
|
try {
|
|
# Mods\BlaxZones: a junction to this repo. Created if missing; replaced only with -Worktree, and put back afterwards.
|
|
if (Test-Path -LiteralPath $modLink) {
|
|
$item = Get-Item -LiteralPath $modLink -Force
|
|
if (-not ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) {
|
|
throw "$modLink is a real folder, not a junction to $repo. Move it away and run again."
|
|
}
|
|
$target = @($item.Target)[0]
|
|
if (-not $target -or (Get-FullPath $target) -ne (Get-FullPath $repo)) {
|
|
if (-not $Worktree) { throw "$modLink points to '$target', not to $repo. Pass -Worktree to run from this copy." }
|
|
cmd /c rmdir "$modLink" | Out-Null
|
|
New-Item -ItemType Junction -Path $modLink -Target $repo | Out-Null
|
|
$restoreTarget = $target
|
|
Write-Host "Mods\BlaxZones points at $repo for this run (was $target)"
|
|
}
|
|
}
|
|
else {
|
|
New-Item -ItemType Junction -Path $modLink -Target $repo | Out-Null
|
|
Write-Host "Created junction $modLink -> $repo"
|
|
}
|
|
|
|
if (-not $NoBuild) {
|
|
foreach ($proj in (Join-Path $repo 'Source\BlaxZones\BlaxZones.csproj'), (Join-Path $testMod 'Source\BlaxZonesTests.csproj')) {
|
|
& dotnet build $proj -c Release -nologo -v q "-p:RimWorldDir=$RimWorldDir"
|
|
if ($LASTEXITCODE -ne 0) { throw "build failed: $proj" }
|
|
}
|
|
}
|
|
|
|
# packageId (lower case) -> About.xml, for every installed local and Workshop mod
|
|
$installed = @{}
|
|
foreach ($about in @(Get-ChildItem (Join-Path $RimWorldDir 'Mods\*\About\About.xml') -ErrorAction SilentlyContinue) +
|
|
@(Get-ChildItem (Join-Path $workshop '*\About\About.xml') -ErrorAction SilentlyContinue)) {
|
|
$x = [xml](Get-Content -LiteralPath $about.FullName -Raw -Encoding UTF8)
|
|
if ($x.ModMetaData.packageId) { $installed[$x.ModMetaData.packageId.Trim().ToLower()] = $x }
|
|
}
|
|
function Add-WithDependencies([string]$id, [System.Collections.ArrayList]$list) {
|
|
$id = $id.ToLower()
|
|
if ($list -contains $id -or $id -like 'ludeon.*') { return }
|
|
$x = $installed[$id]
|
|
if (-not $x) { Write-Warning "not installed, skipped: $id"; return }
|
|
foreach ($dep in @($x.ModMetaData.modDependencies.li)) { if ($dep.packageId) { Add-WithDependencies $dep.packageId $list } }
|
|
[void]$list.Add($id)
|
|
}
|
|
|
|
# Load order: Harmony, core, DLCs, the mods Zones is tested next to, Blax Castes, Blax Zones, Blax Slaves, the tests.
|
|
# Castes loads before Zones (Zones' loadAfter), Zones before Slaves (Slaves lists blax.zones).
|
|
# Every installed DLC is listed as known, so the game does not switch on a left-out DLC as "new".
|
|
$installedDlcs = @(@('Royalty', 'Ideology', 'Biotech', 'Anomaly', 'Odyssey') | Where-Object { Test-Path (Join-Path $RimWorldDir "Data\$_") })
|
|
if ($NoDlc) { $loadDlcs = @() }
|
|
elseif ($Dlc) {
|
|
$wanted = @($Dlc | ForEach-Object { $_ -split ',' } | ForEach-Object { $_.Trim() } | Where-Object { $_ })
|
|
foreach ($w in $wanted) { if ($installedDlcs -notcontains $w) { Write-Warning "DLC not installed or unknown, skipped: $w" } }
|
|
$loadDlcs = @($installedDlcs | Where-Object { $wanted -contains $_ })
|
|
}
|
|
else { $loadDlcs = @($installedDlcs) }
|
|
$mods = New-Object System.Collections.ArrayList
|
|
$expansions = @($installedDlcs | ForEach-Object { 'ludeon.rimworld.' + $_.ToLower() })
|
|
[void]$mods.Add('brrainz.harmony')
|
|
[void]$mods.Add('ludeon.rimworld')
|
|
foreach ($d in $loadDlcs) { [void]$mods.Add('ludeon.rimworld.' + $d.ToLower()) }
|
|
Write-Host ("DLCs: " + $(if ($loadDlcs.Count) { $loadDlcs -join ', ' } else { 'none' }))
|
|
if ($Compat) {
|
|
# Package ids read from the installed copies' About/About.xml (2026-11-29). Mods that change TradeDropSpot or
|
|
# AllLaunchableThingsForTrade (Trade Ships Drop Spot, Trading Control, Trade as Slave or Colonist) stay out.
|
|
foreach ($id in 'orion.hospitality', 'mehni.pickupandhaul', 'jkluch.haultostack', 'rimfridge.kv.rw', 'aelanna.weaponracks',
|
|
'avius.prisonlabor', 'legator.prisonerrealism', 'memegoddess.replacestuff',
|
|
'oskarpotocki.vanillafactionsexpanded.core', 'vanillaexpanded.vfecore', 'vanillaexpanded.vfemedical',
|
|
'dubwise.dubsskylights', 'vanillaexpanded.vee') {
|
|
Add-WithDependencies $id $mods
|
|
}
|
|
Add-WithDependencies 'blax.castes' $mods
|
|
# Blax Defaults joins once it has code; skipped with a warning while it is not installed.
|
|
Add-WithDependencies 'blax.defaults' $mods
|
|
}
|
|
[void]$mods.Add('blax.zones')
|
|
if ($Compat) { Add-WithDependencies 'blax.slaves' $mods }
|
|
[void]$mods.Add('blax.zones.tests')
|
|
|
|
# fresh test data folder: mod list only, no old saves, results or mod settings (files are named Mod_<folder>_<class>.xml)
|
|
New-Item -ItemType Directory -Force (Join-Path $DataDir 'Config') | Out-Null
|
|
Remove-Item -Force -ErrorAction SilentlyContinue (Join-Path $DataDir 'Saves\BlaxZonesTest.rws'), (Join-Path $DataDir 'Saves\BlaxZonesTest3.rws'),
|
|
(Join-Path $DataDir 'BlaxZonesTestResults.txt')
|
|
Get-ChildItem -LiteralPath (Join-Path $DataDir 'Config') -Filter 'Mod_*.xml' -ErrorAction SilentlyContinue |
|
|
Where-Object { $_.Name -like 'Mod_BlaxZones_*' -or $_.Name -like 'Mod_blax.zones_*' -or $_.Name -like 'Mod_BlaxCastes_*' -or
|
|
$_.Name -like 'Mod_blax.castes_*' -or $_.Name -like 'Mod_BlaxSlaves_*' -or $_.Name -like 'Mod_blax.slaves_*' } |
|
|
Remove-Item -Force -ErrorAction SilentlyContinue
|
|
$li = { param($ids) ($ids | ForEach-Object { " <li>$_</li>" }) -join "`n" }
|
|
$modsConfig = "<?xml version=`"1.0`" encoding=`"utf-8`"?>`n<ModsConfigData>`n <version>1.7</version>`n <activeMods>`n" +
|
|
(& $li $mods) + "`n </activeMods>`n <knownExpansions>`n" + (& $li $expansions) + "`n </knownExpansions>`n</ModsConfigData>`n"
|
|
[IO.File]::WriteAllText((Join-Path $DataDir 'Config\ModsConfig.xml'), $modsConfig)
|
|
Write-Host ("Mods: " + ($mods -join ', '))
|
|
|
|
$createdLink = $false
|
|
try {
|
|
# A test-mod link left by a run from another copy that died is replaced, never reused.
|
|
if (Test-Path -LiteralPath $link) {
|
|
$item = Get-Item -LiteralPath $link -Force
|
|
if (-not ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) { throw "$link is a real folder, not a junction. Move it away and run again." }
|
|
$target = @($item.Target)[0]
|
|
if (-not $target -or (Get-FullPath $target) -ne (Get-FullPath $testMod)) {
|
|
Write-Warning "replacing stale test-mod link $link -> $target"
|
|
cmd /c rmdir "$link" | Out-Null
|
|
}
|
|
}
|
|
if (-not (Test-Path -LiteralPath $link)) {
|
|
New-Item -ItemType Junction -Path $link -Target $testMod | Out-Null
|
|
$createdLink = $true
|
|
}
|
|
$gameArgs = "-headless -quicktest -blaxzonestests -NoFileLog -NoCrashDialog `"-savedatafolder=$DataDir`""
|
|
$sw = [Diagnostics.Stopwatch]::StartNew()
|
|
$p = Start-Process -FilePath (Join-Path $RimWorldDir 'RimWorld.exe') -WorkingDirectory $RimWorldDir -ArgumentList $gameArgs `
|
|
-RedirectStandardOutput (Join-Path $DataDir 'game.log') -RedirectStandardError (Join-Path $DataDir 'game.err.log') `
|
|
-WindowStyle Hidden -PassThru
|
|
if ($AllowWhileRunning) {
|
|
try { $p.PriorityClass = [Diagnostics.ProcessPriorityClass]::BelowNormal } catch { Write-Warning "could not lower the test game's priority: $_" }
|
|
}
|
|
if (-not $p.WaitForExit($TimeoutSeconds * 1000)) {
|
|
$p.Kill()
|
|
Write-Warning "timed out after $TimeoutSeconds s, test game (process $($p.Id)) killed"
|
|
}
|
|
Write-Host ("Game ran {0:N0} s. Log: {1}" -f $sw.Elapsed.TotalSeconds, (Join-Path $DataDir 'game.log'))
|
|
}
|
|
finally {
|
|
if ($createdLink -and (Test-Path $link)) { cmd /c rmdir "$link" | Out-Null }
|
|
}
|
|
}
|
|
finally {
|
|
if ($restoreTarget) {
|
|
cmd /c rmdir "$modLink" | Out-Null
|
|
New-Item -ItemType Junction -Path $modLink -Target $restoreTarget | Out-Null
|
|
Write-Host "Mods\BlaxZones points at $restoreTarget again"
|
|
}
|
|
Remove-Item -LiteralPath $lockFile -Force -ErrorAction SilentlyContinue
|
|
}
|
|
|
|
$results = Join-Path $DataDir 'BlaxZonesTestResults.txt'
|
|
if (-not (Test-Path $results)) { Write-Host 'No results file: the suite never started. See game.log.'; exit 2 }
|
|
$lines = Get-Content -LiteralPath $results -Encoding UTF8
|
|
$lines | ForEach-Object { Write-Host $_ }
|
|
if (-not ($lines -match '^END$')) { Write-Host 'The run did not finish.'; exit 2 }
|
|
if ($lines -match '^FAIL ') { exit 1 }
|
|
exit 0
|