GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
VirtualFilesystem.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_APPLICATION_VIRTUAL_FILESYSTEM_HPP
5#define GREM_APPLICATION_VIRTUAL_FILESYSTEM_HPP
6
8
16
17#ifdef GREM_USE_MULTITHREADING
21
22#include <functional> // std::hash
23#include <system_error> // std::error_code
24#include <utility> // std::move
25#endif
26
27namespace grem::application {
28
29class VirtualInputFile; // Forward declaration, to avoid a circular include of VirtualInputFile.hpp.
30class VirtualOutputFile; // Forward declaration, to avoid a circular include of VirtualOutputFile.hpp.
31
36enum class MountPriority : uint8_t {
43
50};
51
92public:
115
134 GREM_API(application) explicit VirtualFilesystem(CStringView programFilepath);
135
137 GREM_API(application) ~VirtualFilesystem() override;
138
141
144
147
150
169 [[nodiscard]] GREM_API(application) String createStandardOutputDirectory(const StandardOutputDirectoryOptions& standardOutputDirectoryOptions);
170
180 [[nodiscard]] GREM_API(application) CStringView getOutputDirectory() const noexcept override;
181
202
227
243
274 GREM_API(application)
276 CStringView filepath, CStringView archiveFileExtension, MountPriority priority = MountPriority::HIGHER,
277 FunctionView<void(CStringView path)> callback = [](CStringView) -> void {});
278
279 [[nodiscard]] GREM_API(application) CStringView findInputArchiveOfFile(CStringView filepath) const noexcept override;
280
281 GREM_API(application) void createOutputDirectory(CStringView filepath) override;
282 GREM_API(application) void createParentOutputDirectories(CStringView filepath) override;
283 GREM_API(application) void deleteOutputFile(CStringView filepath) override;
284
285 [[nodiscard]] GREM_API(application) bool inputFileExists(CStringView filepath) const override;
286 [[nodiscard]] GREM_API(application) File::Metadata getFileMetadata(CStringView filepath) const override;
287
288 GREM_API(application)
289 void forEachInputFilenameInDirectory(CStringView filepath, FunctionView<bool(CStringView filename)> callback) const override; // NOLINT(modernize-use-nodiscard)
290
291 [[nodiscard]] GREM_API(application) InputFileHandle openInputFile(CStringView filepath) const override;
292 [[nodiscard]] GREM_API(application) InputFileHandle tryOpenInputFile(CStringView filepath) const override;
293 [[nodiscard]] GREM_API(application) OutputFileHandle openEmptyOutputFile(CStringView filepath) override;
294 GREM_API(application) void createEmptyOutputFile(CStringView filepath) override;
295 [[nodiscard]] GREM_API(application) OutputFileHandle openOutputFileForAppending(CStringView filepath) override;
296
297private:
298 friend VirtualInputFile;
299 friend VirtualOutputFile;
300
301 String outputDirectory{};
302#ifdef GREM_USE_MULTITHREADING
303 struct SharedFileMutex {
304 struct Value {
305 explicit Value(String canonicalFilepath)
306 : canonicalFilepath(std::move(canonicalFilepath)) {}
307
308 String canonicalFilepath;
309 RecursiveMutex mutex{};
310 };
311
312 struct Hash {
313 using is_transparent = void;
314
315 [[nodiscard]] size_t operator()(const SharedFileMutex& a) const {
316 return stringHasher(a.value->canonicalFilepath);
317 }
318
319 [[nodiscard]] size_t operator()(const String& a) const {
320 return stringHasher(a);
321 }
322
323 private:
324 [[no_unique_address]] std::hash<String> stringHasher;
325 };
326
327 struct Equal {
328 using is_transparent = void;
329
330 [[nodiscard]] bool operator()(const SharedFileMutex& a, const SharedFileMutex& b) const {
331 return a.value->canonicalFilepath == b.value->canonicalFilepath;
332 }
333
334 [[nodiscard]] bool operator()(const SharedFileMutex& a, const String& b) const {
335 return a.value->canonicalFilepath == b;
336 }
337
338 [[nodiscard]] bool operator()(const String& a, const SharedFileMutex& b) const {
339 return a == b.value->canonicalFilepath;
340 }
341
342 [[nodiscard]] bool operator()(const String& a, const String& b) const {
343 return a == b;
344 }
345 };
346
347 SharedPointer<Value> value{};
348
349 SharedFileMutex() noexcept = default;
350
351 explicit SharedFileMutex(String canonicalFilepath)
352 : value(SharedPointer<Value>::create(std::move(canonicalFilepath))) {}
353 };
354
355 mutable HashSet<SharedFileMutex, SharedFileMutex::Hash, SharedFileMutex::Equal> fileMutexes{};
356 mutable Mutex fileMutexesSetMutex{};
357
358 struct FileLock {
359 const VirtualFilesystem* filesystem = nullptr;
360 SharedFileMutex fileMutex{};
361 UniqueLock<RecursiveMutex> lock{};
362
363 FileLock() noexcept = default;
364
365 FileLock(const VirtualFilesystem& filesystem, SharedFileMutex fileMutex)
366 : filesystem(&filesystem)
367 , fileMutex(std::move(fileMutex))
368 , lock(this->fileMutex.value->mutex) {}
369
370 GREM_API(application) ~FileLock();
371
372 FileLock(const FileLock&) = delete;
373 FileLock(FileLock&&) noexcept = default;
374 FileLock& operator=(const FileLock&) = delete;
375 FileLock& operator=(FileLock&&) noexcept = default;
376 };
377
378 [[nodiscard]] GREM_API(application) FileLock lockInputFile(CStringView filepath, std::error_code& errorCode) const;
379 [[nodiscard]] GREM_API(application) FileLock lockOutputFile(CStringView filepath, std::error_code& errorCode) const;
380#endif
381};
382
383} // namespace grem::application
384
385#endif
constexpr Filesystem() noexcept=default
Default constructor.
Definition FunctionView.hpp:29
Generic boxed unique handle to a readable file in any filesystem.
Definition File.hpp:363
Generic boxed unique handle to a writable file in any filesystem.
Definition File.hpp:686
Definition synchronization.hpp:986
VirtualFilesystem(VirtualFilesystem &&)=delete
Moving a filesystem is not allowed, since it manages global state.
OutputFileHandle openOutputFileForAppending(CStringView filepath) override
Open a file relative to the current output directory for appended writing, or create an empty file an...
VirtualFilesystem & operator=(VirtualFilesystem &&)=delete
Moving a filesystem is not allowed, since it manages global state.
InputFileHandle openInputFile(CStringView filepath) const override
Open an input file in the virtual filesystem for reading.
CStringView getOutputDirectory() const noexcept override
Get the current output directory of the virtual filesystem.
OutputFileHandle openEmptyOutputFile(CStringView filepath) override
Create a file relative to the current output directory and open it for writing, overwriting any exist...
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...
void createOutputDirectory(CStringView filepath) override
Create a new host directory relative to the current output directory, while also creating any missing...
bool inputFileExists(CStringView filepath) const override
Check if a given virtual filepath has a corresponding host file mounted.
void unmountInputArchive(CStringView path)
Unmount a previously mounted input directory or archive on the host filesystem from the virtual files...
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...
InputFileHandle tryOpenInputFile(CStringView filepath) const override
Try to open an input file in the virtual filesystem for reading.
~VirtualFilesystem() override
Destructor.
VirtualFilesystem(const VirtualFilesystem &)=delete
Copying 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.
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 createEmptyOutputFile(CStringView filepath) override
Create an empty file relative to the current output directory, overwriting any existing file at the s...
CStringView findInputArchiveOfFile(CStringView filepath) const noexcept override
Get the host filepath of the mounted input archive on the host filesystem that contains a given virtu...
void createParentOutputDirectories(CStringView filepath) override
Create any missing parent directories above a host directory relative to the current output directory...
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.
void deleteOutputFile(CStringView filepath) override
Delete a host file or directory relative to the current output directory.
Unique handle to a readable file in the virtual Filesystem.
Definition VirtualInputFile.hpp:35
Unique handle to a writable file in the virtual Filesystem.
Definition VirtualOutputFile.hpp:35
Definition Application.hpp:18
MountPriority
Mount priority for a newly mounted archive to a VirtualFilesystem, relative to all previously mounted...
Definition VirtualFilesystem.hpp:36
@ HIGHER
Mount the archive at a higher priority than any previously mounted archive, meaning files in the new ...
Definition VirtualFilesystem.hpp:49
@ LOWER
Mount the archive at a lower priority than any previously mounted archive, meaning files in already m...
Definition VirtualFilesystem.hpp:42
GREM_ALWAYS_INLINE void lock(Lockable1 &lock1, Lockable2 &lock2, LockableN &... lockN)
Definition synchronization.hpp:1268
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669
StringBase< char, std::char_traits< char >, std::allocator< char > > String
Definition String.hpp:17
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635
Record of metadata for a specific file.
Definition File.hpp:54
CStringView applicationName
String that uniquely identifies the application among all other applications released by the same org...
Definition VirtualFilesystem.hpp:113
CStringView organizationName
String that commonly identifies the publisher of the application, such as an organization name,...
Definition VirtualFilesystem.hpp:102