GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
grem::application::VirtualFilesystem Class Reference

Persistent system for managing the virtual filesystem. More...

#include <GREM/application/VirtualFilesystem.hpp>

Inheritance diagram for grem::application::VirtualFilesystem:

Classes

struct  StandardOutputDirectoryOptions

Public Member Functions

 VirtualFilesystem (CStringView programFilepath)
 Initialize the virtual filesystem.
 ~VirtualFilesystem () override
 Destructor.
 VirtualFilesystem (const VirtualFilesystem &)=delete
 Copying a filesystem is not allowed, since it manages global state.
 VirtualFilesystem (VirtualFilesystem &&)=delete
 Moving a filesystem is not allowed, since it manages global state.
VirtualFilesystem & operator= (const VirtualFilesystem &)=delete
 Copying a filesystem is not allowed, since it manages global state.
VirtualFilesystem & operator= (VirtualFilesystem &&)=delete
 Moving a filesystem is not allowed, since it manages global state.
String createStandardOutputDirectory (const StandardOutputDirectoryOptions &standardOutputDirectoryOptions)
 Get a suitable output directory for configuration files and other save data on the host platform, which is usually located somwhere within the user's home directory in a sub-folder tailored to this specific application.
CStringView getOutputDirectory () const noexcept override
 Get the current output directory of the virtual filesystem.
void setOutputDirectory (CStringView path)
 Set the output directory of the virtual filesystem, where all output files will be written.
void mountInputArchive (CStringView path, MountPriority priority=MountPriority::HIGHER)
 Mount a directory or archive on the host filesystem to the root input directory of the virtual filesystem.
void unmountInputArchive (CStringView path)
 Unmount a previously mounted input directory or archive on the host filesystem from the virtual filesystem.
void mountInputArchivesInMountedDirectory (CStringView filepath, CStringView archiveFileExtension, MountPriority priority=MountPriority::HIGHER, FunctionView< void(CStringView path)> callback=[](CStringView) -> void {})
 Mount all archives in a given directory on the host filesystem to the root input directory of the virtual filesystem.
CStringView findInputArchiveOfFile (CStringView filepath) const noexcept override
 Get the host filepath of the mounted input archive on the host filesystem that contains a given virtual file.
void createOutputDirectory (CStringView filepath) override
 Create a new host directory relative to the current output directory, while also creating any missing parent directories along the way as needed.
void createParentOutputDirectories (CStringView filepath) override
 Create any missing parent directories above a host directory relative to the current output directory.
void deleteOutputFile (CStringView filepath) override
 Delete a host file or directory relative to the current output directory.
bool inputFileExists (CStringView filepath) const override
 Check if a given virtual filepath has a corresponding host file mounted.
File::Metadata getFileMetadata (CStringView filepath) const override
 Get the metadata of a file that is mounted at a given virtual filepath.
void forEachInputFilenameInDirectory (CStringView filepath, FunctionView< bool(CStringView filename)> callback) const override
 Iterate the filenames of all readable virtual filepaths that are direct children of a given directory.
InputFileHandle openInputFile (CStringView filepath) const override
 Open an input file in the virtual filesystem for reading.
InputFileHandle tryOpenInputFile (CStringView filepath) const override
 Try to open an input file in the virtual filesystem for reading.
OutputFileHandle openEmptyOutputFile (CStringView filepath) override
 Create a file relative to the current output directory and open it for writing, overwriting any existing file at the same filepath.
void createEmptyOutputFile (CStringView filepath) override
 Create an empty file relative to the current output directory, overwriting any existing file at the same filepath.
OutputFileHandle openOutputFileForAppending (CStringView filepath) override
 Open a file relative to the current output directory for appended writing, or create an empty file and open it if one didn't already exist.
Public Member Functions inherited from grem::Filesystem
constexpr Filesystem () noexcept=default
 Default constructor.
virtual ~Filesystem ()=default
 Virtual destructor.
constexpr Filesystem (const Filesystem &)=default
 Copy constructor.
constexpr Filesystem (Filesystem &&) noexcept=default
 Move constructor.
Filesystem & operator= (const Filesystem &)=default
 Copy assignment.
Filesystem & operator= (Filesystem &&) noexcept=default
 Move assignment.
void forEachInputFilenameInDirectory (CStringView filepath, auto callback) const
 Iterate the filenames of all readable virtual filepaths that are direct children of a given directory.
Allocation< byte > readInputFile (CStringView filepath) const
 Open an input file in the virtual filesystem for reading and read its full contents into an array of bytes.
Optional< Allocation< byte > > tryReadInputFile (CStringView filepath) const
 Try to open an input file in the virtual filesystem for reading and read its full contents into an array of bytes.
ArrayList< byte > readInputFileArrayList (CStringView filepath) const
 Open an input file in the virtual filesystem for reading and read its full contents into an array list of bytes.
Optional< ArrayList< byte > > tryReadInputFileArrayList (CStringView filepath) const
 Try to open an input file in the virtual filesystem for reading and read its full contents into an array list of bytes.
Buffer< byte > readInputFileBuffer (CStringView filepath) const
 Open an input file in the virtual filesystem for reading and read its full contents into a buffer of bytes.
Optional< Buffer< byte > > tryReadInputFileBuffer (CStringView filepath) const
 Try to open an input file in the virtual filesystem for reading and read its full contents into a buffer of bytes.
String readInputFileString (CStringView filepath) const
 Open an input file in the virtual filesystem for reading and read its full contents into a string of bytes.
Optional< String > tryReadInputFileString (CStringView filepath) const
 Try to open an input file in the virtual filesystem for reading and read its full contents into a string of bytes.
Allocation< char > readInputFileCString (CStringView filepath) const
 Open an input file in the virtual filesystem for reading and read its full contents into an allocated null-terminated string of bytes.
Allocation< char > tryReadInputFileCString (CStringView filepath) const
 Try to open an input file in the virtual filesystem for reading and read its full contents into an allocated null-terminated string of bytes.

Detailed Description

Persistent system for managing the virtual filesystem.

This system supports multiple host filesystem directories being mounted to the same virtual mount point for reading, such that virtual filepaths are mapped to the corresponding host file with the highest mount priority. When creating and writing files, all output is directed to an optional output directory.

By default, no input archives are mounted, and no output directory is set, meaning that no files can be opened or created. To be able to read and write files, the recommended initialization procedure for a typical application is as follows:

// Construct the virtual filesystem.
VirtualFilesystem filesystem{argv[0]};
// Set the output directory to a standardized location for the current platform.
// See VirtualFilesystem::StandardOutputDirectoryOptions for a description of the expected parameters.
.organizationName = "MyOrg",
.applicationName = "MyApp",
}));
// Mount the current working directory for reading.
// This may be replaced and/or extended with a specific archive in the working directory, such as "data" or "assets.zip".
filesystem.mountInputArchive(".");
// Mount any ".zip" archives found under "custom/" in the main archive(s).
// This step is optional, but makes the application easy to mod with custom files thanks to the higher mount priority.
// If using this, make sure that at least one of the main archives that were mounted above is a regular uncompressed directory,
// since it would be very inconvenient for the user to have to add their custom archives to a compressed file.
filesystem.mountInputArchivesInMountedDirectory("custom", "zip");
// Mount the output directory for reading.
// This allows saved files to be re-read again, with a higher priority than all other archives.
filesystem.mountInputArchive(filesystem.getOutputDirectory());
CStringView getOutputDirectory() const noexcept override
Get the current output directory of the virtual filesystem.
void mountInputArchivesInMountedDirectory(CStringView filepath, CStringView archiveFileExtension, MountPriority priority=MountPriority::HIGHER, FunctionView< void(CStringView path)> callback=[](CStringView) -> void {})
Mount all archives in a given directory on the host filesystem to the root input directory of the vir...
void mountInputArchive(CStringView path, MountPriority priority=MountPriority::HIGHER)
Mount a directory or archive on the host filesystem to the root input directory of the virtual filesy...
void setOutputDirectory(CStringView path)
Set the output directory of the virtual filesystem, where all output files will be written.
String createStandardOutputDirectory(const StandardOutputDirectoryOptions &standardOutputDirectoryOptions)
Get a suitable output directory for configuration files and other save data on the host platform,...
VirtualFilesystem(CStringView programFilepath)
Initialize the virtual filesystem.

Constructor & Destructor Documentation

◆ VirtualFilesystem() [1/3]

grem::application::VirtualFilesystem::VirtualFilesystem ( CStringView programFilepath)
explicit

Initialize the virtual filesystem.

Parameters
programFilepaththe first string in the argument vector passed to the main function of the program, i.e. argv[0].
Exceptions
File::Errorif initialization failed.
std::bad_allocon allocation failure.
Warning
The behavior of passing programFilepath a value other than the argv[0] string received from main is undefined.
There can only be one active virtual filesystem in a program at a time.
See also
setOutputDirectory()
mountInputArchivesInMountedDirectory()
mountInputArchive()

◆ ~VirtualFilesystem()

grem::application::VirtualFilesystem::~VirtualFilesystem ( )
override

Destructor.

◆ VirtualFilesystem() [2/3]

grem::application::VirtualFilesystem::VirtualFilesystem ( const VirtualFilesystem & )
delete

Copying a filesystem is not allowed, since it manages global state.

◆ VirtualFilesystem() [3/3]

grem::application::VirtualFilesystem::VirtualFilesystem ( VirtualFilesystem && )
delete

Moving a filesystem is not allowed, since it manages global state.

Member Function Documentation

◆ operator=() [1/2]

VirtualFilesystem & grem::application::VirtualFilesystem::operator= ( const VirtualFilesystem & )
delete

Copying a filesystem is not allowed, since it manages global state.

◆ operator=() [2/2]

VirtualFilesystem & grem::application::VirtualFilesystem::operator= ( VirtualFilesystem && )
delete

Moving a filesystem is not allowed, since it manages global state.

◆ createStandardOutputDirectory()

String grem::application::VirtualFilesystem::createStandardOutputDirectory ( const StandardOutputDirectoryOptions & standardOutputDirectoryOptions)
nodiscard

Get a suitable output directory for configuration files and other save data on the host platform, which is usually located somwhere within the user's home directory in a sub-folder tailored to this specific application.

Parameters
standardOutputDirectoryOptionscreation options, see StandardOutputDirectoryOptions.
Returns
a directory that can be passed to setOutputDirectory(), or an empty string if a suitable output directory could not be determined.
Exceptions
std::bad_allocon allocation failure.
See also
setOutputDirectory()
getOutputDirectory()

◆ getOutputDirectory()

CStringView grem::application::VirtualFilesystem::getOutputDirectory ( ) const
nodiscardoverridevirtualnoexcept

Get the current output directory of the virtual filesystem.

Returns
the host filepath corresponding to the current output directory, or an empty string if no output directory is currently set.
See also
createStandardOutputDirectory()
setOutputDirectory()

Implements grem::Filesystem.

◆ setOutputDirectory()

void grem::application::VirtualFilesystem::setOutputDirectory ( CStringView path)

Set the output directory of the virtual filesystem, where all output files will be written.

The specified directory will be created if it doesn't already exist.

Parameters
pathhost filepath of the new output directory to set, or an empty string to revert to having no output directory.
Exceptions
File::Erroron failure to set the output directory to the given path.
std::bad_allocon allocation failure.
Note
To be able to read files from the output directory, it must also be mounted using mountInputArchive().
See also
createStandardOutputDirectory()
getOutputDirectory()

◆ mountInputArchive()

void grem::application::VirtualFilesystem::mountInputArchive ( CStringView path,
MountPriority priority = MountPriority::HIGHER )

Mount a directory or archive on the host filesystem to the root input directory of the virtual filesystem.

The supported archive types, besides regular directories, are:

  • ZIP archive (.zip)
  • 7-Zip archive (.7z)
  • ISO 9660 CD-ROM image (.iso)
  • Quake I/II archive (.pak)
  • DOOM engine archive (.wad)
Parameters
pathhost filepath of the directory or archive to mount.
prioritymount priority of the archive to be mounted, see FilesystemMountPriority.
Exceptions
File::Erroron failure to mount the given filepath.
Note
If the given path is empty, this function has no effect.
If the given path is already mounted, no change will occur.
See also
mountInputArchivesInMountedDirectory()
unmountInputArchive()

◆ unmountInputArchive()

void grem::application::VirtualFilesystem::unmountInputArchive ( CStringView path)

Unmount a previously mounted input directory or archive on the host filesystem from the virtual filesystem.

Parameters
pathhost filepath of the directory or archive to unmount, which was previously mounted by mountInputArchive() or mountInputArchivesInMountedDirectory().
Exceptions
File::Erroron failure to unmount the given filepath.
Note
If the given path is empty, this function has no effect.
See also
mountInputArchive()

◆ mountInputArchivesInMountedDirectory()

void grem::application::VirtualFilesystem::mountInputArchivesInMountedDirectory ( CStringView filepath,
CStringView archiveFileExtension,
MountPriority priority = MountPriority::HIGHER,
FunctionView< void(CStringView path)> callback = [](CStringView) -> void {} )

Mount all archives in a given directory on the host filesystem to the root input directory of the virtual filesystem.

This is useful for allowing users to easily create and share modifications or plugins that add or override application resources by simply adding the mod archive to the given directory.

The newly mounted archives will have a higher priority than any previously mounted archives when choosing which host file to map a virtual filepath to, meaning more recently mounted files are preferred.

Parameters
filepathvirtual filepath of the mounted directory to search for archives in.
archiveFileExtensionnon-owning pointer to a null-terminated UTF-8 string of the filename extension of the archive files to mount. If empty, all found archives will be mounted regardless of extension.
prioritymount priority of the archives to be mounted, see FilesystemMountPriority.
callbackcallback to execute for each host filepath of the archives that were mounted, in no specific order.
Exceptions
File::Erroron failure to search for archives or mount an archive.
std::bad_allocon allocation failure.
Note
The found archives are mounted in no particular order.
See also
mountInputArchive()
unmountInputArchive()

◆ findInputArchiveOfFile()

CStringView grem::application::VirtualFilesystem::findInputArchiveOfFile ( CStringView filepath) const
nodiscardoverridevirtualnoexcept

Get the host filepath of the mounted input archive on the host filesystem that contains a given virtual file.

Parameters
filepathvirtual filepath of the file to get the archive of.
Returns
the host filepath of the directory or archive that was previously passed to mountInputArchive(), in which the corresponding virtual file was found, or an empty string if the virtual file was not found at any active mount point.

Implements grem::Filesystem.

◆ createOutputDirectory()

void grem::application::VirtualFilesystem::createOutputDirectory ( CStringView filepath)
overridevirtual

Create a new host directory relative to the current output directory, while also creating any missing parent directories along the way as needed.

Parameters
filepathvirtual filepath, relative to the current output directory, of the new directory to be created.
Exceptions
File::Erroron failure to create the directories.
std::bad_allocon allocation failure.

Implements grem::Filesystem.

◆ createParentOutputDirectories()

void grem::application::VirtualFilesystem::createParentOutputDirectories ( CStringView filepath)
overridevirtual

Create any missing parent directories above a host directory relative to the current output directory.

Parameters
filepathvirtual filepath, relative to the current output directory, of the directory whose missing parent directories to create.
Exceptions
File::Erroron failure to create the directories.
std::bad_allocon allocation failure.

Implements grem::Filesystem.

◆ deleteOutputFile()

void grem::application::VirtualFilesystem::deleteOutputFile ( CStringView filepath)
overridevirtual

Delete a host file or directory relative to the current output directory.

Parameters
filepathvirtual filepath, relative to the current output directory, of the file or directory to delete.
Exceptions
File::Erroron failure to delete the given file or directory.
std::bad_allocon allocation failure.
Warning
If successful, this will delete the actual file that corresponds to the given virtual filepath on the host filesystem; not just the virtual file entry.
Although deleting a file will prevent it from being read again through conventional means, the physical data that was contained in the file may or may not remain untouched on disk, meaning that this function cannot be relied upon to securly erase sensitive data.
Note
Directories must be empty before they can be successfully deleted using this function.

Implements grem::Filesystem.

◆ inputFileExists()

bool grem::application::VirtualFilesystem::inputFileExists ( CStringView filepath) const
nodiscardoverridevirtual

Check if a given virtual filepath has a corresponding host file mounted.

Parameters
filepathvirtual filepath to check for a mounted file.
Returns
true if a file is mounted at the given filepath, false otherwise.
See also
getFileMetadata()
forEachInputFilenameInDirectory()

Implements grem::Filesystem.

◆ getFileMetadata()

File::Metadata grem::application::VirtualFilesystem::getFileMetadata ( CStringView filepath) const
nodiscardoverridevirtual

Get the metadata of a file that is mounted at a given virtual filepath.

Parameters
filepathvirtual filepath of the file to get the metadata of.
Returns
the file metadata, see File::Metadata.
Exceptions
File::Erroron failure to get the metadata, such as if no file is mounted at the given filepath.
std::bad_allocon allocation failure.

Implements grem::Filesystem.

◆ forEachInputFilenameInDirectory()

void grem::application::VirtualFilesystem::forEachInputFilenameInDirectory ( CStringView filepath,
FunctionView< bool(CStringView filename)> callback ) const
overridevirtual

Iterate the filenames of all readable virtual filepaths that are direct children of a given directory.

Parameters
filepathvirtual filepath of the input directory to enumerate.
callbackfunction to execute for each file, which should accept the current filename as a parameter and return a bool that specifies whether to stop the traversal or not. A value of true means to stop and return early, while a value of false means to continue traversing.
Exceptions
File::Erroron failure to enumerate the directory.
std::length_errorif an internal size limit was exceeded.
std::bad_array_new_lengthif an internal size limit was exceeded.
std::bad_allocon allocation failure.
anyexception thrown by the callback function.
Note
This function is not recursive, and only yields the filename component of the direct descendants of the given directory, without the leading directory path. The full virtual filepath of each filepath can be constructed using formatString("{}/{}", filepath, filename), where filepath is the directory filepath that was passed to the function, and filename is the current filename. Note that this path may refer to any kind of file, including a subdirectory. Use getFileMetadata() to find out which kind of file it refers to.
See also
inputFileExists()
getFileMetadata()

Implements grem::Filesystem.

◆ openInputFile()

InputFileHandle grem::application::VirtualFilesystem::openInputFile ( CStringView filepath) const
nodiscardoverridevirtual

Open an input file in the virtual filesystem for reading.

Parameters
filepathvirtual filepath of the mounted input file to open.
Returns
a new file handle with an input stream set up to read the opened file starting at file position 0.
Exceptions
File::Erroron failure to open the file for reading.
std::bad_allocon allocation failure.
See also
tryOpenInputFile()
openEmptyOutputFile()
openOutputFileForAppending()

Implements grem::Filesystem.

◆ tryOpenInputFile()

InputFileHandle grem::application::VirtualFilesystem::tryOpenInputFile ( CStringView filepath) const
nodiscardoverridevirtual

Try to open an input file in the virtual filesystem for reading.

Parameters
filepathvirtual filepath of the mounted input file to open.
Returns
a new file handle with an input stream set up to read the opened file starting at file position 0, or a closed file handle if the file could not be opened.
Exceptions
std::bad_allocon allocation failure.
See also
openInputFile()
openEmptyOutputFile()
openOutputFileForAppending()

Implements grem::Filesystem.

◆ openEmptyOutputFile()

OutputFileHandle grem::application::VirtualFilesystem::openEmptyOutputFile ( CStringView filepath)
nodiscardoverridevirtual

Create a file relative to the current output directory and open it for writing, overwriting any existing file at the same filepath.

Parameters
filepathvirtual filepath of the new file to be created, relative to the current output directory.
Returns
a new file handle with an output stream set up to write to the new empty file that was opened.
Exceptions
File::Erroron failure to delete the existing file, create the new file, or open the new file for writing.
std::bad_allocon allocation failure.
Warning
If successful, any existing host file that corresponds to the given virtual filepath in the output directory on the host filesystem will be deleted on the host filesystem; not just its virtual file entry. All modifications made through the virtual file handle will also be reflected in the physical file on the host file system.
See also
openInputFile()
createEmptyOutputFile()
openOutputFileForAppending()

Implements grem::Filesystem.

◆ createEmptyOutputFile()

void grem::application::VirtualFilesystem::createEmptyOutputFile ( CStringView filepath)
overridevirtual

Create an empty file relative to the current output directory, overwriting any existing file at the same filepath.

Parameters
filepathvirtual filepath of the new file to be created, relative to the current output directory.
Exceptions
File::Erroron failure to delete the existing file or create the new file.
std::bad_allocon allocation failure.
Warning
If successful, any existing host file that corresponds to the given virtual filepath in the output directory on the host filesystem will be deleted on the host filesystem; not just its virtual file entry. All modifications made through the virtual file handle will also be reflected in the physical file on the host file system.
See also
openEmptyOutputFile()

Implements grem::Filesystem.

◆ openOutputFileForAppending()

OutputFileHandle grem::application::VirtualFilesystem::openOutputFileForAppending ( CStringView filepath)
nodiscardoverridevirtual

Open a file relative to the current output directory for appended writing, or create an empty file and open it if one didn't already exist.

Parameters
filepathvirtual filepath of the file to be opened, relative to the current output directory.
Returns
a new file handle with an output stream set up to write to the end of the opened file.
Exceptions
File::Erroron failure to open the file for writing.
std::bad_allocon allocation failure.
Warning
If successful, the existing host file that corresponds to the given virtual filepath in the output directory on the host filesystem will receive all modifications made through the virtual file handle.
Note
A new, empty file will be created if the specified file doesn't already exist.
See also
openInputFile()
openEmptyOutputFile()

Implements grem::Filesystem.


The documentation for this class was generated from the following file: