; RuneStone hooks for the stock Tauri NSIS template. ; tauri.windows.conf.json names this file in bundle.windows.nsis.installerHooks. ; ; Tauri includes this file before the variables, pages, and sections of its template. ; Thus only the NSIS_HOOK_* macros can use template symbols such as ${VERSION} or $UpdateMode. ; The other code reads the version of this installer from its own version resource. ; ; Tauri accepts only SemVer, so scripts/releaseVersion.mjs encodes the hotfix 0.1.23a ; as the toolchain version 0.1.23+1. Tauri writes the Windows version 0.1.23.1 into the ; version resource. ${VersionCompare} on Windows versions gives the release order ; 0.1.23 < 0.1.23a < 0.1.23z < 0.1.23aa < 0.1.24. !include LogicLib.nsh !include WordFunc.nsh ; RuneStone installs for the current user (installMode "currentUser"), so SHCTX is HKCU. ; This key must agree with UNINSTKEY in the Tauri template and with productName. !define /ifndef RUNESTONE_UNINSTKEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\RuneStone" !ifmacrondef RUNESTONE_DOWNGRADE_NOTICE !macro RUNESTONE_DOWNGRADE_NOTICE ${IfNot} ${Silent} MessageBox MB_ICONSTOP|MB_OK "A newer version of RuneStone is installed. This installer cannot replace it." ${EndIf} !macroend !endif ; Converts a version to four numbers: 1.2.3 gives 1.2.3.0, and 1.2.3+4 or 1.2.3.4 gives 1.2.3.4. ; Returns an empty string for other forms and for numbers above 65535. Function RuneStoneNumericVersion Exch $0 ; Input, then the result Push $1 ; Result so far Push $2 ; Character index Push $3 ; Character Push $4 ; Separators so far Push $5 ; Digits in the current number Push $6 ; Value of the current number Push $7 ; Value of the character StrCpy $1 "" StrCpy $2 0 StrCpy $4 0 StrCpy $5 0 StrCpy $6 0 loop: StrCpy $3 $0 1 $2 IntOp $2 $2 + 1 StrCmp $3 "" end StrCmp $3 "." separator StrCmp $3 "+" build ; Only a digit gives the same text after the conversion to a number. IntOp $7 $3 + 0 StrCmp $7 $3 0 invalid IntOp $6 $6 * 10 IntOp $6 $6 + $7 IntCmp $6 65535 0 0 invalid IntOp $5 $5 + 1 StrCpy $1 "$1$3" Goto loop build: ; A SemVer build number can follow only the third number. IntCmp $4 2 separator invalid invalid separator: IntCmp $5 0 invalid IntCmp $4 3 invalid IntOp $4 $4 + 1 StrCpy $1 "$1." StrCpy $5 0 StrCpy $6 0 Goto loop end: IntCmp $5 0 invalid ; Two separators need a fourth number. The separator check above allows at most three. IntCmp $4 2 0 invalid valid StrCpy $1 "$1.0" valid: StrCpy $0 $1 Goto finish invalid: StrCpy $0 "" finish: Pop $7 Pop $6 Pop $5 Pop $4 Pop $3 Pop $2 Pop $1 Exch $0 FunctionEnd ; Converts a Windows version to the release version: 0.1.23.0 gives 0.1.23, ; 0.1.23.1 gives 0.1.23a, and 0.1.23.27 gives 0.1.23aa. Function RuneStoneReleaseName Exch $0 ; Input, then the result Push $1 ; Character index Push $2 ; Character Push $3 ; Revision Push $4 ; Letter suffix StrLen $1 $0 find_dot: IntOp $1 $1 - 1 IntCmp $1 0 0 finish StrCpy $2 $0 1 $1 StrCmp $2 "." 0 find_dot IntOp $2 $1 + 1 StrCpy $3 $0 "" $2 StrCpy $0 $0 $1 StrCpy $4 "" letters: IntCmp $3 0 letters_done letters_done IntOp $3 $3 - 1 IntOp $2 $3 % 26 StrCpy $2 "abcdefghijklmnopqrstuvwxyz" 1 $2 StrCpy $4 "$2$4" IntOp $3 $3 / 26 Goto letters letters_done: StrCpy $0 "$0$4" finish: Pop $4 Pop $3 Pop $2 Pop $1 Exch $0 FunctionEnd ; Returns the Windows version of this installer from its version resource, or an empty string. Function RuneStoneInstallerVersion Push $0 Push $1 Push $2 Push $3 ClearErrors GetDLLVersion "$EXEPATH" $0 $1 ${If} ${Errors} StrCpy $0 "" ${Else} IntOp $2 $0 >> 16 IntOp $2 $2 & 0xFFFF IntOp $0 $0 & 0xFFFF IntOp $3 $1 >> 16 IntOp $3 $3 & 0xFFFF IntOp $1 $1 & 0xFFFF StrCpy $0 "$2.$0.$3.$1" ${EndIf} Pop $3 Pop $2 Pop $1 Exch $0 FunctionEnd ; Returns the installed Windows version, an empty string when RuneStone is not installed, ; or "unknown" when the installed value has an unknown form. Function RuneStoneInstalledVersion Push $0 ReadRegStr $0 SHCTX "${RUNESTONE_UNINSTKEY}" "RuneStoneWindowsVersion" ${If} $0 == "" ; Earlier installers wrote only the toolchain version, such as 0.1.23+1, to DisplayVersion. ReadRegStr $0 SHCTX "${RUNESTONE_UNINSTKEY}" "DisplayVersion" ${EndIf} ${If} $0 != "" Push $0 Call RuneStoneNumericVersion Pop $0 ${IfThen} $0 == "" ${|} StrCpy $0 "unknown" ${|} ${EndIf} Exch $0 FunctionEnd ; Stops setup with exit code 2 when the installed version is newer or unknown. Function RuneStoneCheckDowngrade Push $0 Push $1 Push $2 Call RuneStoneInstalledVersion Pop $0 ${If} $0 != "" Call RuneStoneInstallerVersion Pop $1 StrCpy $2 2 ${If} $0 != "unknown" ${AndIf} $1 != "" ${VersionCompare} $1 $0 $2 ${EndIf} ${If} $2 = 2 !insertmacro RUNESTONE_DOWNGRADE_NOTICE SetErrorLevel 2 Quit ${EndIf} ${EndIf} Pop $2 Pop $1 Pop $0 FunctionEnd ; Interactive and passive (/P) setup: Modern UI calls this function before the first page. !define MUI_CUSTOMFUNCTION_GUIINIT RuneStoneGuiInit Function RuneStoneGuiInit Call RuneStoneCheckDowngrade ; The stock maintenance page looks for an installation in SHCTX. For an upgrade, it ; preselects "Uninstall before installing". In the "all" context, SHCTX is HKLM, where ; a current-user installation has no key. Thus the page skips itself, and setup replaces ; the app in place. The RuneStoneVersionCheck section restores the current-user context ; before any other section runs. SetShellVarContext all FunctionEnd ; All modes, also silent (/S): Tauri includes this file before its own sections, ; so this section runs first. Silent setup shows no pages and does not call RuneStoneGuiInit. Section "-RuneStoneVersionCheck" SetShellVarContext current Call RuneStoneCheckDowngrade SectionEnd ; The stock template writes the toolchain version to DisplayVersion before this hook. !macro NSIS_HOOK_POSTINSTALL Push $0 Push $1 Call RuneStoneInstallerVersion Pop $0 Push $0 Call RuneStoneReleaseName Pop $1 ; Windows shows DisplayVersion in installed apps. WriteRegStr SHCTX "${RUNESTONE_UNINSTKEY}" "DisplayVersion" "$1" WriteRegStr SHCTX "${RUNESTONE_UNINSTKEY}" "RuneStoneReleaseVersion" "$1" ; Later installers compare this value. WriteRegStr SHCTX "${RUNESTONE_UNINSTKEY}" "RuneStoneWindowsVersion" "$0" Pop $1 Pop $0 !macroend