Use a vector internally in VFS.

This commit is contained in:
Henrik Rydgård 2023-03-06 14:37:11 +01:00
parent 7649794164
commit 54af240013
3 changed files with 28 additions and 26 deletions

View File

@ -2,10 +2,8 @@
#include <string>
#include <vector>
#include <cstdio>
#include <inttypes.h>
#include <cstdint>
#include "Common/File/Path.h"

View File

@ -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<File::FileInfo> *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;

View File

@ -1,18 +1,25 @@
#pragma once
#include <vector>
#include <cstdint>
#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<File::FileInfo> *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<VFSEntry> entries_;
};
extern VFS g_VFS;