From 6e29633ccb608460e4433ed08d24c210b6edcb42 Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Sun, 1 Dec 2024 23:46:03 +0100 Subject: [PATCH] finished hashing error --- launcher/firstLaunch/firstlaunch_moc.cpp | 13 +++++++++-- launcher/innoextract.cpp | 24 +++++++++++---------- launcher/innoextract.h | 2 +- launcher/modManager/chroniclesextractor.cpp | 3 +++ 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/launcher/firstLaunch/firstlaunch_moc.cpp b/launcher/firstLaunch/firstlaunch_moc.cpp index 3abebc8dd..aada29ff0 100644 --- a/launcher/firstLaunch/firstlaunch_moc.cpp +++ b/launcher/firstLaunch/firstlaunch_moc.cpp @@ -359,8 +359,9 @@ void FirstLaunchView::extractGogData() return; // should not happen - but avoid deleting wrong folder in any case QString tmpFileExe = tempDir.filePath("h3_gog.exe"); + QString tmpFileBin = tempDir.filePath("h3_gog-1.bin"); QFile(fileExe).copy(tmpFileExe); - QFile(fileBin).copy(tempDir.filePath("h3_gog-1.bin")); + QFile(fileBin).copy(tmpFileBin); QString errorText{}; @@ -387,6 +388,10 @@ void FirstLaunchView::extractGogData() ui->progressBarGog->setValue(progress * 100); qApp->processEvents(); }); + + QString hashError; + if(!errorText.isEmpty()) + hashError = Innoextract::getHashError(tmpFileExe, tmpFileBin); ui->progressBarGog->setVisible(false); ui->pushButtonGogInstall->setVisible(true); @@ -395,8 +400,12 @@ void FirstLaunchView::extractGogData() QStringList dirData = tempDir.entryList({"data"}, QDir::Filter::Dirs); if(!errorText.isEmpty() || dirData.empty() || QDir(tempDir.filePath(dirData.front())).entryList({"*.lod"}, QDir::Filter::Files).empty()) { - if(!errorText.isEmpty()) // + if(!errorText.isEmpty()) + { QMessageBox::critical(this, tr("Extracting error!"), errorText, QMessageBox::Ok, QMessageBox::Ok); + if(!hashError.isEmpty()) + QMessageBox::critical(this, tr("Hash error!"), hashError, QMessageBox::Ok, QMessageBox::Ok); + } else QMessageBox::critical(this, tr("No Heroes III data!"), tr("Selected files do not contain Heroes III data!"), QMessageBox::Ok, QMessageBox::Ok); tempDir.removeRecursively(); diff --git a/launcher/innoextract.cpp b/launcher/innoextract.cpp index 8abc73294..f4d096353 100644 --- a/launcher/innoextract.cpp +++ b/launcher/innoextract.cpp @@ -61,7 +61,7 @@ QString Innoextract::extract(QString installer, QString outDir, std::function bin) +QString Innoextract::getHashError(QString exeFile, QString binFile) { enum filetype { @@ -101,6 +101,7 @@ QString Innoextract::getHashError(QFile exe, std::optional bin) int exeSize = 0; int binSize = 0; + auto exe = QFile(exeFile); if(exe.open(QFile::ReadOnly)) { QCryptographicHash hash(QCryptographicHash::Algorithm::Sha1); if(hash.addData(&exe)) @@ -109,21 +110,22 @@ QString Innoextract::getHashError(QFile exe, std::optional bin) } else return QString{}; // reading problem - if(bin) + if(!binFile.isEmpty()) { - if((*bin).open(QFile::ReadOnly)) { + auto bin = QFile(binFile); + if(bin.open(QFile::ReadOnly)) { QCryptographicHash hash(QCryptographicHash::Algorithm::Sha1); - if(hash.addData(&(*bin))) + if(hash.addData(&bin)) binHash = hash.result().toHex().toLower().toStdString(); - binSize = (*bin).size(); + binSize = bin.size(); } else return QString{}; // reading problem } - QString hashOutput = tr("Hash:\n") + tr("Exe") + ": " + QString::fromStdString(exeHash) + " (" + QString::number(1) + " " + tr("bytes") + ")"; + QString hashOutput = tr("Hash:\n") + tr("Exe") + ": " + QString::fromStdString(exeHash) + " (" + QString::number(exeSize) + " " + tr("bytes") + ")"; if(!binHash.empty()) - hashOutput += "\n" + tr("Bin") + ": " + QString::fromStdString(binHash) + " (" + QString::number(1) + " " + tr("bytes") + ")"; + hashOutput += "\n" + tr("Bin") + ": " + QString::fromStdString(binHash) + " (" + QString::number(binSize) + " " + tr("bytes") + ")"; QString foundKnown; QString exeLang; @@ -139,13 +141,13 @@ QString Innoextract::getHashError(QFile exe, std::optional bin) binLang = lang; it = std::find_if(++it, knownHashes.end(), find); } - if(!exeLang.isEmpty() && !binLang.isEmpty() && exeLang != binLang && bin) + if(!exeLang.isEmpty() && !binLang.isEmpty() && exeLang != binLang && !binFile.isEmpty()) return tr("Language mismatch.\n\n") + foundKnown + "\n\n" + hashOutput; - else if((!exeLang.isEmpty() || !binLang.isEmpty()) && bin) + else if((!exeLang.isEmpty() || !binLang.isEmpty()) && !binFile.isEmpty()) return tr("Only one file known.\n\n") + foundKnown + "\n\n" + hashOutput; - else if(!exeLang.isEmpty() && !bin) + else if(!exeLang.isEmpty() && binFile.isEmpty()) return QString{}; - else if(!exeLang.isEmpty() && bin && exeLang == binLang) + else if(!exeLang.isEmpty() && !binFile.isEmpty() && exeLang == binLang) return QString{}; return tr("Unknown files. Maybe files are corrupted? Please download again.\n\n") + hashOutput; diff --git a/launcher/innoextract.h b/launcher/innoextract.h index adc7b3e06..91284f25a 100644 --- a/launcher/innoextract.h +++ b/launcher/innoextract.h @@ -13,5 +13,5 @@ class Innoextract : public QObject { public: static QString extract(QString installer, QString outDir, std::function cb = nullptr); - QString getHashError(QFile exe, std::optional bin); + static QString getHashError(QString exeFile, QString binFile); }; diff --git a/launcher/modManager/chroniclesextractor.cpp b/launcher/modManager/chroniclesextractor.cpp index 6f58bdd06..7d0309954 100644 --- a/launcher/modManager/chroniclesextractor.cpp +++ b/launcher/modManager/chroniclesextractor.cpp @@ -84,7 +84,10 @@ bool ChroniclesExtractor::extractGogInstaller(QString file) if(!errorText.isEmpty()) { + QString hashError = Innoextract::getHashError(file, {}); QMessageBox::critical(parent, tr("Extracting error!"), errorText); + if(!hashError.isEmpty()) + QMessageBox::critical(parent, tr("Hash error!"), hashError, QMessageBox::Ok, QMessageBox::Ok); return false; }