Skip to content

Commit

Permalink
Finish/fix the page + make Extract7ZipArchive use exceptions on error…
Browse files Browse the repository at this point in the history
…s, just like DownloadTemporaryFile. Todo: fix messages. Also found new issue (not related to this branch): turns out 7zMain.c doesn't work from the current dir like I thought, instead it works from the archive dir. So the DestDir parameter doesn't work at the moment, doh!
  • Loading branch information
martijnlaan committed Nov 14, 2024
1 parent c96fdb0 commit 8d38ccb
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion ISHelp/isxclasses.pas
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ TExtractionWizardPage = class(TOutputProgressWizardPage)
property AbortedByUser: Boolean; read;
procedure Add(const ArchiveFileName, DestDir: String; const FullPaths: Boolean);
procedure Clear;
function Extract: Integer;
procedure Extract;
property ShowArchiveInsteadOfFile: Boolean; read write;
end;

Expand Down
2 changes: 1 addition & 1 deletion ISHelp/isxclasses_wordlists_generated.pas
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ interface
'function CheckItem(Index: Integer; AOperation: TCheckItemOperation): Boolean;',
'function CopyFrom(Source: TStream; ByteCount: Int64; BufferSize: Integer): Int64;',
'function Download: Int64;',
'function Extract: Integer;',
'function Find(S: String; var Index: Integer): Boolean;',
'function FindComponent(AName: String): TComponent;',
'function FindNextPage(CurPage: TNewNotebookPage; GoForward: Boolean): TNewNotebookPage;',
Expand Down Expand Up @@ -124,6 +123,7 @@ interface
'procedure Destroying;',
'procedure Draw(X, Y: Integer; Graphic: TGraphic);',
'procedure Ellipse(X1, Y1, X2, Y2: Integer);',
'procedure Extract;',
'procedure FlipSizeAndCenterIfNeeded(ACenterInsideControl: Boolean; CenterInsideControlCtl: TWinControl; CenterInsideControlInsideClientArea: Boolean);',
'procedure FloodFill(X, Y: Integer; Color: TColor; FillStyle: Byte);',
'procedure Free;',
Expand Down
4 changes: 2 additions & 2 deletions ISHelp/isxfunc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1843,9 +1843,9 @@ end;</pre>
</function>
<function>
<name>Extract7ZipArchive</name>
<prototype>function Extract7ZipArchive(const ArchiveFileName, DestDir: String; const FullPaths: Boolean; const OnExtractionProgress: TOnExtractionProgress): Integer;</prototype>
<prototype>procedure Extract7ZipArchive(const ArchiveFileName, DestDir: String; const FullPaths: Boolean; const OnExtractionProgress: TOnExtractionProgress);</prototype>
<description><p>Extracts the specified 7-Zip archive to the specified directory, with or without using path names.</p>
<p>Returns zero if successful, nonzero otherwise</p>
<p>An exception will be raised if there was an error.</p>
<p>The archive must not be encrypted.</p>
<p>Set OnExtractionProgress to a function to be informed of progress, or <tt>nil</tt> otherwise.</p></description>
<remarks><p>TOnExtractionProgress is defined as:</p>
Expand Down
2 changes: 1 addition & 1 deletion Projects/Src/Compiler.ScriptClasses.pas
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ procedure RegisterExtractionWizardPage_C(Cl: TPSPascalCompiler);
RegisterProperty('ShowArchiveInsteadOfFile', 'Boolean', iptrw);
RegisterMethod('procedure Add(const ArchiveFileName, DestDir: String; const FullPaths: Boolean)');
RegisterMethod('procedure Clear');
RegisterMethod('function Extract: Integer');
RegisterMethod('procedure Extract');
RegisterMethod('procedure Show'); { Without this TOutputProgressWizardPage's Show will be called }
end;
end;
Expand Down
28 changes: 18 additions & 10 deletions Projects/Src/Compression.SevenZipDecoder.pas
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ interface
type
TOnExtractionProgress = function(const ArchiveName, FileName: string; const Progress, ProgressMax: Int64): Boolean of object;

function Extract7ZipArchive(const ArchiveFileName, DestDir: String;
const FullPaths: Boolean; const OnExtractionProgress: TOnExtractionProgress): Integer;
procedure Extract7ZipArchive(const ArchiveFileName, DestDir: String;
const FullPaths: Boolean; const OnExtractionProgress: TOnExtractionProgress);

implementation

uses
Windows, SysUtils, Forms,
PathFunc,
Setup.LoggingFunc, Setup.MainFunc, Setup.InstFunc;
Shared.SetupMessageIDs, SetupLdrAndSetup.Messages, Setup.LoggingFunc, Setup.MainFunc, Setup.InstFunc;

type
TSevenZipDecodeState = record
Expand All @@ -32,6 +32,7 @@ TSevenZipDecodeState = record
ExtractedArchiveName: String;
OnExtractionProgress: TOnExtractionProgress;
LastReportedProgress, LastReportedProgressMax: UInt64;
Aborted: Boolean;
end;

var
Expand Down Expand Up @@ -253,10 +254,13 @@ procedure _ReportProgress(const FileName: PChar; const Progress, ProgressMax: UI

if not Abort and DownloadTemporaryFileOrSevenZipDecodeProcessMessages then
Application.ProcessMessages;

if Abort then
State.Aborted := True;
end;

function Extract7ZipArchive(const ArchiveFileName, DestDir: String;
const FullPaths: Boolean; const OnExtractionProgress: TOnExtractionProgress): Integer;
procedure Extract7ZipArchive(const ArchiveFileName, DestDir: String;
const FullPaths: Boolean; const OnExtractionProgress: TOnExtractionProgress);
begin
if ArchiveFileName = '' then
InternalError('Extract7ZipArchive: Invalid ArchiveFileName value');
Expand All @@ -266,22 +270,26 @@ function Extract7ZipArchive(const ArchiveFileName, DestDir: String;
LogFmt('Extracting 7-Zip archive %s to %s. Full paths? %s', [ArchiveFileName, DestDir, SYesNo[FullPaths]]);

var SaveCurDir := GetCurrentDir;
if not SetCurrentDir(DestDir) then
Exit(-1);
if not ForceDirectories(False, DestDir) or not SetCurrentDir(DestDir) then
raise Exception.Create(FmtSetupMessage(msgErrorDownloadFailed, ['-1', ''])); //todo: fix message
try
State.ExpandedDestDir := AddBackslash(PathExpand(DestDir));
State.LogBuffer := '';
State.ExtractedArchiveName := PathExtractName(ArchiveFileName);
State.OnExtractionProgress := OnExtractionProgress;
State.LastReportedProgress := 0;
State.LastReportedProgressMax := 0;
State.Aborted := False;

Result := IS_7zDec(PChar(ArchiveFileName), FullPaths);

//todo: throw exception on Result <> 0 like DownloadTemporaryFile uses exceptions?
var Res := IS_7zDec(PChar(ArchiveFileName), FullPaths);

if State.LogBuffer <> '' then
Log(State.LogBuffer);

if State.Aborted then
raise Exception.Create(SetupMessages[msgErrorDownloadAborted]) //todo: fix message
else if Res <> 0 then
raise Exception.Create(FmtSetupMessage(msgErrorDownloadFailed, [Res.ToString, ''])) //todo: fix message
finally
SetCurrentDir(SaveCurDir);
end;
Expand Down
8 changes: 3 additions & 5 deletions Projects/Src/Setup.ScriptDlg.pas
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ TExtractionWizardPage = class(TOutputProgressWizardPage)
procedure Initialize; override;
procedure Add(const ArchiveFileName, DestDir: String; const FullPaths: Boolean);
procedure Clear;
function Extract: Integer;
procedure Extract;
property OnExtractionProgress: TOnExtractionProgress write FOnExtractionProgress;
procedure Show; override;
published
Expand Down Expand Up @@ -1180,15 +1180,13 @@ procedure TExtractionWizardPage.Clear;
FArchives.Clear;
end;

function TExtractionWizardPage.Extract: Integer;
procedure TExtractionWizardPage.Extract;
begin
FAbortedByUser := False;

Result := 0;
for var A in FArchives do begin
{ Don't need to set DownloadTemporaryFileOrSevenZipDecodeProcessMessages before extraction since we already process messages ourselves. }
if Extract7ZipArchive(A.FileName, A.DestDir, A.FullPaths, InternalOnExtractionProgress) = 0 then
Inc(Result);
Extract7ZipArchive(A.FileName, A.DestDir, A.FullPaths, InternalOnExtractionProgress);
end;
end;

Expand Down
6 changes: 3 additions & 3 deletions Projects/Src/Setup.ScriptFunc.pas
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ function ScriptDlgProc(Caller: TPSExec; Proc: TPSExternalProcRec; Global, Stack:
raise;
end;
Stack.SetClass(PStart, NewDownloadPage);
end else if Proc.Name = 'CREATEXTRACTIONPAGE' then begin
end else if Proc.Name = 'CREATEEXTRACTIONPAGE' then begin
if IsUninstaller then
NoUninstallFuncError(Proc.Name);
var P: PPSVariantProcPtr := Stack.Items[PStart-3];
Expand Down Expand Up @@ -2118,14 +2118,14 @@ function OtherProc(Caller: TPSExec; Proc: TPSExternalProcRec; Global, Stack: TPS
AscendingTrySizes[I] := VNGetInt(PSGetArrayField(Arr, I));
Stack.SetBool(PStart, TBitmapImage(Stack.GetClass(PStart-1)).InitializeFromIcon(0, PChar(Stack.GetString(PStart-2)), Stack.GetInt(PStart-3), AscendingTrySizes));
end else if Proc.Name = 'EXTRACT7ZIPARCHIVE' then begin
var P: PPSVariantProcPtr := Stack.Items[PStart-4];
var P: PPSVariantProcPtr := Stack.Items[PStart-3];
var OnExtractionProgress: TOnExtractionProgress;
{ ProcNo 0 means nil was passed by the script }
if P.ProcNo <> 0 then
OnExtractionProgress := TOnExtractionProgress(Caller.GetProcAsMethod(P.ProcNo))
else
OnExtractionProgress := nil;
Stack.SetInt(PStart, Extract7ZipArchive(Stack.GetString(PStart-1), Stack.GetString(PStart-2), Stack.GetBool(PStart-3), OnExtractionProgress));
Extract7ZipArchive(Stack.GetString(PStart), Stack.GetString(PStart-1), Stack.GetBool(PStart-2), OnExtractionProgress);
end else if Proc.Name = 'DEBUGGING' then begin
Stack.SetBool(PStart, Debugging);
end else
Expand Down
2 changes: 1 addition & 1 deletion Projects/Src/Shared.ScriptFunc.pas
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ initialization
'function IsDotNetInstalled(const MinVersion: TDotNetVersion; const MinServicePack: Cardinal): Boolean;',
'function IsMsiProductInstalled(const UpgradeCode: String; const PackedMinVersion: Int64): Boolean;',
'function InitializeBitmapImageFromIcon(const BitmapImage: TBitmapImage; const IconFilename: String; const BkColor: TColor; const AscendingTrySizes: TArrayOfInteger): Boolean;',
'function Extract7ZipArchive(const ArchiveFileName, DestDir: String; const FullPaths: Boolean; const OnExtractionProgress: TOnExtractionProgress): Integer;',
'procedure Extract7ZipArchive(const ArchiveFileName, DestDir: String; const FullPaths: Boolean; const OnExtractionProgress: TOnExtractionProgress);',
'function Debugging: Boolean;'
];

Expand Down

0 comments on commit 8d38ccb

Please sign in to comment.