still WIP, but EVEN BETTER!

master
Tibor 1 year ago
parent 51551fcc8d
commit 3e95ac6265

@ -6,6 +6,7 @@
#include <QFileDialog>
#include <QFile>
#include <QProcess>
#include <QDebug>
#include <vector>
#include <string>
@ -89,7 +90,7 @@ void GiantsWdefImporter::revertFile(const std::filesystem::path& path)
}
// TODO implement error handling
fs::copy(dest, path);
fs::copy(dest, path, fs::copy_options::overwrite_existing);
}
void GiantsWdefImporter::setParent(QObject* parent)
@ -99,6 +100,8 @@ void GiantsWdefImporter::setParent(QObject* parent)
QByteArray GiantsWdefImporter::newExeBytes(const QByteArray& wdefsBytes, const std::filesystem::path& giantsMainPath)
{
// Look for checksum in exe file
QByteArray checksum { "\x78\x05\x44\xb6" }; // should be at offset 0x152bff in giantsmain.exe
QFile exeFile { QString::fromStdString(giantsMainPath.string()) };
@ -128,7 +131,9 @@ QByteArray GiantsWdefImporter::newExeBytes(const QByteArray& wdefsBytes, const s
GiantsWdefImporter::StartResult GiantsWdefImporter::startGiantsWithWdefsBytes(QByteArray const& wdefsBytes, fs::path const& wdefsPath, fs::path const& giantsMainPath, QPushButton* button)
{
// switching to QFile here from filesystem for ease of use
// Try writing the wdefs file
// Switching to QFile here from filesystem for ease of use
QFile wdefsFile(QString::fromStdString(wdefsPath.string()));
wdefsFile.open(QIODevice::WriteOnly);
@ -138,6 +143,8 @@ GiantsWdefImporter::StartResult GiantsWdefImporter::startGiantsWithWdefsBytes(QB
return StartResult::FAILURE;
}
// Try writing the exe file
QByteArray newBytes = newExeBytes(wdefsBytes, giantsMainPath);
QFile exeFile(QString::fromStdString(giantsMainPath.string()));
@ -149,6 +156,14 @@ GiantsWdefImporter::StartResult GiantsWdefImporter::startGiantsWithWdefsBytes(QB
return StartResult::FAILURE;
}
// Try starting GiantsMain.exe
if (QOperatingSystemVersion::Windows != QOperatingSystemVersion::currentType()) {
QMessageBox::warning(nullptr, "wdefs", "For now, this program only works on Windows. Please consider contacting taibsu#1841 or Amazed#0001 in the GiantsWD Discord channel for further usage.");
return StartResult::FAILURE;
}
auto giantsFinished = [&]() {
revertFile(giantsMainPath);
revertFile(wdefsPath);
@ -161,20 +176,26 @@ GiantsWdefImporter::StartResult GiantsWdefImporter::startGiantsWithWdefsBytes(QB
QProcess process(_parent);
QObject::connect(&process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), [&]() {
giantsFinished();
QObject::connect(&process, &QProcess::stateChanged, [&](QProcess::ProcessState state) {
switch (state) {
case QProcess::ProcessState::Running:
qDebug() << "Running";
button->setText("Game started");
break;
default:
button->setText("Game not running");
break;
}
});
process.setWorkingDirectory(QString::fromStdString(giantsMainPath.parent_path().string()));
if (QOperatingSystemVersion::Windows != QOperatingSystemVersion::currentType()) {
QMessageBox::warning(nullptr, "wdefs", "For now, this program only works on Windows. Please consider contacting taibsu#1841 or Amazed#0001 in the GiantsWD Discord channel for further usage.");
return StartResult::FAILURE;
}
button->setText("Game started");
QObject::connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[&]() { giantsFinished(); }
);
QObject::connect(&process, &QProcess::errorOccurred,
[&]() { giantsFinished(); }
);
process.setWorkingDirectory(QString::fromStdString(giantsMainPath.parent_path().string()));
process.start(QString::fromStdString(giantsMainPath.string()), { "-launcher" });
return StartResult::SUCCESS;

@ -15,8 +15,9 @@ MainWindow::MainWindow(QWidget *parent)
{
ui->setupUi(this);
_importer.setParent(this);
// Set paths
_importer.setParent(this);
_giantsPath = _importer.locateGiantsFolder();
if (_giantsPath.empty()) {
@ -31,8 +32,6 @@ MainWindow::MainWindow(QWidget *parent)
_wdefsPath = _giantsPath / "Bin" / "wdefs.bin";
_giantsMainPath = _giantsPath / "GiantsMain.exe";
QMetaObject::connectSlotsByName(this);
}
MainWindow::~MainWindow()
@ -60,20 +59,23 @@ void MainWindow::on_pushButton_clicked()
return;
}
QNetworkAccessManager manager;
QNetworkRequest request(text);
QNetworkAccessManager manager(this);
QByteArray requestReply {};
QNetworkReply* reply { manager.get(request) };
QObject::connect(&manager, &QNetworkAccessManager::finished, [&](QNetworkReply* reply) {
if (!reply || !reply->isReadable() || reply->readAll().isEmpty()) {
QMessageBox::critical(nullptr, "wdefs", "No reply from given URL.");
return;
}
if (!reply) {
QMessageBox::critical(nullptr, "wdefs", "Could not download wdefs at URL.");
qDebug() << reply->error();
return;
}
requestReply = reply->readAll();
});
QByteArray answer { reply->readAll() };
manager.get(QNetworkRequest(QUrl(text)));
GiantsWdefImporter::StartResult giantsStartResult = _importer.startGiantsWithWdefsBytes(answer, _wdefsPath, _giantsMainPath, ui->pushButton);
GiantsWdefImporter::StartResult giantsStartResult = _importer.startGiantsWithWdefsBytes(requestReply, _wdefsPath, _giantsMainPath, ui->pushButton);
if (GiantsWdefImporter::StartResult::SUCCESS != giantsStartResult) {
_importer.revertFile(_giantsMainPath);

Loading…
Cancel
Save