-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathsetup-references.ps1
More file actions
122 lines (106 loc) · 6.34 KB
/
Copy pathsetup-references.ps1
File metadata and controls
122 lines (106 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<#
.SYNOPSIS
Generates the local, git-ignored References\ folder the solution compiles against.
.DESCRIPTION
1. Publicizes the Valheim game assemblies (private members become public so the mod can patch them)
into References\Valheim\*_publicized.dll using BepInEx.AssemblyPublicizer.Cli.
2. Builds Blaxxun's APIManager (https://github.com/blaxxun-boop/APIManager) from source, with
Tools\APIManager\nested-types.patch applied, into References\APIManager\APIManager.dll. Auga merges it
into Auga.dll with ILRepack.
Requirements: .NET SDK (for the publicizer tool), Visual Studio 2022 MSBuild, git, a Valheim install and a
BepInEx install (a Gale/r2modman profile or the game folder). Re-run after every Valheim update.
.PARAMETER ValheimDir
Valheim install folder. Defaults to $env:VALHEIM_DIR, then the Steam registry entry, then the default Steam path.
.PARAMETER BepInExDir
Folder that contains BepInEx\ (core, plugins). Defaults to $env:BEPINEX_DIR, then the Gale profile
%APPDATA%\com.kesomannen.gale\valheim\profiles\Auga, then the Valheim folder. Must match Directory.Build.props.
#>
[CmdletBinding()]
param(
[string]$ValheimDir,
[string]$BepInExDir
)
$ErrorActionPreference = 'Stop'
$repo = $PSScriptRoot
# --- locate Valheim ------------------------------------------------------------------------------
if (-not $ValheimDir) { $ValheimDir = $env:VALHEIM_DIR }
if (-not $ValheimDir -or -not (Test-Path $ValheimDir)) {
$reg = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 892970' -ErrorAction SilentlyContinue
if ($reg) { $ValheimDir = $reg.InstallLocation }
}
if (-not $ValheimDir -or -not (Test-Path $ValheimDir)) {
$ValheimDir = 'C:\Program Files (x86)\Steam\steamapps\common\Valheim'
}
$managed = Join-Path $ValheimDir 'valheim_Data\Managed'
if (-not (Test-Path (Join-Path $managed 'assembly_valheim.dll'))) {
throw "Valheim not found at '$ValheimDir' (pass -ValheimDir or set VALHEIM_DIR)."
}
Write-Host "Valheim: $ValheimDir"
# --- locate BepInEx ------------------------------------------------------------------------------
function Test-BepInEx([string]$dir) { $dir -and (Test-Path (Join-Path $dir 'BepInEx\core\BepInEx.dll')) }
if (-not (Test-BepInEx $BepInExDir)) { $BepInExDir = $env:BEPINEX_DIR }
if (-not (Test-BepInEx $BepInExDir)) { $BepInExDir = Join-Path $env:APPDATA 'com.kesomannen.gale\valheim\profiles\Auga' }
if (-not (Test-BepInEx $BepInExDir)) { $BepInExDir = $ValheimDir }
if (-not (Test-BepInEx $BepInExDir)) {
throw 'BepInEx not found (pass -BepInExDir or set BEPINEX_DIR to the folder that contains BepInEx\core).'
}
Write-Host "BepInEx: $BepInExDir"
# --- 1. publicized game assemblies ---------------------------------------------------------------
if (-not (Get-Command assembly-publicizer -ErrorAction SilentlyContinue)) {
Write-Host 'Installing BepInEx.AssemblyPublicizer.Cli (dotnet global tool)...'
dotnet tool install -g BepInEx.AssemblyPublicizer.Cli | Out-Host
$env:PATH = "$env:PATH;$env:USERPROFILE\.dotnet\tools"
}
$refDir = Join-Path $repo 'References\Valheim'
New-Item -ItemType Directory -Force $refDir | Out-Null
$gameAssemblies = @(
'assembly_valheim', 'assembly_guiutils', 'assembly_utils', 'gui_framework',
'assembly_postprocessing', 'assembly_sunshafts'
)
foreach ($name in $gameAssemblies) {
$src = Join-Path $managed "$name.dll"
$dst = Join-Path $refDir "${name}_publicized.dll"
Write-Host "Publicizing $name.dll"
assembly-publicizer $src -o $dst --overwrite | Out-Null
if (-not (Test-Path $dst)) { throw "Publicizer did not produce $dst" }
}
# --- 2. APIManager -------------------------------------------------------------------------------
# APIManager.csproj expects one GamePath containing both valheim_Data\Managed and BepInEx\core, so build it
# against a temporary folder of directory junctions pointing at the real locations.
$apiDir = Join-Path $repo 'References\APIManager'
New-Item -ItemType Directory -Force $apiDir | Out-Null
$work = Join-Path ([System.IO.Path]::GetTempPath()) 'AugaAPIManagerBuild'
if (Test-Path $work) { Remove-Item -Recurse -Force $work }
New-Item -ItemType Directory -Force $work | Out-Null
Write-Host 'Cloning blaxxun-boop/APIManager...'
$src = Join-Path $work 'src'
git clone -q https://github.com/blaxxun-boop/APIManager $src
if (-not (Test-Path (Join-Path $src 'APIManager\APIManager.csproj'))) { throw 'APIManager clone failed.' }
# Tools\APIManager\nested-types.patch: upstream redirects a mod's types nested in an embedded API copy (closures,
# iterators) to the outer Auga type and rewrites definitions, which corrupts the assembly when Cecil writes it
# ("Failed patching ... InvalidCastException" at load for mods that embed the old Auga API shim).
Write-Host 'Applying Tools\APIManager\nested-types.patch...'
git -C $src apply --whitespace=nowarn (Join-Path $repo 'Tools\APIManager\nested-types.patch')
if ($LASTEXITCODE -ne 0) { throw 'APIManager patch did not apply (upstream changed?). See Tools\APIManager\nested-types.patch.' }
$gamePath = Join-Path $work 'GamePath'
New-Item -ItemType Directory -Force $gamePath | Out-Null
New-Item -ItemType Junction -Path (Join-Path $gamePath 'valheim_Data') -Target (Join-Path $ValheimDir 'valheim_Data') | Out-Null
New-Item -ItemType Junction -Path (Join-Path $gamePath 'BepInEx') -Target (Join-Path $BepInExDir 'BepInEx') | Out-Null
$vswhere = 'C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe'
$msbuild = $null
if (Test-Path $vswhere) {
$msbuild = & $vswhere -latest -requires Microsoft.Component.MSBuild -find 'MSBuild\**\Bin\MSBuild.exe' | Select-Object -First 1
}
if (-not $msbuild) { throw 'MSBuild.exe (Visual Studio 2022) not found.' }
Write-Host 'Building APIManager.dll...'
& $msbuild (Join-Path $src 'APIManager\APIManager.csproj') -p:Configuration=Release "-p:GamePath=$gamePath" -nologo -v:m | Out-Host
$built = Join-Path $src 'APIManager\bin\Release\APIManager.dll'
if (-not (Test-Path $built)) { throw 'APIManager build failed.' }
Copy-Item $built (Join-Path $apiDir 'APIManager.dll') -Force
# junctions must be removed as links, not recursed into
(Get-Item (Join-Path $gamePath 'valheim_Data')).Delete()
(Get-Item (Join-Path $gamePath 'BepInEx')).Delete()
Remove-Item -Recurse -Force $work
Write-Host ''
Write-Host 'Done. References\ is ready; build with:'
Write-Host ' msbuild Auga.sln -restore -p:Configuration=Debug'