Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
AssetManager.cpp
Go to the documentation of this file.
1/**
2 * @file AssetManager.cpp
3 * @brief Implementation of AssetManager: TaskGraph loading, caching, and retrieval.
4 * @author Olympe Engine
5 * @date 2026-02-22
6 */
7
8#include "../Core/AssetManager.h"
9
10#include "../TaskSystem/TaskGraphLoader.h"
11#include "../system/system_utils.h"
12
13namespace Olympe {
14
15// ============================================================================
16// Singleton
17// ============================================================================
18
20{
21 static AssetManager s_instance;
22 return s_instance;
23}
24
25// ============================================================================
26// Utilities
27// ============================================================================
28
29AssetID AssetManager::ComputeAssetID(const std::string& path)
30{
31 if (path.empty())
32 {
33 return INVALID_ASSET_ID;
34 }
35
36 // 32-bit FNV-1a hash
37 static const uint32_t FNV_PRIME = 16777619u;
38 static const uint32_t FNV_OFFSET = 2166136261u;
39
41 for (size_t i = 0; i < path.size(); ++i)
42 {
43 hash ^= static_cast<uint32_t>(static_cast<unsigned char>(path[i]));
44 hash *= FNV_PRIME;
45 }
46
47 // Ensure we never return the sentinel value for a non-empty path.
49 {
50 hash = 1u;
51 }
52
53 return hash;
54}
55
56// ============================================================================
57// TaskGraph asset API
58// ============================================================================
59
60AssetID AssetManager::LoadTaskGraph(const std::string& path,
61 std::vector<std::string>& outErrors)
62{
63 if (path.empty())
64 {
65 outErrors.push_back("[AssetManager] LoadTaskGraph: path is empty");
66 return INVALID_ASSET_ID;
67 }
68
69 const AssetID id = ComputeAssetID(path);
70
71 // Return cached entry immediately if already loaded.
72 auto it = m_taskGraphs.find(id);
73 if (it != m_taskGraphs.end())
74 {
75 SYSTEM_LOG << "[AssetManager] LoadTaskGraph: cache hit for '"
76 << path << "' (id=" << id << ")" << std::endl;
77 return id;
78 }
79
80 // Load from disk via TaskGraphLoader.
82 if (raw == nullptr)
83 {
84 SYSTEM_LOG << "[AssetManager] LoadTaskGraph: failed to load '"
85 << path << "'" << std::endl;
86 return INVALID_ASSET_ID;
87 }
88
89 m_taskGraphs[id] = std::unique_ptr<TaskGraphTemplate>(raw);
90
91 SYSTEM_LOG << "[AssetManager] LoadTaskGraph: loaded '"
92 << path << "' (id=" << id << ")" << std::endl;
93
94 return id;
95}
96
98{
99 auto it = m_taskGraphs.find(id);
100 if (it == m_taskGraphs.end())
101 {
102 return nullptr;
103 }
104 return it->second.get();
105}
106
108 std::vector<std::string>& outErrors)
109{
110 AssetID id = LoadTaskGraph(path, outErrors);
111 if (id == INVALID_ASSET_ID)
112 {
113 return nullptr;
114 }
115 return GetTaskGraph(id);
116}
117
119{
120 auto it = m_taskGraphs.find(id);
121 if (it == m_taskGraphs.end())
122 {
123 SYSTEM_LOG << "[AssetManager] UnloadTaskGraph: id=" << id
124 << " not found in cache" << std::endl;
125 return;
126 }
127
128 m_taskGraphs.erase(it);
129
130 SYSTEM_LOG << "[AssetManager] UnloadTaskGraph: released id=" << id << std::endl;
131}
132
133} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Singleton cache for engine assets.
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.
const TaskGraphTemplate * GetTaskGraph(AssetID id) const
Returns a non-owning pointer to the cached TaskGraphTemplate.
static AssetManager & Get()
Returns the singleton AssetManager instance.
static TaskGraphTemplate * LoadFromFile(const std::string &path, std::vector< std::string > &outErrors)
Loads a TaskGraphTemplate from a JSON file on disk.
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.
#define SYSTEM_LOG