cu-trainer/Ultra Paradise Application/build.ps1
| 1 | param([switch]$NewUltraPara, [string]$GameDir = "C:\Program Files (x86)\Steam\steamapps\common\Casualties Unknown") |
| 2 | $ErrorActionPreference = "Stop" |
| 3 | $root = $PSScriptRoot |
| 4 | $M = Join-Path $GameDir "CasualtiesUnknown_Data\Managed" |
| 5 | $csc = "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe" |
| 6 | $obj = Join-Path $root "obj" |
| 7 | $outDir = Join-Path $root "bin" |
| 8 | New-Item -ItemType Directory -Force $obj, $outDir | Out-Null |
| 9 | |
| 10 | $lib = Join-Path $root "lib" |
| 11 | $libs = @("0Harmony","MonoMod.RuntimeDetour","MonoMod.Utils","Mono.Cecil") |
| 12 | $refs = @() |
| 13 | foreach ($n in @("mscorlib","System","System.Core","netstandard","UnityEngine","UnityEngine.CoreModule","UnityEngine.Physics2DModule","UnityEngine.InputLegacyModule","UnityEngine.AudioModule","UnityEngine.UnityWebRequestModule","UnityEngine.UnityWebRequestAudioModule","UnityEngine.UI","UnityEngine.UIModule","UnityEngine.ImageConversionModule","UnityEngine.TextRenderingModule","Unity.TextMeshPro","Assembly-CSharp")) { |
| 14 | $p = Join-Path $M "$n.dll" |
| 15 | if (-not (Test-Path $p)) { throw "missing $p" } |
| 16 | $refs += "/r:`"$p`"" |
| 17 | } |
| 18 | $refs += "/r:`"$(Join-Path $lib '0Harmony.dll')`"" |
| 19 | $res = @() |
| 20 | foreach ($n in $libs) { |
| 21 | $p = Join-Path $lib "$n.dll" |
| 22 | if (-not (Test-Path $p)) { throw "missing $p" } |
| 23 | $res += "/resource:`"$p`",lib.$n.dll" |
| 24 | } |
| 25 | $payload = Join-Path $obj "Payload.dll" |
| 26 | $pa = @("/nologo","/nostdlib+","/noconfig","/target:library","/langversion:5","/optimize+","/out:`"$payload`"") + $refs + $res + @("`"$(Join-Path $root 'src\Payload.cs')`"","`"$(Join-Path $root 'src\Engine.cs')`"","`"$(Join-Path $root 'src\Health.cs')`"","`"$(Join-Path $root 'src\Debug.cs')`"","`"$(Join-Path $root 'src\MyMods.cs')`"","`"$(Join-Path $root 'src\Music.cs')`"") |
| 27 | & $csc $pa |
| 28 | if ($LASTEXITCODE -ne 0) { throw "payload build failed" } |
| 29 | $upRoot = (Resolve-Path (Join-Path $root "..\UltraPara")).Path |
| 30 | if ($NewUltraPara -or -not (Test-Path (Join-Path $upRoot "out\UltraParaInfo.cs"))) { |
| 31 | & powershell -NoProfile -ExecutionPolicy Bypass -File (Join-Path $upRoot "build.ps1") -GameDir $GameDir |
| 32 | if ($LASTEXITCODE -ne 0) { throw "UltraPara build failed" } |
| 33 | } else { Write-Host "Keeping the published UltraPara build (use -NewUltraPara after changing game-side code)" } |
| 34 | $upInfo = Join-Path $upRoot "out\UltraParaInfo.cs" |
| 35 | |
| 36 | $ico = Join-Path $obj "app.ico" |
| 37 | $splashJpg = Join-Path $root "art\splash.jpg" |
| 38 | Add-Type -AssemblyName System.Drawing |
| 39 | $head = [System.Drawing.Bitmap]::FromFile((Join-Path $root "art\experimentHead.png")) |
| 40 | $hw = 20; $hh = 12 |
| 41 | $orange = [System.Drawing.Color]::FromArgb(255, 122, 24) |
| 42 | function New-Tile([int]$s) { |
| 43 | $bmp = New-Object System.Drawing.Bitmap $s, $s |
| 44 | $g = [System.Drawing.Graphics]::FromImage($bmp) |
| 45 | $g.SmoothingMode = "AntiAlias" |
| 46 | $r = [Math]::Max(1, [int]($s * 0.16)) |
| 47 | $path = New-Object System.Drawing.Drawing2D.GraphicsPath |
| 48 | $path.AddArc(0, 0, $r * 2, $r * 2, 180, 90); $path.AddArc($s - $r * 2 - 1, 0, $r * 2, $r * 2, 270, 90) |
| 49 | $path.AddArc($s - $r * 2 - 1, $s - $r * 2 - 1, $r * 2, $r * 2, 0, 90); $path.AddArc(0, $s - $r * 2 - 1, $r * 2, $r * 2, 90, 90) |
| 50 | $path.CloseFigure() |
| 51 | $br = New-Object System.Drawing.SolidBrush $orange |
| 52 | $g.FillPath($br, $path) |
| 53 | $br.Dispose(); $path.Dispose() |
| 54 | $scale = [Math]::Floor(($s * 0.86) / $hw) |
| 55 | $g.SmoothingMode = "None" |
| 56 | if ($scale -ge 1) { |
| 57 | $g.InterpolationMode = "NearestNeighbor"; $g.PixelOffsetMode = "Half" |
| 58 | $w = $hw * $scale; $h = $hh * $scale |
| 59 | } else { |
| 60 | $g.InterpolationMode = "HighQualityBicubic"; $g.PixelOffsetMode = "HighQuality" |
| 61 | $w = [int]($s * 0.86); $h = [int]($w * $hh / $hw) |
| 62 | } |
| 63 | $w = [int]$w; $h = [int]$h |
| 64 | $x = [int][Math]::Floor(($s - $w) / 2); $y = [int][Math]::Floor(($s - $h) / 2) |
| 65 | $dst = New-Object System.Drawing.Rectangle -ArgumentList $x, $y, $w, $h |
| 66 | $srcR = New-Object System.Drawing.Rectangle -ArgumentList 0, 0, $hw, $hh |
| 67 | $g.DrawImage([System.Drawing.Image]$script:head, $dst, $srcR, [System.Drawing.GraphicsUnit]::Pixel) |
| 68 | $g.Dispose() |
| 69 | return $bmp |
| 70 | } |
| 71 | $pngs = @() |
| 72 | foreach ($s in 256, 64, 48, 32, 24, 16) { |
| 73 | $bmp = New-Tile $s |
| 74 | $ms = New-Object System.IO.MemoryStream |
| 75 | if ($s -eq 256) { |
| 76 | $bmp.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png) |
| 77 | $bmp.Save((Join-Path $root "art\icon.png"), [System.Drawing.Imaging.ImageFormat]::Png) |
| 78 | } else { |
| 79 | $dib = New-Object System.IO.BinaryWriter $ms |
| 80 | $dib.Write([uint32]40); $dib.Write([int32]$s); $dib.Write([int32]($s * 2)); $dib.Write([uint16]1); $dib.Write([uint16]32) |
| 81 | $dib.Write([uint32]0); $dib.Write([uint32]($s * $s * 4)); $dib.Write([int32]0); $dib.Write([int32]0); $dib.Write([uint32]0); $dib.Write([uint32]0) |
| 82 | for ($yy = $s - 1; $yy -ge 0; $yy--) { |
| 83 | for ($xx = 0; $xx -lt $s; $xx++) { |
| 84 | $c = $bmp.GetPixel($xx, $yy) |
| 85 | $dib.Write([byte]$c.B); $dib.Write([byte]$c.G); $dib.Write([byte]$c.R); $dib.Write([byte]$c.A) |
| 86 | } |
| 87 | } |
| 88 | $maskRow = [int]([Math]::Ceiling($s / 32.0) * 4) |
| 89 | $dib.Write((New-Object byte[] ($maskRow * $s))) |
| 90 | $dib.Flush() |
| 91 | } |
| 92 | $bmp.Dispose() |
| 93 | $pngs += ,@($s, $ms.ToArray()) |
| 94 | } |
| 95 | $tile = New-Tile 48 |
| 96 | $tilePng = Join-Path $obj "tile.png" |
| 97 | $tile.Save($tilePng, [System.Drawing.Imaging.ImageFormat]::Png) |
| 98 | $tile.Dispose() |
| 99 | $head.Dispose() |
| 100 | $fs = [System.IO.File]::Create($ico) |
| 101 | $bw = New-Object System.IO.BinaryWriter $fs |
| 102 | $bw.Write([uint16]0); $bw.Write([uint16]1); $bw.Write([uint16]$pngs.Count) |
| 103 | $offset = 6 + 16 * $pngs.Count |
| 104 | foreach ($p in $pngs) { |
| 105 | $sz = $p[0]; $data = $p[1] |
| 106 | $bw.Write([byte]($sz % 256)); $bw.Write([byte]($sz % 256)); $bw.Write([byte]0); $bw.Write([byte]0) |
| 107 | $bw.Write([uint16]1); $bw.Write([uint16]32); $bw.Write([uint32]$data.Length); $bw.Write([uint32]$offset) |
| 108 | $offset += $data.Length |
| 109 | } |
| 110 | foreach ($p in $pngs) { $bw.Write([byte[]]$p[1]) } |
| 111 | $bw.Close() |
| 112 | |
| 113 | $exe = Join-Path $outDir "Ultra Paradise CU-Trainer.exe" |
| 114 | $old = Join-Path $outDir "Ultra Paradise.exe" |
| 115 | if (Test-Path $old) { Remove-Item $old } |
| 116 | $fw = "C:\Windows\Microsoft.NET\Framework64\v4.0.30319" |
| 117 | $winmd = Get-ChildItem "C:\Program Files (x86)\Windows Kits\10\UnionMetadata" -Directory | Where-Object { $_.Name -match "^\d" } | Sort-Object { [version]$_.Name } -Descending | ForEach-Object { Join-Path $_.FullName "Windows.winmd" } | Where-Object { Test-Path $_ } | Select-Object -First 1 |
| 118 | if (-not $winmd) { throw "missing Windows.winmd (Windows SDK)" } |
| 119 | $a = @("/nologo","/target:winexe","/platform:x64","/langversion:5","/optimize+","/win32icon:`"$ico`"","/out:`"$exe`"", |
| 120 | "/r:`"$fw\System.dll`"","/r:`"$fw\System.Drawing.dll`"","/r:`"$fw\System.Windows.Forms.dll`"","/r:`"$fw\System.Web.Extensions.dll`"","/r:`"$fw\System.IO.Compression.dll`"","/r:`"$fw\System.IO.Compression.FileSystem.dll`"","/r:`"$fw\System.Xaml.dll`"","/r:`"$fw\WPF\PresentationCore.dll`"","/r:`"$fw\WPF\WindowsBase.dll`"","/r:`"$winmd`"","/r:`"$fw\System.Runtime.WindowsRuntime.dll`"","/r:`"$fw\System.Runtime.dll`"","/r:`"$fw\System.Threading.Tasks.dll`"","/r:`"$fw\System.Runtime.InteropServices.WindowsRuntime.dll`"", |
| 121 | "/resource:`"$splashJpg`",UltraParadise.splash.jpg","/resource:`"$tilePng`",UltraParadise.tile.png","/resource:`"$(Join-Path $root 'art\logo-nexus.png')`",UltraParadise.logo-nexus.png","/resource:`"$(Join-Path $root 'art\logo-gamebanana.png')`",UltraParadise.logo-gamebanana.png","/resource:`"$(Join-Path $root 'art\logo-discord.png')`",UltraParadise.logo-discord.png", |
| 122 | "`"$(Join-Path $root 'src\Trainer.cs')`"","`"$(Join-Path $root 'src\Theme.cs')`"","`"$(Join-Path $root 'src\DebugWindow.cs')`"","`"$(Join-Path $root 'src\Loader.cs')`"","`"$(Join-Path $root 'src\MyModsPanel.cs')`"","`"$(Join-Path $root 'src\MusicTab.cs')`"","`"$(Join-Path $root 'src\NowPlaying.cs')`"","`"$(Join-Path $root 'src\Tools.cs')`"","`"$(Join-Path $root 'src\Lyrics.cs')`"","`"$(Join-Path $root 'src\Thunderstore.cs')`"","`"$upInfo`"") |
| 123 | & $csc $a |
| 124 | if ($LASTEXITCODE -ne 0) { throw "trainer build failed" } |
| 125 | Write-Host ("payload {0} bytes, exe {1} bytes" -f (Get-Item $payload).Length, (Get-Item $exe).Length) |