1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +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) {
errorText = Innoextract::extract(tmpFileExe, tempDir.path(), [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!");
}
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,19 +14,14 @@
#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)
{
if(create)
bool ChroniclesExtractor::createTempDir()
{
tempDir = QDir(pathToQString(VCMIDirs::get().userDataPath()));
if(tempDir.cd("tmp"))
@ -37,13 +32,15 @@ bool ChroniclesExtractor::handleTempDir(bool create)
tempDir.mkdir("tmp");
if(!tempDir.cd("tmp"))
return false; // should not happen - but avoid deleting wrong folder in any case
}
else
tempDir.removeRecursively();
return true;
}
void ChroniclesExtractor::removeTempDir()
{
tempDir.removeRecursively();
}
int ChroniclesExtractor::getChronicleNo(QFile & file)
{
if(!file.open(QIODevice::ReadOnly))
@ -79,49 +76,11 @@ 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!");
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) {
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);
});
}
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())
{
@ -130,7 +89,6 @@ bool ChroniclesExtractor::extractGogInstaller(QString file)
}
return true;
#endif
}
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;