RacesOfValheim/scripts/package.ps1

83 lines
4.6 KiB
PowerShell

# Builds the Thunderstore and Nexus zips. Called by the PackageThunderstore target in
# RacesOfValheim.csproj on a Release build.
#
# Compress-Archive is not used on purpose: on Windows PowerShell 5.1 it writes entry names with
# backslashes (Translations\English\items.json). The zip format only allows "/", so extractors
# on Linux and Steam Deck turned each text file into one flat file with backslashes in its name,
# Jotunn found no Translations folder, and every text in the mod showed up as [key].
param(
[Parameter(Mandatory)] [string] $Dll,
[Parameter(Mandatory)] [string] $Manifest,
[Parameter(Mandatory)] [string] $Readme,
[Parameter(Mandatory)] [string] $Icon,
[Parameter(Mandatory)] [string] $Translations,
[Parameter(Mandatory)] [string] $OutDir
)
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName System.IO.Compression
Add-Type -AssemblyName System.IO.Compression.FileSystem
function New-ModZip([string] $zipPath, [object[]] $entries) {
if (Test-Path $zipPath) { Remove-Item $zipPath -Force }
$zip = [System.IO.Compression.ZipFile]::Open($zipPath, [System.IO.Compression.ZipArchiveMode]::Create)
try {
foreach ($e in $entries) {
$name = $e.Name.Replace('\', '/')
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $e.Source, $name) | Out-Null
}
} finally {
$zip.Dispose()
}
}
# Every translation file, named by its path under the Translations folder.
$root = (Resolve-Path $Translations).Path.TrimEnd('\')
$texts = Get-ChildItem $root -Recurse -File -Filter *.json | ForEach-Object {
[pscustomobject]@{ Source = $_.FullName; Rel = $_.FullName.Substring($root.Length + 1) }
}
# The version is read back out of manifest.json, so manifest.json stays the single place a
# release is numbered.
$version = (Get-Content $Manifest -Raw | ConvertFrom-Json).version_number
# Thunderstore renders the README on the mod page but does not resolve in-page anchors, so a
# link like [changelog](#v151) goes nowhere there. The package gets a copy with those links turned
# into plain text - the same thing nexus-readme.py does for Nexus. The README in the repo keeps
# them, because they work on the wiki and on the git host.
$readmeText = [System.IO.File]::ReadAllText((Resolve-Path $Readme).Path)
$readmeText = [regex]::Replace($readmeText, '\[([^\]]+)\]\(#[^)]*\)', '$1')
# Thunderstore refuses a README over 100000 characters (hit at v1.6.7). Stop here rather than
# ship a zip the upload rejects: cut the oldest changelog versions in README.md and move the
# "Versions before vX are no longer listed here" line up with them. The wiki keeps them all.
if ($readmeText.Length -gt 100000) {
throw "README.md is $($readmeText.Length) characters; Thunderstore allows 100000. Cut the oldest changelog versions in README.md."
}
$packagedReadme = Join-Path ([System.IO.Path]::GetTempPath()) ("RacesOfValheim_README_" + [guid]::NewGuid().ToString('N') + ".md")
[System.IO.File]::WriteAllText($packagedReadme, $readmeText, (New-Object System.Text.UTF8Encoding($false)))
# The mod itself goes under plugins/, and that folder name is the whole fix for 1.5.1's missing
# texts. Mod managers (r2modman, the Thunderstore Mod Manager, Gale) copy what sits inside a
# top-level plugins/ folder into BepInEx/plugins/<Author-Mod>/ keeping its structure, but they
# flatten any folder they do not recognise: 1.5.1's Translations/English/items.json landed as a
# loose items.json beside the DLL, the Italian copy of each file overwrote the English one, and
# Jotunn - which only reads JSON whose grandparent folder is called Translations - found none.
# Reproduced in a clean r2modman profile. manifest.json, README.md and icon.png stay at the root,
# where Thunderstore requires them.
$thunderstore = @(
[pscustomobject]@{ Source = $Dll; Name = 'plugins/RacesOfValheim.dll' }
[pscustomobject]@{ Source = $Manifest; Name = 'manifest.json' }
[pscustomobject]@{ Source = $packagedReadme; Name = 'README.md' }
[pscustomobject]@{ Source = $Icon; Name = 'icon.png' }
) + ($texts | ForEach-Object { [pscustomobject]@{ Source = $_.Source; Name = "plugins/Translations/$($_.Rel)" } })
# Nexus users unzip straight into plugins, so the texts travel inside a RacesOfValheim folder.
$nexus = @(
[pscustomobject]@{ Source = $Dll; Name = 'RacesOfValheim.dll' }
) + ($texts | ForEach-Object { [pscustomobject]@{ Source = $_.Source; Name = "RacesOfValheim/Translations/$($_.Rel)" } })
New-ModZip (Join-Path $OutDir "RacesOfValheim_Thunderstore_$version.zip") $thunderstore
New-ModZip (Join-Path $OutDir "RacesOfValheim_Nexus_$version.zip") $nexus
Remove-Item $packagedReadme -Force