CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/File/VFS/VFS.h
Views: 1401
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 std::string toString() const = 0;
60
};
61
62
class VFS : public VFSInterface {
63
public:
64
~VFS() { Clear(); }
65
void Register(const char *prefix, VFSBackend *reader);
66
void Clear();
67
68
// Use delete [] to release the returned memory.
69
// Always allocates an extra zero byte at the end, so that it
70
// can be used for text like shader sources.
71
uint8_t *ReadFile(const char *filename, size_t *size) override;
72
bool GetFileInfo(const char *filename, File::FileInfo *fileInfo);
73
bool GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter = nullptr) override;
74
75
private:
76
struct VFSEntry {
77
const char *prefix;
78
VFSBackend *reader;
79
};
80
std::vector<VFSEntry> entries_;
81
};
82
83
extern VFS g_VFS;
84
85