Loading...
Searching...
No Matches
AssetRegistry.h
Go to the documentation of this file.
1//
2// Created by denzel on 31/10/2025.
3//
4
5#pragma once
6#include <filesystem>
7#include <string>
8#include <unordered_map>
9
10namespace hellfire {
11 using AssetID = uint64_t;
12 constexpr AssetID INVALID_ASSET_ID = 0;
13
14 enum class AssetType {
15 MODEL,
16 MESH,
17 TEXTURE,
19 SCENE,
20 SHADER,
21 UNKNOWN,
22 };
23
25 uint64_t uuid;
26 std::filesystem::path filepath; // Relative to project root
28 std::string name;
29 uint64_t last_modified; // For detecting changes
30 };
31
32 /**
33 * @brief Registry for storing assets
34 */
36 public:
37 explicit AssetRegistry(const std::filesystem::path &registry_file, const std::filesystem::path &project_root);
38
40
41 // Registration
42 AssetID register_asset(const std::filesystem::path &filepath);
43 AssetID register_asset(const std::filesystem::path &filepath, AssetType type);
44 void unregister_asset(AssetID uuid);
45
46 std::vector<AssetID> register_directory(const std::filesystem::path &directory_path, bool recursive);
47 void refresh_assets(); // Re-scan and update modified times
48
49 // Lookup
50 std::optional<AssetMetadata> get_asset(AssetID uuid) const;
51 std::optional<AssetID> get_uuid_by_path(const std::filesystem::path &filepath);
54
55 // Path utilities
56 bool asset_exists(AssetID uuid) const;
57 std::filesystem::path get_absolute_path(AssetID uuid);
58 std::filesystem::path get_relative_path(AssetID uuid);
59
60 bool has_asset_changed(AssetID uuid) const;
62
63 // Serialization
64 void save();
65 bool load();
66
67 // Utility methods
68 void clear();
69 size_t get_asset_count() const { return assets_.size(); }
70
71 // Project root management
72 void set_project_root(const std::filesystem::path &project_root);
73 const std::filesystem::path& get_project_root() { return project_root_; }
74
75 static AssetType get_type_from_extension(const std::filesystem::path &filepath);
76 private:
77 std::unordered_map<AssetID, AssetMetadata> assets_;
78 std::unordered_map<std::filesystem::path, AssetID> path_to_uuid_;
79 std::filesystem::path registry_file_;
80 std::filesystem::path project_root_;
81
82 // Generate UUID from filepath
83 AssetID generate_uuid(const std::filesystem::path &filepath);
84
85 // Convert between absolute and relative paths
86 std::filesystem::path to_relative_path(const std::filesystem::path &absolute_path) const;
87 std::filesystem::path to_absolute_path(const std::filesystem::path &relative_path) const;
88
89 // Internal helpers
90 void rebuild_path_map();
91 uint64_t get_file_last_modified(const std::filesystem::path &filepath) const;
92 };
93}
std::shared_ptr< Texture > get_texture(AssetID id)
size_t get_loaded_mesh_count() const
std::unordered_map< AssetID, std::shared_ptr< Texture > > texture_cache_
AssetManager(AssetRegistry &registry)
std::shared_ptr< Mesh > get_mesh(AssetID id)
size_t get_loaded_material_count() const
void unload(AssetID id)
AssetRegistry & registry_
std::unordered_map< AssetID, std::shared_ptr< Material > > material_cache_
size_t get_loaded_texture_count() const
std::shared_ptr< Material > get_material(AssetID id)
std::unordered_map< AssetID, std::shared_ptr< Mesh > > mesh_cache_
Registry for storing assets.
const std::filesystem::path & get_project_root()
std::optional< AssetID > get_uuid_by_path(const std::filesystem::path &filepath)
AssetID generate_uuid(const std::filesystem::path &filepath)
void unregister_asset(AssetID uuid)
bool has_asset_changed(AssetID uuid) const
void set_project_root(const std::filesystem::path &project_root)
std::filesystem::path get_absolute_path(AssetID uuid)
std::unordered_map< std::filesystem::path, AssetID > path_to_uuid_
std::filesystem::path registry_file_
AssetID register_asset(const std::filesystem::path &filepath, AssetType type)
std::unordered_map< AssetID, AssetMetadata > assets_
std::filesystem::path to_absolute_path(const std::filesystem::path &relative_path) const
static AssetType get_type_from_extension(const std::filesystem::path &filepath)
std::filesystem::path to_relative_path(const std::filesystem::path &absolute_path) const
AssetID register_asset(const std::filesystem::path &filepath)
std::vector< AssetID > register_directory(const std::filesystem::path &directory_path, bool recursive)
std::vector< AssetMetadata > get_assets_by_type(AssetType type)
std::filesystem::path get_relative_path(AssetID uuid)
bool asset_exists(AssetID uuid) const
size_t get_asset_count() const
std::vector< AssetID > get_modified_assets() const
AssetRegistry(const std::filesystem::path &registry_file, const std::filesystem::path &project_root)
std::optional< AssetMetadata > get_asset(AssetID uuid) const
std::filesystem::path project_root_
std::vector< AssetMetadata > get_all_assets() const
uint64_t get_file_last_modified(const std::filesystem::path &filepath) const
static std::optional< MaterialData > load(const std::filesystem::path &filepath)
static std::shared_ptr< Mesh > load(const std::filesystem::path &filepath)
constexpr AssetID INVALID_ASSET_ID
std::filesystem::path filepath