Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/File/VFS/VFS.h
3187 views
1
#pragma once
2
3
#include <vector>
4
#include <cstdint>
5
6
#include "Common/File/DirListing.h"
7
8
// Basic read-only virtual file system. Used to manage assets on Android, where we have to
9
// read them manually out of the APK zipfile, while being able to run on other
10
// platforms as well with the appropriate directory set-up.
11
12
// Note that this is kinda similar in concept to Core/MetaFileSystem.h, but that one
13
// is specifically for operations done by the emulated PSP, while this is for operations
14
// on the system level, like loading assets, and maybe texture packs. Also, as mentioned,
15
// this one is read-only, so a bit smaller and simpler.
16
17
// VFSBackend instances can be used on their own, without the VFS, to serve as an abstraction of
18
// a single directory or ZIP file.
19
20
// The VFSFileReference level of abstraction is there to hold things like zip file indices,
21
// for fast re-open etc.
22
23
class VFSFileReference {
24
public:
25
virtual ~VFSFileReference() {}
26
};
27
28
class VFSOpenFile {
29
public:
30
virtual ~VFSOpenFile() {}
31
};
32
33
// Common interface parts between VFSBackend and VFS.
34
// Sometimes you don't need the VFS multiplexing and only have a VFSBackend *, sometimes you do need it,
35
// and it would be cool to be able to use the same interface, like when loading INI files.
36
class VFSInterface {
37
public:
38
virtual ~VFSInterface() {}
39
virtual uint8_t *ReadFile(const char *path, size_t *size) = 0;
40
// If listing already contains files, it'll be cleared.
41
virtual bool GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter = nullptr) = 0;
42
};
43
44
class VFSBackend : public VFSInterface {
45
public:
46
virtual VFSFileReference *GetFile(const char *path) = 0;
47
virtual bool GetFileInfo(VFSFileReference *vfsReference, File::FileInfo *fileInfo) = 0;
48
virtual void ReleaseFile(VFSFileReference *vfsReference) = 0;
49
50
// Must write the size of the file to *size. Both backends can do this efficiently here,
51
// avoiding a call to GetFileInfo.
52
virtual VFSOpenFile *OpenFileForRead(VFSFileReference *vfsReference, size_t *size) = 0;
53
virtual void Rewind(VFSOpenFile *vfsOpenFile) = 0;
54
virtual size_t Read(VFSOpenFile *vfsOpenFile, void *buffer, size_t length) = 0;
55
virtual void CloseFile(VFSOpenFile *vfsOpenFile) = 0;
56
57
// Filter support is optional but nice to have
58
virtual bool GetFileInfo(const char *path, File::FileInfo *info) = 0;
59
virtual bool Exists(const char *path) {
60
File::FileInfo info{};
61
return GetFileInfo(path, &info) && info.exists;
62
}
63
64
virtual std::string toString() const = 0;
65
};
66
67
class VFS : public VFSInterface {
68
public:
69
~VFS() { Clear(); }
70
void Register(const char *prefix, VFSBackend *reader);
71
void Clear();
72
73
// Use delete [] to release the returned memory.
74
// Always allocates an extra zero byte at the end, so that it
75
// can be used for text like shader sources.
76
uint8_t *ReadFile(const char *filename, size_t *size) override;
77
bool GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter = nullptr) override;
78
79
bool GetFileInfo(const char *filename, File::FileInfo *fileInfo);
80
bool Exists(const char *path);
81
82
private:
83
struct VFSEntry {
84
const char *prefix;
85
VFSBackend *reader;
86
};
87
std::vector<VFSEntry> entries_;
88
};
89
90
extern VFS g_VFS;
91
92