4#include <foundation/Common.h>
5#include <foundation/Random.h>
7#include <nlohmann/json.hpp>
11#include "StreamUtils.h"
16#define INT_BASED_PARAMETER(name, type) \
17 class name : public IParameter<type> \
20 name(const String ¶mName, const type &defaultValue) : IParameter<type>(paramName, defaultValue) {} \
22 int32 Write(nlohmann::json &target) const override \
24 target["value"] = m_Value; \
25 return sizeof(type); \
28 int32 Read(nlohmann::json &target) override \
30 m_Value = target["value"].get<type>(); \
31 return sizeof(type); \
34 type IncrementAndGet() \
36 SetValue(GetValue() + 1); \
40 type GetAndIncrement() \
42 type value = GetValue(); \
43 SetValue(GetValue() + 1); \
47 type DecrementAndGet() \
49 SetValue(GetValue() - 1); \
53 type GetAndDecrement() \
55 type value = GetValue(); \
56 SetValue(GetValue() - 1); \
79 virtual int32
Write(nlohmann::json &target)
const = 0;
87 virtual int32
Read(nlohmann::json &target) = 0;
89 virtual void SetDisplayName(
const String &name) = 0;
116 IParameter(
const String &name,
const T &defaultValue) : m_Value(defaultValue), m_Name(name), m_DisplayName(name)
120 ~IParameter()
override =
default;
134 [[nodiscard]] T
GetValue()
const {
return m_Value; }
136 void SetDisplayName(
const String &name)
override { m_DisplayName = name; }
153 INT_BASED_PARAMETER(Int8Parameter, int8);
154 INT_BASED_PARAMETER(IntParameter, int32);
155 INT_BASED_PARAMETER(Int64Parameter, int64);
156 INT_BASED_PARAMETER(BoolParameter,
bool);
159#define WIDEN2(x) L##x
160#define WIDEN(x) WIDEN2(x)
161#define DEFINE_ENUM_VALUE(parameter, Tenum, value, ...) parameter.Bind(Tenum::value, WIDEN(#value));
167 class BaseEnumParameter :
public IParameter<int32>
176 BaseEnumParameter(
const String &name, int32 defaultValue) : IParameter(name, defaultValue) {}
179 int32
Write(nlohmann::json &target)
const override
181 target[
"value"] = m_Value;
182 return sizeof(int32);
186 int32
Read(nlohmann::json &target)
override
188 m_Value = target[
"value"];
189 return sizeof(int32);
215 class EnumParameter :
public BaseEnumParameter
218 EnumParameter(
const String &name,
const T &defaultValue) :
219 BaseEnumParameter(name,
static_cast<int32
>(defaultValue))
236 void SetEnum(
const T &value) { m_Value =
static_cast<int32
>(value); }
242 T
GetEnum()
const {
return static_cast<T
>(m_Value); }
249 class StringParameter :
public IParameter<String>
252 StringParameter(
const String &name,
const String &defaultValue) : IParameter(name, defaultValue) {}
255 int32
Write(nlohmann::json &target)
const override
257 target[
"value"] = m_Value.Get();
258 return m_Value.GetLength();
262 int32
Read(nlohmann::json &target)
override
264 m_Value = target[
"value"].get<std::string>();
265 return m_Value.GetLength();
276 virtual ~GameObject() =
default;
279 friend class GameSerializer;
280 friend class GlobalGameObjectRepository;
286 std::vector<IBaseParameter *> m_Parameters;
314 const auto &iterator = std::ranges::find_if(m_Parameters, [&name](
IBaseParameter *parameter)
315 {
return parameter->
GetName() == name; });
317 if (iterator == m_Parameters.end())
319 ERR(
"GameObject::findParameter: Couldn't find parameter with name '{}'", name);
357 std::vector<IBaseParameter *>
GetParameters()
const {
return m_Parameters; }
373 friend class GameSerializer;
376 std::unordered_map<String, GameObject *> m_GameObjects;
378 std::unordered_map<String, GameObject *> &getMap() {
return m_GameObjects; }
379 const std::unordered_map<String, GameObject *> &getMap()
const {
return m_GameObjects; }
392 GameObject *
object = m_GameObjects.at(identifier);
402 const int32 objects =
static_cast<int32
>(m_GameObjects.size());
403 for (
const auto &[key,
object]: m_GameObjects)
405 if (key.StartsWith(
"Global::"))
410 m_GameObjects.clear();
412 INFO(
"[GameObjectRegistry::Clear] Purged {} items", objects);
423 auto it = std::find_if(m_GameObjects.begin(), m_GameObjects.end(),
424 [identifier](
const auto &pair) { return pair.second->GetUniqueId() == identifier; });
426 if (it != m_GameObjects.end())
440 m_GameObjects.insert(std::make_pair(name,
object));
448 class ParentGameObject :
public GameObject,
protected std::vector<GameObject *>
450 friend class GameSerializer;
453 ParentGameObject() : GameObject() {}
455 explicit ParentGameObject(
const String &identifier) : GameObject(identifier) {}
462 virtual void Add(GameObject *
object) { this->push_back(
object); }
472 friend class GameSerializer;
473 inline static std::unordered_map<String, GameObject *> m_Registry;
482 for (
auto &
object: m_Registry | std::views::values)
493 m_Registry.insert(std::make_pair(object->
GetIdentifer(),
object));
502 if (m_Registry.find(name) == m_Registry.end())
504 ERR(
"GlobalGameObjectRepository::CreateGameObjectInstance: returned nullptr for name {}", name);
508 return m_Registry.find(name)->second->
clone();
std::unordered_map< int32, WideString > m_Bindings
Enum bindings.
Definition GameObject.h:174
int32 GetBindingCount() const
Returns binding count.
Definition GameObject.h:206
int32 Read(nlohmann::json &target) override
Parses text representation of data from JSON object.
Definition GameObject.h:186
WideString GetBinding(int32 index) const
Returns bound name for enum value.
Definition GameObject.h:199
int32 Write(nlohmann::json &target) const override
Writes text representation of data to JSON object.
Definition GameObject.h:179
void SetEnum(const T &value)
Casts enum to integer and sets it.
Definition GameObject.h:236
void Bind(const T &value, const WideString &text)
Sets binding for given enum value.
Definition GameObject.h:229
T GetEnum() const
Casts integer to enum and returns.
Definition GameObject.h:242
Represents named set of parameters which can be serialized.
Definition GameObject.h:274
virtual void onPostLoad()
Event called after object load from file.
Definition GameObject.h:343
void addParameter(IBaseParameter *parameter)
Adds parameter to the list.
Definition GameObject.h:304
virtual String GetIdentifer() const =0
Returns common identifier of GameObject. To be filled by derived class.
virtual GameObject * clone()=0
Clones GameObject.
IBaseParameter * findParameter(const String &name)
Returns parameter with given name.
Definition GameObject.h:312
StringParameter m_Identifier
Random GameObject identifier.
Definition GameObject.h:293
virtual void onPreSerialize()
Event called before object gets serialized.
Definition GameObject.h:349
std::vector< IBaseParameter * > GetParameters() const
Returns parameters list.
Definition GameObject.h:357
String GetUniqueId() const
Returns GameObject unique id.
Definition GameObject.h:364
Storage of GameObjects (scene). GameObjectRegistry take over ownership of object.
Definition GameObject.h:372
void RegisterGameObject(const String &name, GameObject *object)
Registers given GameObject.
Definition GameObject.h:438
GameObject * GetGameObjectByUniqueId(const String &identifier)
Find GameObject with unique id.
Definition GameObject.h:421
GameObject * GetGameObject(const String &identifier)
Find GameObject with given name.
Definition GameObject.h:390
void Clear()
Deletes all GameObject from registry.
Definition GameObject.h:400
Contains all GameObjects associated with name. Used by serialization system.
Definition GameObject.h:470
static void Clear()
Deletes all objects in repository.
Definition GameObject.h:480
static GameObject * CreateGameObjectInstance(const String &name)
Creates a GameObject instance.
Definition GameObject.h:500
static void RegisterGameObject(GameObject *object)
Registers GameObject in repository.
Definition GameObject.h:491
Represents parameter which can be serializable. Used by IParameter.
Definition GameObject.h:69
virtual String GetDisplayName() const =0
Returns parameter display name.
virtual int32 Read(nlohmann::json &target)=0
Parses text representation of data from JSON object.
virtual String GetName() const =0
Returns parameter name.
virtual int32 Write(nlohmann::json &target) const =0
Writes text representation of data to JSON object.
T GetValue() const
Returns value.
Definition GameObject.h:134
String GetDisplayName() const override
Returns parameter display name.
Definition GameObject.h:150
String GetName() const override
Returns parameter name.
Definition GameObject.h:143
void SetValue(const T &value)
Sets value.
Definition GameObject.h:127
virtual void Add(GameObject *object)
Adds GameObject to parent.
Definition GameObject.h:462
String based parameter.
Definition GameObject.h:250
int32 Read(nlohmann::json &target) override
Parses text representation of data from JSON object.
Definition GameObject.h:262
int32 Write(nlohmann::json &target) const override
Writes text representation of data to JSON object.
Definition GameObject.h:255
A utility which contains essential function for pseudorandom generating.
Definition Random.h:16
Represents a mutable sequence of characters, providing various member functions for string manipulati...
Definition String.h:16
Represents a mutable sequence of utf-16 characters, providing various member functions for string man...
Definition WideString.h:14