#pragma once #include "DeerCore/Serialization/WorldSettings.h" #include "DeerCore/EntityEnviroment.h" #include "DeerCore/Serialization/Entity.h" #include "DeerCore/Serialization/Components/TransformComponent.h" #ifdef DEER_RENDER #include "DeerRender/Serialization/Components/MeshComponentSerialization.h" #endif #include "cereal/cereal.hpp" #include namespace Deer { struct EntitySerialization { WorldSerializationSettings& settings; EntityEnvironment* entityEnvironment; uint32_t entityId; template void saveComponent(Archive& archive, Entity& entity, const char* name) const { std::string containsComponentString = "Contains_"; containsComponentString += name; bool hasComponent = entity.hasComponent(); archive(cereal::make_nvp(containsComponentString.c_str(), hasComponent)); if (hasComponent) { S serialization{ .component = entity.getComponent(), .settings = settings}; archive(cereal::make_nvp(name, serialization)); } } template void loadComponent(Archive& archive, Entity& entity, const char* name) { std::string containsComponentString = "Contains_"; containsComponentString += name; bool hasComponent; archive(cereal::make_nvp(containsComponentString.c_str(), hasComponent)); if (hasComponent) { S serialization{ .component = entity.addComponent(), .settings = settings}; archive(cereal::make_nvp(name, serialization)); } } template void save(Archive& archive) const { Entity& entity = entityEnvironment->getEntity(entityId); TagComponent& tag = entity.getComponent(); RelationshipComponent& relation = entity.getComponent(); archive(cereal::make_nvp("Id", entityId)); archive(cereal::make_nvp("Tag", tag.tag)); archive(cereal::make_nvp("NetworkBehaviour", tag.networkBehaviour)); archive(cereal::make_nvp("Parent", relation.parent_id)); TransformComponentSerialization transformSerialization{ .settings = settings, .component = entity.getComponent()}; archive(cereal::make_nvp("TransformComponent", transformSerialization)); if (settings.includeClient) { #ifdef DEER_RENDER saveComponent(archive, entity, "MeshComponent"); #else DEER_CORE_ERROR("Can not include client on Deer Core Headless Compile"); #endif } } template void load(Archive& archive) { archive(cereal::make_nvp("Id", entityId)); Entity& entity = entityEnvironment->createEntityWithId(entityId); TagComponent& tag = entity.getComponent(); RelationshipComponent& relation = entity.getComponent(); archive(cereal::make_nvp("Tag", tag.tag)); archive(cereal::make_nvp("NetworkBehaviour", tag.networkBehaviour)); archive(cereal::make_nvp("Parent", tag.networkBehaviour)); TransformComponentSerialization transformSerialization{ .settings = settings, .component = entity.getComponent()}; archive(cereal::make_nvp("TransformComponent", transformSerialization)); if (settings.includeClient) { #ifdef DEER_RENDER loadComponent(archive, entity, "MeshComponent"); #else DEER_CORE_ERROR("Can not include client on Deer Core Headless Compile"); #endif } } }; } // namespace Deer