[code]
var
NotBackupDirExists: boolean;
procedure MakeBackup();
var
curPath, curFile, myBackupDir: string;
begin
if NotBackupDirExists then
begin
curPath := Copy(CurrentFileName, Pos('{app}\', CurrentFileName)+Length('{app}\'), Length(CurrentFileName));
curPath := ExtractFileDir(curPath);
curFile := ExtractFileName(CurrentFileName);
myBackupDir := ExpandConstant('{app}\_Backup\')+curPath;
if FileExists(ExpandConstant('{app}\')+AddBackslash(curPath)+curFile) then
begin
if not DirExists(myBackupDir) then ForceDirectories(myBackupDir);
FileCopy(ExpandConstant('{app}\')+AddBackslash(curPath)+curFile, AddBackslash(myBackupDir)+curFile, false);
end;
end;
end;
procedure CurStepChanged(CurStep: TSetupStep);
begin
if CurStep = ssInstall then
begin
if not DirExists(ExpandConstant('{app}\_Backup')) then
begin
NotBackupDirExists := True;
ForceDirectories(ExpandConstant('{app}\_Backup'));
end;
if NotBackupDirExists then
RenameFile(ExpandConstant('{app}\bin'), ExpandConstant('{app}\_Backup\bin'));
end;
end;
function InitializeSetup(): Boolean;
begin
Result := True;
if RegKeyExists(HKEY_LOCAL_MACHINE,
'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{#GameID}_is1') or
RegKeyExists(HKEY_CURRENT_USER,
'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{#GameID}_is1') then
begin
MsgBox('Приложение уже установлено. Выполните деинсталляцию.', mbInformation, MB_OK);
Result := False;
end;
end;
procedure CreateBackup;
var
srcFile, destFile: string;
begin
srcFile:= ExpandConstant(CurrentFileName);
destFile:= srcFile + '.bak';
DeleteFile(destFile);
RenameFile(srcFile, destFile);
end;
procedure RestoreBackup(backupDir: string);
var
srcFile, destFile: string;
FSR, DSR: TFindRec;
FindResult: Boolean;
APath: string;
begin
APath := AddBackslash(backupDir);
FindResult := FindFirst(APath + '*.bak', FSR);
try
while FindResult do
begin
if FSR.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then
begin
srcFile:= APath + FSR.Name;
destFile:= Copy(srcFile, 0, Length(srcFile)-4);
DeleteFile(destFile);
RenameFile(srcFile, destFile);
end;
FindResult := FindNext(FSR);
end;
FindResult := FindFirst(APath + '*.*', DSR);
while FindResult do
begin
if ((DSR.Attributes and FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY) and
not ((DSR.Name = '.') or (DSR.Name = '..')) then
{Recursion} RestoreBackup(APath + DSR.Name);
FindResult := FindNext(DSR);
end;
finally
FindClose(FSR);
FindClose(DSR);
end;
end;
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
if CurUninstallStep = usDone then
begin
DelTree(ExpandConstant('{app}\{#BinDirName}'), true, true, true);
RenameFile(ExpandConstant('{app}\{#BinDirName}_Backup'), ExpandConstant('{app}\{#BinDirName}'));
end;
end;