installer.nsh 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. ; 清单是当前打开的元数据,自引用条目不能当作普通载荷删除。
  89. StrCmp $R1 "${ZCODE_INSTALL_MANIFEST_NAME}" zcodeManifestRead
  90. GetFullPathName $R2 "$INSTDIR\$R1"
  91. StrCmp $R2 "$INSTDIR\$R1" 0 zcodeManifestRead
  92. ; 当前版本卸载器与外层安装器是两个进程;逐项记录到卸载器日志,便于核对真正尝试删除的文件。
  93. !ifdef BUILD_UNINSTALLER
  94. !insertmacro ZCodeReportUninstallerStage "cleanup-file path=$R1"
  95. !endif
  96. ClearErrors
  97. Delete "$INSTDIR\$R1"
  98. IfErrors zcodeManifestDeleteFailed
  99. Goto zcodeManifestRead
  100. zcodeManifestDeleteFailed:
  101. FileClose $R0
  102. !ifdef BUILD_UNINSTALLER
  103. !insertmacro ZCodeReportUninstallerStage "cleanup-failed reason=permission-or-disk-space"
  104. !endif
  105. Abort "无法删除旧版本文件:$INSTDIR\$R1"
  106. zcodeManifestClose:
  107. FileClose $R0
  108. Goto zcodeManifestDone
  109. zcodeManifestMissing:
  110. ; 首次从旧版本升级时没有清单,不能猜测所有权并删除用户文件。
  111. !ifdef BUILD_UNINSTALLER
  112. !insertmacro ZCodeReportUninstallerStage "cleanup-skipped reason=manifest-missing action=preserve"
  113. !endif
  114. ClearErrors
  115. zcodeManifestDone:
  116. !ifdef BUILD_UNINSTALLER
  117. !insertmacro customRemoveFilesDiagnosticsComplete
  118. !endif
  119. ${else}
  120. ; 普通卸载仍保持 electron-builder 的全量删除语义;ownership 清单只约束覆盖更新。
  121. SetOutPath $TEMP
  122. RMDir /r $INSTDIR
  123. ${endIf}
  124. !macroend
  125. !ifndef BUILD_UNINSTALLER
  126. Var ZCodeInstallerLogPath
  127. Var ZCodeInstallerLogUnavailable
  128. Var ZCodeInstallerProcessRole
  129. Var ZCodeUninstallerDetailsUnavailable
  130. Var ZCodePreviousUninstallerSupportsManifest
  131. ; 详情面板和文件日志共用同一条阶段事件,避免静默安装丢失关键上下文。
  132. !macro ZCodeReportInstallerStage MESSAGE
  133. SetDetailsPrint listonly
  134. DetailPrint "ZCode: ${MESSAGE}"
  135. Push "${MESSAGE}"
  136. Call ZCodeWriteInstallerLog
  137. !macroend
  138. Function ZCodeWriteInstallerLog
  139. Exch $R9
  140. Push $R0
  141. Push $R1
  142. Push $R2
  143. StrCmp $ZCodeInstallerLogPath "" zcodeInstallerLogDone
  144. StrCmp $ZCodeInstallerLogUnavailable "1" zcodeInstallerLogDone
  145. StrCpy $R2 0
  146. zcodeInstallerLogOpen:
  147. ClearErrors
  148. FileOpen $R1 $ZCodeInstallerLogPath a
  149. IfErrors zcodeInstallerLogRetry zcodeInstallerLogWrite
  150. zcodeInstallerLogRetry:
  151. IntOp $R2 $R2 + 1
  152. IntCmp $R2 3 zcodeInstallerLogFailed zcodeInstallerLogWait zcodeInstallerLogFailed
  153. zcodeInstallerLogWait:
  154. Sleep 50
  155. Goto zcodeInstallerLogOpen
  156. zcodeInstallerLogWrite:
  157. System::Call "kernel32::GetCurrentProcessId() i.R0"
  158. FileSeek $R1 0 END
  159. FileWrite $R1 "[pid=$R0] $R9$\r$\n"
  160. FileClose $R1
  161. Goto zcodeInstallerLogDone
  162. zcodeInstallerLogFailed:
  163. StrCpy $ZCodeInstallerLogUnavailable "1"
  164. ClearErrors
  165. zcodeInstallerLogDone:
  166. Pop $R2
  167. Pop $R1
  168. Pop $R0
  169. Pop $R9
  170. FunctionEnd
  171. Function ZCodeResetUninstallerLog
  172. StrCpy $ZCodeUninstallerDetailsUnavailable ""
  173. ClearErrors
  174. FileOpen $R0 "${ZCODE_UNINSTALLER_LOG_PATH}" w
  175. IfErrors zcodeUninstallerDetailsResetFailed zcodeUninstallerDetailsResetSucceeded
  176. zcodeUninstallerDetailsResetSucceeded:
  177. FileClose $R0
  178. Goto zcodeUninstallerDetailsResetDone
  179. zcodeUninstallerDetailsResetFailed:
  180. ; 外层详情不能读取旧卸载器日志时仍继续安装,文件日志和退出码仍是最终依据。
  181. StrCpy $ZCodeUninstallerDetailsUnavailable "1"
  182. ClearErrors
  183. zcodeUninstallerDetailsResetDone:
  184. FunctionEnd
  185. Function ZCodeShowUninstallerCleanupDetails
  186. Push $R0
  187. Push $R1
  188. Push $R2
  189. StrCmp $ZCodeUninstallerDetailsUnavailable "1" zcodeShowUninstallerDetailsDone
  190. ClearErrors
  191. FileOpen $R0 "${ZCODE_UNINSTALLER_LOG_PATH}" r
  192. IfErrors zcodeShowUninstallerDetailsDone
  193. zcodeShowUninstallerDetailsRead:
  194. ClearErrors
  195. FileRead $R0 $R1
  196. IfErrors zcodeShowUninstallerDetailsClose
  197. StrCmp $R1 "" zcodeShowUninstallerDetailsRead
  198. SetDetailsPrint listonly
  199. DetailPrint "ZCode: cleanup-log $R1"
  200. Goto zcodeShowUninstallerDetailsRead
  201. zcodeShowUninstallerDetailsClose:
  202. FileClose $R0
  203. zcodeShowUninstallerDetailsDone:
  204. Pop $R2
  205. Pop $R1
  206. Pop $R0
  207. FunctionEnd
  208. !macro preInit
  209. Call ZCodeInitializeInstallerLog
  210. !macroend
  211. !macro customInit
  212. IfSilent zcodeInstallerInitSilent zcodeInstallerInitInteractive
  213. zcodeInstallerInitSilent:
  214. !insertmacro ZCodeReportInstallerStage "installer-initialized mode=silent"
  215. Goto zcodeInstallerInitDone
  216. zcodeInstallerInitInteractive:
  217. !insertmacro ZCodeReportInstallerStage "installer-initialized mode=interactive"
  218. zcodeInstallerInitDone:
  219. !macroend
  220. ; 这些宏由打包时的 electron-builder installSection.nsh 补丁按安装顺序调用。
  221. ; 只有阶段 marker 写入详情和日志,解压文件明细由 NSIS 的 File 命令在 listonly 模式输出。
  222. !macro customInstallSectionStarted
  223. !insertmacro ZCodeReportInstallerStage "install-started"
  224. !macroend
  225. !macro customInstallCleanupStarted
  226. Call ZCodeResetUninstallerLog
  227. !insertmacro ZCodeReportInstallerStage "cleanup-started"
  228. !macroend
  229. !macro customInstallCleanupCompleted
  230. !insertmacro ZCodeReportInstallerStage "cleanup-completed"
  231. Call ZCodeShowUninstallerCleanupDetails
  232. !macroend
  233. !macro customInstallExtractStarted
  234. !insertmacro ZCodeReportInstallerStage "extract-started"
  235. !macroend
  236. !macro customInstallExtractCompleted
  237. !insertmacro ZCodeReportInstallerStage "extract-completed"
  238. !macroend
  239. !macro customInstallShortcutsStarted
  240. !insertmacro ZCodeReportInstallerStage "shortcuts-started"
  241. !macroend
  242. !macro customInstallShortcutsCompleted
  243. !insertmacro ZCodeReportInstallerStage "shortcuts-completed"
  244. !macroend
  245. Function ZCodeDetectPreviousUninstallerCapabilities
  246. StrCpy $ZCodePreviousUninstallerSupportsManifest "0"
  247. ; manifest 是卸载器能力标记:存在即表示旧卸载器会按清单选择性删除。
  248. IfFileExists "$INSTDIR\${ZCODE_INSTALL_MANIFEST_NAME}" 0 zcodePreviousUninstallerCapabilityCheckNested
  249. StrCpy $ZCodePreviousUninstallerSupportsManifest "1"
  250. Return
  251. zcodePreviousUninstallerCapabilityCheckNested:
  252. ; assisted installer 的目录页会在后续 instfilesPre 才补上 APP_FILENAME 子目录,提前兼容两种形态。
  253. IfFileExists "$INSTDIR\${APP_FILENAME}\${ZCODE_INSTALL_MANIFEST_NAME}" 0 zcodePreviousUninstallerCapabilityDone
  254. StrCpy $ZCodePreviousUninstallerSupportsManifest "1"
  255. zcodePreviousUninstallerCapabilityDone:
  256. FunctionEnd
  257. !macro ZCodeCleanupErrorMessage CHINESE ENGLISH
  258. ${If} $LANGUAGE == 2052
  259. MessageBox MB_OK|MB_ICONSTOP "${CHINESE}" /SD IDOK
  260. ${Else}
  261. MessageBox MB_OK|MB_ICONSTOP "${ENGLISH}" /SD IDOK
  262. ${EndIf}
  263. !macroend
  264. Var ZCodeCleanupSource
  265. Var ZCodeCleanupBackup
  266. Var ZCodeCleanupMode
  267. Var ZCodeCleanupFailed
  268. Var ZCodeCleanupManifest
  269. Var ZCodeCleanupManifestHasSelf
  270. ; 旧卸载器已删除的文件不会自行恢复。新安装器在调用旧代码前保存受管文件,
  271. ; 清理失败只补回缺失项,避免覆盖仍被占用的原文件;备份失败时不进入旧卸载器。
  272. ; 自定义 include 先于 common.nsh;延迟展开,确保卸载器名称已定义,避免正式打包 warning 6000。
  273. !macro ZCodeDefineCleanupSnapshot
  274. Function ZCodeCleanupSnapshot
  275. Push $R0
  276. Push $R1
  277. Push $R2
  278. Push $R3
  279. Push $R4
  280. Push $R5
  281. StrCpy $R5 ""
  282. StrCpy $ZCodeCleanupFailed "0"
  283. StrCmp $ZCodeCleanupMode "restore" zcodeSnapshotRead
  284. StrCpy $ZCodeCleanupBackup ""
  285. StrCpy $ZCodeCleanupManifestHasSelf "0"
  286. IfFileExists "$ZCodeCleanupSource\${ZCODE_INSTALL_MANIFEST_NAME}" 0 zcodeSnapshotDone
  287. ${GetParent} $ZCodeCleanupSource $R0
  288. ClearErrors
  289. GetTempFileName $ZCodeCleanupBackup $R0
  290. IfErrors zcodeSnapshotFailed
  291. Delete "$ZCodeCleanupBackup"
  292. CreateDirectory "$ZCodeCleanupBackup"
  293. IfErrors zcodeSnapshotFailed
  294. System::Call 'kernel32::CopyFileW(w "$ZCodeCleanupSource\${ZCODE_INSTALL_MANIFEST_NAME}", w "$ZCodeCleanupBackup\${ZCODE_INSTALL_MANIFEST_NAME}", i 1) i.R3 ?e'
  295. Pop $R4
  296. StrCmp $R3 "0" zcodeSnapshotFailed
  297. GetTempFileName $ZCodeCleanupManifest "$ZCodeCleanupBackup"
  298. IfErrors zcodeSnapshotFailed
  299. FileOpen $R5 "$ZCodeCleanupManifest" w
  300. IfErrors zcodeSnapshotFailed
  301. zcodeSnapshotRead:
  302. StrCmp $ZCodeCleanupBackup "" zcodeSnapshotDone
  303. ClearErrors
  304. FileOpen $R0 "$ZCodeCleanupBackup\${ZCODE_INSTALL_MANIFEST_NAME}" r
  305. IfErrors zcodeSnapshotFailed
  306. zcodeSnapshotNext:
  307. ClearErrors
  308. FileRead $R0 $R1
  309. IfErrors zcodeSnapshotClose
  310. StrCpy $R1 $R1 -2
  311. StrCmp $R1 "" zcodeSnapshotNext
  312. StrCmp $ZCodeCleanupMode "restore" zcodeSnapshotValidateEntry
  313. StrCmp $R1 "${ZCODE_INSTALL_MANIFEST_NAME}" zcodeSnapshotSelfEntry
  314. FileWrite $R5 "$R1$\r$\n"
  315. IfErrors zcodeSnapshotFailedClose
  316. Goto zcodeSnapshotValidateEntry
  317. zcodeSnapshotSelfEntry:
  318. StrCpy $ZCodeCleanupManifestHasSelf "1"
  319. Goto zcodeSnapshotNext
  320. zcodeSnapshotFailedClose:
  321. StrCpy $ZCodeCleanupFailed "1"
  322. Goto zcodeSnapshotClose
  323. zcodeSnapshotValidateEntry:
  324. ; 只接受清单中规范的相对路径,拒绝越界、通配符和卸载器自身。
  325. StrCpy $R2 $R1 1
  326. StrCmp $R2 "\" zcodeSnapshotNext
  327. StrCmp $R2 "/" zcodeSnapshotNext
  328. StrCpy $R2 $R1 2
  329. StrCmp $R2 ".." zcodeSnapshotNext
  330. StrCmp $R1 "${UNINSTALL_FILENAME}" zcodeSnapshotNext
  331. ; 清单已在循环前单独备份;自引用时再次排他复制会因目标存在而中止更新。
  332. StrCmp $R1 "${ZCODE_INSTALL_MANIFEST_NAME}" zcodeSnapshotNext
  333. ; Windows NSIS 对已删除的目标返回空路径;恢复时校验仍存在的备份,不能跳过待补回文件。
  334. StrCpy $R3 "$ZCodeCleanupSource"
  335. StrCmp $ZCodeCleanupMode "restore" 0 +2
  336. StrCpy $R3 "$ZCodeCleanupBackup"
  337. GetFullPathName $R2 "$R3\$R1"
  338. StrCmp $R2 "$R3\$R1" 0 zcodeSnapshotNext
  339. StrCpy $R2 0
  340. zcodeSnapshotValidate:
  341. StrCpy $R3 $R1 1 $R2
  342. StrCmp $R3 "" zcodeSnapshotCopy
  343. StrCmp $R3 "*" zcodeSnapshotInvalidPath
  344. StrCmp $R3 "?" zcodeSnapshotInvalidPath
  345. StrCmp $R3 ":" zcodeSnapshotInvalidPath
  346. IntOp $R2 $R2 + 1
  347. Goto zcodeSnapshotValidate
  348. zcodeSnapshotInvalidPath:
  349. StrCpy $ZCodeCleanupFailed "1"
  350. Goto zcodeSnapshotClose
  351. zcodeSnapshotCopy:
  352. StrCmp $ZCodeCleanupMode "restore" zcodeSnapshotRestore
  353. IfFileExists "$ZCodeCleanupSource\$R1" 0 zcodeSnapshotNext
  354. ${GetParent} "$ZCodeCleanupBackup\$R1" $R2
  355. CreateDirectory "$R2"
  356. System::Call 'kernel32::CopyFileW(w "$ZCodeCleanupSource\$R1", w "$ZCodeCleanupBackup\$R1", i 1) i.R3 ?e'
  357. Pop $R4
  358. Goto zcodeSnapshotCopyResult
  359. zcodeSnapshotRestore:
  360. IfFileExists "$ZCodeCleanupBackup\$R1" 0 zcodeSnapshotNext
  361. IfFileExists "$ZCodeCleanupSource\$R1" 0 zcodeSnapshotRestoreMissing
  362. ; 同名目录不能充当恢复后的文件,否则会误报已恢复主程序。
  363. System::Call 'kernel32::GetFileAttributesW(w "$ZCodeCleanupSource\$R1") i.R3'
  364. IntOp $R3 $R3 & 0x10
  365. StrCmp $R3 0 zcodeSnapshotNext
  366. StrCpy $ZCodeCleanupFailed "1"
  367. Goto zcodeSnapshotNext
  368. zcodeSnapshotRestoreMissing:
  369. ${GetParent} "$ZCodeCleanupSource\$R1" $R2
  370. CreateDirectory "$R2"
  371. System::Call 'kernel32::CopyFileW(w "$ZCodeCleanupBackup\$R1", w "$ZCodeCleanupSource\$R1", i 1) i.R3 ?e'
  372. Pop $R4
  373. zcodeSnapshotCopyResult:
  374. ; System 的 ?e 在 CopyFileW 返回时保存 Win32 错误,避免插件后续调用覆盖 GetLastError。
  375. StrCmp $R3 "0" 0 zcodeSnapshotNext
  376. !insertmacro ZCodeReportInstallerStage "cleanup-$ZCodeCleanupMode-file-failed path=$R1 win32-error=$R4"
  377. StrCpy $ZCodeCleanupFailed "1"
  378. StrCmp $ZCodeCleanupMode "restore" zcodeSnapshotNext
  379. zcodeSnapshotClose:
  380. FileClose $R0
  381. StrCmp $ZCodeCleanupMode "restore" zcodeSnapshotRestoreManifest
  382. FileClose $R5
  383. StrCpy $R5 ""
  384. StrCmp $ZCodeCleanupFailed "1" zcodeSnapshotDone
  385. StrCmp $ZCodeCleanupManifestHasSelf "1" 0 zcodeSnapshotDone
  386. ; 旧卸载器也会删除打开中的清单。完整备份后,仅原子移除自引用项供旧代码读取。
  387. System::Call 'kernel32::MoveFileExW(w "$ZCodeCleanupManifest", w "$ZCodeCleanupSource\${ZCODE_INSTALL_MANIFEST_NAME}", i 9) i.R3 ?e'
  388. Pop $R4
  389. StrCmp $R3 "0" zcodeSnapshotFailed zcodeSnapshotDone
  390. zcodeSnapshotRestoreManifest:
  391. ; 旧清单可能仍存在但已去掉自引用;失败回滚须恢复原始字节,而非仅补不存在的文件。
  392. System::Call 'kernel32::CopyFileW(w "$ZCodeCleanupBackup\${ZCODE_INSTALL_MANIFEST_NAME}", w "$ZCodeCleanupSource\${ZCODE_INSTALL_MANIFEST_NAME}", i 0) i.R3 ?e'
  393. Pop $R4
  394. StrCmp $R3 "0" zcodeSnapshotFailed zcodeSnapshotDone
  395. zcodeSnapshotFailed:
  396. StrCpy $ZCodeCleanupFailed "1"
  397. zcodeSnapshotDone:
  398. StrCmp $R5 "" +2
  399. FileClose $R5
  400. !insertmacro ZCodeReportInstallerStage "cleanup-$ZCodeCleanupMode-completed failed=$ZCodeCleanupFailed backup=$ZCodeCleanupBackup"
  401. Pop $R5
  402. Pop $R4
  403. Pop $R3
  404. Pop $R2
  405. Pop $R1
  406. Pop $R0
  407. FunctionEnd
  408. !macroend
  409. !macro customOldUninstallPrepare
  410. StrCpy $ZCodeCleanupSource "$installationDir"
  411. StrCpy $ZCodeCleanupMode "backup"
  412. Call ZCodeCleanupSnapshot
  413. ${If} $ZCodeCleanupFailed == "1"
  414. ; 此时尚未运行旧卸载器,原文件仍完整;失败的半份备份可以安全清理。
  415. RMDir /r "$ZCodeCleanupBackup"
  416. !insertmacro ZCodeCleanupErrorMessage "无法备份旧版本文件,安装已停止,旧版本未改动。请检查权限与剩余空间。" "Cannot back up the previous version. Setup stopped without changing it. Check permissions and free disk space."
  417. SetErrorLevel 2
  418. Quit
  419. ${EndIf}
  420. !macroend
  421. !macro customOldUninstallRetryFailed
  422. ; 上游把任意卸载非零都显示成应用未关闭;实际进程检查仍由 CHECK_APP_RUNNING 负责。
  423. !insertmacro ZCodeReportInstallerStage "cleanup-retries-exhausted exit-code=$R0"
  424. ${If} $LANGUAGE == 2052
  425. MessageBox MB_RETRYCANCEL|MB_ICONEXCLAMATION "旧版本清理失败(错误码 $R0)。请排除文件占用或权限问题后重试,或取消以恢复旧版本。日志:$ZCodeInstallerLogPath" /SD IDCANCEL IDRETRY OneMoreAttempt
  426. ${Else}
  427. MessageBox MB_RETRYCANCEL|MB_ICONEXCLAMATION "Previous version cleanup failed (exit code $R0). Resolve file locks or permissions and retry, or cancel to restore the previous version. Log: $ZCodeInstallerLogPath" /SD IDCANCEL IDRETRY OneMoreAttempt
  428. ${EndIf}
  429. !macroend
  430. Function ZCodeFinishOldUninstall
  431. ${If} $R0 != 0
  432. StrCpy $ZCodeCleanupMode "restore"
  433. Call ZCodeCleanupSnapshot
  434. ${If} $ZCodeCleanupFailed == "1"
  435. !insertmacro ZCodeCleanupErrorMessage "旧版本文件恢复失败,备份已保留:$ZCodeCleanupBackup。详细日志:$ZCodeInstallerLogPath" "Cannot restore the previous version. Backup retained: $ZCodeCleanupBackup. Log: $ZCodeInstallerLogPath"
  436. Return
  437. ${EndIf}
  438. ${EndIf}
  439. StrCmp $ZCodeCleanupBackup "" zcodeFinishOldUninstallDone
  440. RMDir /r "$ZCodeCleanupBackup"
  441. StrCpy $ZCodeCleanupBackup ""
  442. zcodeFinishOldUninstallDone:
  443. FunctionEnd
  444. !macro customUnInstallCheck
  445. Call ZCodeFinishOldUninstall
  446. ; handleUninstallResult 会把旧卸载器的退出码放在 $R0;失败时显示清理诊断,
  447. ; 不再复用 appCannotBeClosed(该文案只适用于进程占用)。
  448. ${if} $R0 != 0
  449. ; 修复原因:静默自动更新无人值守,未设置 /SD 的模态框会一直等待用户点击,
  450. ; 使明确的退出码无法返回 electron-updater。静默时自动采用 IDOK,交互时仍显示提示。
  451. SetDetailsPrint listonly
  452. DetailPrint "ZCode: cleanup-failed exit-code=$R0"
  453. Call ZCodeShowUninstallerCleanupDetails
  454. ; 最终错误也沿用安装器语言,避免英文 Retry/Cancel 之后固定切回中文。
  455. !insertmacro ZCodeCleanupErrorMessage "旧版本清理失败(错误码 $R0)。可能是文件被占用、权限不足或磁盘空间不足。详细日志:${ZCODE_UNINSTALLER_LOG_PATH}" "Previous version cleanup failed (exit code $R0). Files may be in use, permissions may be insufficient, or disk space may be low. Log: ${ZCODE_UNINSTALLER_LOG_PATH}"
  456. SetErrorLevel 2
  457. Quit
  458. ${endif}
  459. !macroend
  460. !macro customUnInstallCheckCurrentUser
  461. ; per-machine 安装切换到 HKCU 旧版本时,electron-builder 会走另一条 hook;
  462. ; 复用同一诊断,避免同一个清理失败因注册表根键不同又退回默认文案。
  463. !insertmacro customUnInstallCheck
  464. !macroend
  465. !endif
  466. !define ZCODE_INSTALL_DIR_BACK_BUTTON_WIDTH 180
  467. !macro customHeader
  468. !ifndef BUILD_UNINSTALLER
  469. !insertmacro ZCodeDefineCleanupSnapshot
  470. ; 修复原因:异步生成的 header 可能先 include 本文件,再注册 UAC 插件目录。
  471. ; 在 customHeader 展开函数,确保插件已注册;preInit 仍调用同一函数和真实 UAC 判据。
  472. Function ZCodeInitializeInstallerLog
  473. Push $R0
  474. Push $R1
  475. Push $R2
  476. StrCpy $ZCodeInstallerLogUnavailable ""
  477. ${If} ${ZCODE_INSTALLER_IS_ELEVATED_INNER}
  478. StrCpy $ZCodeInstallerProcessRole "elevated-inner"
  479. StrCpy $ZCodeInstallerLogPath "${ZCODE_INSTALLER_ELEVATED_LOG_PATH}"
  480. ${Else}
  481. StrCpy $ZCodeInstallerProcessRole "outer"
  482. StrCpy $R0 $CMDLINE
  483. ClearErrors
  484. ${GetOptions} $R0 "/LOG=" $R1
  485. IfErrors zcodeInstallerLogUseDefault
  486. StrCmp $R1 "" zcodeInstallerLogUseDefault
  487. StrCpy $ZCodeInstallerLogPath $R1
  488. Goto zcodeInstallerLogPathReady
  489. zcodeInstallerLogUseDefault:
  490. StrCpy $ZCodeInstallerLogPath "${ZCODE_INSTALLER_DEFAULT_LOG_PATH}"
  491. zcodeInstallerLogPathReady:
  492. ${GetParent} $ZCodeInstallerLogPath $R2
  493. StrCmp $R2 "" zcodeInstallerLogInitialized
  494. CreateDirectory "$R2"
  495. ${EndIf}
  496. zcodeInstallerLogInitialized:
  497. !insertmacro ZCodeReportInstallerStage "installer-process-started role=$ZCodeInstallerProcessRole"
  498. Pop $R2
  499. Pop $R1
  500. Pop $R0
  501. FunctionEnd
  502. ; 修复原因:electron-builder 的 common.nsh 先设置 ShowInstDetails nevershow;
  503. ; hide 在该模板组合下仍可能留下空白列表且没有可展开入口,因此直接常显阶段详情。
  504. ShowInstDetails show
  505. !ifdef allowToChangeInstallationDirectory
  506. ; 修复原因:electron-builder 已在 assistedInstaller.nsh 中用该开关生成安装目录页面,
  507. ; 但 installUtil.nsh 随后还会用它禁止无 --updated 的手动覆盖保留快捷方式,导致旧卸载器
  508. ; 调用 UninstShortcut 注销开始菜单固定项。页面生成后撤掉开关,让自动更新和手动覆盖
  509. ; 在同一安装目录覆盖时都通过 KeepShortcuts 保留同一个 .lnk。
  510. !undef allowToChangeInstallationDirectory
  511. !endif
  512. !endif
  513. !macroend
  514. !ifndef BUILD_UNINSTALLER
  515. ; 修复原因:electron-builder 会先编译卸载器,但快捷方式目标读取只在安装更新流程中调用。
  516. ; 若把函数带入卸载器,NSIS 会产生 6010 未引用告警,并在 /WX 下直接中断 Windows CI。
  517. Function ZCodeReadShortcutTarget
  518. Exch $R9
  519. Push $R1
  520. Push $R2
  521. StrCpy $R2 ""
  522. System::Call 'Kernel32::SetEnvironmentVariableW(w "ZCODE_SHORTCUT_PATH", w "$R9") i.R1'
  523. StrCmp $R1 "0" zcodeReadShortcutTargetDone 0
  524. 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)"`
  525. Pop $R1
  526. Pop $R2
  527. StrCmp $R1 "0" zcodeReadShortcutTargetDone 0
  528. StrCpy $R2 ""
  529. zcodeReadShortcutTargetDone:
  530. System::Call 'Kernel32::SetEnvironmentVariableW(w "ZCODE_SHORTCUT_PATH", p 0) i.R1'
  531. StrCpy $R9 "$R2"
  532. Pop $R2
  533. Pop $R1
  534. Exch $R9
  535. FunctionEnd
  536. !endif
  537. !macro ZCodeRepairShortcutIfNeeded SHORTCUT_PATH LABEL_PREFIX
  538. ${if} ${FileExists} "${SHORTCUT_PATH}"
  539. Push "${SHORTCUT_PATH}"
  540. Call ZCodeReadShortcutTarget
  541. Pop $R0
  542. StrCmp $R0 "$appExe" ${LABEL_PREFIX}Done 0
  543. ; 修复原因:历史版本可能留下指向已移动 exe 的 .lnk,但无条件覆盖正确快捷方式会让
  544. ; 部分 Windows 11 丢失“所有应用”索引或用户固定关系,因此只修复目标不一致的项。
  545. ClearErrors
  546. CreateShortCut "${SHORTCUT_PATH}" "$appExe" "" "$appExe" 0 "" "" "${APP_DESCRIPTION}"
  547. IfErrors ${LABEL_PREFIX}Failed ${LABEL_PREFIX}Succeeded
  548. ${LABEL_PREFIX}Failed:
  549. DetailPrint "Unable to repair shortcut: ${SHORTCUT_PATH}"
  550. ClearErrors
  551. Goto ${LABEL_PREFIX}Done
  552. ${LABEL_PREFIX}Succeeded:
  553. WinShell::SetLnkAUMI "${SHORTCUT_PATH}" "${APP_ID}"
  554. ; 修复后的 .lnk 必须在最后一次写入后通知 Shell,避免开始菜单继续使用旧索引。
  555. System::Call 'Shell32::SHChangeNotify(i 0x00002000, i 0x0005, w "${SHORTCUT_PATH}", p 0)'
  556. ${LABEL_PREFIX}Done:
  557. ${endIf}
  558. !macroend
  559. !macro customInstall
  560. !ifndef BUILD_UNINSTALLER
  561. !insertmacro ZCodeReportInstallerStage "install-finalization-started"
  562. !endif
  563. ${if} ${isUpdated}
  564. ${orIf} $keepShortcuts == "true"
  565. !ifndef DO_NOT_CREATE_START_MENU_SHORTCUT
  566. !insertmacro ZCodeRepairShortcutIfNeeded "$newStartMenuLink" zcodeStartMenuShortcutRepair
  567. !endif
  568. !ifndef DO_NOT_CREATE_DESKTOP_SHORTCUT
  569. !insertmacro ZCodeRepairShortcutIfNeeded "$newDesktopLink" zcodeDesktopShortcutRepair
  570. !endif
  571. ${endIf}
  572. ; 修复原因:手动覆盖没有 --updated,继承旧快捷方式时仍需检查目标;首次安装没有旧项,
  573. ; 不应额外启动 PowerShell。用户已删除的快捷方式也不会重建。
  574. ; assisted installer 完成页始终直接运行本次安装落盘的 exe。
  575. StrCpy $launchLink "$appExe"
  576. !ifndef BUILD_UNINSTALLER
  577. !insertmacro ZCodeReportInstallerStage "install-completed"
  578. !endif
  579. !macroend
  580. !macro customPageAfterChangeDir
  581. Function ZCodeResizeInstallDirBackButton
  582. GetDlgItem $1 $HWNDPARENT 3
  583. StrCmp $1 0 zcodeResizeInstallDirBackButtonDone 0
  584. System::Call "*(i 0, i 0, i 0, i 0) p.r2"
  585. StrCmp $2 0 zcodeResizeInstallDirBackButtonDone 0
  586. System::Call "user32::GetWindowRect(p r1, p r2)"
  587. System::Call "user32::MapWindowPoints(p 0, p $HWNDPARENT, p r2, i 2)"
  588. System::Call "*$2(i.r3,i.r4,i.r5,i.r6)"
  589. System::Free $2
  590. IntOp $7 $5 - $3
  591. IntOp $8 $6 - $4
  592. IntCmp $7 ${ZCODE_INSTALL_DIR_BACK_BUTTON_WIDTH} zcodeResizeInstallDirBackButtonDone zcodeResizeInstallDirBackButtonResize zcodeResizeInstallDirBackButtonDone
  593. zcodeResizeInstallDirBackButtonResize:
  594. ; 修复原因:阻断页把“上一步”改成中文动作文案,NSIS 默认按钮宽度可能裁掉文字。
  595. ; 保持右边缘不动向左扩宽,避免和右侧“安装/取消”按钮重叠。
  596. IntOp $3 $5 - ${ZCODE_INSTALL_DIR_BACK_BUTTON_WIDTH}
  597. System::Call "user32::MoveWindow(p r1, i r3, i r4, i ${ZCODE_INSTALL_DIR_BACK_BUTTON_WIDTH}, i r8, i 1)"
  598. zcodeResizeInstallDirBackButtonDone:
  599. FunctionEnd
  600. Function ZCodeFindNestedDataDir
  601. Exch $R9
  602. Push $0
  603. Push $1
  604. StrCpy $R2 ""
  605. IfFileExists "$R9\.zcode\*.*" 0 +2
  606. StrCpy $R2 "$R9\.zcode"
  607. StrCmp $R2 "" 0 zcodeFindNestedDataDirDone
  608. IfFileExists "$R9\.zcode" 0 zcodeFindNestedDataDirListChildren
  609. StrCpy $R2 "$R9\.zcode"
  610. StrCmp $R2 "" 0 zcodeFindNestedDataDirDone
  611. zcodeFindNestedDataDirListChildren:
  612. FindFirst $0 $1 "$R9\*"
  613. IfErrors zcodeFindNestedDataDirDone
  614. zcodeFindNestedDataDirNext:
  615. StrCmp $1 "" zcodeFindNestedDataDirClose
  616. StrCmp $1 "." zcodeFindNestedDataDirContinue
  617. StrCmp $1 ".." zcodeFindNestedDataDirContinue
  618. IfFileExists "$R9\$1\*.*" 0 zcodeFindNestedDataDirContinue
  619. Push "$R9\$1"
  620. Call ZCodeFindNestedDataDir
  621. StrCmp $R2 "" zcodeFindNestedDataDirContinue zcodeFindNestedDataDirClose
  622. zcodeFindNestedDataDirContinue:
  623. FindNext $0 $1
  624. IfErrors zcodeFindNestedDataDirClose
  625. Goto zcodeFindNestedDataDirNext
  626. zcodeFindNestedDataDirClose:
  627. FindClose $0
  628. zcodeFindNestedDataDirDone:
  629. Pop $1
  630. Pop $0
  631. Pop $R9
  632. FunctionEnd
  633. Function ZCodeBlockInstallDirContainsData
  634. Call ZCodeDetectPreviousUninstallerCapabilities
  635. StrCmp $ZCodePreviousUninstallerSupportsManifest "1" zcodeInstallDirDataBlockSkip
  636. ; Bugfix: 用户可能把数据存储目录放进安装目录,Windows 更新覆盖安装目录时会清掉 .zcode。
  637. ; assisted installer 会把不含应用名的选择目录补成 "$INSTDIR\${APP_FILENAME}",所以这里按相同规则计算最终安装目录。
  638. ${StrContains} $R1 "${APP_FILENAME}" "$INSTDIR"
  639. StrCmp $R1 "" 0 zcodeInstallDirDataBlockUseSelectedDir
  640. StrCpy $R0 "$INSTDIR\${APP_FILENAME}"
  641. Goto zcodeInstallDirDataBlockCheckDir
  642. zcodeInstallDirDataBlockUseSelectedDir:
  643. StrCpy $R0 "$INSTDIR"
  644. zcodeInstallDirDataBlockCheckDir:
  645. ; 修复原因:旧阻断只检查最终安装目录直属的 .zcode,漏掉 data\.zcode 等子目录数据。
  646. ; 安装器覆盖安装时会管理整个安装目录树,递归命中任意 .zcode 都必须阻断。
  647. Push "$R0"
  648. Call ZCodeFindNestedDataDir
  649. StrCmp $R2 "" zcodeInstallDirDataBlockSkip zcodeInstallDirDataBlockFound
  650. zcodeInstallDirDataBlockFound:
  651. IfSilent zcodeInstallDirDataBlockSilent
  652. !insertmacro MUI_HEADER_TEXT "需要修改安装目录" "当前安装目录或其子目录包含 ZCode 数据目录"
  653. nsDialogs::Create 1018
  654. Pop $0
  655. StrCmp $0 error zcodeInstallDirDataBlockDialogFailed 0
  656. ${NSD_CreateLabel} 0u 0u 300u 44u "检测到该安装目录或其子目录中存在 .zcode 数据目录:$\r$\n$R2"
  657. Pop $1
  658. ${NSD_CreateLabel} 0u 54u 300u 70u "为避免历史会话和配置被安装器清理,请返回上一步选择其他安装目录。$\r$\n$\r$\n当前目录不能继续安装。"
  659. Pop $1
  660. GetDlgItem $1 $HWNDPARENT 1
  661. EnableWindow $1 0
  662. GetDlgItem $1 $HWNDPARENT 3
  663. EnableWindow $1 1
  664. SendMessage $1 ${WM_SETTEXT} 0 "STR:重选目录"
  665. Call ZCodeResizeInstallDirBackButton
  666. nsDialogs::Show
  667. Return
  668. zcodeInstallDirDataBlockDialogFailed:
  669. MessageBox MB_OK|MB_ICONSTOP "检测到安装目录或其子目录中存在 .zcode 数据目录,安装已停止。请重新运行安装器并选择其他安装目录。"
  670. SetErrorLevel 1
  671. Quit
  672. zcodeInstallDirDataBlockSilent:
  673. SetErrorLevel 1
  674. Quit
  675. zcodeInstallDirDataBlockSkip:
  676. Abort
  677. FunctionEnd
  678. Function ZCodeBlockInstallDirContainsDataLeave
  679. ; 修复原因:阻断页的下一步按钮已禁用,但自动化或系统快捷键仍可能触发下一页。
  680. ; leave 回调只处理继续前进的路径,这里强制留在当前页,确保用户只能返回修改安装目录。
  681. Abort
  682. FunctionEnd
  683. Page custom ZCodeBlockInstallDirContainsData ZCodeBlockInstallDirContainsDataLeave
  684. !macroend