Files
ppsspp/Common/File/VFS/VFS.h

84 lines
3.0 KiB
C
Raw Permalink Normal View History

2012-11-27 16:38:24 +01:00
#pragma once
2012-03-24 23:39:19 +01:00
#include <vector>
2023-03-06 14:37:11 +01:00
#include <cstdint>
#include "Common/File/DirListing.h"
2023-03-06 14:37:11 +01:00
// Basic read-only virtual file system. Used to manage assets on Android, where we have to
2012-03-31 11:16:13 +02:00
// read them manually out of the APK zipfile, while being able to run on other
// platforms as well with the appropriate directory set-up.
2023-03-06 14:37:11 +01:00
// 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.
2023-03-06 17:30:13 +01:00
// VFSBackend instances can be used on their own, without the VFS, to serve as an abstraction of
// a single directory or ZIP file.
// The VFSFileReference level of abstraction is there to hold things like zip file indices,
// for fast re-open etc.
class VFSFileReference {
public:
virtual ~VFSFileReference() {}
};
class VFSOpenFile {
public:
virtual ~VFSOpenFile() {}
};
// Common interface parts between VFSBackend and VFS.
// Sometimes you don't need the VFS multiplexing and only have a VFSBackend *, sometimes you do need it,
// and it would be cool to be able to use the same interface, like when loading INI files.
class VFSInterface {
2023-03-06 14:29:47 +01:00
public:
virtual ~VFSInterface() {}
2023-03-06 15:30:39 +01:00
virtual uint8_t *ReadFile(const char *path, size_t *size) = 0;
// If listing already contains files, it'll be cleared.
virtual bool GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter = nullptr) = 0;
};
class VFSBackend : public VFSInterface {
public:
2023-03-06 17:30:13 +01:00
virtual VFSFileReference *GetFile(const char *path) = 0;
virtual bool GetFileInfo(VFSFileReference *vfsReference, File::FileInfo *fileInfo) = 0;
virtual void ReleaseFile(VFSFileReference *vfsReference) = 0;
2023-03-06 17:30:13 +01:00
// Must write the size of the file to *size. Both backends can do this efficiently here,
// avoiding a call to GetFileInfo.
virtual VFSOpenFile *OpenFileForRead(VFSFileReference *vfsReference, size_t *size) = 0;
virtual void Rewind(VFSOpenFile *vfsOpenFile) = 0;
virtual size_t Read(VFSOpenFile *vfsOpenFile, void *buffer, size_t length) = 0;
virtual void CloseFile(VFSOpenFile *vfsOpenFile) = 0;
2023-03-06 17:30:13 +01:00
2023-03-06 14:29:47 +01:00
// Filter support is optional but nice to have
virtual bool GetFileInfo(const char *path, File::FileInfo *info) = 0;
virtual std::string toString() const = 0;
};
2012-03-24 23:39:19 +01:00
class VFS : public VFSInterface {
public:
2023-03-06 14:37:11 +01:00
~VFS() { Clear(); }
2023-03-06 15:30:39 +01:00
void Register(const char *prefix, VFSBackend *reader);
void Clear();
2012-03-24 23:39:19 +01:00
// Use delete [] to release the returned memory.
// Always allocates an extra zero byte at the end, so that it
// can be used for text like shader sources.
uint8_t *ReadFile(const char *filename, size_t *size) override;
bool GetFileInfo(const char *filename, File::FileInfo *fileInfo);
bool GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter = nullptr) override;
private:
struct VFSEntry {
const char *prefix;
2023-03-06 15:30:39 +01:00
VFSBackend *reader;
};
2023-03-06 14:37:11 +01:00
std::vector<VFSEntry> entries_;
};
extern VFS g_VFS;