Qt: Refactor out common generator code

This commit is contained in:
Vicki Pfau 2022-11-05 03:23:05 -07:00
parent f847502f4a
commit 8770200874
7 changed files with 38 additions and 66 deletions

View File

@ -191,7 +191,13 @@ void ForwarderController::gotBuild(QNetworkReply* reply) {
QByteArray data = reply->readAll();
m_sourceFile.write(data);
m_sourceFile.close();
m_generator->rebuild(m_sourceFile.fileName(), m_outFilename);
QString extracted = m_generator->extract(m_sourceFile.fileName());
if (extracted.isNull()) {
emit buildFailed();
return;
}
m_generator->rebuild(extracted, m_outFilename);
}
void ForwarderController::cleanup() {

View File

@ -10,6 +10,10 @@
#include "ForwarderGenerator3DS.h"
#include "ForwarderGeneratorVita.h"
#include "utils.h"
#include "VFileDevice.h"
#include <mgba-util/vfs.h>
using namespace QGBA;
@ -84,6 +88,29 @@ QString ForwarderGenerator::systemHumanName(ForwarderGenerator::System system) {
return {};
}
QString ForwarderGenerator::extract(const QString& archive) {
VDir* inArchive = VFileDevice::openArchive(archive);
if (!inArchive) {
return {};
}
bool gotFile = extractMatchingFile(inArchive, [this](VDirEntry* dirent) -> QString {
if (dirent->type(dirent) != VFS_FILE) {
return {};
}
QString filename(dirent->name(dirent));
if (!filename.endsWith("." + extension())) {
return {};
}
return "tmp." + extension();
});
inArchive->close(inArchive);
if (gotFile) {
return QLatin1String("tmp.") + extension();
}
return {};
}
QString ForwarderGenerator::base36(const QByteArray& bytes, int length) {
static const char* alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
QString buffer(length, 'X');

View File

@ -48,6 +48,7 @@ public:
static QString systemName(System);
static QString systemHumanName(System);
virtual QString extract(const QString& archive);
virtual void rebuild(const QString& source, const QString& target) = 0;
signals:

View File

@ -6,8 +6,6 @@
#include "ForwarderGenerator3DS.h"
#include "ConfigController.h"
#include "utils.h"
#include "VFileDevice.h"
#include <QDir>
#include <QFile>
@ -34,39 +32,11 @@ QList<QPair<QString, QSize>> ForwarderGenerator3DS::imageTypes() const {
}
void ForwarderGenerator3DS::rebuild(const QString& source, const QString& target) {
m_cia = dumpCia(source);
if (m_cia.isNull()) {
emit buildFailed();
return;
}
m_cia = source;
m_target = target;
extractCia();
}
QString ForwarderGenerator3DS::dumpCia(const QString& archive) {
VDir* inArchive = VFileDevice::openArchive(archive);
if (!inArchive) {
return {};
}
bool gotFile = extractMatchingFile(inArchive, [](VDirEntry* dirent) -> QString {
if (dirent->type(dirent) != VFS_FILE) {
return {};
}
QString filename(dirent->name(dirent));
if (!filename.endsWith(".cia")) {
return {};
}
return "tmp.cia";
});
inArchive->close(inArchive);
if (gotFile) {
return QLatin1String("tmp.cia");
}
return {};
}
void ForwarderGenerator3DS::extractCia() {
m_currentProc = std::make_unique<QProcess>();
m_currentProc->setProgram("ctrtool");

View File

@ -43,7 +43,6 @@ private slots:
void cleanup();
private:
QString dumpCia(const QString& archive);
void init3dstoolArgs(QStringList& args, const QString& file, const QString& createType = {});
std::unique_ptr<QProcess> m_currentProc;

View File

@ -8,7 +8,6 @@
#include <QFileInfo>
#include <QTemporaryFile>
#include "utils.h"
#include "VFileDevice.h"
#include <mgba-util/sfo.h>
@ -30,15 +29,9 @@ QList<QPair<QString, QSize>> ForwarderGeneratorVita::imageTypes() const {
}
void ForwarderGeneratorVita::rebuild(const QString& source, const QString& target) {
QString vpk = dumpVpk(source);
if (vpk.isNull()) {
emit buildFailed();
return;
}
QFile vpkFile(vpk);
QFile vpkFile(source);
VDir* outdir = VDirOpenZip(target.toLocal8Bit().constData(), O_WRONLY | O_CREAT | O_TRUNC);
if (outdir && !copyAssets(vpk, outdir)) {
if (outdir && !copyAssets(source, outdir)) {
outdir->close(outdir);
outdir = nullptr;
}
@ -79,29 +72,6 @@ void ForwarderGeneratorVita::rebuild(const QString& source, const QString& targe
emit buildComplete();
}
QString ForwarderGeneratorVita::dumpVpk(const QString& archive) {
VDir* inArchive = VFileDevice::openArchive(archive);
if (!inArchive) {
return {};
}
bool gotFile = extractMatchingFile(inArchive, [](VDirEntry* dirent) -> QString {
if (dirent->type(dirent) != VFS_FILE) {
return {};
}
QString filename(dirent->name(dirent));
if (!filename.endsWith(".vpk")) {
return {};
}
return "tmp.vpk";
});
inArchive->close(inArchive);
if (gotFile) {
return QLatin1String("tmp.vpk");
}
return {};
}
bool ForwarderGeneratorVita::copyAssets(const QString& vpk, VDir* outdir) {
VDir* indir = VDirOpenZip(vpk.toLocal8Bit().constData(), O_RDONLY);
if (!indir) {

View File

@ -25,7 +25,6 @@ public:
void rebuild(const QString& source, const QString& target) override;
private:
QString dumpVpk(const QString& archive);
bool copyAssets(const QString& vpk, VDir* out);
QString makeSerial() const;
void writeSfo(VFile* out);