Esempio n. 1
0
void BaseEntity::Deserialize(SerializableNodeObject* parentNode, EngineContext* engineContext)
{
    if (!parentNode)
    {
        LOG("Cannot deserialize Entity: Node is null.");
        return;
    }

    // Position.
    /*SerializableProperty* positionProp = (SerializableProperty*)parentNode->FindNodeByName("position");
    if (positionProp)
    	this->SetPosition(positionProp->Value.toVec3f());

    // Pose.
    SerializableProperty* poseProp = (SerializableProperty*)parentNode->FindNodeByName("pose");
    if (poseProp)
    	this->SetPose(poseProp->Value.toMat4f());

    // Direction.
    SerializableProperty* directionProp = (SerializableProperty*)parentNode->FindNodeByName("direction");
    if (directionProp)
    	this->SetDirection(directionProp->Value.toVec3f());

    // Scale.
    SerializableProperty* scaleProp = (SerializableProperty*)parentNode->FindNodeByName("scale");
    if (scaleProp)
    	this->SetScale(scaleProp->Value.toVec3f());*/

    auto nbNodes = parentNode->GetChildNodesCount();
    auto nodes = parentNode->GetChildNodes();

    for (size_t i = 0; i < nbNodes; ++i)
    {
        auto node = nodes[i];

        if (node->GetType() == SerializableNodeType::ARRAY && node->Name.toLower() == "properties")
        {
            auto nbProperties = node->GetChildNodesCount();
            auto properties = node->GetChildNodes();

            for (size_t p = 0; p < nbProperties; ++p)
            {
                if (properties[p]->GetType() != SerializableNodeType::OBJECT)
                    continue;

                PropertyDescriptor pDesc;
                pDesc.Deserialize((SerializableNodeObject*)properties[p], engineContext);

                Property* prop = this->AddProperty(pDesc);

                // Look for the value node.
                SerializableProperty* valueNode = (SerializableProperty*)properties[p]->FindNodeByName("value");

                if (valueNode)
                    prop->Set(valueNode->Value);
            }
        }
        else if (node->GetType() == SerializableNodeType::ARRAY && node->Name.toLower() == "components")
        {
            auto nbComponents = node->GetChildNodesCount();
            auto components = node->GetChildNodes();

            for (size_t c = 0; c < nbComponents; ++c)
            {
                if (components[c]->GetType() != SerializableNodeType::OBJECT)
                    continue;

                SerializableNodeObject* nodeInfo = (SerializableNodeObject*)components[c];

                BaseComponent* component = BaseComponent::DeserializeComponent(nodeInfo, engineContext, this);

                if (!component)
                {
                    LOG("Unable to deserialize component. Skipping.");
                    continue;
                }

                this->AddComponent(component);

                //component->Deserialize(nodeInfo);
            }
        }
        else if (node->GetType() == SerializableNodeType::PROPERTY)
        {
            if (node->Name.toLower() == "class")
            {
                this->SetClass(node->Value.c_str());
            }
        }
    }
}