在 Inno Setup 的漂亮任务对话框 window 中显示有关下载文件的错误哈希的信息

Display the info about wrong hash of downloaded file in a nice task dialog window in Inno Setup

这个问题我最初是在另一个平台上问的(here)。

在 Inno Setup 中,它具有以下消息定义:

ErrorFileHash2=Invalid file hash: expected %1, found %2

当安装程序尝试下载 运行 具有错误哈希值的文件时,将显示此消息。

在我的脚本中我有:

function NextButtonClick(CurPageID: integer): boolean;
begin
    Result := True;
 
  if (CurPageID = wpSelectTasks) then
  begin
    DownloadPage.Clear;
    if (WizardIsTaskSelected('downloadhelp')) then
      AddFileForDownload('{#HelpDocSetupURL}', 'HelpDocSetup.exe', 
        '{#GetSHA256OfFile("..\HelpNDoc\CHM\Output\MSAHelpDocumentationSetup.exe")}');
  end
    else
  if (CurPageID = wpReady) then
  begin
    DownloadPage.Show;
    try
      try
        DownloadPage.Download;
        Result := True;
      except
        SuppressibleMsgBox(
          AddPeriod(GetExceptionMessage), mbCriticalError, MB_OK, IDOK);
        Result := False;
      end;
    finally
      DownloadPage.Hide;
    end;
  end;
end;

出现问题时显示的错误消息相当难看。向我建议了以下内容:

It only shows a message box if you don't handle the exception. Use try/except and then you can do things like re-raising the exception with a filename added or using a task dialog.

我想我会试试消息框设计器:

创建以下代码:

// Display a message box
SuppressibleTaskDialogMsgBox(
  'Unable to download [file]', 'This is because the checksum value does not match',
  mbError, MB_OK, ['OK'], 0, IDOK);

但我不知道我在这里做什么。

只需替换您当前的:

SuppressibleMsgBox(
  AddPeriod(GetExceptionMessage), mbCriticalError, MB_OK, IDOK);

使用您的新代码:

SuppressibleTaskDialogMsgBox(
  'Unable to download [file]', 'This is because the checksum value does not match',
  mbError, MB_OK, ['OK'], 0, IDOK);


如果要识别下载失败,可以使用DownloadPage.Msg2Label.Caption的值(移动消息框就可以看到)

如果您需要在消息中包含哈希值,则必须解析错误消息中的数据。这有点脆弱。但是如果你提供一个回退消息,以防解析失败,它是可行的。

以下函数尝试从任何标准 Inno Setup 字符串中解析数据:

function ParseDataFromSetupMessage(
  Msg: string; ID: TSetupMessageID; var Data: TArrayOfString): Boolean;
var
  MsgOrig, Pattern, PatternOrig, S: string;
  I, P, P2: Integer;
begin
  try
    MsgOrig := Msg;
    Pattern := SetupMessage(ID);
    PatternOrig := Pattern;
    while (Msg <> '') and (Pattern <> '') do
    begin
      P := Pos('%', Pattern);
      if (P = 0) or (P = Length(Pattern)) or (P > 1) then
      begin
        if (P = 0) or (P = Length(Pattern)) then P := Length(Pattern) + 1;

        if Copy(Msg, 1, P - 1) <> Copy(Pattern, 1, P - 1) then Abort;

        Delete(Msg, 1, P - 1);
        Delete(Pattern, 1, P - 1);
      end
        else
      if (Pattern[2] < '1') or (Pattern[2] > '9') then
      begin
        if Copy(Msg, 1, 1) <> '%' then Abort;
        Delete(Pattern, 1, 1);
        Delete(Msg, 1, 1);
      end
        else
      begin
        I := StrToInt(Pattern[2]);
        Delete(Pattern, 1, 2);
        if Length(Pattern) = 0 then
        begin
          S := Msg;
          SetLength(Msg, 0);
        end
          else
        begin
          P := Pos('%', Pattern);
          if P = 0 then P := Length(Pattern) + 1;
          P2 := Pos(Copy(Pattern, 1, P - 1), Msg);
          if P2 = 0 then Abort;
          S := Copy(Msg, 1, P2 - 1);
          Delete(Msg, 1, P2 - 1);
        end;

        if GetArrayLength(Data) < I then
          SetArrayLength(Data, I);
        Data[I - 1] := S;
      end;
    end;

    if Msg <> Pattern then Abort;

    Result := True;
  except
    Log(Format('"%s" does not seem to match format string "%s".', [
      MsgOrig, PatternOrig]));
    Result := False;
  end;
end;

您可以像这样在 except 块中使用两者:

except
  Msg := GetExceptionMessage;
  if ParseDataFromSetupMessage(Msg, msgErrorFileHash2, Data) then
  begin
    Expected := Data[0];
    Hash := Data[1];
    Msg :=
      'This is because the checksum value does not match.' + #13+
      'Download: ' + DownloadPage.Msg2Label.Caption + #13 +
      'Expected: ' + Expected + #13 +
      'Got: ' + Hash;
  end
    else
  begin
    // Failed for other reasons?
    Msg :=
      'Download has failed.' + #13+
      'Download: ' + DownloadPage.Msg2Label.Caption + #13 +
      'Details: ' + Msg;
  end;
  SuppressibleTaskDialogMsgBox(
    'Unable to download', Msg, mbError, MB_OK, ['OK'], 0, IDOK);
  Result := False;
end;