2026-03-01 17:48:56 +01:00

106 lines
3.4 KiB
C++

#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 <string>
namespace Deer {
struct EntitySerialization {
WorldSerializationSettings& settings;
EntityEnvironment* entityEnvironment;
uint32_t entityId;
template <class Archive, class T, class S>
void saveComponent(Archive& archive, Entity& entity, const char* name) const {
std::string containsComponentString = "Contains_";
containsComponentString += name;
bool hasComponent = entity.hasComponent<T>();
archive(cereal::make_nvp(containsComponentString.c_str(), hasComponent));
if (hasComponent) {
S serialization{
.component = entity.getComponent<T>(),
.settings = settings};
archive(cereal::make_nvp(name, serialization));
}
}
template <class Archive, class T, class S>
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<T>(),
.settings = settings};
archive(cereal::make_nvp(name, serialization));
}
}
template <class Archive>
void save(Archive& archive) const {
Entity& entity = entityEnvironment->getEntity(entityId);
TagComponent& tag = entity.getComponent<TagComponent>();
RelationshipComponent& relation = entity.getComponent<RelationshipComponent>();
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<TransformComponent>()};
archive(cereal::make_nvp("TransformComponent", transformSerialization));
if (settings.includeClient) {
#ifdef DEER_RENDER
saveComponent<Archive, MeshComponent, MeshComponentSerialization>(archive, entity, "MeshComponent");
#else
DEER_CORE_ERROR("Can not include client on Deer Core Headless Compile");
#endif
}
}
template <class Archive>
void load(Archive& archive) {
archive(cereal::make_nvp("Id", entityId));
Entity& entity = entityEnvironment->createEntityWithId(entityId);
TagComponent& tag = entity.getComponent<TagComponent>();
RelationshipComponent& relation = entity.getComponent<RelationshipComponent>();
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<TransformComponent>()};
archive(cereal::make_nvp("TransformComponent", transformSerialization));
if (settings.includeClient) {
#ifdef DEER_RENDER
loadComponent<Archive, MeshComponent, MeshComponentSerialization>(archive, entity, "MeshComponent");
#else
DEER_CORE_ERROR("Can not include client on Deer Core Headless Compile");
#endif
}
}
};
} // namespace Deer