Skip to content

Commit

Permalink
VFS: Add boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Mar 7, 2023
1 parent 9fe7eab commit 23b310f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 1 deletion.
36 changes: 36 additions & 0 deletions Common/File/VFS/DirectoryReader.cpp
Expand Up @@ -29,3 +29,39 @@ bool DirectoryReader::GetFileInfo(const char *path, File::FileInfo *info) {
Path new_path = Path(path).StartsWith(path_) ? Path(path) : path_ / path;
return File::GetFileInfo(new_path, info);
}

class DirectoryReaderFileReference : public VFSFileReference {
public:
Path path;
};

class DirectoryReaderOpenFile : public VFSOpenFile {
public:
FILE *file;
};

VFSFileReference *DirectoryReader::GetFile(const char *path) {
return nullptr;
}

void DirectoryReader::ReleaseFile(VFSFileReference *reference) {
DirectoryReaderFileReference *file = (DirectoryReaderFileReference *)reference;
}

VFSOpenFile *DirectoryReader::OpenFileForRead(VFSFileReference *reference) {
DirectoryReaderFileReference *file = (DirectoryReaderFileReference *)reference;
return nullptr;
}

void DirectoryReader::Rewind(VFSOpenFile *openFile) {
DirectoryReaderOpenFile *file = (DirectoryReaderOpenFile *)openFile;
}

size_t DirectoryReader::Read(VFSOpenFile *openFile, uint8_t *buffer, size_t length) {
DirectoryReaderOpenFile *file = (DirectoryReaderOpenFile *)openFile;
return 0;
}

void DirectoryReader::CloseFile(VFSOpenFile *openFile) {
DirectoryReaderOpenFile *file = (DirectoryReaderOpenFile *)openFile;
}
10 changes: 9 additions & 1 deletion Common/File/VFS/DirectoryReader.h
Expand Up @@ -9,6 +9,15 @@ class DirectoryReader : public VFSBackend {
explicit DirectoryReader(const Path &path);
// use delete[] on the returned value.
uint8_t *ReadFile(const char *path, size_t *size) override;

virtual VFSFileReference *GetFile(const char *path) override;
virtual void ReleaseFile(VFSFileReference *reference) override;

virtual VFSOpenFile *OpenFileForRead(VFSFileReference *reference) override;
virtual void Rewind(VFSOpenFile *openFile) override;
virtual size_t Read(VFSOpenFile *openFile, uint8_t *buffer, size_t length) override;
virtual void CloseFile(VFSOpenFile *openFile) override;

bool GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter) override;
bool GetFileInfo(const char *path, File::FileInfo *info) override;
std::string toString() const override {
Expand All @@ -18,4 +27,3 @@ class DirectoryReader : public VFSBackend {
private:
Path path_;
};

24 changes: 24 additions & 0 deletions Common/File/VFS/VFS.h
Expand Up @@ -14,6 +14,22 @@
// 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.

// 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 inteface 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.
Expand All @@ -27,6 +43,14 @@ class VFSBackend : public VFSInterface {
public:
// use delete[] to release the returned memory.

virtual VFSFileReference *GetFile(const char *path) = 0;
virtual void ReleaseFile(VFSFileReference *file) = 0;

virtual VFSOpenFile *OpenFileForRead(VFSFileReference *reference) = 0;
virtual void Rewind(VFSOpenFile *file) = 0;
virtual size_t Read(VFSOpenFile *file, uint8_t *buffer, size_t length) = 0;
virtual void CloseFile(VFSOpenFile *file) = 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;
Expand Down
34 changes: 34 additions & 0 deletions Common/File/VFS/ZipFileReader.cpp
Expand Up @@ -164,3 +164,37 @@ bool ZipFileReader::GetFileInfo(const char *path, File::FileInfo *info) {
info->size = zstat.size;
return true;
}

class ZipFileReaderFileReference : public VFSFileReference {
public:
};

class ZipFileReaderOpenFile : public VFSOpenFile {
public:
};

VFSFileReference *ZipFileReader::GetFile(const char *path) {
return nullptr;
}

void ZipFileReader::ReleaseFile(VFSFileReference *reference) {
ZipFileReaderFileReference *file = (ZipFileReaderFileReference *)reference;
}

VFSOpenFile *ZipFileReader::OpenFileForRead(VFSFileReference *reference) {
ZipFileReaderFileReference *file = (ZipFileReaderFileReference *)reference;
return nullptr;
}

void ZipFileReader::Rewind(VFSOpenFile *openFile) {
ZipFileReaderOpenFile *file = (ZipFileReaderOpenFile *)openFile;
}

size_t ZipFileReader::Read(VFSOpenFile *openFile, uint8_t *buffer, size_t length) {
ZipFileReaderOpenFile *file = (ZipFileReaderOpenFile *)openFile;
return 0;
}

void ZipFileReader::CloseFile(VFSOpenFile *openFile) {
ZipFileReaderOpenFile *file = (ZipFileReaderOpenFile *)openFile;
}
9 changes: 9 additions & 0 deletions Common/File/VFS/ZipFileReader.h
Expand Up @@ -20,6 +20,15 @@ class ZipFileReader : public VFSBackend {
~ZipFileReader();
// use delete[] on the returned value.
uint8_t *ReadFile(const char *path, size_t *size) override;

VFSFileReference *GetFile(const char *path) override;
void ReleaseFile(VFSFileReference *reference) override;

VFSOpenFile *OpenFileForRead(VFSFileReference *reference) override;
void Rewind(VFSOpenFile *openFile) override;
size_t Read(VFSOpenFile *openFile, uint8_t *buffer, size_t length) override;
void CloseFile(VFSOpenFile *openFile) override;

bool GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter) override;
bool GetFileInfo(const char *path, File::FileInfo *info) override;
std::string toString() const override {
Expand Down

0 comments on commit 23b310f

Please sign in to comment.