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