Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
BlackboardVariablePresetRegistry.cpp
Go to the documentation of this file.
1/**
2 * @file BlackboardVariablePresetRegistry.cpp
3 * @brief Implementation of BlackboardVariablePresetRegistry.
4 * @author Olympe Engine
5 * @date 2026-03-15
6 *
7 * C++14 compliant — no std::optional, structured bindings, std::filesystem.
8 */
9
11
12#include "../json_helper.h"
13#include "../system/system_utils.h"
14
15#include <algorithm>
16#include <fstream>
17#include <mutex>
18
19namespace Olympe {
20
21// ---------------------------------------------------------------------------
22// Singleton
23// ---------------------------------------------------------------------------
24
25/*static*/
31
32// ---------------------------------------------------------------------------
33// LoadFromFile
34// ---------------------------------------------------------------------------
35
36bool BlackboardVariablePresetRegistry::LoadFromFile(const std::string& filePath)
37{
38 Clear();
39
40 json root;
41 if (!JsonHelper::LoadJsonFromFile(filePath, root))
42 {
43 SYSTEM_LOG << "[BlackboardVariablePresetRegistry] LoadFromFile: failed to open '"
44 << filePath << "'\n";
45 return false;
46 }
47
48 if (!root.contains("availableVariables") || !root["availableVariables"].is_array())
49 {
50 SYSTEM_LOG << "[BlackboardVariablePresetRegistry] LoadFromFile: missing or invalid"
51 " 'availableVariables' array in '" << filePath << "'\n";
52 return false;
53 }
54
55 const json& arr = root["availableVariables"];
56 m_presets.reserve(arr.size());
57
58 for (size_t i = 0; i < arr.size(); ++i)
59 {
60 const json& item = arr[i];
61
62 if (!item.contains("name") || !item["name"].is_string())
63 {
64 SYSTEM_LOG << "[BlackboardVariablePresetRegistry] LoadFromFile: entry " << i
65 << " missing 'name' — skipping\n";
66 continue;
67 }
68
71 preset.type = JsonHelper::GetString(item, "type");
72 preset.description = JsonHelper::GetString(item, "description");
73 preset.category = JsonHelper::GetString(item, "category");
74 preset.defaultValue = JsonHelper::GetString(item, "default");
75
76 if (preset.name.empty())
77 {
78 SYSTEM_LOG << "[BlackboardVariablePresetRegistry] LoadFromFile: entry " << i
79 << " has empty 'name' — skipping\n";
80 continue;
81 }
82
83 if (m_nameIndex.find(preset.name) != m_nameIndex.end())
84 {
85 SYSTEM_LOG << "[BlackboardVariablePresetRegistry] LoadFromFile: duplicate preset '"
86 << preset.name << "' — skipping\n";
87 continue;
88 }
89
90 m_nameIndex[preset.name] = m_presets.size();
91 m_presets.push_back(preset);
92 }
93
94 // Sort by name for stable ordering
95 std::sort(m_presets.begin(), m_presets.end(),
97 return a.name < b.name;
98 });
99
100 // Rebuild index after sort
101 m_nameIndex.clear();
102 for (size_t i = 0; i < m_presets.size(); ++i)
103 m_nameIndex[m_presets[i].name] = i;
104
105 SYSTEM_LOG << "[BlackboardVariablePresetRegistry] LoadFromFile: loaded "
106 << m_presets.size() << " presets from '" << filePath << "'\n";
107 return true;
108}
109
110// ---------------------------------------------------------------------------
111// Accessors
112// ---------------------------------------------------------------------------
113
114const std::vector<BlackboardVariablePreset>&
116{
117 std::lock_guard<std::mutex> lock(m_mutex);
118 return m_presets;
119}
120
123{
124 std::unordered_map<std::string, size_t>::const_iterator it = m_nameIndex.find(name);
125 if (it == m_nameIndex.end())
126 return nullptr;
127 return &m_presets[it->second];
128}
129
130std::vector<BlackboardVariablePreset>
131BlackboardVariablePresetRegistry::GetByCategory(const std::string& category) const
132{
133 std::vector<BlackboardVariablePreset> result;
134 for (size_t i = 0; i < m_presets.size(); ++i)
135 {
136 if (m_presets[i].category == category)
137 result.push_back(m_presets[i]);
138 }
139 return result;
140}
141
143{
144 std::vector<std::string> categories;
145 for (size_t i = 0; i < m_presets.size(); ++i)
146 {
147 const std::string& cat = m_presets[i].category;
148 if (cat.empty())
149 continue;
150 bool found = false;
151 for (size_t j = 0; j < categories.size(); ++j)
152 {
153 if (categories[j] == cat)
154 {
155 found = true;
156 break;
157 }
158 }
159 if (!found)
160 categories.push_back(cat);
161 }
162 std::sort(categories.begin(), categories.end());
163 return categories;
164}
165
166bool BlackboardVariablePresetRegistry::HasPreset(const std::string& name) const
167{
168 return m_nameIndex.find(name) != m_nameIndex.end();
169}
170
172{
173 return m_presets.size();
174}
175
177{
178 m_presets.clear();
179 m_nameIndex.clear();
180}
181
182} // namespace Olympe
Singleton registry for blackboard variable presets loaded from JSON config.
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Singleton registry for global blackboard variable presets.
const BlackboardVariablePreset * GetPreset(const std::string &name) const
Returns a pointer to the preset with the given name, or nullptr.
std::mutex m_mutex
Guards m_presets during concurrent access.
std::vector< BlackboardVariablePreset > m_presets
size_t GetCount() const
Returns the number of loaded presets.
const std::vector< BlackboardVariablePreset > & GetAllPresets() const
Returns all loaded presets, sorted by name.
static BlackboardVariablePresetRegistry & Instance()
Returns the singleton instance.
std::unordered_map< std::string, size_t > m_nameIndex
name -> index in m_presets
bool LoadFromFile(const std::string &filePath)
Loads presets from a JSON file.
std::vector< BlackboardVariablePreset > GetByCategory(const std::string &category) const
Returns all presets belonging to a given category.
bool HasPreset(const std::string &name) const
Returns true if a preset with the given name exists.
void Clear()
Clears all loaded presets and resets the registry.
std::vector< std::string > GetAllCategories() const
Returns all unique category names present in the registry.
std::string GetString(const json &j, const std::string &key, const std::string &defaultValue="")
Safely get a string value from JSON.
bool LoadJsonFromFile(const std::string &filepath, json &j)
Load and parse a JSON file.
Definition json_helper.h:42
< Provides AssetID and INVALID_ASSET_ID
nlohmann::json json
Metadata for a single blackboard variable preset entry.
std::string category
Category label (e.g. "Targeting", "Movement")
std::string name
Variable key (e.g. "targetDistance")
#define SYSTEM_LOG