1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-20 20:23:03 +02:00

code review (second part)

This commit is contained in:
Laserlicht 2024-09-02 23:36:42 +02:00
parent 609906f015
commit 7655149d52
6 changed files with 122 additions and 117 deletions

View File

@ -17,6 +17,7 @@ set(launcher_SRCS
firstLaunch/firstlaunch_moc.cpp
main.cpp
helper.cpp
innoextract.cpp
mainwindow_moc.cpp
languages.cpp
launcherdirs.cpp
@ -53,6 +54,7 @@ set(launcher_HEADERS
updatedialog_moc.h
main.h
helper.h
innoextract.h
prepare.h
)

View File

@ -21,11 +21,7 @@
#include "../../lib/filesystem/Filesystem.h"
#include "../helper.h"
#include "../languages.h"
#ifdef ENABLE_INNOEXTRACT
#include "cli/extract.hpp"
#include "setup/version.hpp"
#endif
#include "../innoextract.h"
#ifdef VCMI_IOS
#include "ios/selectdirectory.h"
@ -386,44 +382,11 @@ void FirstLaunchView::extractGogData()
if(isGogGalaxyExe(tmpFileExe))
errorText = tr("You've provided GOG Galaxy installer! This file doesn't contain the game. Please download the offline backup game installer!");
::extract_options o;
o.extract = true;
// standard settings
o.gog_galaxy = true;
o.codepage = 0U;
o.output_dir = tempDir.path().toStdString();
o.extract_temp = true;
o.extract_unknown = true;
o.filenames.set_expand(true);
o.preserve_file_times = true; // also correctly closes file -> without it: on Windows the files are not written completely
try
{
if(errorText.isEmpty())
process_file(tmpFileExe.toStdString(), o, [this](float progress) {
ui->progressBarGog->setValue(progress * 100);
qApp->processEvents();
});
}
catch(const std::ios_base::failure & e)
{
errorText = tr("Stream error while extracting files!\nerror reason: ");
errorText += e.what();
}
catch(const format_error & e)
{
errorText = e.what();
}
catch(const std::runtime_error & e)
{
errorText = e.what();
}
catch(const setup::version_error &)
{
errorText = tr("Not a supported Inno Setup installer!");
}
if(errorText.isEmpty())
errorText = Innoextract::extract(tmpFileExe, tempDir.path(), [this](float progress) {
ui->progressBarGog->setValue(progress * 100);
qApp->processEvents();
});
ui->progressBarGog->setVisible(false);
ui->pushButtonGogInstall->setVisible(true);

62
launcher/innoextract.cpp Normal file
View File

@ -0,0 +1,62 @@
/*
* innoextract.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "innoextract.h"
#ifdef ENABLE_INNOEXTRACT
#include "cli/extract.hpp"
#include "setup/version.hpp"
#endif
QString Innoextract::extract(QString installer, QString outDir, std::function<void (float percent)> cb)
{
QString errorText{};
#ifdef ENABLE_INNOEXTRACT
::extract_options o;
o.extract = true;
// standard settings
o.gog_galaxy = true;
o.codepage = 0U;
o.output_dir = outDir.toStdString();
o.extract_temp = true;
o.extract_unknown = true;
o.filenames.set_expand(true);
o.preserve_file_times = true; // also correctly closes file -> without it: on Windows the files are not written completely
try
{
process_file(installer.toStdString(), o, cb);
}
catch(const std::ios_base::failure & e)
{
errorText = tr("Stream error while extracting files!\nerror reason: ");
errorText += e.what();
}
catch(const format_error & e)
{
errorText = e.what();
}
catch(const std::runtime_error & e)
{
errorText = e.what();
}
catch(const setup::version_error &)
{
errorText = tr("Not a supported Inno Setup installer!");
}
#else
errorText = tr("VCMI was compiled without innoextract support, which is needed to extract exe files!");
#endif
return errorText;
}

16
launcher/innoextract.h Normal file
View File

@ -0,0 +1,16 @@
/*
* innoextract.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#pragma once
class Innoextract : public QObject
{
public:
static QString extract(QString installer, QString outDir, std::function<void (float percent)> cb = nullptr);
};

View File

@ -14,36 +14,33 @@
#include "../../lib/VCMIDirs.h"
#include "../../lib/filesystem/CArchiveLoader.h"
#ifdef ENABLE_INNOEXTRACT
#include "cli/extract.hpp"
#include "setup/version.hpp"
#endif
#include "../innoextract.h"
ChroniclesExtractor::ChroniclesExtractor(QWidget *p, std::function<void(float percent)> cb) :
parent(p), cb(cb)
{
}
bool ChroniclesExtractor::handleTempDir(bool create)
bool ChroniclesExtractor::createTempDir()
{
if(create)
tempDir = QDir(pathToQString(VCMIDirs::get().userDataPath()));
if(tempDir.cd("tmp"))
{
tempDir = QDir(pathToQString(VCMIDirs::get().userDataPath()));
if(tempDir.cd("tmp"))
{
tempDir.removeRecursively(); // remove if already exists (e.g. previous run)
tempDir.cdUp();
}
tempDir.mkdir("tmp");
if(!tempDir.cd("tmp"))
return false; // should not happen - but avoid deleting wrong folder in any case
tempDir.removeRecursively(); // remove if already exists (e.g. previous run)
tempDir.cdUp();
}
else
tempDir.removeRecursively();
tempDir.mkdir("tmp");
if(!tempDir.cd("tmp"))
return false; // should not happen - but avoid deleting wrong folder in any case
return true;
}
void ChroniclesExtractor::removeTempDir()
{
tempDir.removeRecursively();
}
int ChroniclesExtractor::getChronicleNo(QFile & file)
{
if(!file.open(QIODevice::ReadOnly))
@ -79,58 +76,19 @@ int ChroniclesExtractor::getChronicleNo(QFile & file)
bool ChroniclesExtractor::extractGogInstaller(QString file)
{
#ifndef ENABLE_INNOEXTRACT
QMessageBox::critical(parent, tr("Innoextract functionality missing"), "VCMI was compiled without innoextract support, which is needed to extract chroncles!");
QString errorText = Innoextract::extract(file, tempDir.path(), [this](float progress) {
float overallProgress = ((1.0 / static_cast<float>(fileCount)) * static_cast<float>(extractionFile)) + (progress / static_cast<float>(fileCount));
if(cb)
cb(overallProgress);
});
if(!errorText.isEmpty())
{
QMessageBox::critical(parent, tr("Extracting error!"), errorText);
return false;
#else
::extract_options o;
o.extract = true;
}
// standard settings
o.gog_galaxy = true;
o.codepage = 0U;
o.output_dir = tempDir.path().toStdString();
o.extract_temp = true;
o.extract_unknown = true;
o.filenames.set_expand(true);
o.preserve_file_times = true; // also correctly closes file -> without it: on Windows the files are not written completely
QString errorText = "";
try
{
process_file(file.toStdString(), o, [this](float progress) {
float overallProgress = ((1.0 / static_cast<float>(fileCount)) * static_cast<float>(extractionFile)) + (progress / static_cast<float>(fileCount));
if(cb)
cb(overallProgress);
});
}
catch(const std::ios_base::failure & e)
{
errorText = tr("Stream error while extracting files!\nerror reason: ");
errorText += e.what();
}
catch(const format_error & e)
{
errorText = e.what();
}
catch(const std::runtime_error & e)
{
errorText = e.what();
}
catch(const setup::version_error &)
{
errorText = tr("Not a supported Inno Setup installer!");
}
if(!errorText.isEmpty())
{
QMessageBox::critical(parent, tr("Extracting error!"), errorText);
return false;
}
return true;
#endif
return true;
}
void ChroniclesExtractor::createBaseMod() const
@ -143,8 +101,8 @@ void ChroniclesExtractor::createBaseMod() const
QJsonObject mod
{
{ "modType", "Expansion" },
{ "name", "Heroes Chronicles" },
{ "description", "Heroes Chronicles" },
{ "name", tr("Heroes Chronicles") },
{ "description", tr("Heroes Chronicles") },
{ "author", "3DO" },
{ "version", "1.0" },
{ "contact", "vcmi.eu" },
@ -161,11 +119,14 @@ void ChroniclesExtractor::createChronicleMod(int no)
dir.removeRecursively();
dir.mkpath(".");
QByteArray tmpChronicles = chronicles.at(no);
tmpChronicles.replace('\0', "");
QJsonObject mod
{
{ "modType", "Expansion" },
{ "name", "Heroes Chronicles - " + QString::number(no) },
{ "description", "Heroes Chronicles - " + QString::number(no) },
{ "name", QString::number(no) + " - " + QString(tmpChronicles) },
{ "description", tr("Heroes Chronicles") + " - " + QString::number(no) + " - " + QString(tmpChronicles) },
{ "author", "3DO" },
{ "version", "1.0" },
{ "contact", "vcmi.eu" },
@ -250,7 +211,7 @@ void ChroniclesExtractor::installChronicles(QStringList exe)
if(!chronicleNo)
continue;
if(!handleTempDir(true))
if(!createTempDir())
continue;
if(!extractGogInstaller(f))
@ -259,6 +220,6 @@ void ChroniclesExtractor::installChronicles(QStringList exe)
createBaseMod();
createChronicleMod(chronicleNo);
handleTempDir(false);
removeTempDir();
}
}

View File

@ -22,7 +22,8 @@ class ChroniclesExtractor : public QObject
int extractionFile;
int fileCount;
bool handleTempDir(bool create);
bool createTempDir();
void removeTempDir();
int getChronicleNo(QFile & file);
bool extractGogInstaller(QString filePath);
void createBaseMod() const;