//
// read_component
//
void ClientSidePrediction::read_component(MemoryBuffer& message, Node* node)
{
    auto network = GetSubsystem<Network>();
    auto scene = network->GetServerConnection()->GetScene();

    // Read component ID
    auto componentID = message.ReadUInt();
    // Read component type
    auto type = message.ReadStringHash();

    // Check if the component by this ID and type already exists in this node
    auto component = scene->GetComponent(componentID);
    if (!component || component->GetType() != type || component->GetNode() != node)
    {
        if (component)
            component->Remove();
        component = node->CreateComponent(type, LOCAL, componentID);
    }

    // If was unable to create the component, would desync the message and therefore have to abort
    if (!component)
    {
        LOGERROR("CreateNode message parsing aborted due to unknown component");
        return;
    }

    // Read attributes and apply
    read_network_attributes(*component, message);
    component->ApplyAttributes();
}
//
// read_node
//
void ClientSidePrediction::read_node(MemoryBuffer& message)
{
    auto network = GetSubsystem<Network>();
    auto scene = network->GetServerConnection()->GetScene();

    auto node_id = message.ReadUInt();
    auto node = scene->GetNode(node_id);
    bool new_node = false;

    // Create the node if it doesn't exist
    if (!node)
    {
        new_node = true;
        // Add initially to the root level. May be moved as we receive the parent attribute
        node = scene->CreateChild(node_id, LOCAL);
        // Create smoothed transform component
        node->CreateComponent<SmoothedTransform>(LOCAL);
    }
    else
    {
        // Remove the node from the unused nodes list
        unused_nodes.erase(node);
    }

    // Read attributes
    read_network_attributes(*node, message);
    // ApplyAttributes() is deliberately skipped, as Node has no attributes that require late applying.
    // Furthermore it would propagate to components and child nodes, which is not desired in this case

    if (new_node)
    {
        // Snap the motion smoothing immediately to the end
        auto transform = node->GetComponent<SmoothedTransform>();
        if (transform)
            transform->Update(1.0f, 0.0f);
    }

    // Read user variables
    unsigned num_vars = message.ReadVLE();
    for (; num_vars > 0; --num_vars)
    {
        auto key = message.ReadStringHash();
        node->SetVar(key, message.ReadVariant());
    }

    // Read components
    unsigned num_components = message.ReadVLE();
    for (; num_components > 0; --num_components)
        read_component(message, node);
}