Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
ParameterSchema.h
Go to the documentation of this file.
1/*
2Olympe Engine V2 - 2025
3Nicolas Chereau
4nchereau@gmail.com
5
6This file is part of Olympe Engine V2.
7
8ParameterSchema purpose: Provides a schema registry for mapping blueprint parameters
9to ECS component fields with type validation and default values.
10*/
11
12#pragma once
13
14#include <string>
15#include <map>
16#include <set>
17#include <memory>
18#include "ComponentDefinition.h"
19
20// Forward declarations
21namespace nlohmann {
22 template <typename T> class basic_json;
24}
25
26// Parameter schema entry defining how a blueprint parameter maps to a component field
28{
29 std::string parameterName; // Name of the parameter in the blueprint
30 std::string targetComponent; // Target component type (e.g., "Position_data")
31 std::string targetField; // Target field name in the component
32 ComponentParameter::Type expectedType; // Expected parameter type
33 bool isRequired = false; // Is this parameter required?
34 ComponentParameter defaultValue; // Default value if not provided
35
37 ParameterSchemaEntry(const std::string& name,
38 const std::string& component,
39 const std::string& field,
41 bool required = false,
43 : parameterName(name)
46 , expectedType(type)
47 , isRequired(required)
49 {}
50};
51
52// Component schema containing all parameters for a component type
54{
55 std::string componentType; // Component type name
56 std::map<std::string, ParameterSchemaEntry> parameters; // Parameter name -> schema entry
57 std::set<std::string> requiredParams; // Set of required parameter names
58
59 ComponentSchema() = default;
60 explicit ComponentSchema(const std::string& type) : componentType(type) {}
61};
62
63// Forward declarations
64struct PrefabBlueprint;
65
66// Singleton registry for parameter schemas
68{
69public:
70 // Get singleton instance
72 {
74 instance.EnsureInitialized(); // Auto-initialize on first access
75 return instance;
76 }
77
78 // Delete copy/move constructors
83
84 // Initialize built-in schemas for standard components (public for manual reinit if needed)
86
87 // Load schema from JSON file (new unified method)
88 bool LoadFromJSON(const std::string& filepath);
89
90 // Legacy method for backward compatibility
91 bool LoadSchemaFromFile(const std::string& filepath);
92
93 // Get count of registered schemas
94 size_t GetSchemaCount() const;
95
96 // Find a parameter schema entry by parameter name (checks all components)
97 const ParameterSchemaEntry* FindParameterSchema(const std::string& parameterName) const;
98
99 // Get complete schema for a specific component type
100 const ComponentSchema* GetComponentSchema(const std::string& componentType) const;
101
102 // Validate a parameter against its schema
103 bool ValidateParameter(const std::string& parameterName, const ComponentParameter& param) const;
104
105 // Register a new parameter schema entry
107
108 // Automatic schema discovery methods
111
112private:
113 // Private constructor for singleton
116
117 // Auto-initialization
118 bool isInitialized_ = false;
119 void EnsureInitialized();
120
121 // Auto-register discovered parameter
123 const std::string& componentType,
124 const std::string& paramName,
126 const ComponentParameter& defaultValue
127 );
128
129 // Helper methods for JSON loading
132
133 // Storage
134 std::map<std::string, ComponentSchema> componentSchemas_; // Component type -> schema
135 std::map<std::string, std::string> parameterToComponent_; // Parameter name -> component type (for fast lookup)
136 std::map<std::string, std::string> aliasToParameter_; // Alias -> canonical parameter name
137};
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
ComponentParameter ParseDefaultValue(const nlohmann::json &valueJson, ComponentParameter::Type type) const
const ParameterSchemaEntry * FindParameterSchema(const std::string &parameterName) const
void RegisterParameterSchema(const ParameterSchemaEntry &entry)
ParameterSchemaRegistry()=default
bool LoadSchemaFromFile(const std::string &filepath)
void DiscoverComponentSchema(const ComponentDefinition &componentDef)
bool ValidateParameter(const std::string &parameterName, const ComponentParameter &param) const
bool LoadFromJSON(const std::string &filepath)
static ParameterSchemaRegistry & GetInstance()
const ComponentSchema * GetComponentSchema(const std::string &componentType) const
void DiscoverSchemasFromPrefab(const PrefabBlueprint &prefab)
std::map< std::string, ComponentSchema > componentSchemas_
ParameterSchemaRegistry & operator=(const ParameterSchemaRegistry &)=delete
ParameterSchemaRegistry(ParameterSchemaRegistry &&)=delete
std::map< std::string, std::string > aliasToParameter_
void AutoRegisterParameter(const std::string &componentType, const std::string &paramName, ComponentParameter::Type paramType, const ComponentParameter &defaultValue)
ComponentParameter::Type StringToParameterType(const std::string &typeStr) const
ParameterSchemaRegistry & operator=(ParameterSchemaRegistry &&)=delete
ParameterSchemaRegistry(const ParameterSchemaRegistry &)=delete
std::map< std::string, std::string > parameterToComponent_
~ParameterSchemaRegistry()=default
nlohmann::json json
std::set< std::string > requiredParams
ComponentSchema()=default
std::string componentType
std::map< std::string, ParameterSchemaEntry > parameters
ComponentSchema(const std::string &type)
ComponentParameter defaultValue
ParameterSchemaEntry(const std::string &name, const std::string &component, const std::string &field, ComponentParameter::Type type, bool required=false, const ComponentParameter &defValue=ComponentParameter())
ComponentParameter::Type expectedType
std::string targetComponent
ParameterSchemaEntry()=default