Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
AssetManager.h
Go to the documentation of this file.
1/**
2 * @file AssetManager.h
3 * @brief Central cache for loaded engine assets, including TaskGraphTemplate instances.
4 * @author Olympe Engine
5 * @date 2026-02-22
6 *
7 * @details
8 * AssetManager is a singleton that loads, caches, and manages the lifetime of
9 * shared engine assets. Phase 1.3 adds support for TaskGraphTemplate assets:
10 *
11 * - LoadTaskGraph() : load from file (or return cached entry) -> AssetID
12 * - GetTaskGraph() : retrieve a cached template by AssetID
13 * - UnloadTaskGraph() : release a cached template from memory
14 *
15 * Asset IDs are 32-bit FNV-1a hashes of the normalised file path so that
16 * the same path always yields the same ID without requiring a central registry.
17 *
18 * Ownership:
19 * AssetManager owns all loaded TaskGraphTemplate instances via unique_ptr.
20 * Callers receive a raw pointer (non-owning) from GetTaskGraph().
21 * The pointer remains valid until UnloadTaskGraph() or ~AssetManager() is called.
22 *
23 * C++14 compliant - no std::variant, std::optional, or C++17/20 features.
24 */
25
26#pragma once
27
28#include <string>
29#include <vector>
30#include <unordered_map>
31#include <memory>
32#include <cstdint>
33
34#include "../TaskSystem/TaskGraphTemplate.h"
35
36namespace Olympe {
37
38// ============================================================================
39// Type aliases
40// ============================================================================
41
42/// Opaque asset identifier: 32-bit FNV-1a hash of the asset file path.
44
45/// Sentinel value indicating an invalid / unloaded asset.
46static const AssetID INVALID_ASSET_ID = 0u;
47
48// ============================================================================
49// AssetManager
50// ============================================================================
51
52/**
53 * @class AssetManager
54 * @brief Singleton cache for engine assets.
55 *
56 * @details
57 * Call Get() to obtain the singleton instance. All TaskGraphTemplate assets
58 * loaded through LoadTaskGraph() are owned by AssetManager; callers must not
59 * delete the pointers returned by GetTaskGraph().
60 */
62public:
63
64 // -----------------------------------------------------------------------
65 // Singleton
66 // -----------------------------------------------------------------------
67
68 /**
69 * @brief Returns the singleton AssetManager instance.
70 */
71 static AssetManager& Get();
72
73 // -----------------------------------------------------------------------
74 // TaskGraph asset API
75 // -----------------------------------------------------------------------
76
77 /**
78 * @brief Loads a TaskGraphTemplate from @p path and caches it.
79 *
80 * If the asset has already been loaded (same path hash), the cached
81 * AssetID is returned immediately without re-reading the file.
82 *
83 * @param path Absolute or relative path to the JSON task graph file.
84 * @param outErrors Receives human-readable error messages on failure.
85 * @return A non-zero AssetID on success, or INVALID_ASSET_ID on failure.
86 *
87 * @note AssetManager owns the loaded template. Do NOT delete the pointer
88 * returned by GetTaskGraph().
89 */
90 AssetID LoadTaskGraph(const std::string& path,
91 std::vector<std::string>& outErrors);
92
93 /**
94 * @brief Returns a non-owning pointer to the cached TaskGraphTemplate.
95 *
96 * @param id AssetID previously returned by LoadTaskGraph().
97 * @return Pointer to the template, or nullptr if @p id is unknown.
98 *
99 * @note The returned pointer is valid until UnloadTaskGraph() or the
100 * AssetManager is destroyed. Do NOT delete it.
101 */
102 const TaskGraphTemplate* GetTaskGraph(AssetID id) const;
103
104 /**
105 * @brief Loads a TaskGraphTemplate from @p path and returns a direct pointer.
106 *
107 * Convenience wrapper around LoadTaskGraph() + GetTaskGraph() for use in
108 * code that needs a template pointer without managing AssetIDs (e.g.,
109 * SubGraph execution in VSGraphExecutor).
110 *
111 * @param path Absolute or relative path to the JSON task graph file.
112 * @param outErrors Receives human-readable error messages on failure.
113 * @return Non-owning pointer to the template, or nullptr on failure.
114 *
115 * @note AssetManager retains ownership. Do NOT delete the returned pointer.
116 */
117 const TaskGraphTemplate* LoadTaskGraphFromFile(const std::string& path,
118 std::vector<std::string>& outErrors);
119
120 /**
121 * @brief Releases the cached TaskGraphTemplate for @p id.
122 *
123 * After this call, any raw pointers previously obtained from
124 * GetTaskGraph() for the same @p id are dangling and must not be used.
125 *
126 * @param id AssetID returned by LoadTaskGraph().
127 */
128 void UnloadTaskGraph(AssetID id);
129
130 // -----------------------------------------------------------------------
131 // Utilities
132 // -----------------------------------------------------------------------
133
134 /**
135 * @brief Computes the 32-bit FNV-1a hash of a file path string.
136 *
137 * The hash is used as the AssetID so that the same path always maps to
138 * the same identifier. Exposed publicly so callers can pre-compute IDs.
139 *
140 * @param path Any non-empty string.
141 * @return 32-bit FNV-1a hash. Returns INVALID_ASSET_ID (0) only if the
142 * input is empty.
143 */
144 static AssetID ComputeAssetID(const std::string& path);
145
146private:
147
148 // Prevent external construction / copying
149 AssetManager() = default;
150 ~AssetManager() = default;
151 AssetManager(const AssetManager&) = delete;
153
154 // -----------------------------------------------------------------------
155 // Members
156 // -----------------------------------------------------------------------
157
158 /// Cached task graph templates: AssetID -> owned template.
159 std::unordered_map<AssetID, std::unique_ptr<TaskGraphTemplate>> m_taskGraphs;
160};
161
162} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Singleton cache for engine assets.
AssetManager & operator=(const AssetManager &)=delete
std::unordered_map< AssetID, std::unique_ptr< TaskGraphTemplate > > m_taskGraphs
Cached task graph templates: AssetID -> owned template.
AssetID LoadTaskGraph(const std::string &path, std::vector< std::string > &outErrors)
Loads a TaskGraphTemplate from path and caches it.
static AssetID ComputeAssetID(const std::string &path)
Computes the 32-bit FNV-1a hash of a file path string.
const TaskGraphTemplate * LoadTaskGraphFromFile(const std::string &path, std::vector< std::string > &outErrors)
Loads a TaskGraphTemplate from path and returns a direct pointer.
void UnloadTaskGraph(AssetID id)
Releases the cached TaskGraphTemplate for id.
AssetManager(const AssetManager &)=delete
const TaskGraphTemplate * GetTaskGraph(AssetID id) const
Returns a non-owning pointer to the cached TaskGraphTemplate.
static AssetManager & Get()
Returns the singleton AssetManager instance.
Immutable, shareable task graph asset.
< Provides AssetID and INVALID_ASSET_ID
uint32_t AssetID
Opaque asset identifier: 32-bit FNV-1a hash of the asset file path.
static const AssetID INVALID_ASSET_ID
Sentinel value indicating an invalid / unloaded asset.