VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
vex::VirtualFileSystem Class Reference

This class provides abstraction of file system needed for loading packed and unpacked assets. More...

#include <VirtualFileSystem.hpp>

Classes

struct  FileData
 Simple struct needed to represent loaded file with data and size. More...
struct  VPKHeader
 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...

Public Member Functions

 VirtualFileSystem ()
 Constructor for VirtualFileSystem.
bool initialize (const std::string &base_path)
 Initializes the virtual file system.
std::unique_ptr< FileDataload_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 Public Member Functions

static void SetVpakKey (const std::string &key)
 Sets the VPAK decryption key.
static std::string GetVpakKey ()
 Gets the current VPAK decryption key.

Private Member Functions

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 VPKFileEntryfind_file_entry (const std::string &virtual_path)
 Method to find a file entry by its virtual path.

Private Attributes

std::unique_ptr< LoadedVPKm_loaded_vpk
std::string m_base_path
bool m_use_packed_assets
std::mutex stream_mutex

Static Private Attributes

static std::string s_vpak_key = "FALLBACK_VPAK_KEY_0000"

Detailed Description

This class provides abstraction of file system needed for loading packed and unpacked assets.

Definition at line 26 of file VirtualFileSystem.hpp.

Constructor & Destructor Documentation

◆ VirtualFileSystem()

vex::VirtualFileSystem::VirtualFileSystem ( )

Constructor for VirtualFileSystem.

Definition at line 20 of file VirtualFileSystem.cpp.

◆ ~VirtualFileSystem()

vex::VirtualFileSystem::~VirtualFileSystem ( )

Definition at line 24 of file VirtualFileSystem.cpp.

Member Function Documentation

◆ clean_path()

std::string vex::VirtualFileSystem::clean_path ( const std::string & path)
private

cleans path from unwanted string eg "Assets/"

Definition at line 299 of file VirtualFileSystem.cpp.

◆ 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
    conststd::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()

const VirtualFileSystem::VPKFileEntry * vex::VirtualFileSystem::find_file_entry ( const std::string & virtual_path)
private

Method to find a file entry by its virtual path.

Definition at line 138 of file VirtualFileSystem.cpp.

◆ 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
conststd::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

Gets the current VPAK decryption key.

Definition at line 16 of file VirtualFileSystem.cpp.

◆ 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
conststd::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
    conststd::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()

std::unique_ptr< VirtualFileSystem::FileData > vex::VirtualFileSystem::load_file ( const std::string & virtual_path)

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
    conststd::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
conststd::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
    conststd::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
conststd::string& base_path - The context path (e.g., the path of the file requesting the resolve).
conststd::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

Sets the VPAK decryption key.

Definition at line 12 of file VirtualFileSystem.cpp.

Member Data Documentation

◆ m_base_path

std::string vex::VirtualFileSystem::m_base_path
private

Definition at line 158 of file VirtualFileSystem.hpp.

◆ m_loaded_vpk

std::unique_ptr<LoadedVPK> vex::VirtualFileSystem::m_loaded_vpk
private

Definition at line 157 of file VirtualFileSystem.hpp.

◆ m_use_packed_assets

bool vex::VirtualFileSystem::m_use_packed_assets
private

Definition at line 159 of file VirtualFileSystem.hpp.

◆ s_vpak_key

std::string vex::VirtualFileSystem::s_vpak_key = "FALLBACK_VPAK_KEY_0000"
staticprivate

Definition at line 104 of file VirtualFileSystem.hpp.

◆ stream_mutex

std::mutex vex::VirtualFileSystem::stream_mutex
private

Definition at line 161 of file VirtualFileSystem.hpp.


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