mirror of
https://github.com/antopilo/Nuake.git
synced 2026-09-15 20:08:54 +03:00
Started reflection based component serialization
This commit is contained in:
@@ -1,9 +1,17 @@
|
||||
#pragma once
|
||||
#include "Nuake/Core/Core.h"
|
||||
#include "Nuake/Core/Maths.h"
|
||||
#include "Nuake/Scene/Components/Component.h"
|
||||
#include "Nuake/Resource/Serializable.h"
|
||||
#include "Nuake/FileSystem/File.h"
|
||||
|
||||
#include <concepts>
|
||||
|
||||
namespace Nuake
|
||||
{
|
||||
template<typename T>
|
||||
concept IsComponentT = std::derived_from<T, Component>;
|
||||
|
||||
// This will output JSON data from any component using the reflection data
|
||||
class ComponentSerializer
|
||||
{
|
||||
@@ -12,26 +20,70 @@ namespace Nuake
|
||||
~ComponentSerializer() = default;
|
||||
|
||||
public:
|
||||
template<typename T>
|
||||
json Serialize(const T& component)
|
||||
template<IsComponentT T>
|
||||
json Serialize(T& component)
|
||||
{
|
||||
json jsonSnippet;
|
||||
|
||||
const entt::meta_type& meta = entt::resolve<T>();
|
||||
const entt::meta_any& metaAny = meta.from_void(static_cast<void*>(&component));
|
||||
const std::string componentName = Component::GetName(meta);
|
||||
|
||||
json cursor = j[componentName];
|
||||
json& cursor = jsonSnippet[componentName];
|
||||
for (auto [fst, dataType] : meta.data())
|
||||
{
|
||||
const std::string fieldName = fst.name();
|
||||
const entt::meta_any value = meta.data(fieldName).get(component);
|
||||
if (value.type() == entt::resolve<bool>())
|
||||
const ComponentFieldTrait fieldTraits = dataType.traits<ComponentFieldTrait>();
|
||||
entt::id_type dataId = dataType.type().id();
|
||||
|
||||
auto propName = dataType.prop(HashedName::DisplayName);
|
||||
const char* displayNameC = *propName.value().try_cast<const char*>();
|
||||
std::string displayName = displayNameC;
|
||||
auto fieldVal = dataType.get(metaAny);
|
||||
if (dataType.type() == entt::resolve<float>())
|
||||
{
|
||||
cursor[fieldName] = value.cast<bool>();
|
||||
float value = fieldVal.cast<float>();
|
||||
cursor[displayName] = value;
|
||||
}
|
||||
else if (value.type() == entt::resolve<std::string>())
|
||||
else if(dataType.type() == entt::resolve<int32_t>())
|
||||
{
|
||||
cursor[fieldName] = value.cast<std::string>();
|
||||
int32_t value = fieldVal.cast<int32_t>();
|
||||
cursor[displayName] = value;
|
||||
}
|
||||
else if (dataType.type() == entt::resolve<bool>())
|
||||
{
|
||||
bool value = fieldVal.cast<bool>();
|
||||
cursor[displayName] = value;
|
||||
}
|
||||
else if (dataType.type() == entt::resolve<Vector2>())
|
||||
{
|
||||
Vector2 value = fieldVal.cast<Vector2>();
|
||||
cursor[displayName]["x"] = value.x;
|
||||
cursor[displayName]["y"] = value.y;
|
||||
}
|
||||
else if (dataType.type() == entt::resolve<Vector3>())
|
||||
{
|
||||
Vector3 value = fieldVal.cast<Vector3>();
|
||||
cursor[displayName]["x"] = value.x;
|
||||
cursor[displayName]["y"] = value.y;
|
||||
cursor[displayName]["z"] = value.z;
|
||||
}
|
||||
else if (dataType.type() == entt::resolve<Vector4>())
|
||||
{
|
||||
Vector4 value = fieldVal.cast<Vector4>();
|
||||
cursor[displayName]["x"] = value.x;
|
||||
cursor[displayName]["y"] = value.y;
|
||||
cursor[displayName]["z"] = value.z;
|
||||
cursor[displayName]["w"] = value.w;
|
||||
}
|
||||
else if (dataType.type() == entt::resolve<ResourceFile>())
|
||||
{
|
||||
ResourceFile value = fieldVal.cast<ResourceFile>();
|
||||
bool validFile = value.file != nullptr && value.file->Exist();
|
||||
cursor["validFile" + displayName] = validFile;
|
||||
if (validFile)
|
||||
{
|
||||
cursor["file" + displayName] = value.file->GetRelativePath();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace Nuake
|
||||
private:
|
||||
|
||||
public:
|
||||
virtual void VisitNameComponent(const NameComponent& name)
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user