From 54af2400135e8fef2639ecbc09a2cf1d2ea401b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 6 Mar 2023 14:37:11 +0100 Subject: [PATCH] Use a vector internally in VFS. --- Common/File/DirListing.h | 4 +--- Common/File/VFS/VFS.cpp | 34 ++++++++++++++++------------------ Common/File/VFS/VFS.h | 16 +++++++++++----- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/Common/File/DirListing.h b/Common/File/DirListing.h index c53d420588..4ad9ce0c3e 100644 --- a/Common/File/DirListing.h +++ b/Common/File/DirListing.h @@ -2,10 +2,8 @@ #include #include - #include - -#include +#include #include "Common/File/Path.h" diff --git a/Common/File/VFS/VFS.cpp b/Common/File/VFS/VFS.cpp index 3dc9c66a1f..e765c9ca78 100644 --- a/Common/File/VFS/VFS.cpp +++ b/Common/File/VFS/VFS.cpp @@ -6,17 +6,15 @@ VFS g_VFS; void VFS::Register(const char *prefix, AssetReader *reader) { - entries_[numEntries_].prefix = prefix; - entries_[numEntries_].reader = reader; + entries_.push_back(VFSEntry{ prefix, reader }); DEBUG_LOG(IO, "Registered VFS for prefix %s: %s", prefix, reader->toString().c_str()); - numEntries_++; } void VFS::Clear() { - for (int i = 0; i < numEntries_; i++) { - delete entries_[i].reader; + for (auto &entry : entries_) { + delete entry.reader; } - numEntries_ = 0; + entries_.clear(); } // TODO: Use Path more. @@ -41,13 +39,13 @@ uint8_t *VFS::ReadFile(const char *filename, size_t *size) { int fn_len = (int)strlen(filename); bool fileSystemFound = false; - for (int i = 0; i < numEntries_; i++) { - int prefix_len = (int)strlen(entries_[i].prefix); + for (const auto &entry : entries_) { + int prefix_len = (int)strlen(entry.prefix); if (prefix_len >= fn_len) continue; - if (0 == memcmp(filename, entries_[i].prefix, prefix_len)) { + if (0 == memcmp(filename, entry.prefix, prefix_len)) { fileSystemFound = true; // INFO_LOG(IO, "Prefix match: %s (%s) -> %s", entries[i].prefix, filename, filename + prefix_len); - uint8_t *data = entries_[i].reader->ReadAsset(filename + prefix_len, size); + uint8_t *data = entry.reader->ReadAsset(filename + prefix_len, size); if (data) return data; else @@ -71,12 +69,12 @@ bool VFS::GetFileListing(const char *path, std::vector *listing, int fn_len = (int)strlen(path); bool fileSystemFound = false; - for (int i = 0; i < numEntries_; i++) { - int prefix_len = (int)strlen(entries_[i].prefix); + for (const auto &entry : entries_) { + int prefix_len = (int)strlen(entry.prefix); if (prefix_len >= fn_len) continue; - if (0 == memcmp(path, entries_[i].prefix, prefix_len)) { + if (0 == memcmp(path, entry.prefix, prefix_len)) { fileSystemFound = true; - if (entries_[i].reader->GetFileListing(path + prefix_len, listing, filter)) { + if (entry.reader->GetFileListing(path + prefix_len, listing, filter)) { return true; } } @@ -97,12 +95,12 @@ bool VFS::GetFileInfo(const char *path, File::FileInfo *info) { bool fileSystemFound = false; int fn_len = (int)strlen(path); - for (int i = 0; i < numEntries_; i++) { - int prefix_len = (int)strlen(entries_[i].prefix); + for (const auto &entry : entries_) { + int prefix_len = (int)strlen(entry.prefix); if (prefix_len >= fn_len) continue; - if (0 == memcmp(path, entries_[i].prefix, prefix_len)) { + if (0 == memcmp(path, entry.prefix, prefix_len)) { fileSystemFound = true; - if (entries_[i].reader->GetFileInfo(path + prefix_len, info)) + if (entry.reader->GetFileInfo(path + prefix_len, info)) return true; else continue; diff --git a/Common/File/VFS/VFS.h b/Common/File/VFS/VFS.h index c6a2479e80..41ac6ae3f9 100644 --- a/Common/File/VFS/VFS.h +++ b/Common/File/VFS/VFS.h @@ -1,18 +1,25 @@ #pragma once #include +#include #include "Common/File/DirListing.h" -// Basic virtual file system. Used to manage assets on Android, where we have to +// Basic read-only virtual file system. Used to manage assets on Android, where we have to // read them manually out of the APK zipfile, while being able to run on other // platforms as well with the appropriate directory set-up. +// Note that this is kinda similar in concept to Core/MetaFileSystem.h, but that one +// is specifically for operations done by the emulated PSP, while this is for operations +// on the system level, like loading assets, and maybe texture packs. Also, as mentioned, +// this one is read-only, so a bit smaller and simpler. + class AssetReader { public: virtual ~AssetReader() {} - // use delete[] + // use delete[] to release the returned memory. virtual uint8_t *ReadAsset(const char *path, size_t *size) = 0; + // Filter support is optional but nice to have virtual bool GetFileListing(const char *path, std::vector *listing, const char *filter = 0) = 0; virtual bool GetFileInfo(const char *path, File::FileInfo *info) = 0; @@ -21,6 +28,7 @@ public: class VFS { public: + ~VFS() { Clear(); } void Register(const char *prefix, AssetReader *reader); void Clear(); @@ -36,9 +44,7 @@ private: const char *prefix; AssetReader *reader; }; - - VFSEntry entries_[16]; - int numEntries_ = 0; + std::vector entries_; }; extern VFS g_VFS;