ダウンロードしたファイルの間違ったハッシュに関する情報を、InnoSetupの素敵なタスクダイアログウィンドウに表示します

アンドリュー・トラックル

私はもともと別のプラットフォーム(ここ)でこの質問について尋ねました

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;

問題があるときに表示されるエラーメッセージはかなり醜いです。以下が私に提案されました:

例外を処理しない場合にのみメッセージボックスが表示されます。try / exceptionを使用すると、ファイル名を追加して例外を再発生させたり、タスクダイアログを使用したりすることができます。

メッセージボックスデザイナーを試してみようと思いました。

ここに画像の説明を入力してください

これにより、次のコードが作成されます。

// 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(メッセージボックスを移動すると表示されます)。

メッセージにハッシュを含める必要がある場合は、エラーメッセージのデータを解析する必要があります。それは少し壊れやすいアプローチです。ただし、フォールバックメッセージを提供すると、解析が失敗した場合に備えて実行可能です。

次の関数は、標準のInnoSetup文字列からデータを解析しようとします。

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;

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ