| 1 | Unicode true |
| 2 | RequestExecutionLevel user |
| 3 | ; Per-file decompression avoids a full payload copy in the system TEMP drive. |
| 4 | SetCompressor lzma |
| 5 | SetCompressorDictSize 32 |
| 6 | ManifestDPIAware true |
| 7 | ManifestSupportedOS all |
| 8 | |
| 9 | !include MUI2.nsh |
| 10 | !define MUI_ICON "Cage.ico" |
| 11 | !define MUI_UNICON "Cage.ico" |
| 12 | !include LogicLib.nsh |
| 13 | !include FileFunc.nsh |
| 14 | !include x64.nsh |
| 15 | !include WinVer.nsh |
| 16 | !include nsDialogs.nsh |
| 17 | |
| 18 | !ifndef APP_DIR |
| 19 | !error "APP_DIR is required" |
| 20 | !endif |
| 21 | !ifndef VERSION |
| 22 | !error "VERSION is required" |
| 23 | !endif |
| 24 | !ifndef VERSION_NUMERIC |
| 25 | !error "VERSION_NUMERIC is required" |
| 26 | !endif |
| 27 | !ifndef OUTPUT_FILE |
| 28 | !error "OUTPUT_FILE is required" |
| 29 | !endif |
| 30 | |
| 31 | !define APP_ID "io.github.lee-xeo.claude-cage" |
| 32 | !define UNINSTALL_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\ClaudeCage" |
| 33 | !define WEBVIEW2_APP_ID "{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}" |
| 34 | Name "Cage ${VERSION}" |
| 35 | OutFile "${OUTPUT_FILE}" |
| 36 | InstallDir "$LOCALAPPDATA\Programs\Cage" |
| 37 | InstallDirRegKey HKCU "${UNINSTALL_KEY}" "InstallLocation" |
| 38 | VIProductVersion "${VERSION_NUMERIC}" |
| 39 | VIAddVersionKey /LANG=1033 "ProductName" "Cage" |
| 40 | VIAddVersionKey /LANG=1033 "ProductVersion" "${VERSION}" |
| 41 | VIAddVersionKey /LANG=1033 "FileVersion" "${VERSION}" |
| 42 | VIAddVersionKey /LANG=1033 "FileDescription" "Cage Windows Installer" |
| 43 | VIAddVersionKey /LANG=1033 "LegalCopyright" "Cage contributors" |
| 44 | |
| 45 | !ifdef SIGN_SCRIPT |
| 46 | !uninstfinalize '"${SIGN_PWSH}" -NoProfile -File "${SIGN_SCRIPT}" -Path "%1"' = 0 |
| 47 | !endif |
| 48 | |
| 49 | !define MUI_ABORTWARNING |
| 50 | !define MUI_FINISHPAGE_RUN "$INSTDIR\Cage.exe" |
| 51 | !define MUI_FINISHPAGE_RUN_NOTCHECKED |
| 52 | !define MUI_FINISHPAGE_TEXT "Cage is installed.$\r$\n$\r$\nIf virtualization is unavailable, enable Windows Hypervisor Platform in Windows Features and restart Windows. Your projects and Cage data are preserved during upgrades and uninstall." |
| 53 | !insertmacro MUI_PAGE_WELCOME |
| 54 | !insertmacro MUI_PAGE_LICENSE "${APP_DIR}/LICENSE.txt" |
| 55 | !define MUI_DIRECTORYPAGE_TEXT_TOP "$(CageDirectoryHelp)" |
| 56 | !insertmacro MUI_PAGE_DIRECTORY |
| 57 | Page custom ShortcutOptionsPage ShortcutOptionsLeave |
| 58 | !insertmacro MUI_PAGE_INSTFILES |
| 59 | !insertmacro MUI_PAGE_FINISH |
| 60 | !insertmacro MUI_UNPAGE_CONFIRM |
| 61 | !insertmacro MUI_UNPAGE_INSTFILES |
| 62 | !insertmacro MUI_LANGUAGE "English" |
| 63 | !insertmacro MUI_LANGUAGE "SimpChinese" |
| 64 | |
| 65 | LangString CageDirectoryHelp ${LANG_ENGLISH} "Choose where to install the Cage application. On first launch, you can choose a separate drive for the Linux image, download cache, and virtual disks before starting the environment." |
| 66 | LangString CageDirectoryHelp ${LANG_SIMPCHINESE} "请选择 Cage 程序的安装目录。首次打开 Cage 后,可以在启动环境前单独选择存放 Linux 镜像、下载缓存和虚拟磁盘的磁盘。" |
| 67 | LangString CageShortcutsTitle ${LANG_ENGLISH} "Shortcuts" |
| 68 | LangString CageShortcutsTitle ${LANG_SIMPCHINESE} "快捷方式" |
| 69 | LangString CageShortcutsHelp ${LANG_ENGLISH} "Choose where to add Cage shortcuts for your Windows account." |
| 70 | LangString CageShortcutsHelp ${LANG_SIMPCHINESE} "选择为当前 Windows 用户添加的 Cage 快捷方式。" |
| 71 | LangString CageDesktopShortcut ${LANG_ENGLISH} "Add a desktop shortcut" |
| 72 | LangString CageDesktopShortcut ${LANG_SIMPCHINESE} "添加桌面快捷方式" |
| 73 | LangString CageStartMenuShortcuts ${LANG_ENGLISH} "Add to the Start menu" |
| 74 | LangString CageStartMenuShortcuts ${LANG_SIMPCHINESE} "添加到开始菜单" |
| 75 | |
| 76 | Var UpdateMode |
| 77 | Var StageDir |
| 78 | Var BackupDir |
| 79 | Var ExistingInstall |
| 80 | Var DesktopShortcut |
| 81 | Var StartMenuShortcuts |
| 82 | Var DesktopShortcutControl |
| 83 | Var StartMenuShortcutsControl |
| 84 | Var InstallationMutex |
| 85 | Var TransactionRecord |
| 86 | Var DiagnosticFile |
| 87 | Var InstallPhase |
| 88 | Var InstallOutcome |
| 89 | Var HelperExit |
| 90 | Var DiagnosticArguments |
| 91 | |
| 92 | Function WriteInstallDiagnostic |
| 93 | ${If} $DiagnosticFile != "" |
| 94 | Push $2 |
| 95 | Push $3 |
| 96 | StrCpy $3 0 |
| 97 | IfErrors 0 +2 |
| 98 | StrCpy $3 1 |
| 99 | ClearErrors |
| 100 | FileOpen $2 "$DiagnosticFile" w |
| 101 | ${IfNot} ${Errors} |
| 102 | FileWrite $2 '{"phase":"$InstallPhase","status":"$InstallOutcome","helperExit":"$HelperExit"}$\r$\n' |
| 103 | FileClose $2 |
| 104 | ${EndIf} |
| 105 | ${If} $3 == 1 |
| 106 | SetErrors |
| 107 | ${Else} |
| 108 | ClearErrors |
| 109 | ${EndIf} |
| 110 | Pop $3 |
| 111 | Pop $2 |
| 112 | ${EndIf} |
| 113 | FunctionEnd |
| 114 | |
| 115 | !macro InstallPhase NAME |
| 116 | StrCpy $InstallPhase "${NAME}" |
| 117 | Call WriteInstallDiagnostic |
| 118 | !macroend |
| 119 | |
| 120 | Function un.WriteInstallDiagnostic |
| 121 | ${If} $DiagnosticFile != "" |
| 122 | Push $2 |
| 123 | Push $3 |
| 124 | StrCpy $3 0 |
| 125 | IfErrors 0 +2 |
| 126 | StrCpy $3 1 |
| 127 | ClearErrors |
| 128 | FileOpen $2 "$DiagnosticFile" w |
| 129 | ${IfNot} ${Errors} |
| 130 | FileWrite $2 '{"phase":"$InstallPhase","status":"$InstallOutcome","helperExit":"$HelperExit"}$\r$\n' |
| 131 | FileClose $2 |
| 132 | ${EndIf} |
| 133 | ${If} $3 == 1 |
| 134 | SetErrors |
| 135 | ${Else} |
| 136 | ClearErrors |
| 137 | ${EndIf} |
| 138 | Pop $3 |
| 139 | Pop $2 |
| 140 | ${EndIf} |
| 141 | FunctionEnd |
| 142 | |
| 143 | !macro UninstallPhase NAME |
| 144 | StrCpy $InstallPhase "${NAME}" |
| 145 | Call un.WriteInstallDiagnostic |
| 146 | !macroend |
| 147 | |
| 148 | Function un.onUninstFailed |
| 149 | StrCpy $InstallOutcome "failed" |
| 150 | Call un.WriteInstallDiagnostic |
| 151 | FunctionEnd |
| 152 | |
| 153 | Function un.onUninstSuccess |
| 154 | StrCpy $InstallOutcome "success" |
| 155 | !insertmacro UninstallPhase "completed" |
| 156 | FunctionEnd |
| 157 | |
| 158 | Function .onInstFailed |
| 159 | StrCpy $InstallOutcome "failed" |
| 160 | Call WriteInstallDiagnostic |
| 161 | FunctionEnd |
| 162 | |
| 163 | Function .onInstSuccess |
| 164 | StrCpy $InstallOutcome "success" |
| 165 | Call WriteInstallDiagnostic |
| 166 | FunctionEnd |
| 167 | |
| 168 | !macro AcquireInstallationMutex |
| 169 | ; NSIS and UpdateInstaller both reject an existing named object and keep |
| 170 | ; their handle until the transaction ends. No caller waits for ownership. |
| 171 | System::Call 'kernel32::CreateMutexW(p 0, i 0, w "Global\Cage.Installation") p.r0 ?e' |
| 172 | Pop $1 |
| 173 | ${If} $0 == 0 |
| 174 | SetErrorLevel 2 |
| 175 | Abort "无法锁定 Cage 安装操作,请核对系统权限后重试。" |
| 176 | ${EndIf} |
| 177 | StrCpy $InstallationMutex $0 |
| 178 | ${If} $1 == 183 |
| 179 | SetErrorLevel 2 |
| 180 | Abort "另一个 Cage 安装或卸载仍在进行,请等待完成后重试。" |
| 181 | ${EndIf} |
| 182 | !macroend |
| 183 | |
| 184 | Function .onGUIEnd |
| 185 | ${If} $InstallationMutex != "" |
| 186 | System::Call 'kernel32::CloseHandle(p $InstallationMutex)' |
| 187 | ${EndIf} |
| 188 | FunctionEnd |
| 189 | |
| 190 | Function un.onGUIEnd |
| 191 | ${If} $InstallationMutex != "" |
| 192 | System::Call 'kernel32::CloseHandle(p $InstallationMutex)' |
| 193 | ${EndIf} |
| 194 | FunctionEnd |
| 195 | |
| 196 | Function LoadShortcutOptions |
| 197 | ; Fresh installs enable both entries. Older installs inherit actual shortcuts. |
| 198 | StrCpy $DesktopShortcut 1 |
| 199 | StrCpy $StartMenuShortcuts 1 |
| 200 | ReadRegStr $0 HKCU "${UNINSTALL_KEY}" "InstallLocation" |
| 201 | ${If} $0 != "" |
| 202 | StrCpy $DesktopShortcut 0 |
| 203 | StrCpy $StartMenuShortcuts 0 |
| 204 | ${If} ${FileExists} "$DESKTOP\Cage.lnk" |
| 205 | StrCpy $DesktopShortcut 1 |
| 206 | ${EndIf} |
| 207 | ${If} ${FileExists} "$SMPROGRAMS\Cage\Cage.lnk" |
| 208 | StrCpy $StartMenuShortcuts 1 |
| 209 | ${EndIf} |
| 210 | ${EndIf} |
| 211 | ClearErrors |
| 212 | ReadRegDWORD $0 HKCU "${UNINSTALL_KEY}" "DesktopShortcut" |
| 213 | ${IfNot} ${Errors} |
| 214 | StrCpy $DesktopShortcut $0 |
| 215 | ${EndIf} |
| 216 | ClearErrors |
| 217 | ReadRegDWORD $0 HKCU "${UNINSTALL_KEY}" "StartMenuShortcuts" |
| 218 | ${IfNot} ${Errors} |
| 219 | StrCpy $StartMenuShortcuts $0 |
| 220 | ${EndIf} |
| 221 | ${GetParameters} $0 |
| 222 | ClearErrors |
| 223 | ${GetOptions} $0 "/DESKTOPSHORTCUT=" $1 |
| 224 | ${IfNot} ${Errors} |
| 225 | ${If} $1 != "0" |
| 226 | ${AndIf} $1 != "1" |
| 227 | SetErrorLevel 2 |
| 228 | Abort "DESKTOPSHORTCUT must be 0 or 1." |
| 229 | ${EndIf} |
| 230 | StrCpy $DesktopShortcut $1 |
| 231 | ${EndIf} |
| 232 | ClearErrors |
| 233 | ${GetOptions} $0 "/STARTMENUSHORTCUT=" $1 |
| 234 | ${IfNot} ${Errors} |
| 235 | ${If} $1 != "0" |
| 236 | ${AndIf} $1 != "1" |
| 237 | SetErrorLevel 2 |
| 238 | Abort "STARTMENUSHORTCUT must be 0 or 1." |
| 239 | ${EndIf} |
| 240 | StrCpy $StartMenuShortcuts $1 |
| 241 | ${EndIf} |
| 242 | FunctionEnd |
| 243 | |
| 244 | Function ShortcutOptionsPage |
| 245 | !insertmacro MUI_HEADER_TEXT "$(CageShortcutsTitle)" "$(CageShortcutsHelp)" |
| 246 | nsDialogs::Create 1018 |
| 247 | Pop $0 |
| 248 | ${If} $0 == error |
| 249 | Abort |
| 250 | ${EndIf} |
| 251 | ${NSD_CreateCheckbox} 0 8u 100% 16u "$(CageDesktopShortcut)" |
| 252 | Pop $DesktopShortcutControl |
| 253 | ${NSD_SetState} $DesktopShortcutControl $DesktopShortcut |
| 254 | ${NSD_CreateCheckbox} 0 36u 100% 16u "$(CageStartMenuShortcuts)" |
| 255 | Pop $StartMenuShortcutsControl |
| 256 | ${NSD_SetState} $StartMenuShortcutsControl $StartMenuShortcuts |
| 257 | ${NSD_OnBack} ShortcutOptionsLeave |
| 258 | nsDialogs::Show |
| 259 | FunctionEnd |
| 260 | |
| 261 | Function ShortcutOptionsLeave |
| 262 | ${NSD_GetState} $DesktopShortcutControl $DesktopShortcut |
| 263 | ${NSD_GetState} $StartMenuShortcutsControl $StartMenuShortcuts |
| 264 | FunctionEnd |
| 265 | |
| 266 | Function SetDefaultInstallDirectory |
| 267 | ; NSIS removes /D= from $CMDLINE, so inspect the original command line. |
| 268 | System::Call 'kernel32::GetCommandLineW() w .r0' |
| 269 | ClearErrors |
| 270 | ${GetOptions} $0 "/D=" $1 |
| 271 | ${IfNot} ${Errors} |
| 272 | Return |
| 273 | ${EndIf} |
| 274 | ; Keep upgrades in the registered location, including installations on C:. |
| 275 | ReadRegStr $0 HKCU "${UNINSTALL_KEY}" "InstallLocation" |
| 276 | ${If} $0 != "" |
| 277 | StrCpy $INSTDIR $0 |
| 278 | Return |
| 279 | ${EndIf} |
| 280 | System::Call 'kernel32::GetDriveTypeW(w "D:\") i .r0' |
| 281 | ${If} $0 == 3 |
| 282 | StrCpy $INSTDIR "D:\Cage" |
| 283 | ${EndIf} |
| 284 | FunctionEnd |
| 285 | |
| 286 | !macro CheckStopped FILE |
| 287 | ${If} ${FileExists} "$INSTDIR\${FILE}" |
| 288 | ; Exclusive write access also detects images whose VM/controller has no window. |
| 289 | System::Call 'kernel32::CreateFileW(w "$INSTDIR\${FILE}", i 0x40000000, i 0, p 0, i 3, i 0, p 0) p .r0' |
| 290 | ${If} $0 == -1 |
| 291 | StrCpy $HelperExit "application_file_in_use" |
| 292 | SetErrorLevel 2 |
| 293 | Abort "Quit Cage and stop its environment before installing or uninstalling." |
| 294 | ${EndIf} |
| 295 | System::Call 'kernel32::CloseHandle(p r0)' |
| 296 | ${EndIf} |
| 297 | !macroend |
| 298 | |
| 299 | Function .onInit |
| 300 | StrCpy $InstallOutcome "running" |
| 301 | StrCpy $HelperExit "" |
| 302 | ${GetParameters} $0 |
| 303 | ${GetOptions} $0 "/DIAGNOSTICS=" $DiagnosticFile |
| 304 | StrCpy $DiagnosticArguments "" |
| 305 | ${If} $DiagnosticFile != "" |
| 306 | StrCpy $DiagnosticArguments '-Diagnostics "$DiagnosticFile.helper.json"' |
| 307 | ${EndIf} |
| 308 | !insertmacro InstallPhase "initializing" |
| 309 | SetShellVarContext current |
| 310 | SetRegView 64 |
| 311 | ${IfNot} ${IsNativeAMD64} |
| 312 | MessageBox MB_ICONSTOP "This Cage package requires an Intel/AMD 64-bit version of Windows. Windows on ARM and 32-bit Windows are not supported." |
| 313 | SetErrorLevel 1633 |
| 314 | Quit |
| 315 | ${EndIf} |
| 316 | ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "CurrentBuildNumber" |
| 317 | ${If} $0 < 19045 |
| 318 | MessageBox MB_ICONSTOP "Cage requires Windows 10 22H2 (build 19045) or Windows 11. Update Windows and try again." |
| 319 | SetErrorLevel 1633 |
| 320 | Quit |
| 321 | ${EndIf} |
| 322 | Call SetDefaultInstallDirectory |
| 323 | Call LoadShortcutOptions |
| 324 | StrCpy $UpdateMode "0" |
| 325 | ${GetParameters} $0 |
| 326 | ClearErrors |
| 327 | ${GetOptions} $0 "/UPDATE" $1 |
| 328 | ${IfNot} ${Errors} |
| 329 | StrCpy $UpdateMode "1" |
| 330 | SetSilent silent |
| 331 | ${EndIf} |
| 332 | FunctionEnd |
| 333 | |
| 334 | Function CheckDestination |
| 335 | ${GetRoot} "$INSTDIR" $0 |
| 336 | ${If} $0 == "" |
| 337 | ${OrIf} $INSTDIR == $0 |
| 338 | ${OrIf} $INSTDIR == "$0\" |
| 339 | SetErrorLevel 2 |
| 340 | Abort "Choose a dedicated application folder." |
| 341 | ${EndIf} |
| 342 | StrCpy $ExistingInstall "0" |
| 343 | IfFileExists "$INSTDIR\Cage.install-id" existing |
| 344 | ; IfFileExists with a wildcard also matches an empty directory. |
| 345 | ${DirState} "$INSTDIR" $0 |
| 346 | StrCmp $0 1 foreign done |
| 347 | existing: |
| 348 | FileOpen $0 "$INSTDIR\Cage.install-id" r |
| 349 | FileRead $0 $1 |
| 350 | FileClose $0 |
| 351 | ${If} $1 != "${APP_ID}" |
| 352 | Goto foreign |
| 353 | ${EndIf} |
| 354 | StrCpy $ExistingInstall "1" |
| 355 | ; Never stop processes by their name or force-close user workloads. |
| 356 | !insertmacro CheckStopped "Cage.exe" |
| 357 | !insertmacro CheckStopped "resources\cage.exe" |
| 358 | !insertmacro CheckStopped "resources\mihomo.exe" |
| 359 | !insertmacro CheckStopped "resources\runtime\qemu\qemu-system-x86_64.exe" |
| 360 | Goto done |
| 361 | foreign: |
| 362 | SetErrorLevel 2 |
| 363 | Abort "The destination contains files from another installation. Choose an empty folder." |
| 364 | done: |
| 365 | ClearErrors |
| 366 | FunctionEnd |
| 367 | |
| 368 | Function RestorePreservedFiles |
| 369 | nsExec::ExecToStack '"$SYSDIR\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File "$PLUGINSDIR\installation-files.ps1" -Operation restore -Target "$BackupDir" -Source "$StageDir" -Receipt "$TransactionRecord" $DiagnosticArguments' |
| 370 | Pop $0 |
| 371 | Pop $1 |
| 372 | ${If} $0 != 0 |
| 373 | DetailPrint "$1" |
| 374 | SetErrorLevel 2 |
| 375 | Abort "安装恢复尚未完成。文件分别保留在 $BackupDir 和 $StageDir,请保留这两个目录后重试。" |
| 376 | ${EndIf} |
| 377 | ClearErrors |
| 378 | Rename "$BackupDir" "$INSTDIR" |
| 379 | ${If} ${Errors} |
| 380 | SetErrorLevel 2 |
| 381 | Abort "原安装目录未能恢复。文件保留在 $BackupDir,新程序保留在 $StageDir。" |
| 382 | ${EndIf} |
| 383 | nsExec::ExecToStack '"$SYSDIR\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File "$PLUGINSDIR\installation-files.ps1" -Operation complete -Target "$INSTDIR" -Receipt "$TransactionRecord" $DiagnosticArguments' |
| 384 | Pop $0 |
| 385 | Pop $1 |
| 386 | ${If} $0 != 0 |
| 387 | SetErrorLevel 2 |
| 388 | Abort "仍有文件需要核对,安装回执保留在 $TransactionRecord。请保留相关目录后处理。" |
| 389 | ${EndIf} |
| 390 | FunctionEnd |
| 391 | |
| 392 | Section "Cage" Main |
| 393 | !insertmacro InstallPhase "acquiring_installation_lock" |
| 394 | !insertmacro AcquireInstallationMutex |
| 395 | StrCpy $TransactionRecord "$INSTDIR.cage-installation.json" |
| 396 | ${If} ${FileExists} "$TransactionRecord" |
| 397 | SetErrorLevel 2 |
| 398 | Abort "存在未处理的安装事务记录:$TransactionRecord。请先按记录恢复保留的目录,再重试。" |
| 399 | ${EndIf} |
| 400 | !insertmacro InstallPhase "checking_destination" |
| 401 | Call CheckDestination |
| 402 | ${GetParent} "$INSTDIR" $0 |
| 403 | CreateDirectory "$0" |
| 404 | GetTempFileName $StageDir "$0" |
| 405 | Delete "$StageDir" |
| 406 | CreateDirectory "$StageDir" |
| 407 | !insertmacro InstallPhase "unpacking_application" |
| 408 | SetOutPath "$StageDir" |
| 409 | ClearErrors |
| 410 | !ifdef NSIS_WIN32_MAKENSIS |
| 411 | File /r "${APP_DIR}\*" |
| 412 | !else |
| 413 | File /r "${APP_DIR}/*" |
| 414 | !endif |
| 415 | WriteUninstaller "$StageDir\Uninstall.exe" |
| 416 | ${If} ${Errors} |
| 417 | SetOutPath "$TEMP" |
| 418 | RMDir /r "$StageDir" |
| 419 | SetErrorLevel 2 |
| 420 | Abort "Could not unpack Cage. Check free disk space and folder permissions." |
| 421 | ${EndIf} |
| 422 | |
| 423 | ; Install the bundled offline runtime only when WebView2 is absent. |
| 424 | !insertmacro InstallPhase "detecting_webview_runtime" |
| 425 | SetRegView 32 |
| 426 | ReadRegStr $0 HKLM "SOFTWARE\Microsoft\EdgeUpdate\Clients\${WEBVIEW2_APP_ID}" "pv" |
| 427 | ReadRegStr $1 HKCU "Software\Microsoft\EdgeUpdate\Clients\${WEBVIEW2_APP_ID}" "pv" |
| 428 | SetRegView 64 |
| 429 | ${If} $0 == "" |
| 430 | ${OrIf} $0 == "0.0.0.0" |
| 431 | ${If} $1 == "" |
| 432 | ${OrIf} $1 == "0.0.0.0" |
| 433 | DetailPrint "Installing the bundled Microsoft WebView2 runtime..." |
| 434 | !insertmacro InstallPhase "installing_webview_runtime" |
| 435 | ExecWait '"$StageDir\resources\runtime\MicrosoftEdgeWebView2RuntimeInstallerX64.exe" /silent /install' $0 |
| 436 | StrCpy $HelperExit $0 |
| 437 | Call WriteInstallDiagnostic |
| 438 | ${If} $0 != 0 |
| 439 | SetOutPath "$TEMP" |
| 440 | RMDir /r "$StageDir" |
| 441 | SetErrorLevel 2 |
| 442 | Abort "Microsoft WebView2 installation failed ($0). Install the runtime and retry." |
| 443 | ${EndIf} |
| 444 | ${EndIf} |
| 445 | ${EndIf} |
| 446 | |
| 447 | SetOutPath "$TEMP" |
| 448 | StrCpy $BackupDir "" |
| 449 | ${If} $ExistingInstall == "1" |
| 450 | ${GetParent} "$INSTDIR" $0 |
| 451 | GetTempFileName $BackupDir "$0" |
| 452 | Delete "$BackupDir" |
| 453 | ${EndIf} |
| 454 | InitPluginsDir |
| 455 | SetOutPath "$PLUGINSDIR" |
| 456 | File /oname=installation-files.ps1 "installation-files.ps1" |
| 457 | File /oname=verified-file-deletion.cs "verified-file-deletion.cs" |
| 458 | DetailPrint "正在检查并保留已有项目和修改文件…" |
| 459 | !insertmacro InstallPhase "preparing_file_transaction" |
| 460 | nsExec::ExecToStack '"$SYSDIR\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File "$PLUGINSDIR\installation-files.ps1" -Operation prepare -Target "$StageDir" -Source "$INSTDIR" -Backup "$BackupDir" -Receipt "$TransactionRecord" $DiagnosticArguments' |
| 461 | Pop $0 |
| 462 | Pop $1 |
| 463 | StrCpy $HelperExit $0 |
| 464 | Call WriteInstallDiagnostic |
| 465 | DetailPrint "$1" |
| 466 | ${If} $0 != 0 |
| 467 | SetOutPath "$TEMP" |
| 468 | RMDir /r "$StageDir" |
| 469 | SetErrorLevel 2 |
| 470 | Abort "无法安全保留现有文件,安装已停止。原安装目录已保留,请检查磁盘空间、文件占用和安装日志。" |
| 471 | ${EndIf} |
| 472 | SetOutPath "$TEMP" |
| 473 | ${If} $ExistingInstall == "1" |
| 474 | !insertmacro InstallPhase "moving_previous_application" |
| 475 | ClearErrors |
| 476 | Rename "$INSTDIR" "$BackupDir" |
| 477 | ${If} ${Errors} |
| 478 | nsExec::ExecToStack '"$SYSDIR\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File "$PLUGINSDIR\installation-files.ps1" -Operation complete -Target "$INSTDIR" -Receipt "$TransactionRecord" $DiagnosticArguments' |
| 479 | Pop $0 |
| 480 | Pop $1 |
| 481 | RMDir /r "$StageDir" |
| 482 | SetErrorLevel 2 |
| 483 | Abort "Could not move the installed application. Quit Cage and retry." |
| 484 | ${EndIf} |
| 485 | DetailPrint "正在迁入个人文件并保留原有链接…" |
| 486 | !insertmacro InstallPhase "moving_personal_files" |
| 487 | nsExec::ExecToStack '"$SYSDIR\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File "$PLUGINSDIR\installation-files.ps1" -Operation move -Target "$StageDir" -Source "$BackupDir" -Receipt "$TransactionRecord" $DiagnosticArguments' |
| 488 | Pop $0 |
| 489 | Pop $1 |
| 490 | StrCpy $HelperExit $0 |
| 491 | Call WriteInstallDiagnostic |
| 492 | DetailPrint "$1" |
| 493 | ${If} $0 != 0 |
| 494 | Call RestorePreservedFiles |
| 495 | RMDir /r "$StageDir" |
| 496 | SetErrorLevel 2 |
| 497 | Abort "个人文件未能迁入新程序目录,原安装已恢复。请关闭占用文件的程序后重试。" |
| 498 | ${EndIf} |
| 499 | ${Else} |
| 500 | RMDir "$INSTDIR" |
| 501 | ${EndIf} |
| 502 | !insertmacro InstallPhase "publishing_application" |
| 503 | ClearErrors |
| 504 | Rename "$StageDir" "$INSTDIR" |
| 505 | ${If} ${Errors} |
| 506 | ${If} $BackupDir != "" |
| 507 | Call RestorePreservedFiles |
| 508 | ${EndIf} |
| 509 | RMDir /r "$StageDir" |
| 510 | SetErrorLevel 2 |
| 511 | Abort "Could not install Cage. The previous installation has been restored." |
| 512 | ${EndIf} |
| 513 | ${If} $BackupDir != "" |
| 514 | !insertmacro InstallPhase "cleaning_previous_application" |
| 515 | DetailPrint "正在清理已核对的旧程序文件…" |
| 516 | nsExec::ExecToStack '"$SYSDIR\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File "$PLUGINSDIR\installation-files.ps1" -Operation cleanup -Target "$INSTDIR" -Source "$BackupDir" -Receipt "$TransactionRecord" $DiagnosticArguments' |
| 517 | Pop $0 |
| 518 | Pop $1 |
| 519 | StrCpy $HelperExit $0 |
| 520 | Call WriteInstallDiagnostic |
| 521 | DetailPrint "$1" |
| 522 | ${If} $0 != 0 |
| 523 | DetailPrint "旧目录中仍有需要保留的文件:$BackupDir" |
| 524 | SetErrorLevel 2 |
| 525 | Abort "安装仍有待恢复的文件。请保留 $TransactionRecord 及其中记录的目录,再处理恢复。" |
| 526 | ${EndIf} |
| 527 | ${Else} |
| 528 | !insertmacro InstallPhase "completing_file_transaction" |
| 529 | nsExec::ExecToStack '"$SYSDIR\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File "$PLUGINSDIR\installation-files.ps1" -Operation complete -Target "$INSTDIR" -Receipt "$TransactionRecord" $DiagnosticArguments' |
| 530 | Pop $0 |
| 531 | Pop $1 |
| 532 | StrCpy $HelperExit $0 |
| 533 | Call WriteInstallDiagnostic |
| 534 | ${If} $0 != 0 |
| 535 | SetErrorLevel 2 |
| 536 | Abort "安装回执未能完成,记录保留在 $TransactionRecord。" |
| 537 | ${EndIf} |
| 538 | ${EndIf} |
| 539 | !insertmacro InstallPhase "registering_installation" |
| 540 | WriteRegStr HKCU "${UNINSTALL_KEY}" "DisplayName" "Cage" |
| 541 | WriteRegStr HKCU "${UNINSTALL_KEY}" "DisplayVersion" "${VERSION}" |
| 542 | WriteRegStr HKCU "${UNINSTALL_KEY}" "Publisher" "Cage contributors" |
| 543 | WriteRegStr HKCU "${UNINSTALL_KEY}" "InstallLocation" "$INSTDIR" |
| 544 | WriteRegStr HKCU "${UNINSTALL_KEY}" "UninstallString" '$\"$INSTDIR\Uninstall.exe$\"' |
| 545 | WriteRegStr HKCU "${UNINSTALL_KEY}" "QuietUninstallString" '$\"$INSTDIR\Uninstall.exe$\" /S' |
| 546 | WriteRegStr HKCU "${UNINSTALL_KEY}" "DisplayIcon" "$INSTDIR\Cage.exe,0" |
| 547 | WriteRegDWORD HKCU "${UNINSTALL_KEY}" "NoModify" 1 |
| 548 | WriteRegDWORD HKCU "${UNINSTALL_KEY}" "NoRepair" 1 |
| 549 | SetOutPath "$INSTDIR" |
| 550 | ${If} $DesktopShortcut == 1 |
| 551 | CreateShortcut "$DESKTOP\Cage.lnk" "$INSTDIR\Cage.exe" |
| 552 | ${Else} |
| 553 | Delete "$DESKTOP\Cage.lnk" |
| 554 | ${EndIf} |
| 555 | ${If} $StartMenuShortcuts == 1 |
| 556 | CreateDirectory "$SMPROGRAMS\Cage" |
| 557 | CreateShortcut "$SMPROGRAMS\Cage\Cage.lnk" "$INSTDIR\Cage.exe" |
| 558 | CreateShortcut "$SMPROGRAMS\Cage\Uninstall Cage.lnk" "$INSTDIR\Uninstall.exe" |
| 559 | ${Else} |
| 560 | Delete "$SMPROGRAMS\Cage\Cage.lnk" |
| 561 | Delete "$SMPROGRAMS\Cage\Uninstall Cage.lnk" |
| 562 | RMDir "$SMPROGRAMS\Cage" |
| 563 | ${EndIf} |
| 564 | WriteRegDWORD HKCU "${UNINSTALL_KEY}" "DesktopShortcut" $DesktopShortcut |
| 565 | WriteRegDWORD HKCU "${UNINSTALL_KEY}" "StartMenuShortcuts" $StartMenuShortcuts |
| 566 | !insertmacro InstallPhase "completed" |
| 567 | SetErrorLevel 0 |
| 568 | SectionEnd |
| 569 | |
| 570 | Function un.onInit |
| 571 | StrCpy $InstallOutcome "running" |
| 572 | StrCpy $HelperExit "" |
| 573 | StrCpy $DiagnosticArguments "" |
| 574 | ${GetParameters} $0 |
| 575 | ${GetOptions} $0 "/DIAGNOSTICS=" $DiagnosticFile |
| 576 | ${If} $DiagnosticFile != "" |
| 577 | StrCpy $DiagnosticArguments '-Diagnostics "$DiagnosticFile.helper.json"' |
| 578 | ${EndIf} |
| 579 | !insertmacro UninstallPhase "checking_installation_identity" |
| 580 | SetShellVarContext current |
| 581 | SetRegView 64 |
| 582 | FileOpen $0 "$INSTDIR\Cage.install-id" r |
| 583 | FileRead $0 $1 |
| 584 | FileClose $0 |
| 585 | ${If} $1 != "${APP_ID}" |
| 586 | StrCpy $HelperExit "identity_mismatch" |
| 587 | Call un.WriteInstallDiagnostic |
| 588 | SetErrorLevel 2 |
| 589 | Abort "This is not a Cage installation folder." |
| 590 | ${EndIf} |
| 591 | !insertmacro UninstallPhase "checking_stopped_processes" |
| 592 | !insertmacro CheckStopped "Cage.exe" |
| 593 | !insertmacro CheckStopped "resources\cage.exe" |
| 594 | !insertmacro CheckStopped "resources\mihomo.exe" |
| 595 | !insertmacro CheckStopped "resources\runtime\qemu\qemu-system-x86_64.exe" |
| 596 | FunctionEnd |
| 597 | |
| 598 | Section "Uninstall" |
| 599 | !insertmacro UninstallPhase "acquiring_installation_lock" |
| 600 | !insertmacro AcquireInstallationMutex |
| 601 | !insertmacro UninstallPhase "checking_pending_transaction" |
| 602 | ${If} ${FileExists} "$INSTDIR.cage-installation.json" |
| 603 | StrCpy $HelperExit "pending_install_transaction" |
| 604 | Call un.WriteInstallDiagnostic |
| 605 | SetErrorLevel 2 |
| 606 | Abort "存在待恢复的安装事务,请先处理 $INSTDIR.cage-installation.json 中记录的目录。" |
| 607 | ${EndIf} |
| 608 | InitPluginsDir |
| 609 | SetOutPath "$PLUGINSDIR" |
| 610 | File /oname=installation-files.ps1 "installation-files.ps1" |
| 611 | File /oname=verified-file-deletion.cs "verified-file-deletion.cs" |
| 612 | File /oname=install-files.json "${FILE_MANIFEST}" |
| 613 | ; PowerShell 5.1 Add-Type must not resolve NSIS's native System.dll as a |
| 614 | ; managed compiler reference from the plugin working directory. |
| 615 | SetOutPath "$TEMP" |
| 616 | !insertmacro UninstallPhase "removing_owned_files" |
| 617 | nsExec::ExecToStack '"$SYSDIR\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File "$PLUGINSDIR\installation-files.ps1" -Operation uninstall -Target "$INSTDIR" -Manifest "$PLUGINSDIR\install-files.json" $DiagnosticArguments' |
| 618 | Pop $0 |
| 619 | Pop $1 |
| 620 | StrCpy $HelperExit $0 |
| 621 | Call un.WriteInstallDiagnostic |
| 622 | DetailPrint "$1" |
| 623 | ${If} $0 != 0 |
| 624 | SetErrorLevel 2 |
| 625 | Abort "部分程序文件正在使用或无法读取,卸载未完成。个人文件已保留,请关闭占用程序后重试。" |
| 626 | ${EndIf} |
| 627 | !insertmacro UninstallPhase "removing_shortcuts_and_registration" |
| 628 | Delete "$DESKTOP\Cage.lnk" |
| 629 | Delete "$SMPROGRAMS\Cage\Cage.lnk" |
| 630 | Delete "$SMPROGRAMS\Cage\Uninstall Cage.lnk" |
| 631 | RMDir "$SMPROGRAMS\Cage" |
| 632 | DeleteRegKey HKCU "${UNINSTALL_KEY}" |
| 633 | !insertmacro UninstallPhase "removing_install_directory" |
| 634 | RMDir "$INSTDIR" |
| 635 | SectionEnd |