installer.nsh 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. !include nsDialogs.nsh
  2. !include FileFunc.nsh
  3. !ifndef ZCODE_INSTALLER_DEFAULT_LOG_PATH
  4. !define ZCODE_INSTALLER_DEFAULT_LOG_PATH "$TEMP\ZCode-installer.log"
  5. !endif
  6. !ifndef ZCODE_INSTALLER_ELEVATED_LOG_PATH
  7. !define ZCODE_INSTALLER_ELEVATED_LOG_PATH "$WINDIR\Logs\ZCode-installer.log"
  8. !endif
  9. !ifndef ZCODE_INSTALLER_IS_ELEVATED_INNER
  10. ; 修复原因:来源只在测试夹具模拟内层,正式默认恒假会让提权进程继续使用调用方 /LOG。
  11. ; 使用 electron-builder 同一 UAC 判据;隔离夹具仍可显式替换,不改变真正的提权流程。
  12. !include UAC.nsh
  13. !define ZCODE_INSTALLER_IS_ELEVATED_INNER `${UAC_IsInnerInstance}`
  14. !endif
  15. !ifndef ZCODE_INSTALL_MANIFEST_NAME
  16. !define ZCODE_INSTALL_MANIFEST_NAME ".zcode-install-manifest"
  17. !endif
  18. !ifndef ZCODE_UNINSTALLER_LOG_PATH
  19. !define ZCODE_UNINSTALLER_LOG_PATH "$TEMP\ZCode-uninstaller.log"
  20. !endif
  21. !ifndef ZCODE_UNINSTALLER_FUNCTION_PREFIX
  22. !define ZCODE_UNINSTALLER_FUNCTION_PREFIX "un."
  23. !endif
  24. !ifdef BUILD_UNINSTALLER
  25. Var ZCodeUninstallerLogUnavailable
  26. ; 卸载器只在更新时删除旧文件;单独记录清理阶段,避免外层把权限/空间错误误报成应用仍在运行。
  27. !macro ZCodeReportUninstallerStage MESSAGE
  28. DetailPrint "ZCode: ${MESSAGE}"
  29. Push "${MESSAGE}"
  30. Call ${ZCODE_UNINSTALLER_FUNCTION_PREFIX}ZCodeWriteUninstallerLog
  31. !macroend
  32. Function ${ZCODE_UNINSTALLER_FUNCTION_PREFIX}ZCodeWriteUninstallerLog
  33. Exch $R9
  34. Push $R0
  35. Push $R1
  36. Push $R2
  37. StrCmp $ZCodeUninstallerLogUnavailable "1" zcodeUninstallerLogDone
  38. ClearErrors
  39. FileOpen $R1 "${ZCODE_UNINSTALLER_LOG_PATH}" a
  40. IfErrors zcodeUninstallerLogFailed zcodeUninstallerLogWrite
  41. zcodeUninstallerLogWrite:
  42. System::Call "kernel32::GetCurrentProcessId() i.R0"
  43. FileSeek $R1 0 END
  44. FileWrite $R1 "[pid=$R0] $R9$\r$\n"
  45. FileClose $R1
  46. Goto zcodeUninstallerLogDone
  47. zcodeUninstallerLogFailed:
  48. ; 日志不可写不应改变卸载结果,保留原始清理错误供外层处理。
  49. StrCpy $ZCodeUninstallerLogUnavailable "1"
  50. ClearErrors
  51. zcodeUninstallerLogDone:
  52. Pop $R2
  53. Pop $R1
  54. Pop $R0
  55. Pop $R9
  56. FunctionEnd
  57. !macro customRemoveFilesDiagnosticsStart
  58. !insertmacro ZCodeReportUninstallerStage "cleanup-started"
  59. !macroend
  60. !macro customRemoveFilesDiagnosticsComplete
  61. !insertmacro ZCodeReportUninstallerStage "cleanup-completed"
  62. !macroend
  63. !endif
  64. !macro customRemoveFiles
  65. ; Bugfix:electron-builder 默认在更新时递归删除整个 $INSTDIR,用户放入的无关文件也会被清掉。
  66. ; 只按上一版本随包生成的所有权清单删除,清单缺失时迁移旧版本采用 fail-open 保留策略。
  67. ${if} ${isUpdated}
  68. !ifdef BUILD_UNINSTALLER
  69. !insertmacro customRemoveFilesDiagnosticsStart
  70. !endif
  71. ClearErrors
  72. FileOpen $R0 "$INSTDIR\${ZCODE_INSTALL_MANIFEST_NAME}" r
  73. IfErrors zcodeManifestMissing
  74. zcodeManifestRead:
  75. ClearErrors
  76. FileRead $R0 $R1
  77. IfErrors zcodeManifestClose
  78. ; NSIS FileRead 保留行尾 CRLF;打包清单统一使用换行结尾,先去掉两个行尾字符。
  79. StrCpy $R1 $R1 -2
  80. StrCmp $R1 "" zcodeManifestRead
  81. ; 拒绝绝对路径和 .. 前缀,避免损坏或篡改清单越界删除。
  82. StrCpy $R2 $R1 1
  83. StrCmp $R2 "\\" zcodeManifestRead
  84. StrCmp $R2 "/" zcodeManifestRead
  85. StrCpy $R2 $R1 2
  86. StrCmp $R2 ".." zcodeManifestRead
  87. StrCmp $R1 "${UNINSTALL_FILENAME}" zcodeManifestRead
  88. GetFullPathName $R2 "$INSTDIR\$R1"
  89. StrCmp $R2 "$INSTDIR\$R1" 0 zcodeManifestRead
  90. ; 当前版本卸载器与外层安装器是两个进程;逐项记录到卸载器日志,便于核对真正尝试删除的文件。
  91. !ifdef BUILD_UNINSTALLER
  92. !insertmacro ZCodeReportUninstallerStage "cleanup-file path=$R1"
  93. !endif
  94. ClearErrors
  95. Delete "$INSTDIR\$R1"
  96. IfErrors zcodeManifestDeleteFailed
  97. Goto zcodeManifestRead
  98. zcodeManifestDeleteFailed:
  99. FileClose $R0
  100. !ifdef BUILD_UNINSTALLER
  101. !insertmacro ZCodeReportUninstallerStage "cleanup-failed reason=permission-or-disk-space"
  102. !endif
  103. Abort "无法删除旧版本文件:$INSTDIR\$R1"
  104. zcodeManifestClose:
  105. FileClose $R0
  106. Goto zcodeManifestDone
  107. zcodeManifestMissing:
  108. ; 首次从旧版本升级时没有清单,不能猜测所有权并删除用户文件。
  109. !ifdef BUILD_UNINSTALLER
  110. !insertmacro ZCodeReportUninstallerStage "cleanup-skipped reason=manifest-missing action=preserve"
  111. !endif
  112. ClearErrors
  113. zcodeManifestDone:
  114. !ifdef BUILD_UNINSTALLER
  115. !insertmacro customRemoveFilesDiagnosticsComplete
  116. !endif
  117. ${else}
  118. ; 普通卸载仍保持 electron-builder 的全量删除语义;ownership 清单只约束覆盖更新。
  119. SetOutPath $TEMP
  120. RMDir /r $INSTDIR
  121. ${endIf}
  122. !macroend
  123. !ifndef BUILD_UNINSTALLER
  124. Var ZCodeInstallerLogPath
  125. Var ZCodeInstallerLogUnavailable
  126. Var ZCodeInstallerProcessRole
  127. Var ZCodeUninstallerDetailsUnavailable
  128. Var ZCodePreviousUninstallerSupportsManifest
  129. ; 详情面板和文件日志共用同一条阶段事件,避免静默安装丢失关键上下文。
  130. !macro ZCodeReportInstallerStage MESSAGE
  131. SetDetailsPrint listonly
  132. DetailPrint "ZCode: ${MESSAGE}"
  133. Push "${MESSAGE}"
  134. Call ZCodeWriteInstallerLog
  135. !macroend
  136. Function ZCodeWriteInstallerLog
  137. Exch $R9
  138. Push $R0
  139. Push $R1
  140. Push $R2
  141. StrCmp $ZCodeInstallerLogPath "" zcodeInstallerLogDone
  142. StrCmp $ZCodeInstallerLogUnavailable "1" zcodeInstallerLogDone
  143. StrCpy $R2 0
  144. zcodeInstallerLogOpen:
  145. ClearErrors
  146. FileOpen $R1 $ZCodeInstallerLogPath a
  147. IfErrors zcodeInstallerLogRetry zcodeInstallerLogWrite
  148. zcodeInstallerLogRetry:
  149. IntOp $R2 $R2 + 1
  150. IntCmp $R2 3 zcodeInstallerLogFailed zcodeInstallerLogWait zcodeInstallerLogFailed
  151. zcodeInstallerLogWait:
  152. Sleep 50
  153. Goto zcodeInstallerLogOpen
  154. zcodeInstallerLogWrite:
  155. System::Call "kernel32::GetCurrentProcessId() i.R0"
  156. FileSeek $R1 0 END
  157. FileWrite $R1 "[pid=$R0] $R9$\r$\n"
  158. FileClose $R1
  159. Goto zcodeInstallerLogDone
  160. zcodeInstallerLogFailed:
  161. StrCpy $ZCodeInstallerLogUnavailable "1"
  162. ClearErrors
  163. zcodeInstallerLogDone:
  164. Pop $R2
  165. Pop $R1
  166. Pop $R0
  167. Pop $R9
  168. FunctionEnd
  169. Function ZCodeResetUninstallerLog
  170. StrCpy $ZCodeUninstallerDetailsUnavailable ""
  171. ClearErrors
  172. FileOpen $R0 "${ZCODE_UNINSTALLER_LOG_PATH}" w
  173. IfErrors zcodeUninstallerDetailsResetFailed zcodeUninstallerDetailsResetSucceeded
  174. zcodeUninstallerDetailsResetSucceeded:
  175. FileClose $R0
  176. Goto zcodeUninstallerDetailsResetDone
  177. zcodeUninstallerDetailsResetFailed:
  178. ; 外层详情不能读取旧卸载器日志时仍继续安装,文件日志和退出码仍是最终依据。
  179. StrCpy $ZCodeUninstallerDetailsUnavailable "1"
  180. ClearErrors
  181. zcodeUninstallerDetailsResetDone:
  182. FunctionEnd
  183. Function ZCodeShowUninstallerCleanupDetails
  184. Push $R0
  185. Push $R1
  186. Push $R2
  187. StrCmp $ZCodeUninstallerDetailsUnavailable "1" zcodeShowUninstallerDetailsDone
  188. ClearErrors
  189. FileOpen $R0 "${ZCODE_UNINSTALLER_LOG_PATH}" r
  190. IfErrors zcodeShowUninstallerDetailsDone
  191. zcodeShowUninstallerDetailsRead:
  192. ClearErrors
  193. FileRead $R0 $R1
  194. IfErrors zcodeShowUninstallerDetailsClose
  195. StrCmp $R1 "" zcodeShowUninstallerDetailsRead
  196. SetDetailsPrint listonly
  197. DetailPrint "ZCode: cleanup-log $R1"
  198. Goto zcodeShowUninstallerDetailsRead
  199. zcodeShowUninstallerDetailsClose:
  200. FileClose $R0
  201. zcodeShowUninstallerDetailsDone:
  202. Pop $R2
  203. Pop $R1
  204. Pop $R0
  205. FunctionEnd
  206. !macro preInit
  207. Call ZCodeInitializeInstallerLog
  208. !macroend
  209. !macro customInit
  210. IfSilent zcodeInstallerInitSilent zcodeInstallerInitInteractive
  211. zcodeInstallerInitSilent:
  212. !insertmacro ZCodeReportInstallerStage "installer-initialized mode=silent"
  213. Goto zcodeInstallerInitDone
  214. zcodeInstallerInitInteractive:
  215. !insertmacro ZCodeReportInstallerStage "installer-initialized mode=interactive"
  216. zcodeInstallerInitDone:
  217. !macroend
  218. ; 这些宏由打包时的 electron-builder installSection.nsh 补丁按安装顺序调用。
  219. ; 只有阶段 marker 写入详情和日志,解压文件明细由 NSIS 的 File 命令在 listonly 模式输出。
  220. !macro customInstallSectionStarted
  221. !insertmacro ZCodeReportInstallerStage "install-started"
  222. !macroend
  223. !macro customInstallCleanupStarted
  224. Call ZCodeResetUninstallerLog
  225. !insertmacro ZCodeReportInstallerStage "cleanup-started"
  226. !macroend
  227. !macro customInstallCleanupCompleted
  228. !insertmacro ZCodeReportInstallerStage "cleanup-completed"
  229. Call ZCodeShowUninstallerCleanupDetails
  230. !macroend
  231. !macro customInstallExtractStarted
  232. !insertmacro ZCodeReportInstallerStage "extract-started"
  233. !macroend
  234. !macro customInstallExtractCompleted
  235. !insertmacro ZCodeReportInstallerStage "extract-completed"
  236. !macroend
  237. !macro customInstallShortcutsStarted
  238. !insertmacro ZCodeReportInstallerStage "shortcuts-started"
  239. !macroend
  240. !macro customInstallShortcutsCompleted
  241. !insertmacro ZCodeReportInstallerStage "shortcuts-completed"
  242. !macroend
  243. Function ZCodeDetectPreviousUninstallerCapabilities
  244. StrCpy $ZCodePreviousUninstallerSupportsManifest "0"
  245. ; manifest 是卸载器能力标记:存在即表示旧卸载器会按清单选择性删除。
  246. IfFileExists "$INSTDIR\${ZCODE_INSTALL_MANIFEST_NAME}" 0 zcodePreviousUninstallerCapabilityCheckNested
  247. StrCpy $ZCodePreviousUninstallerSupportsManifest "1"
  248. Return
  249. zcodePreviousUninstallerCapabilityCheckNested:
  250. ; assisted installer 的目录页会在后续 instfilesPre 才补上 APP_FILENAME 子目录,提前兼容两种形态。
  251. IfFileExists "$INSTDIR\${APP_FILENAME}\${ZCODE_INSTALL_MANIFEST_NAME}" 0 zcodePreviousUninstallerCapabilityDone
  252. StrCpy $ZCodePreviousUninstallerSupportsManifest "1"
  253. zcodePreviousUninstallerCapabilityDone:
  254. FunctionEnd
  255. !macro customUnInstallCheck
  256. ; handleUninstallResult 会把旧卸载器的退出码放在 $R0;失败时显示清理诊断,
  257. ; 不再复用 appCannotBeClosed(该文案只适用于进程占用)。
  258. ${if} $R0 != 0
  259. ; 修复原因:静默自动更新无人值守,未设置 /SD 的模态框会一直等待用户点击,
  260. ; 使明确的退出码无法返回 electron-updater。静默时自动采用 IDOK,交互时仍显示提示。
  261. SetDetailsPrint listonly
  262. DetailPrint "ZCode: cleanup-failed exit-code=$R0"
  263. Call ZCodeShowUninstallerCleanupDetails
  264. MessageBox MB_OK|MB_ICONSTOP "旧版本清理失败(错误码 $R0)。可能是文件被占用、权限不足或磁盘空间不足。详细日志:${ZCODE_UNINSTALLER_LOG_PATH}" /SD IDOK
  265. SetErrorLevel 2
  266. Quit
  267. ${endif}
  268. !macroend
  269. !macro customUnInstallCheckCurrentUser
  270. ; per-machine 安装切换到 HKCU 旧版本时,electron-builder 会走另一条 hook;
  271. ; 复用同一诊断,避免同一个清理失败因注册表根键不同又退回默认文案。
  272. !insertmacro customUnInstallCheck
  273. !macroend
  274. !endif
  275. !define ZCODE_INSTALL_DIR_BACK_BUTTON_WIDTH 180
  276. !macro customHeader
  277. !ifndef BUILD_UNINSTALLER
  278. ; 修复原因:异步生成的 header 可能先 include 本文件,再注册 UAC 插件目录。
  279. ; 在 customHeader 展开函数,确保插件已注册;preInit 仍调用同一函数和真实 UAC 判据。
  280. Function ZCodeInitializeInstallerLog
  281. Push $R0
  282. Push $R1
  283. Push $R2
  284. StrCpy $ZCodeInstallerLogUnavailable ""
  285. ${If} ${ZCODE_INSTALLER_IS_ELEVATED_INNER}
  286. StrCpy $ZCodeInstallerProcessRole "elevated-inner"
  287. StrCpy $ZCodeInstallerLogPath "${ZCODE_INSTALLER_ELEVATED_LOG_PATH}"
  288. ${Else}
  289. StrCpy $ZCodeInstallerProcessRole "outer"
  290. StrCpy $R0 $CMDLINE
  291. ClearErrors
  292. ${GetOptions} $R0 "/LOG=" $R1
  293. IfErrors zcodeInstallerLogUseDefault
  294. StrCmp $R1 "" zcodeInstallerLogUseDefault
  295. StrCpy $ZCodeInstallerLogPath $R1
  296. Goto zcodeInstallerLogPathReady
  297. zcodeInstallerLogUseDefault:
  298. StrCpy $ZCodeInstallerLogPath "${ZCODE_INSTALLER_DEFAULT_LOG_PATH}"
  299. zcodeInstallerLogPathReady:
  300. ${GetParent} $ZCodeInstallerLogPath $R2
  301. StrCmp $R2 "" zcodeInstallerLogInitialized
  302. CreateDirectory "$R2"
  303. ${EndIf}
  304. zcodeInstallerLogInitialized:
  305. !insertmacro ZCodeReportInstallerStage "installer-process-started role=$ZCodeInstallerProcessRole"
  306. Pop $R2
  307. Pop $R1
  308. Pop $R0
  309. FunctionEnd
  310. ; 修复原因:electron-builder 的 common.nsh 先设置 ShowInstDetails nevershow;
  311. ; hide 在该模板组合下仍可能留下空白列表且没有可展开入口,因此直接常显阶段详情。
  312. ShowInstDetails show
  313. !ifdef allowToChangeInstallationDirectory
  314. ; 修复原因:electron-builder 已在 assistedInstaller.nsh 中用该开关生成安装目录页面,
  315. ; 但 installUtil.nsh 随后还会用它禁止无 --updated 的手动覆盖保留快捷方式,导致旧卸载器
  316. ; 调用 UninstShortcut 注销开始菜单固定项。页面生成后撤掉开关,让自动更新和手动覆盖
  317. ; 在同一安装目录覆盖时都通过 KeepShortcuts 保留同一个 .lnk。
  318. !undef allowToChangeInstallationDirectory
  319. !endif
  320. !endif
  321. !macroend
  322. !ifndef BUILD_UNINSTALLER
  323. ; 修复原因:electron-builder 会先编译卸载器,但快捷方式目标读取只在安装更新流程中调用。
  324. ; 若把函数带入卸载器,NSIS 会产生 6010 未引用告警,并在 /WX 下直接中断 Windows CI。
  325. Function ZCodeReadShortcutTarget
  326. Exch $R9
  327. Push $R1
  328. Push $R2
  329. StrCpy $R2 ""
  330. System::Call 'Kernel32::SetEnvironmentVariableW(w "ZCODE_SHORTCUT_PATH", w "$R9") i.R1'
  331. StrCmp $R1 "0" zcodeReadShortcutTargetDone 0
  332. nsExec::ExecToStack /TIMEOUT=5000 `"$SYSDIR\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -NonInteractive -Command "[Console]::Out.Write(([Activator]::CreateInstance([type]::GetTypeFromProgID('WScript.Shell'))).CreateShortcut([Environment]::GetEnvironmentVariable('ZCODE_SHORTCUT_PATH')).TargetPath)"`
  333. Pop $R1
  334. Pop $R2
  335. StrCmp $R1 "0" zcodeReadShortcutTargetDone 0
  336. StrCpy $R2 ""
  337. zcodeReadShortcutTargetDone:
  338. System::Call 'Kernel32::SetEnvironmentVariableW(w "ZCODE_SHORTCUT_PATH", p 0) i.R1'
  339. StrCpy $R9 "$R2"
  340. Pop $R2
  341. Pop $R1
  342. Exch $R9
  343. FunctionEnd
  344. !endif
  345. !macro ZCodeRepairShortcutIfNeeded SHORTCUT_PATH LABEL_PREFIX
  346. ${if} ${FileExists} "${SHORTCUT_PATH}"
  347. Push "${SHORTCUT_PATH}"
  348. Call ZCodeReadShortcutTarget
  349. Pop $R0
  350. StrCmp $R0 "$appExe" ${LABEL_PREFIX}Done 0
  351. ; 修复原因:历史版本可能留下指向已移动 exe 的 .lnk,但无条件覆盖正确快捷方式会让
  352. ; 部分 Windows 11 丢失“所有应用”索引或用户固定关系,因此只修复目标不一致的项。
  353. ClearErrors
  354. CreateShortCut "${SHORTCUT_PATH}" "$appExe" "" "$appExe" 0 "" "" "${APP_DESCRIPTION}"
  355. IfErrors ${LABEL_PREFIX}Failed ${LABEL_PREFIX}Succeeded
  356. ${LABEL_PREFIX}Failed:
  357. DetailPrint "Unable to repair shortcut: ${SHORTCUT_PATH}"
  358. ClearErrors
  359. Goto ${LABEL_PREFIX}Done
  360. ${LABEL_PREFIX}Succeeded:
  361. WinShell::SetLnkAUMI "${SHORTCUT_PATH}" "${APP_ID}"
  362. ; 修复后的 .lnk 必须在最后一次写入后通知 Shell,避免开始菜单继续使用旧索引。
  363. System::Call 'Shell32::SHChangeNotify(i 0x00002000, i 0x0005, w "${SHORTCUT_PATH}", p 0)'
  364. ${LABEL_PREFIX}Done:
  365. ${endIf}
  366. !macroend
  367. !macro customInstall
  368. !ifndef BUILD_UNINSTALLER
  369. !insertmacro ZCodeReportInstallerStage "install-finalization-started"
  370. !endif
  371. ${if} ${isUpdated}
  372. ${orIf} $keepShortcuts == "true"
  373. !ifndef DO_NOT_CREATE_START_MENU_SHORTCUT
  374. !insertmacro ZCodeRepairShortcutIfNeeded "$newStartMenuLink" zcodeStartMenuShortcutRepair
  375. !endif
  376. !ifndef DO_NOT_CREATE_DESKTOP_SHORTCUT
  377. !insertmacro ZCodeRepairShortcutIfNeeded "$newDesktopLink" zcodeDesktopShortcutRepair
  378. !endif
  379. ${endIf}
  380. ; 修复原因:手动覆盖没有 --updated,继承旧快捷方式时仍需检查目标;首次安装没有旧项,
  381. ; 不应额外启动 PowerShell。用户已删除的快捷方式也不会重建。
  382. ; assisted installer 完成页始终直接运行本次安装落盘的 exe。
  383. StrCpy $launchLink "$appExe"
  384. !ifndef BUILD_UNINSTALLER
  385. !insertmacro ZCodeReportInstallerStage "install-completed"
  386. !endif
  387. !macroend
  388. !macro customPageAfterChangeDir
  389. Function ZCodeResizeInstallDirBackButton
  390. GetDlgItem $1 $HWNDPARENT 3
  391. StrCmp $1 0 zcodeResizeInstallDirBackButtonDone 0
  392. System::Call "*(i 0, i 0, i 0, i 0) p.r2"
  393. StrCmp $2 0 zcodeResizeInstallDirBackButtonDone 0
  394. System::Call "user32::GetWindowRect(p r1, p r2)"
  395. System::Call "user32::MapWindowPoints(p 0, p $HWNDPARENT, p r2, i 2)"
  396. System::Call "*$2(i.r3,i.r4,i.r5,i.r6)"
  397. System::Free $2
  398. IntOp $7 $5 - $3
  399. IntOp $8 $6 - $4
  400. IntCmp $7 ${ZCODE_INSTALL_DIR_BACK_BUTTON_WIDTH} zcodeResizeInstallDirBackButtonDone zcodeResizeInstallDirBackButtonResize zcodeResizeInstallDirBackButtonDone
  401. zcodeResizeInstallDirBackButtonResize:
  402. ; 修复原因:阻断页把“上一步”改成中文动作文案,NSIS 默认按钮宽度可能裁掉文字。
  403. ; 保持右边缘不动向左扩宽,避免和右侧“安装/取消”按钮重叠。
  404. IntOp $3 $5 - ${ZCODE_INSTALL_DIR_BACK_BUTTON_WIDTH}
  405. System::Call "user32::MoveWindow(p r1, i r3, i r4, i ${ZCODE_INSTALL_DIR_BACK_BUTTON_WIDTH}, i r8, i 1)"
  406. zcodeResizeInstallDirBackButtonDone:
  407. FunctionEnd
  408. Function ZCodeFindNestedDataDir
  409. Exch $R9
  410. Push $0
  411. Push $1
  412. StrCpy $R2 ""
  413. IfFileExists "$R9\.zcode\*.*" 0 +2
  414. StrCpy $R2 "$R9\.zcode"
  415. StrCmp $R2 "" 0 zcodeFindNestedDataDirDone
  416. IfFileExists "$R9\.zcode" 0 zcodeFindNestedDataDirListChildren
  417. StrCpy $R2 "$R9\.zcode"
  418. StrCmp $R2 "" 0 zcodeFindNestedDataDirDone
  419. zcodeFindNestedDataDirListChildren:
  420. FindFirst $0 $1 "$R9\*"
  421. IfErrors zcodeFindNestedDataDirDone
  422. zcodeFindNestedDataDirNext:
  423. StrCmp $1 "" zcodeFindNestedDataDirClose
  424. StrCmp $1 "." zcodeFindNestedDataDirContinue
  425. StrCmp $1 ".." zcodeFindNestedDataDirContinue
  426. IfFileExists "$R9\$1\*.*" 0 zcodeFindNestedDataDirContinue
  427. Push "$R9\$1"
  428. Call ZCodeFindNestedDataDir
  429. StrCmp $R2 "" zcodeFindNestedDataDirContinue zcodeFindNestedDataDirClose
  430. zcodeFindNestedDataDirContinue:
  431. FindNext $0 $1
  432. IfErrors zcodeFindNestedDataDirClose
  433. Goto zcodeFindNestedDataDirNext
  434. zcodeFindNestedDataDirClose:
  435. FindClose $0
  436. zcodeFindNestedDataDirDone:
  437. Pop $1
  438. Pop $0
  439. Pop $R9
  440. FunctionEnd
  441. Function ZCodeBlockInstallDirContainsData
  442. Call ZCodeDetectPreviousUninstallerCapabilities
  443. StrCmp $ZCodePreviousUninstallerSupportsManifest "1" zcodeInstallDirDataBlockSkip
  444. ; Bugfix: 用户可能把数据存储目录放进安装目录,Windows 更新覆盖安装目录时会清掉 .zcode。
  445. ; assisted installer 会把不含应用名的选择目录补成 "$INSTDIR\${APP_FILENAME}",所以这里按相同规则计算最终安装目录。
  446. ${StrContains} $R1 "${APP_FILENAME}" "$INSTDIR"
  447. StrCmp $R1 "" 0 zcodeInstallDirDataBlockUseSelectedDir
  448. StrCpy $R0 "$INSTDIR\${APP_FILENAME}"
  449. Goto zcodeInstallDirDataBlockCheckDir
  450. zcodeInstallDirDataBlockUseSelectedDir:
  451. StrCpy $R0 "$INSTDIR"
  452. zcodeInstallDirDataBlockCheckDir:
  453. ; 修复原因:旧阻断只检查最终安装目录直属的 .zcode,漏掉 data\.zcode 等子目录数据。
  454. ; 安装器覆盖安装时会管理整个安装目录树,递归命中任意 .zcode 都必须阻断。
  455. Push "$R0"
  456. Call ZCodeFindNestedDataDir
  457. StrCmp $R2 "" zcodeInstallDirDataBlockSkip zcodeInstallDirDataBlockFound
  458. zcodeInstallDirDataBlockFound:
  459. IfSilent zcodeInstallDirDataBlockSilent
  460. !insertmacro MUI_HEADER_TEXT "需要修改安装目录" "当前安装目录或其子目录包含 ZCode 数据目录"
  461. nsDialogs::Create 1018
  462. Pop $0
  463. StrCmp $0 error zcodeInstallDirDataBlockDialogFailed 0
  464. ${NSD_CreateLabel} 0u 0u 300u 44u "检测到该安装目录或其子目录中存在 .zcode 数据目录:$\r$\n$R2"
  465. Pop $1
  466. ${NSD_CreateLabel} 0u 54u 300u 70u "为避免历史会话和配置被安装器清理,请返回上一步选择其他安装目录。$\r$\n$\r$\n当前目录不能继续安装。"
  467. Pop $1
  468. GetDlgItem $1 $HWNDPARENT 1
  469. EnableWindow $1 0
  470. GetDlgItem $1 $HWNDPARENT 3
  471. EnableWindow $1 1
  472. SendMessage $1 ${WM_SETTEXT} 0 "STR:重选目录"
  473. Call ZCodeResizeInstallDirBackButton
  474. nsDialogs::Show
  475. Return
  476. zcodeInstallDirDataBlockDialogFailed:
  477. MessageBox MB_OK|MB_ICONSTOP "检测到安装目录或其子目录中存在 .zcode 数据目录,安装已停止。请重新运行安装器并选择其他安装目录。"
  478. SetErrorLevel 1
  479. Quit
  480. zcodeInstallDirDataBlockSilent:
  481. SetErrorLevel 1
  482. Quit
  483. zcodeInstallDirDataBlockSkip:
  484. Abort
  485. FunctionEnd
  486. Function ZCodeBlockInstallDirContainsDataLeave
  487. ; 修复原因:阻断页的下一步按钮已禁用,但自动化或系统快捷键仍可能触发下一页。
  488. ; leave 回调只处理继续前进的路径,这里强制留在当前页,确保用户只能返回修改安装目录。
  489. Abort
  490. FunctionEnd
  491. Page custom ZCodeBlockInstallDirContainsData ZCodeBlockInstallDirContainsDataLeave
  492. !macroend