Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Installer: Prompt for downgrading and prompt for uninstall if Odamex is already installed #772

183 changes: 183 additions & 0 deletions installer/windows/odamex.iss
Expand Up @@ -129,6 +129,189 @@ Name: {group}\Odamex Server; Filename: {app}\odasrv.exe; WorkingDir: {app}
Name: {group}\Odamex User Folder; Filename: "%USERPROFILE%\Documents\My Games\Odamex"
Name: {group}\{cm:UninstallProgram,Odamex}; Filename: {uninstallexe}

[Code]
function GetUninstallString: string;
var
sUnInstPath: string;
sUnInstallString: String;
begin
Result := '';
sUnInstPath := ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\{#SetupSetting("AppID")}_is1');
sUnInstallString := '';
if not RegQueryStringValue(HKLM, sUnInstPath, 'UninstallString', sUnInstallString) then
RegQueryStringValue(HKCU, sUnInstPath, 'UninstallString', sUnInstallString);
Result := sUnInstallString;
end;


function GetRegistryVersion: string;
var
sUnInstPath: string;
sVersionString: String;
begin
Result := '';
sUnInstPath := ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\{#SetupSetting("AppID")}_is1');
sVersionString := '';
if not RegQueryStringValue(HKLM, sUnInstPath, 'DisplayVersion', sVersionString) then
RegQueryStringValue(HKCU, sUnInstPath, 'DisplayVersion', sVersionString);
Result := sVersionString;
end;


function IsUpgrade: Boolean;
begin
Result := (GetUninstallString() <> '');
end;


function Count(What, Where: String): Integer;
begin
Result := 0;
if Length(What) = 0 then
exit;
while Pos(What,Where)>0 do
begin
Where := Copy(Where,Pos(What,Where)+Length(What),Length(Where));
Result := Result + 1;
end;
end;


//split text to array
procedure Explode(var ADest: TArrayOfString; aText, aSeparator: String);
var tmp: Integer;
begin
if aSeparator='' then
exit;

SetArrayLength(ADest,Count(aSeparator,aText)+1)

tmp := 0;
repeat
if Pos(aSeparator,aText)>0 then
begin

ADest[tmp] := Copy(aText,1,Pos(aSeparator,aText)-1);
aText := Copy(aText,Pos(aSeparator,aText)+Length(aSeparator),Length(aText));
tmp := tmp + 1;

end else
begin

ADest[tmp] := aText;
aText := '';

end;
until Length(aText)=0;
end;


//compares two version numbers, returns -1 if vA is newer, 0 if both are identical, 1 if vB is newer
function CompareVersion(vA,vB: String): Integer;
var tmp: TArrayOfString;
verA,verB: Array of Integer;
i,len: Integer;
begin

StringChange(vA,'-','.');
StringChange(vB,'-','.');

Explode(tmp,vA,'.');
SetArrayLength(verA,GetArrayLength(tmp));
for i := 0 to GetArrayLength(tmp) - 1 do
verA[i] := StrToIntDef(tmp[i],0);

Explode(tmp,vB,'.');
SetArrayLength(verB,GetArrayLength(tmp));
for i := 0 to GetArrayLength(tmp) - 1 do
verB[i] := StrToIntDef(tmp[i],0);

len := GetArrayLength(verA);
if GetArrayLength(verB) < len then
len := GetArrayLength(verB);

for i := 0 to len - 1 do
if verA[i] < verB[i] then
begin
Result := 1;
exit;
end else
if verA[i] > verB[i] then
begin
Result := -1;
exit
end;

if GetArrayLength(verA) < GetArrayLength(verB) then
begin
Result := 1;
exit;
end else
if GetArrayLength(verA) > GetArrayLength(verB) then
begin
Result := -1;
exit;
end;

Result := 0;
end;


function InitializeSetup(): Boolean;
var
V: Integer;
iUpgradeResult: Integer;
iResultCode: Integer;
iVersionCompare: Integer;
sUnInstallString: string;
sVersion: string;
sOldVersion: string;
bUpgrade: Boolean;
bSilent: Boolean;
begin
bSilent := WizardSilent();
if bSilent = False then
begin
bUpgrade := IsUpgrade();
if bUpgrade then
begin
sVersion := '{#SetupSetting("AppVersion")}';
sOldVersion := GetRegistryVersion();
iVersionCompare := CompareVersion(sOldVersion, sVersion);
if iVersionCompare = -1 then
iUpgradeResult := MsgBox(ExpandConstant('The version of Odamex you are about to install is a downgrade to the currently installed version. Do you want to proceed with the installation?'), mbConfirmation, MB_YESNO);
if iVersionCompare = 0 then
iUpgradeResult := MsgBox(ExpandConstant('The version of Odamex you are about to install is identical to the currently installed version. Do you want to proceed with the installation?'), mbConfirmation, MB_YESNO);
if iVersionCompare = 1 then
iUpgradeResult := IDYES;

if iUpgradeResult = IDYES then
begin
V := MsgBox(ExpandConstant('Odamex has been detected on this machine. If you do not uninstall, there will be an in-place installation of Odamex to the same path. Do you want to uninstall the previous installation?'), mbConfirmation, MB_YESNO);

if V = IDYES then
begin
sUnInstallString := GetUninstallString();
sUnInstallString := RemoveQuotes(sUnInstallString);
Exec(ExpandConstant(sUnInstallString), '', '', SW_SHOW, ewWaitUntilTerminated, iResultCode);
if iResultCode <> 0 then
begin
Result := False;
exit;
end;
end;
end
else
begin
Result := False;
exit;
end;
end;
end;

Result := True;
end;

[Run]
Filename: {app}\odalaunch.exe; Description: {cm:LaunchProgram,Odalaunch}; Flags: nowait postinstall skipifsilent

Expand Down