Deserialization WIP
This commit is contained in:
parent
c4ed199165
commit
cebb1a2cf8
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include "ECS_Base/Managers/SHEntityManager.h"
|
||||
#include "Scene/SHSceneManager.h"
|
||||
#include "Tools/SHException.h"
|
||||
|
||||
|
@ -29,18 +30,60 @@ namespace SHADE
|
|||
auto root = sceneGraph.GetRoot();
|
||||
|
||||
SHASSERT(root != nullptr, "Root is null. Failed to serialize scene to node.");
|
||||
|
||||
|
||||
auto const& children = root->GetChildren();
|
||||
out << YAML::BeginDoc;
|
||||
for (auto child : children)
|
||||
{
|
||||
out << YAML::BeginSeq;
|
||||
out << SerializeEntityToNode(child);
|
||||
out << YAML::EndSeq;
|
||||
}
|
||||
|
||||
auto pred = [&out](SHSceneNode* node){out << SerializeEntityToNode(node);};
|
||||
sceneGraph.Traverse(pred);
|
||||
//out << SerializeEntityToNode(child);
|
||||
|
||||
out << YAML::EndDoc;
|
||||
}
|
||||
|
||||
static void DeserializeEntity(YAML::iterator& it, YAML::Node const& node, std::vector<EntityID>& createdEntities, EntityID parentEID = MAX_EID)
|
||||
{
|
||||
if(!node[EIDNode])
|
||||
return;
|
||||
EntityID eid = node[EIDNode].as<EntityID>();
|
||||
std::string name = "Default";
|
||||
if(node[EntityNameNode])
|
||||
name = node[EntityNameNode].as<std::string>();
|
||||
//Compile component IDs
|
||||
const auto componentIDList = SHSerialization::GetComponentIDList(node[ComponentsNode]);
|
||||
eid = SHEntityManager::CreateEntity(componentIDList, eid, name, parentEID);
|
||||
createdEntities.push_back(eid);
|
||||
if(node[NumberOfChildrenNode])
|
||||
{ int numOfChildren = node[NumberOfChildrenNode].as<int>();
|
||||
for(int i = 0; i < numOfChildren; ++i)
|
||||
{
|
||||
DeserializeEntity(it, *it, createdEntities, eid);
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SHSerialization::DeserializeSceneFromFile(std::string const& fileData)
|
||||
{
|
||||
YAML::Node entities = YAML::Load(fileData.c_str());
|
||||
std::vector<EntityID> createdEntities{};
|
||||
|
||||
//Create Entities
|
||||
for(auto it = entities.begin(); it != entities.end(); ++it)
|
||||
{
|
||||
DeserializeEntity(it, (*it), createdEntities);
|
||||
}
|
||||
|
||||
//Initialize Entity
|
||||
for(auto it = entities.begin(); it != entities.end(); ++it)
|
||||
{
|
||||
//For each node, Deserialize component
|
||||
//Recurse through properties
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::string SHSerialization::SerializeEntityToString()
|
||||
{
|
||||
return std::string();
|
||||
|
@ -53,16 +96,19 @@ namespace SHADE
|
|||
YAML::Node SHSerialization::SerializeEntityToNode(SHSceneNode* sceneNode)
|
||||
{
|
||||
YAML::Node node;
|
||||
if (!sceneNode)
|
||||
auto eid = sceneNode->GetEntityID();
|
||||
auto entity = SHEntityManager::GetEntityByID(eid);
|
||||
if (!sceneNode || !entity)
|
||||
{
|
||||
node = YAML::Null;
|
||||
return node;
|
||||
}
|
||||
auto eid = sceneNode->GetEntityID();
|
||||
node["EID"] = eid;
|
||||
node["isActive"] = sceneNode->IsActive();
|
||||
|
||||
node[EIDNode] = eid;
|
||||
node[EntityNameNode] = entity->name;
|
||||
node[IsActiveNode] = sceneNode->IsActive();
|
||||
auto const& children = sceneNode->GetChildren();
|
||||
node["NumberOfChildren"] = children.size();
|
||||
node[NumberOfChildrenNode] = children.size();
|
||||
|
||||
YAML::Node components;
|
||||
|
||||
|
@ -70,7 +116,27 @@ namespace SHADE
|
|||
{
|
||||
components[rttr::type::get<SHTransformComponent>().get_name().data()] = SHSerializationHelper::SerializeComponentToNode(transform);
|
||||
}
|
||||
node["Components"] = components;
|
||||
node[ComponentsNode] = components;
|
||||
return node;
|
||||
}
|
||||
|
||||
template<typename ComponentType, std::enable_if_t<std::is_base_of_v<SHComponent, ComponentType>, bool> = true>
|
||||
std::optional<ComponentTypeID> GetComponentID(YAML::Node const & componentNode)
|
||||
{
|
||||
if(componentNode[rttr::type::get<ComponentType>().get_name().data()])
|
||||
return {SHFamilyID<SHComponent>::GetID<ComponentType>()};
|
||||
else
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::vector<ComponentTypeID> SHSerialization::GetComponentIDList(YAML::Node const& componentsNode)
|
||||
{
|
||||
std::vector<ComponentTypeID> componentIDList;
|
||||
|
||||
auto id = GetComponentID<SHTransformComponent>(componentsNode);
|
||||
if(id.has_value())
|
||||
componentIDList.push_back(id.value());
|
||||
|
||||
return componentIDList;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#include <filesystem>
|
||||
|
||||
#include <ECS_Base/Components/SHComponent.h>
|
||||
#include "SH_API.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class Emitter;
|
||||
|
@ -15,14 +15,27 @@ namespace YAML
|
|||
namespace SHADE
|
||||
{
|
||||
class SHSceneNode;
|
||||
|
||||
constexpr const char* ComponentsNode = "Components";
|
||||
constexpr const char* EntityNameNode = "Name";
|
||||
constexpr const char* EIDNode = "EID";
|
||||
constexpr const char* IsActiveNode = "IsActive";
|
||||
constexpr const char* NumberOfChildrenNode = "NumberOfChildren";
|
||||
|
||||
struct SH_API SHSerialization
|
||||
{
|
||||
//TODO: change paths to resource ID
|
||||
static void SerializeSceneToFile(std::filesystem::path const& path);
|
||||
static std::string SerializeSceneToString();
|
||||
static void SerializeSceneToEmitter(YAML::Emitter& out);
|
||||
|
||||
static void DeserializeSceneFromFile(std::string const& fileData);
|
||||
|
||||
|
||||
static std::string SerializeEntityToString();
|
||||
static void SerializeEntityToFile(std::filesystem::path const& path);
|
||||
static YAML::Node SerializeEntityToNode(SHSceneNode* sceneNode);
|
||||
|
||||
static std::vector<ComponentTypeID> GetComponentIDList(YAML::Node const& componentsNode);
|
||||
private:
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue