diff --git a/Nuake/Source/Nuake/Resource/Serializer/ComponentSerializer.h b/Nuake/Source/Nuake/Resource/Serializer/ComponentSerializer.h index ec843781..264cc83b 100644 --- a/Nuake/Source/Nuake/Resource/Serializer/ComponentSerializer.h +++ b/Nuake/Source/Nuake/Resource/Serializer/ComponentSerializer.h @@ -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 namespace Nuake { + template + concept IsComponentT = std::derived_from; + // This will output JSON data from any component using the reflection data class ComponentSerializer { @@ -12,26 +20,70 @@ namespace Nuake ~ComponentSerializer() = default; public: - template - json Serialize(const T& component) + template + json Serialize(T& component) { json jsonSnippet; const entt::meta_type& meta = entt::resolve(); + const entt::meta_any& metaAny = meta.from_void(static_cast(&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()) + const ComponentFieldTrait fieldTraits = dataType.traits(); + entt::id_type dataId = dataType.type().id(); + + auto propName = dataType.prop(HashedName::DisplayName); + const char* displayNameC = *propName.value().try_cast(); + std::string displayName = displayNameC; + auto fieldVal = dataType.get(metaAny); + if (dataType.type() == entt::resolve()) { - cursor[fieldName] = value.cast(); + float value = fieldVal.cast(); + cursor[displayName] = value; } - else if (value.type() == entt::resolve()) + else if(dataType.type() == entt::resolve()) { - cursor[fieldName] = value.cast(); + int32_t value = fieldVal.cast(); + cursor[displayName] = value; + } + else if (dataType.type() == entt::resolve()) + { + bool value = fieldVal.cast(); + cursor[displayName] = value; + } + else if (dataType.type() == entt::resolve()) + { + Vector2 value = fieldVal.cast(); + cursor[displayName]["x"] = value.x; + cursor[displayName]["y"] = value.y; + } + else if (dataType.type() == entt::resolve()) + { + Vector3 value = fieldVal.cast(); + cursor[displayName]["x"] = value.x; + cursor[displayName]["y"] = value.y; + cursor[displayName]["z"] = value.z; + } + else if (dataType.type() == entt::resolve()) + { + Vector4 value = fieldVal.cast(); + 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 value = fieldVal.cast(); + bool validFile = value.file != nullptr && value.file->Exist(); + cursor["validFile" + displayName] = validFile; + if (validFile) + { + cursor["file" + displayName] = value.file->GetRelativePath(); + } } } diff --git a/Nuake/Source/Nuake/Serialization/ComponentSerializer.h b/Nuake/Source/Nuake/Serialization/ComponentSerializer.h index eca4f041..3d6236ab 100644 --- a/Nuake/Source/Nuake/Serialization/ComponentSerializer.h +++ b/Nuake/Source/Nuake/Serialization/ComponentSerializer.h @@ -7,7 +7,7 @@ namespace Nuake private: public: - virtual void VisitNameComponent(const NameComponent& name) + }; } \ No newline at end of file