Qt: Split out some helper functions

This commit is contained in:
Vicki Pfau 2022-10-31 19:46:56 -07:00
parent 9ed00c95b6
commit 3f24047abb
5 changed files with 53 additions and 27 deletions

View File

@ -72,3 +72,19 @@ QString ForwarderGenerator::systemName(ForwarderGenerator::System system) {
return {};
}
QString ForwarderGenerator::base36(const QByteArray& bytes, int length) {
static const char* alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
QString buffer(length, 'X');
quint32 running = 0;
for (int i = 0, j = 0; i < length; ++i) {
if (running < 36) {
running <<= 8;
running |= static_cast<quint8>(bytes[j]);
++j;
}
buffer[i] = alphabet[running % 36];
running /= 36;
}
return buffer;
}

View File

@ -48,6 +48,8 @@ public:
protected:
ForwarderGenerator(int imageTypes, QObject* parent = nullptr);
static QString base36(const QByteArray&, int length);
private:
QString m_title;
QString m_romPath;

View File

@ -8,6 +8,7 @@
#include <QFileInfo>
#include <QTemporaryFile>
#include "utils.h"
#include "VFileDevice.h"
#include <mgba-util/sfo.h>
@ -77,29 +78,20 @@ bool ForwarderGeneratorVita::rebuild(const QString& source, const QString& targe
}
QString ForwarderGeneratorVita::dumpVpk(const QString& archive) {
bool gotFile = false;
VDir* inArchive = VFileDevice::openArchive(archive);
if (!inArchive) {
return {};
}
for (VDirEntry* dirent = inArchive->listNext(inArchive); dirent; dirent = inArchive->listNext(inArchive)) {
bool gotFile = extractMatchingFile(inArchive, [](VDirEntry* dirent) -> QString {
if (dirent->type(dirent) != VFS_FILE) {
continue;
return {};
}
QString filename(dirent->name(dirent));
if (!filename.endsWith(".vpk")) {
continue;
return {};
}
VFile* outfile = VFileOpen("tmp.vpk", O_WRONLY | O_TRUNC | O_CREAT);
VFile* vpk = inArchive->openFile(inArchive, dirent->name(dirent), O_RDONLY);
VFileDevice::copyFile(vpk, outfile);
vpk->close(vpk);
outfile->close(outfile);
gotFile = true;
break;
}
return "tmp.vpk";
});
inArchive->close(inArchive);
if (gotFile) {
@ -156,16 +148,8 @@ bool ForwarderGeneratorVita::copyAssets(const QString& vpk, VDir* outdir) {
}
QString ForwarderGeneratorVita::makeSerial() const {
QByteArray hash = hashRom();
quint32 hashBits = (hash[0] << 24) | (hash[1] << 16) | (hash[2] << 8) | hash[3];
QString serial("MFXXXXXXX");
for (int i = 0; i < 7; ++i) {
static const char alphabet[37] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
serial[i + 2] = alphabet[hashBits % 36];
hashBits /= 36;
}
QString serial("MF");
serial += base36(hashRom(), 7);
return serial;
}

View File

@ -1,15 +1,17 @@
/* Copyright (c) 2013-2017 Jeffrey Pfau
/* Copyright (c) 2013-2022 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "utils.h"
#include <mgba/core/version.h>
#include <QCoreApplication>
#include <QObject>
#include "VFileDevice.h"
#include <mgba/core/version.h>
namespace QGBA {
QString niceSizeFormat(size_t filesize) {
@ -111,4 +113,20 @@ QString romFilters(bool includeMvl) {
return filters.join(";;");
}
bool extractMatchingFile(VDir* dir, std::function<QString (VDirEntry*)> filter) {
for (VDirEntry* entry = dir->listNext(dir); entry; entry = dir->listNext(dir)) {
QString target = filter(entry);
if (target.isNull()) {
continue;
}
VFile* outfile = VFileOpen(target.toUtf8().constData(), O_WRONLY | O_TRUNC | O_CREAT);
VFile* infile = dir->openFile(dir, entry->name(entry), O_RDONLY);
VFileDevice::copyFile(infile, outfile);
infile->close(infile);
outfile->close(outfile);
return true;
}
return false;
}
}

View File

@ -7,6 +7,7 @@
#include <mgba/core/core.h>
#include <mgba-util/socket.h>
#include <mgba-util/vfs.h>
#include <QHostAddress>
#include <QRect>
@ -14,6 +15,10 @@
#include <QString>
#include <algorithm>
#include <functional>
struct VDir;
struct VDirEntry;
namespace QGBA {
@ -68,5 +73,6 @@ constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
#endif
QString romFilters(bool includeMvl = false);
bool extractMatchingFile(VDir* dir, std::function<QString (VDirEntry*)> filter);
}