From 472660a5d3429b1979a10fe45922a4173e08a910 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sun, 30 Oct 2022 02:48:39 -0700 Subject: [PATCH] Qt: Pay down some VFile technical debt --- src/platform/qt/CoreManager.cpp | 18 ++++++++---------- src/platform/qt/VFileDevice.cpp | 8 ++++++-- src/platform/qt/VFileDevice.h | 2 +- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/platform/qt/CoreManager.cpp b/src/platform/qt/CoreManager.cpp index b5fc3e413..cac1c2560 100644 --- a/src/platform/qt/CoreManager.cpp +++ b/src/platform/qt/CoreManager.cpp @@ -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()); diff --git a/src/platform/qt/VFileDevice.cpp b/src/platform/qt/VFileDevice.cpp index 9ef822cde..18231a263 100644 --- a/src/platform/qt/VFileDevice.cpp +++ b/src/platform/qt/VFileDevice.cpp @@ -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); diff --git a/src/platform/qt/VFileDevice.h b/src/platform/qt/VFileDevice.h index 2f0a94662..15f79bbcb 100644 --- a/src/platform/qt/VFileDevice.h +++ b/src/platform/qt/VFileDevice.h @@ -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);