|
| 1 | +param( |
| 2 | + [switch]$SkipBuild, |
| 3 | + [switch]$WithTests, |
| 4 | + [string]$VelocityVersion, |
| 5 | + [int]$Port = 25577 |
| 6 | +) |
| 7 | + |
| 8 | +$ErrorActionPreference = "Stop" |
| 9 | +$ProgressPreference = "SilentlyContinue" |
| 10 | + |
| 11 | +$projectRoot = Split-Path $PSScriptRoot -Parent |
| 12 | +$runtimeRoot = Join-Path $projectRoot ".run\velocity" |
| 13 | +$pluginsDir = Join-Path $runtimeRoot "plugins" |
| 14 | +$resolvedJava = Get-Command java.exe -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty Source |
| 15 | +$javaCmd = if ($env:JAVA_HOME) { |
| 16 | + Join-Path $env:JAVA_HOME "bin\java.exe" |
| 17 | +} elseif ($resolvedJava) { |
| 18 | + $resolvedJava |
| 19 | +} else { |
| 20 | + "java" |
| 21 | +} |
| 22 | + |
| 23 | +function Get-ProjectMetadata { |
| 24 | + [xml]$pom = Get-Content (Join-Path $projectRoot "pom.xml") |
| 25 | + $finalName = $pom.project.build.finalName |
| 26 | + if ([string]::IsNullOrWhiteSpace($finalName)) { |
| 27 | + $finalName = $pom.project.artifactId |
| 28 | + } |
| 29 | + |
| 30 | + [pscustomobject]@{ |
| 31 | + FinalName = $finalName |
| 32 | + JavaVersion = [int]$pom.project.properties.'java.version' |
| 33 | + VelocityVersion = [string]$pom.project.properties.'velocity.api.version' |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +$project = Get-ProjectMetadata |
| 38 | +$pluginJar = Join-Path $projectRoot ("target\" + $project.FinalName + ".jar") |
| 39 | + |
| 40 | +function Invoke-Build { |
| 41 | + $args = @("package") |
| 42 | + if (-not $WithTests) { |
| 43 | + $args = @("-Pskip-tests", "package") |
| 44 | + } |
| 45 | + |
| 46 | + Write-Host "Building plugin..." |
| 47 | + & mvn @args |
| 48 | + if ($LASTEXITCODE -ne 0) { |
| 49 | + throw "Build failed." |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function Get-LatestVelocityBuild { |
| 54 | + param( |
| 55 | + [string]$RequestedVersion |
| 56 | + ) |
| 57 | + |
| 58 | + $headers = @{ "User-Agent" = "Tensa-DevRunner/1.0" } |
| 59 | + $projectInfo = Invoke-RestMethod -Headers $headers -Uri "https://fill.papermc.io/v3/projects/velocity" |
| 60 | + [string[]]$availableVersions = $projectInfo.versions.'3.0.0' |
| 61 | + |
| 62 | + if (-not $availableVersions -or $availableVersions.Count -eq 0) { |
| 63 | + throw "Fill v3 did not return any Velocity 3.x versions." |
| 64 | + } |
| 65 | + |
| 66 | + $version = if ($RequestedVersion -and $availableVersions -contains $RequestedVersion) { |
| 67 | + $RequestedVersion |
| 68 | + } else { |
| 69 | + if ($RequestedVersion) { |
| 70 | + Write-Warning "Velocity version '$RequestedVersion' is not published via Fill v3. Falling back to the latest available Velocity 3.x build." |
| 71 | + } |
| 72 | + $availableVersions[0] |
| 73 | + } |
| 74 | + |
| 75 | + $builds = Invoke-RestMethod -Headers $headers -Uri "https://fill.papermc.io/v3/projects/velocity/versions/$([uri]::EscapeDataString($version))/builds" |
| 76 | + if (-not $builds -or $builds.Count -eq 0) { |
| 77 | + throw "No Velocity builds found for version $version." |
| 78 | + } |
| 79 | + |
| 80 | + $stableBuilds = @($builds | Where-Object { $_.channel -eq "STABLE" }) |
| 81 | + $build = if ($stableBuilds.Count -gt 0) { |
| 82 | + $stableBuilds | Sort-Object id | Select-Object -Last 1 |
| 83 | + } else { |
| 84 | + $builds | Sort-Object id | Select-Object -Last 1 |
| 85 | + } |
| 86 | + |
| 87 | + $download = $build.downloads.'server:default' |
| 88 | + if (-not $download) { |
| 89 | + throw "Velocity build $($build.id) for version $version does not expose a server:default download." |
| 90 | + } |
| 91 | + |
| 92 | + return [pscustomobject]@{ |
| 93 | + Version = $version |
| 94 | + Build = [int]$build.id |
| 95 | + FileName = [string]$download.name |
| 96 | + Sha256 = [string]$download.checksums.sha256 |
| 97 | + Url = [string]$download.url |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +function Ensure-FileHash { |
| 102 | + param( |
| 103 | + [string]$Path, |
| 104 | + [string]$ExpectedSha256 |
| 105 | + ) |
| 106 | + |
| 107 | + if (-not (Test-Path $Path)) { |
| 108 | + return $false |
| 109 | + } |
| 110 | + |
| 111 | + $actual = (Get-FileHash -Algorithm SHA256 -Path $Path).Hash.ToLowerInvariant() |
| 112 | + return $actual -eq $ExpectedSha256.ToLowerInvariant() |
| 113 | +} |
| 114 | + |
| 115 | +function Ensure-VelocityRuntime { |
| 116 | + New-Item -ItemType Directory -Force -Path $runtimeRoot | Out-Null |
| 117 | + New-Item -ItemType Directory -Force -Path $pluginsDir | Out-Null |
| 118 | + |
| 119 | + $buildInfo = Get-LatestVelocityBuild -RequestedVersion $VelocityVersion |
| 120 | + $serverJar = Join-Path $runtimeRoot $buildInfo.FileName |
| 121 | + |
| 122 | + if (-not (Ensure-FileHash -Path $serverJar -ExpectedSha256 $buildInfo.Sha256)) { |
| 123 | + Write-Host "Downloading Velocity $($buildInfo.Version) build $($buildInfo.Build)..." |
| 124 | + Invoke-WebRequest -Uri $buildInfo.Url -OutFile $serverJar |
| 125 | + if (-not (Ensure-FileHash -Path $serverJar -ExpectedSha256 $buildInfo.Sha256)) { |
| 126 | + throw "Downloaded Velocity jar failed SHA256 verification." |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return $serverJar |
| 131 | +} |
| 132 | + |
| 133 | +function Ensure-DevConfig { |
| 134 | + $velocityToml = Join-Path $runtimeRoot "velocity.toml" |
| 135 | + $forwardingSecret = Join-Path $runtimeRoot "forwarding.secret" |
| 136 | + |
| 137 | + if (-not (Test-Path $forwardingSecret)) { |
| 138 | + [guid]::NewGuid().ToString("N") | Set-Content -Path $forwardingSecret -NoNewline -Encoding UTF8 |
| 139 | + } |
| 140 | + |
| 141 | + if (-not (Test-Path $velocityToml)) { |
| 142 | + @" |
| 143 | +config-version = "2.7" |
| 144 | +bind = "127.0.0.1:$Port" |
| 145 | +motd = "<green>Tensa Dev Proxy</green>" |
| 146 | +show-max-players = 100 |
| 147 | +online-mode = false |
| 148 | +force-key-authentication = false |
| 149 | +player-info-forwarding-mode = "none" |
| 150 | +forwarding-secret-file = "forwarding.secret" |
| 151 | +announce-forge = false |
| 152 | +kick-existing-players = false |
| 153 | +ping-passthrough = "disabled" |
| 154 | +log-command-executions = true |
| 155 | +log-player-connections = true |
| 156 | +try = [ "lobby" ] |
| 157 | +
|
| 158 | +[servers] |
| 159 | +lobby = "127.0.0.1:25566" |
| 160 | +"@ | Set-Content -Path $velocityToml -Encoding UTF8 |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +function Sync-PluginJar { |
| 165 | + param( |
| 166 | + [string]$SourceJar |
| 167 | + ) |
| 168 | + |
| 169 | + $destinationJar = Join-Path $pluginsDir ($project.FinalName + ".jar") |
| 170 | + $legacyJar = Join-Path $pluginsDir "TENSA.jar" |
| 171 | + |
| 172 | + if ((Test-Path $legacyJar) -and ($legacyJar -ne $destinationJar)) { |
| 173 | + Remove-Item -Path $legacyJar -Force |
| 174 | + } |
| 175 | + |
| 176 | + Copy-Item -Path $SourceJar -Destination $destinationJar -Force |
| 177 | + return $destinationJar |
| 178 | +} |
| 179 | + |
| 180 | +function Test-JavaVersion { |
| 181 | + $stdoutPath = [System.IO.Path]::GetTempFileName() |
| 182 | + $stderrPath = [System.IO.Path]::GetTempFileName() |
| 183 | + try { |
| 184 | + $process = Start-Process -FilePath $javaCmd ` |
| 185 | + -ArgumentList "-version" ` |
| 186 | + -NoNewWindow ` |
| 187 | + -Wait ` |
| 188 | + -PassThru ` |
| 189 | + -RedirectStandardOutput $stdoutPath ` |
| 190 | + -RedirectStandardError $stderrPath |
| 191 | + |
| 192 | + $versionLines = @() |
| 193 | + if (Test-Path $stdoutPath) { |
| 194 | + $versionLines += Get-Content $stdoutPath |
| 195 | + } |
| 196 | + if (Test-Path $stderrPath) { |
| 197 | + $versionLines += Get-Content $stderrPath |
| 198 | + } |
| 199 | + |
| 200 | + $versionOutput = $versionLines | Select-Object -First 1 |
| 201 | + $javaExitCode = $process.ExitCode |
| 202 | + } finally { |
| 203 | + Remove-Item $stdoutPath, $stderrPath -ErrorAction SilentlyContinue |
| 204 | + } |
| 205 | + |
| 206 | + if ($javaExitCode -ne 0 -or [string]::IsNullOrWhiteSpace($versionOutput)) { |
| 207 | + throw "Unable to start Java from '$javaCmd'." |
| 208 | + } |
| 209 | + |
| 210 | + if ($versionOutput -match 'version "(\d+)(?:\.(\d+))?') { |
| 211 | + $detected = [int]$matches[1] |
| 212 | + if ($detected -lt $project.JavaVersion) { |
| 213 | + throw "Java $($project.JavaVersion)+ is required, but '$versionOutput' was detected." |
| 214 | + } |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +if (-not $SkipBuild) { |
| 219 | + Invoke-Build |
| 220 | +} |
| 221 | + |
| 222 | +if (-not (Test-Path $pluginJar)) { |
| 223 | + throw "Built plugin jar not found at $pluginJar." |
| 224 | +} |
| 225 | + |
| 226 | +Test-JavaVersion |
| 227 | +$requestedVelocityVersion = if ($VelocityVersion) { $VelocityVersion } else { $project.VelocityVersion } |
| 228 | +$serverJar = Ensure-VelocityRuntime -RequestedVersion $requestedVelocityVersion |
| 229 | +Ensure-DevConfig |
| 230 | +$installedPluginJar = Sync-PluginJar -SourceJar $pluginJar |
| 231 | + |
| 232 | +Push-Location $runtimeRoot |
| 233 | +try { |
| 234 | + Write-Host "Starting Velocity from $serverJar with plugin $installedPluginJar" |
| 235 | + & $javaCmd "-jar" $serverJar |
| 236 | + exit $LASTEXITCODE |
| 237 | +} finally { |
| 238 | + Pop-Location |
| 239 | +} |
0 commit comments