Files
AstraWeapons/tools/install-mods.ps1
T

64 lines
3.3 KiB
PowerShell
Raw Normal View History

param([string]$ServerDirectory = 'C:/Users/ScarPC/Documents/NeoForge Server 26.2', [switch]$IncludeAstraPlus)
$ErrorActionPreference='Stop'
Add-Type -AssemblyName System.IO.Compression.FileSystem
$repo=Split-Path $PSScriptRoot -Parent
$mods=(Resolve-Path -LiteralPath (Join-Path $ServerDirectory 'mods')).Path
$jobs=@(
@{Id='astraweapons';Jar=(Join-Path $repo 'build/libs/AstraWeapons-0.1.1.jar')}
)
if($IncludeAstraPlus){$jobs=@(@{Id='astraplus';Jar=(Join-Path (Split-Path $repo -Parent) 'AstraPlus/build/libs/AstraPlus-0.9.8.jar')})+$jobs}
function Get-ModIds([string]$Jar) {
$zip=[IO.Compression.ZipFile]::OpenRead($Jar)
try {
$entry=$zip.GetEntry('META-INF/neoforge.mods.toml')
if(!$entry){return @()}
$reader=[IO.StreamReader]::new($entry.Open())
try {$descriptor=$reader.ReadToEnd()}finally{$reader.Dispose()}
foreach($section in ($descriptor -split '\[\[mods\]\]' | Select-Object -Skip 1)) {
$body=($section -split '\[\[',2)[0]
$match=[regex]::Match($body,'(?m)^\s*modId\s*=\s*"([^"]+)"')
if($match.Success){$match.Groups[1].Value}
}
}finally{$zip.Dispose()}
}
$installed=@(Get-ChildItem -LiteralPath $mods -File -Filter '*.jar' | ForEach-Object {
[PSCustomObject]@{Path=$_.FullName;Name=$_.Name;Ids=@(Get-ModIds $_.FullName)}
})
foreach($job in $jobs) {
$source=(Resolve-Path -LiteralPath $job.Jar).Path
if(@(Get-ModIds $source) -notcontains $job.Id){throw "Incorrect built mod: $source"}
$job.Jar=$source
$job.Old=@($installed | Where-Object {$_.Ids -contains $job.Id})
$target=Join-Path $mods (Split-Path $source -Leaf)
if((Test-Path -LiteralPath $target) -and $target -notin $job.Old.Path){throw "Target filename belongs to a different mod: $target"}
foreach($old in $job.Old) {
$resolved=(Resolve-Path -LiteralPath $old.Path).Path
if((Split-Path $resolved -Parent) -ne $mods){throw "Installed JAR is outside mods root: $resolved"}
# Fail before moving anything if a running process locks either installed mod.
$stream=[IO.File]::Open($resolved,[IO.FileMode]::Open,[IO.FileAccess]::ReadWrite,[IO.FileShare]::None)
$stream.Dispose()
}
}
$archiveRoot=Join-Path $ServerDirectory 'old-mods'
$archive=Join-Path $archiveRoot (Get-Date -Format 'yyyyMMdd-HHmmss')
if(Test-Path -LiteralPath $archive){$archive+='-'+[guid]::NewGuid().ToString('N').Substring(0,6)}
New-Item -ItemType Directory -Path $archive | Out-Null
$receipt=@()
foreach($job in $jobs) {
$saved=@()
foreach($old in $job.Old) {
$destination=Join-Path $archive $old.Name
if(Test-Path -LiteralPath $destination){throw "Archive already contains $destination"}
Move-Item -LiteralPath $old.Path -Destination $destination
$saved+=$destination
}
$target=Join-Path $mods (Split-Path $job.Jar -Leaf)
Copy-Item -LiteralPath $job.Jar -Destination $target
$expected=(Get-FileHash -LiteralPath $job.Jar -Algorithm SHA256).Hash
$actual=(Get-FileHash -LiteralPath $target -Algorithm SHA256).Hash
if($expected -ne $actual){throw "Installed JAR SHA-256 mismatch: $target"}
$receipt+=[PSCustomObject]@{Mod=$job.Id;Installed=$target;SHA256=$actual;Archived=$saved}
}
$receipt | ConvertTo-Json -Depth 5 | Set-Content -LiteralPath (Join-Path $repo 'build/install-receipt.json') -Encoding utf8
$receipt | Format-List