This class provides abstraction of file system needed for loading packed and unpacked assets.
More...
#include <VirtualFileSystem.hpp>
|
| struct | FileData |
| | Simple struct needed to represent loaded file with data and size. More...
|
| | struct defining vpak file header data, used to validate file integrity. More...
|
| struct | VPKFileEntry |
| | struct defining virtual file "header", used to identify files in the virtual file system. More...
|
| struct | LoadedVPK |
| | struct holding data of loaded vpk file. More...
|
| class | VPKStream |
| | class implementing custom file stream functionality for virtual file system. Inherits from std::istream. More...
|
|
| | VirtualFileSystem () |
| | Constructor for VirtualFileSystem.
|
| bool | initialize (const std::string &base_path) |
| | Initializes the virtual file system.
|
| std::unique_ptr< FileData > | load_file (const std::string &virtual_path) |
| | Loads a file into memory from the specified path.
|
| std::unique_ptr< std::istream > | open_file_stream (const std::string &virtual_path) |
| | Opens a file stream for reading from a specified path.
|
| bool | file_exists (const std::string &virtual_path) |
| | Checks if a file exists in the currently active file system mode.
|
| size_t | get_file_size (const std::string &virtual_path) |
| | Gets the size of a file in bytes.
|
| std::vector< std::string > | list_files (const std::string &virtual_dir="") |
| | Lists all files contained within a specific directory.
|
| std::string | resolve_relative_path (const std::string &base_path, const std::string &relative_path) |
| | Resolves a relative path to a normalized, cleaner format.
|
| std::string | get_base_path () const |
| | Gets the base path of the virtual file system.
|
|
| static void | SetVpakKey (const std::string &key) |
| | Sets the VPAK decryption key.
|
| static std::string | GetVpakKey () |
| | Gets the current VPAK decryption key.
|
|
| bool | load_vpk_file (const std::string &vpk_path) |
| | Method to load a VPK file.
|
| std::string | clean_path (const std::string &path) |
| | cleans path from unwanted string eg "Assets/"
|
| const VPKFileEntry * | find_file_entry (const std::string &virtual_path) |
| | Method to find a file entry by its virtual path.
|
|
| static std::string | s_vpak_key = "FALLBACK_VPAK_KEY_0000" |
This class provides abstraction of file system needed for loading packed and unpacked assets.
Definition at line 26 of file VirtualFileSystem.hpp.
◆ VirtualFileSystem()
| vex::VirtualFileSystem::VirtualFileSystem |
( |
| ) |
|
◆ ~VirtualFileSystem()
| vex::VirtualFileSystem::~VirtualFileSystem |
( |
| ) |
|
◆ clean_path()
| std::string vex::VirtualFileSystem::clean_path |
( |
const std::string & | path | ) |
|
|
private |
◆ file_exists()
| bool vex::VirtualFileSystem::file_exists |
( |
const std::string & | virtual_path | ) |
|
Checks if a file exists in the currently active file system mode.
- Packed Mode: Searches the m_loaded_vpk->file_names vector for the cleaned path.
- Loose Mode: Uses std::filesystem::exists on the physical disk path.
- Parameters
-
| const | std::string& virtual_path - The path to check. |
- Returns
- bool - true if the file exists, false otherwise.
Definition at line 233 of file VirtualFileSystem.cpp.
◆ find_file_entry()
◆ get_base_path()
| std::string vex::VirtualFileSystem::get_base_path |
( |
| ) |
const |
|
inline |
Gets the base path of the virtual file system.
- Returns
- std::string - base path.
Definition at line 95 of file VirtualFileSystem.hpp.
◆ get_file_size()
| size_t vex::VirtualFileSystem::get_file_size |
( |
const std::string & | virtual_path | ) |
|
Gets the size of a file in bytes.
Returns entry->data_size for packed files or fs::file_size for loose files.
- Parameters
-
| const | std::string& virtual_path - The path of the file. |
- Returns
- size_t - Size of the file in bytes, or 0 if not found.
Definition at line 250 of file VirtualFileSystem.cpp.
◆ GetVpakKey()
| std::string vex::VirtualFileSystem::GetVpakKey |
( |
| ) |
|
|
static |
◆ initialize()
| bool vex::VirtualFileSystem::initialize |
( |
const std::string & | base_path | ) |
|
Initializes the virtual file system.
Sets the base path and checks for the existence of a packed asset file (Assets/assets.vpk). If the VPK exists, it attempts to load the header and file table. If the VPK is missing (or in Debug builds), it falls back to loose file loading (m_use_packed_assets = false).
- Parameters
-
| const | std::string& base_path - The root directory path (usually the executable directory) to initialize the VFS from. |
- Returns
- bool - true if initialization (VPK load or fallback setup) is successful, false otherwise.
Definition at line 27 of file VirtualFileSystem.cpp.
◆ list_files()
| std::vector< std::string > vex::VirtualFileSystem::list_files |
( |
const std::string & | virtual_dir = "" | ) |
|
Lists all files contained within a specific directory.
- Packed Mode: Iterates VPK file names and filters by the provided directory prefix.
- Loose Mode: Uses fs::recursive_directory_iterator to traverse the physical Assets/ directory.
- Parameters
-
| const | std::string& virtual_dir - The directory path to filter by (default is root). |
- Returns
- std::vector<std::string> - A vector of relative file paths found.
Definition at line 274 of file VirtualFileSystem.cpp.
◆ load_file()
Loads a file into memory from the specified path.
- Packed Mode: Locates the file entry in the loaded VPK, seeks to the data offset, and reads entry->data_size bytes.
- Loose Mode: Reads the file from disk using std::ifstream, resolving the path relative to the base directory.
- Parameters
-
| const | std::string& virtual_path - The relative path or unique ID of the file to load. |
- Returns
- std::unique_ptr<FileData> - Unique pointer to the struct containing the raw data vector and size, or nullptr if not found.
Definition at line 156 of file VirtualFileSystem.cpp.
◆ load_vpk_file()
| bool vex::VirtualFileSystem::load_vpk_file |
( |
const std::string & | vpk_path | ) |
|
|
private |
Method to load a VPK file.
- Parameters
-
| const | std::string& vpk_path - path to vpk file |
- Returns
- true if the file was loaded successfully, false otherwise
Definition at line 47 of file VirtualFileSystem.cpp.
◆ open_file_stream()
| std::unique_ptr< std::istream > vex::VirtualFileSystem::open_file_stream |
( |
const std::string & | virtual_path | ) |
|
Opens a file stream for reading from a specified path.
- Packed Mode: Reads the specific file chunk from the VPK into a memory buffer (protected by stream_mutex) and returns a custom VPKStream wrapped around that buffer.
- Loose Mode: returns a standard std::ifstream opened in binary mode.
- Parameters
-
| const | std::string& virtual_path - The relative path to the file. |
- Returns
- std::unique_ptr<std::istream> - Unique pointer to the input stream, or nullptr if the file cannot be opened.
Definition at line 205 of file VirtualFileSystem.cpp.
◆ resolve_relative_path()
| std::string vex::VirtualFileSystem::resolve_relative_path |
( |
const std::string & | base_path, |
|
|
const std::string & | relative_path ) |
Resolves a relative path to a normalized, cleaner format.
Uses std::filesystem::lexically_normal() to resolve .. and . segments, and ensures consistent separators via clean_path.
- Parameters
-
| const | std::string& base_path - The context path (e.g., the path of the file requesting the resolve). |
| const | std::string& relative_path - The path relative to base_path. |
- Returns
- std::string - The resolved, cleaned virtual path.
Definition at line 319 of file VirtualFileSystem.cpp.
◆ SetVpakKey()
| void vex::VirtualFileSystem::SetVpakKey |
( |
const std::string & | key | ) |
|
|
static |
◆ m_base_path
| std::string vex::VirtualFileSystem::m_base_path |
|
private |
◆ m_loaded_vpk
| std::unique_ptr<LoadedVPK> vex::VirtualFileSystem::m_loaded_vpk |
|
private |
◆ m_use_packed_assets
| bool vex::VirtualFileSystem::m_use_packed_assets |
|
private |
◆ s_vpak_key
| std::string vex::VirtualFileSystem::s_vpak_key = "FALLBACK_VPAK_KEY_0000" |
|
staticprivate |
◆ stream_mutex
| std::mutex vex::VirtualFileSystem::stream_mutex |
|
private |
The documentation for this class was generated from the following files: