|
| 1 | +# Woodlanders Launcher - Windows Installer with JRE Auto-Download |
| 2 | +# Version: ${VERSION} |
| 3 | + |
| 4 | +param( |
| 5 | + [switch]$Silent = $false |
| 6 | +) |
| 7 | + |
| 8 | +$ErrorActionPreference = "Stop" |
| 9 | + |
| 10 | +# Configuration |
| 11 | +$APP_NAME = "Woodlanders Launcher" |
| 12 | +$APP_VERSION = "0.1.0" |
| 13 | +$JRE_VERSION = "21" |
| 14 | +$INSTALL_DIR = "$env:LOCALAPPDATA\Woodlanders\Launcher" |
| 15 | +$JRE_DIR = "$INSTALL_DIR\jre" |
| 16 | +$DESKTOP_SHORTCUT = "$env:USERPROFILE\Desktop\Woodlanders Launcher.lnk" |
| 17 | +$START_MENU_DIR = "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\Woodlanders" |
| 18 | + |
| 19 | +# Liberica JDK Full (includes JavaFX) download URL |
| 20 | +$JRE_DOWNLOAD_URL = "https://download.bell-sw.com/java/21.0.5+11/bellsoft-jdk21.0.5+11-windows-amd64-full.zip" |
| 21 | + |
| 22 | +function Write-Status { |
| 23 | + param([string]$Message) |
| 24 | + if (-not $Silent) { |
| 25 | + Write-Host $Message -ForegroundColor Cyan |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function Write-Success { |
| 30 | + param([string]$Message) |
| 31 | + if (-not $Silent) { |
| 32 | + Write-Host "✓ $Message" -ForegroundColor Green |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function Write-Error-Message { |
| 37 | + param([string]$Message) |
| 38 | + Write-Host "✗ $Message" -ForegroundColor Red |
| 39 | +} |
| 40 | + |
| 41 | +function Test-JavaInstalled { |
| 42 | + try { |
| 43 | + $javaVersion = java -version 2>&1 | Select-String "version" | ForEach-Object { $_.ToString() } |
| 44 | + if ($javaVersion -match "(\d+)\.(\d+)") { |
| 45 | + $majorVersion = [int]$matches[1] |
| 46 | + if ($majorVersion -ge 17) { |
| 47 | + return $true |
| 48 | + } |
| 49 | + } |
| 50 | + } catch { |
| 51 | + return $false |
| 52 | + } |
| 53 | + return $false |
| 54 | +} |
| 55 | + |
| 56 | +function Download-JRE { |
| 57 | + Write-Status "Java 21+ not found. Downloading bundled JDK with JavaFX..." |
| 58 | + |
| 59 | + # Check if JRE is already installed |
| 60 | + if (Test-Path "$JRE_DIR\bin\java.exe") { |
| 61 | + Write-Success "JDK already installed" |
| 62 | + return $true |
| 63 | + } |
| 64 | + |
| 65 | + # Clean up any previous failed installation |
| 66 | + if (Test-Path $JRE_DIR) { |
| 67 | + Remove-Item $JRE_DIR -Recurse -Force -ErrorAction SilentlyContinue |
| 68 | + } |
| 69 | + |
| 70 | + # Create JRE directory |
| 71 | + New-Item -ItemType Directory -Force -Path $JRE_DIR | Out-Null |
| 72 | + |
| 73 | + $jreZip = "$env:TEMP\woodlanders-jre.zip" |
| 74 | + |
| 75 | + try { |
| 76 | + Write-Status "Downloading JDK $JRE_VERSION with JavaFX (this may take a few minutes)..." |
| 77 | + |
| 78 | + # Download JRE |
| 79 | + $ProgressPreference = 'SilentlyContinue' |
| 80 | + Invoke-WebRequest -Uri $JRE_DOWNLOAD_URL -OutFile $jreZip -UseBasicParsing |
| 81 | + $ProgressPreference = 'Continue' |
| 82 | + |
| 83 | + Write-Status "Extracting JDK..." |
| 84 | + $tempExtract = "$env:TEMP\woodlanders-jre-extract" |
| 85 | + if (Test-Path $tempExtract) { |
| 86 | + Remove-Item $tempExtract -Recurse -Force |
| 87 | + } |
| 88 | + Expand-Archive -Path $jreZip -DestinationPath $tempExtract -Force |
| 89 | + |
| 90 | + # Find the extracted JDK directory (usually has a version number) |
| 91 | + $extractedDir = Get-ChildItem -Path $tempExtract -Directory | Select-Object -First 1 |
| 92 | + |
| 93 | + # Move contents to JRE_DIR |
| 94 | + Get-ChildItem -Path $extractedDir.FullName | Move-Item -Destination $JRE_DIR -Force |
| 95 | + |
| 96 | + # Cleanup |
| 97 | + Remove-Item $tempExtract -Recurse -Force |
| 98 | + Remove-Item $jreZip -Force |
| 99 | + |
| 100 | + Write-Success "JDK with JavaFX downloaded and installed" |
| 101 | + return $true |
| 102 | + } catch { |
| 103 | + Write-Error-Message "Failed to download JDK: $_" |
| 104 | + return $false |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +function Install-Application { |
| 109 | + Write-Status "Installing $APP_NAME..." |
| 110 | + |
| 111 | + # Create installation directory |
| 112 | + New-Item -ItemType Directory -Force -Path $INSTALL_DIR | Out-Null |
| 113 | + |
| 114 | + # Copy application files |
| 115 | + $scriptDir = if ($PSScriptRoot) { $PSScriptRoot } else { Split-Path -Parent $MyInvocation.MyCommand.Path } |
| 116 | + |
| 117 | + if (Test-Path "$scriptDir\woodlanders-launcher.jar") { |
| 118 | + Copy-Item "$scriptDir\woodlanders-launcher.jar" -Destination $INSTALL_DIR -Force |
| 119 | + Write-Success "Application files copied" |
| 120 | + } else { |
| 121 | + Write-Error-Message "Application JAR not found in: $scriptDir" |
| 122 | + return $false |
| 123 | + } |
| 124 | + |
| 125 | + # Copy icon if exists |
| 126 | + if (Test-Path "$scriptDir\launcher.ico") { |
| 127 | + Copy-Item "$scriptDir\launcher.ico" -Destination $INSTALL_DIR -Force |
| 128 | + } |
| 129 | + |
| 130 | + return $true |
| 131 | +} |
| 132 | + |
| 133 | +function Create-LauncherScript { |
| 134 | + $launcherScript = @' |
| 135 | +@echo off |
| 136 | +setlocal |
| 137 | +
|
| 138 | +set "APP_DIR=%~dp0" |
| 139 | +set "JAVAFX_CACHE=%USERPROFILE%\.cache\woodlanders-javafx" |
| 140 | +
|
| 141 | +if exist "%APP_DIR%jre\bin\java.exe" ( |
| 142 | + set "JAVA_CMD=%APP_DIR%jre\bin\java.exe" |
| 143 | +) else ( |
| 144 | + set "JAVA_CMD=java" |
| 145 | +) |
| 146 | +
|
| 147 | +"%JAVA_CMD%" -Djavafx.cachedir="%JAVAFX_CACHE%" -jar "%APP_DIR%woodlanders-launcher.jar" |
| 148 | +if %ERRORLEVEL% neq 0 ( |
| 149 | + echo. |
| 150 | + echo Application failed to start. |
| 151 | + pause |
| 152 | +) |
| 153 | +
|
| 154 | +endlocal |
| 155 | +'@ |
| 156 | + |
| 157 | + $launcherScript | Out-File -FilePath "$INSTALL_DIR\launcher.bat" -Encoding ASCII -Force |
| 158 | + Write-Success "Launcher script created" |
| 159 | +} |
| 160 | + |
| 161 | +function Create-Shortcuts { |
| 162 | + $WshShell = New-Object -ComObject WScript.Shell |
| 163 | + |
| 164 | + # Desktop shortcut |
| 165 | + $Shortcut = $WshShell.CreateShortcut($DESKTOP_SHORTCUT) |
| 166 | + $Shortcut.TargetPath = "$INSTALL_DIR\launcher.bat" |
| 167 | + $Shortcut.WorkingDirectory = $INSTALL_DIR |
| 168 | + $Shortcut.Description = "Woodlanders Game Launcher" |
| 169 | + if (Test-Path "$INSTALL_DIR\launcher.ico") { |
| 170 | + $Shortcut.IconLocation = "$INSTALL_DIR\launcher.ico" |
| 171 | + } |
| 172 | + $Shortcut.Save() |
| 173 | + Write-Success "Desktop shortcut created" |
| 174 | + |
| 175 | + # Start Menu shortcut |
| 176 | + New-Item -ItemType Directory -Force -Path $START_MENU_DIR | Out-Null |
| 177 | + $StartMenuShortcut = $WshShell.CreateShortcut("$START_MENU_DIR\Woodlanders Launcher.lnk") |
| 178 | + $StartMenuShortcut.TargetPath = "$INSTALL_DIR\launcher.bat" |
| 179 | + $StartMenuShortcut.WorkingDirectory = $INSTALL_DIR |
| 180 | + $StartMenuShortcut.Description = "Woodlanders Game Launcher" |
| 181 | + if (Test-Path "$INSTALL_DIR\launcher.ico") { |
| 182 | + $StartMenuShortcut.IconLocation = "$INSTALL_DIR\launcher.ico" |
| 183 | + } |
| 184 | + $StartMenuShortcut.Save() |
| 185 | + Write-Success "Start Menu shortcut created" |
| 186 | +} |
| 187 | + |
| 188 | +function Show-CompletionMessage { |
| 189 | + Write-Host "" |
| 190 | + Write-Host "=========================================" -ForegroundColor Green |
| 191 | + Write-Host " Installation Complete!" -ForegroundColor Green |
| 192 | + Write-Host "=========================================" -ForegroundColor Green |
| 193 | + Write-Host "" |
| 194 | + Write-Host "Installed to: $INSTALL_DIR" |
| 195 | + Write-Host "" |
| 196 | + Write-Host "You can now launch $APP_NAME from:" |
| 197 | + Write-Host " • Desktop shortcut" |
| 198 | + Write-Host " • Start Menu > Woodlanders" |
| 199 | + Write-Host "" |
| 200 | + |
| 201 | + if (-not $Silent) { |
| 202 | + $launch = Read-Host "Launch now? (Y/n)" |
| 203 | + if ($launch -ne 'n' -and $launch -ne 'N') { |
| 204 | + Write-Host "" |
| 205 | + Write-Host "Launching $APP_NAME..." -ForegroundColor Cyan |
| 206 | + # Use cmd /c to launch the batch file and detach from PowerShell |
| 207 | + $process = Start-Process -FilePath "$INSTALL_DIR\launcher.bat" -PassThru |
| 208 | + Start-Sleep -Seconds 2 |
| 209 | + Write-Host "Launcher started. You can close this window." -ForegroundColor Green |
| 210 | + } |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +# Main installation process |
| 215 | +try { |
| 216 | + Write-Host "" |
| 217 | + Write-Host "=========================================" -ForegroundColor Cyan |
| 218 | + Write-Host " $APP_NAME Installer" -ForegroundColor Cyan |
| 219 | + Write-Host " Version: $APP_VERSION" -ForegroundColor Cyan |
| 220 | + Write-Host "=========================================" -ForegroundColor Cyan |
| 221 | + Write-Host "" |
| 222 | + |
| 223 | + # Check if Java is installed |
| 224 | + if (Test-JavaInstalled) { |
| 225 | + Write-Success "Java 17+ found on system" |
| 226 | + } else { |
| 227 | + if (-not (Download-JRE)) { |
| 228 | + throw "Failed to download and install JRE" |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + # Install application |
| 233 | + if (-not (Install-Application)) { |
| 234 | + throw "Failed to install application" |
| 235 | + } |
| 236 | + |
| 237 | + # Create launcher script |
| 238 | + Create-LauncherScript |
| 239 | + |
| 240 | + # Create shortcuts |
| 241 | + Create-Shortcuts |
| 242 | + |
| 243 | + # Show completion message |
| 244 | + Show-CompletionMessage |
| 245 | + |
| 246 | +} catch { |
| 247 | + Write-Host "" |
| 248 | + Write-Error-Message "Installation failed: $_" |
| 249 | + Write-Host "" |
| 250 | + if (-not $Silent) { |
| 251 | + Read-Host "Press Enter to exit" |
| 252 | + } |
| 253 | + exit 1 |
| 254 | +} |
0 commit comments