templates/installer: Refactor installer code for easier changes

This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2020-12-04 06:46:31 +01:00 committed by Xaymar
parent 002d507cc8
commit 80ce8b9061

View file

@ -2,9 +2,11 @@
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES! ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
#define MyAppName "@PROJECT_FULL_NAME@" #define MyAppName "@PROJECT_FULL_NAME@"
#define MyAppVersion "@PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@.@PROJECT_VERSION_PATCH@.@PROJECT_VERSION_TWEAK@" #define MyAppVersion "@VERSION_MAJOR@.@VERSION_MINOR@.@VERSION_PATCH@.@VERSION_TWEAK@"
#define MyAppVersionText "@VERSION_STRING@"
#define MyAppPublisher "Xaymars Technology Workshop" #define MyAppPublisher "Xaymars Technology Workshop"
#define MyAppURL "https://www.xaymar.com/" #define MyAppURL "https://xaymar.com/"
#define MyAppCopyright "@PROJECT_COPYRIGHT_YEARS@ @PROJECT_AUTHORS@"
[Setup] [Setup]
; NOTE: The value of AppId uniquely identifies this application. ; NOTE: The value of AppId uniquely identifies this application.
@ -22,12 +24,17 @@ AppUpdatesURL={#MyAppURL}
AppMutex={#MyAppName} AppMutex={#MyAppName}
; Versioning ; Versioning
VersionInfoProductName={#MyAppName}
VersionInfoProductVersion={#MyAppVersion}
VersionInfoVersion={#MyAppVersion} VersionInfoVersion={#MyAppVersion}
VersionInfoProductTextVersion={#MyAppVersionText}
VersionInfoTextVersion={#MyAppVersionText}
VersionInfoCompany={#MyAppPublisher} VersionInfoCompany={#MyAppPublisher}
VersionInfoCopyright={#MyAppCopyright}
VersionInfoDescription={#MyAppName} Setup VersionInfoDescription={#MyAppName} Setup
; Architecture (Platform is always Windows) ; Architecture (Platform is always Windows)
ArchitecturesInstallIn64BitMode=@ARCH@ ArchitecturesInstallIn64BitMode=x64 arm64 ia64
ArchitecturesAllowed=@ARCH@ ArchitecturesAllowed=@ARCH@
; Wizard Information ; Wizard Information
@ -36,7 +43,8 @@ WizardResizable=yes
SetupIconFile="@PROJECT_SOURCE_DIR@/media/icon.ico" SetupIconFile="@PROJECT_SOURCE_DIR@/media/icon.ico"
; Other Information ; Other Information
DefaultDirName={code:GetDirName} UsePreviousAppDir=yes
DefaultDirName={code:GetDefaultDirectory}
DefaultGroupName={#MyAppName} DefaultGroupName={#MyAppName}
AllowNoIcons=yes AllowNoIcons=yes
LicenseFile="@ISS_SOURCE_DIR@/LICENSE" LicenseFile="@ISS_SOURCE_DIR@/LICENSE"
@ -51,7 +59,7 @@ Name: "english"; MessagesFile: "compiler:Default.isl"
[Files] [Files]
Source: "@ISS_FILES_DIR@/*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs Source: "@ISS_FILES_DIR@/*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "@PROJECT_SOURCE_DIR@/third-party/msvc-redist-helper.exe"; DestDir: "{app}"; DestName: "msvc-redist-helper.exe"; Flags: ignoreversion dontcopy noencryption Source: "@PROJECT_SOURCE_DIR@/templates/msvc-redist-helper.exe"; DestDir: "{app}"; DestName: "msvc-redist-helper.exe"; Flags: ignoreversion dontcopy noencryption
; NOTE: Don't use "Flags: ignoreversion" on any shared system files ; NOTE: Don't use "Flags: ignoreversion" on any shared system files
[Icons] [Icons]
@ -59,78 +67,104 @@ Name: "{group}\{cm:ProgramOnTheWeb,{#MyAppName}}"; Filename: "{#MyAppURL}"
Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}" Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"
[Code] [Code]
function GetDirName(Value: string): string; // ------------------------------------------------------------------------------------------------------------------ //
var function FindRegistryKey(): String;
InstallPath: string;
begin begin
// initialize default path, which will be returned when the following registry Result := ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\{#emit SetupSetting('AppId')}_is1');
// key queries fail due to missing keys or for some different reason
Result := ExpandConstant('{pf}\obs-studio');
// query the first registry value; if this succeeds, return the obtained value
if RegQueryStringValue(HKLM32, 'SOFTWARE\OBS Studio', '', InstallPath) then
Result := InstallPath
end; end;
///////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------------------------------------------------------ //
function GetUninstallString(): String; function GetDefaultDirectory(Value: String): String;
var var
sUnInstPath: String; sInstallPath: String;
sUnInstallString: String;
begin begin
sUnInstPath := ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\{#emit SetupSetting("AppId")}_is1'); // 1. Use the path we were given on call.
sUnInstallString := ''; sInstallPath := Value;
if not RegQueryStringValue(HKLM, sUnInstPath, 'UninstallString', sUnInstallString) then
RegQueryStringValue(HKCU, sUnInstPath, 'UninstallString', sUnInstallString); // 2. If that was empty, try and find it ourselves from the registry.
Result := sUnInstallString; if (sInstallPath = '') then
RegQueryStringValue(HKA64, FindRegistryKey(), 'InstallLocation', sInstallPath);
// 2. If empty, try and find the "Local Machine" installation of OBS Studio.
if (sInstallPath = '') then
RegQueryStringValue(HKLM64, 'SOFTWARE\OBS Studio', '', sInstallPath);
// 3. If empty, try and find the "Current User" installation of OBS Studio.
if (sInstallPath = '') then
RegQueryStringValue(HKCU64, 'SOFTWARE\OBS Studio', '', sInstallPath);
// 4. If empty, try and find the 32-bit "Local Machine" installation of OBS Studio.
if (sInstallPath = '') then
RegQueryStringValue(HKLM32, 'SOFTWARE\OBS Studio', '', sInstallPath);
// 5. If empty, try and find the 32-bit "Current User" installation of OBS Studio.
if (sInstallPath = '') then
RegQueryStringValue(HKCU32, 'SOFTWARE\OBS Studio', '', sInstallPath);
// 6. If empty, just use the default path.
if (sInstallPath = '') then
sInstallPath := ExpandConstant('{commonpf}\obs-studio');
Result := sInstallPath
end; end;
///////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------------------------------------------------------ //
function GetUninstallerPath(): String;
var
sRegistryKey: String;
sUninstallerPath: String;
begin
sRegistryKey := FindRegistryKey();
RegQueryStringValue(HKLM64, sRegistryKey, 'UninstallString', sUninstallerPath);
if (sUninstallerPath = '') then
RegQueryStringValue(HKCU64, sRegistryKey, 'UninstallString', sUninstallerPath);
if (sUninstallerPath = '') then
RegQueryStringValue(HKLM32, sRegistryKey, 'UninstallString', sUninstallerPath);
if (sUninstallerPath = '') then
RegQueryStringValue(HKCU32, sRegistryKey, 'UninstallString', sUninstallerPath);
Result := sUninstallerPath;
end;
// ------------------------------------------------------------------------------------------------------------------ //
function IsUpgrade(): Boolean; function IsUpgrade(): Boolean;
begin begin
Result := (GetUninstallString() <> ''); Result := (GetUninstallerPath() <> '');
end; end;
///////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------------------------------------------------------ //
function UnInstallOldVersion(): Integer; function UninstallOldVersion(): Integer;
var var
sUnInstallString: String; sUninstallerPath: String;
iResultCode: Integer; iResultCode: Integer;
begin begin
// Return Values: Result := 0;
// 1 - uninstall string is empty sUninstallerPath := GetUninstallerPath();
// 2 - error executing the UnInstallString if (sUninstallerPath <> '') then begin
// 3 - successfully executed the UnInstallString sUninstallerPath := RemoveQuotes(sUninstallerPath);
if Exec(sUninstallerPath, '/VERYSILENT /NORESTART /SUPPRESSMSGBOXES', '', SW_HIDE, ewWaitUntilTerminated, iResultCode) then begin
// default return value Result := iResultCode
Result := 0; end else begin
Result := 1
// get the uninstall string of the old app end;
sUnInstallString := GetUninstallString(); end;
if sUnInstallString <> '' then begin
sUnInstallString := RemoveQuotes(sUnInstallString);
if Exec(sUnInstallString, '/VERYSILENT /NORESTART /SUPPRESSMSGBOXES','', SW_HIDE, ewWaitUntilTerminated, iResultCode) then
Result := 3
else
Result := 2;
end else
Result := 1;
end; end;
///////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------------------------------------------------------ //
procedure CurStepChanged(CurStep: TSetupStep); function PrepareToInstall(var NeedsRestart: Boolean): String;
var var
ResultCode: Integer; iResultCode: Integer;
begin begin
if (CurStep=ssInstall) then // Attempt to remove old version if it exists.
begin if (IsUpgrade()) then begin
if (IsUpgrade()) then UninstallOldVersion();
begin end;
UnInstallOldVersion();
end; // Also ensure that we have the necessary prerequisites installed to run the program.
end;
if (CurStep=ssPostInstall) then
begin
ExtractTemporaryFile('msvc-redist-helper.exe'); ExtractTemporaryFile('msvc-redist-helper.exe');
Exec(ExpandConstant('{tmp}\msvc-redist-helper.exe'), '2019', '', SW_HIDE, ewWaitUntilTerminated, ResultCode); Exec(ExpandConstant('{tmp}\msvc-redist-helper.exe'), '2019', '', SW_HIDE, ewWaitUntilTerminated, iResultCode);
end;
end; end;