mirror of https://github.com/mgba-emu/mgba.git
Qt: Pay down some VFile technical debt
This commit is contained in:
parent
2a5417e3ce
commit
472660a5d3
|
@ -31,6 +31,7 @@ void CoreManager::setMultiplayerController(MultiplayerController* multiplayer) {
|
|||
CoreController* CoreManager::loadGame(const QString& path) {
|
||||
QFileInfo info(path);
|
||||
if (!info.isReadable()) {
|
||||
// Open specific file in archive
|
||||
QString fname = info.fileName();
|
||||
QString base = info.path();
|
||||
if (base.endsWith("/") || base.endsWith(QDir::separator())) {
|
||||
|
@ -40,13 +41,8 @@ CoreController* CoreManager::loadGame(const QString& path) {
|
|||
if (dir) {
|
||||
VFile* vf = dir->openFile(dir, fname.toUtf8().constData(), O_RDONLY);
|
||||
if (vf) {
|
||||
struct VFile* vfclone = VFileMemChunk(NULL, vf->size(vf));
|
||||
uint8_t buffer[2048];
|
||||
ssize_t read;
|
||||
while ((read = vf->read(vf, buffer, sizeof(buffer))) > 0) {
|
||||
vfclone->write(vfclone, buffer, read);
|
||||
}
|
||||
vf->close(vf);
|
||||
struct VFile* vfclone = VFileDevice::openMemory(vf->size(vf));
|
||||
VFileDevice::copyFile(vf, vfclone);
|
||||
vf = vfclone;
|
||||
}
|
||||
dir->close(dir);
|
||||
|
@ -59,15 +55,16 @@ CoreController* CoreManager::loadGame(const QString& path) {
|
|||
VFile* vf = nullptr;
|
||||
VDir* archive = VDirOpenArchive(path.toUtf8().constData());
|
||||
if (archive) {
|
||||
// Open first file in archive
|
||||
VFile* vfOriginal = VDirFindFirst(archive, [](VFile* vf) {
|
||||
return mCoreIsCompatible(vf) != mPLATFORM_NONE;
|
||||
});
|
||||
if (vfOriginal) {
|
||||
ssize_t size = vfOriginal->size(vfOriginal);
|
||||
if (size > 0) {
|
||||
void* mem = vfOriginal->map(vfOriginal, size, MAP_READ);
|
||||
vf = VFileMemChunk(mem, size);
|
||||
vfOriginal->unmap(vfOriginal, mem, size);
|
||||
struct VFile* vfclone = VFileDevice::openMemory(vfOriginal->size(vfOriginal));
|
||||
VFileDevice::copyFile(vfOriginal, vfclone);
|
||||
vf = vfclone;
|
||||
}
|
||||
vfOriginal->close(vfOriginal);
|
||||
}
|
||||
|
@ -75,6 +72,7 @@ CoreController* CoreManager::loadGame(const QString& path) {
|
|||
}
|
||||
QDir dir(info.dir());
|
||||
if (!vf) {
|
||||
// Open bare file
|
||||
vf = VFileOpen(info.canonicalFilePath().toUtf8().constData(), O_RDONLY);
|
||||
}
|
||||
return loadGame(vf, info.fileName(), dir.canonicalPath());
|
||||
|
|
|
@ -172,8 +172,8 @@ VFile* VFileDevice::open(const QString& path, int mode) {
|
|||
return VFileOpen(path.toUtf8().constData(), mode);
|
||||
}
|
||||
|
||||
VFile* VFileDevice::openMemory() {
|
||||
return VFileMemChunk(nullptr, 0);
|
||||
VFile* VFileDevice::openMemory(quint64 size) {
|
||||
return VFileMemChunk(nullptr, size);
|
||||
}
|
||||
|
||||
VDir* VFileDevice::openDir(const QString& path) {
|
||||
|
@ -186,6 +186,10 @@ VDir* VFileDevice::openArchive(const QString& path) {
|
|||
|
||||
bool VFileDevice::copyFile(VFile* input, VFile* output) {
|
||||
uint8_t buffer[0x800];
|
||||
|
||||
input->seek(input, 0, SEEK_SET);
|
||||
output->seek(output, 0, SEEK_SET);
|
||||
|
||||
ssize_t size;
|
||||
while ((size = input->read(input, buffer, sizeof(buffer))) > 0) {
|
||||
output->write(output, buffer, size);
|
||||
|
|
|
@ -38,7 +38,7 @@ public:
|
|||
static VFile* wrap(QBuffer*, QIODevice::OpenMode);
|
||||
|
||||
static VFile* open(const QString& path, int mode);
|
||||
static VFile* openMemory();
|
||||
static VFile* openMemory(quint64 size = 0);
|
||||
static VDir* openDir(const QString& path);
|
||||
static VDir* openArchive(const QString& path);
|
||||
|
||||
|
|
Loading…
Reference in New Issue