Example #1
0
bool Animatable::LoadJSON(const JSONValue& source, bool setInstanceDefault)
{
    if (!Serializable::LoadJSON(source, setInstanceDefault))
        return false;

    SetObjectAnimation(0);
    attributeAnimationInfos_.Clear();

    JSONValue value = source.Get("objectanimation");
    if (!value.IsNull())
    {
        SharedPtr<ObjectAnimation> objectAnimation(new ObjectAnimation(context_));
        if (!objectAnimation->LoadJSON(value))
            return false;

        SetObjectAnimation(objectAnimation);
    }

    JSONValue attributeAnimationValue = source.Get("attributeanimation");

    if (attributeAnimationValue.IsNull())
        return true;

    if (!attributeAnimationValue.IsObject())
    {
        URHO3D_LOGWARNING("'attributeanimation' value is present in JSON data, but is not a JSON object; skipping it");
        return true;
    }

    const JSONObject& attributeAnimationObject = attributeAnimationValue.GetObject();
    for (JSONObject::ConstIterator it = attributeAnimationObject.Begin(); it != attributeAnimationObject.End(); it++)
    {
        String name = it->first_;
        JSONValue value = it->second_;
        SharedPtr<ValueAnimation> attributeAnimation(new ValueAnimation(context_));
        if (!attributeAnimation->LoadJSON(it->second_))
            return false;

        String wrapModeString = source.Get("wrapmode").GetString();
        WrapMode wrapMode = WM_LOOP;
        for (int i = 0; i <= WM_CLAMP; ++i)
        {
            if (wrapModeString == wrapModeNames[i])
            {
                wrapMode = (WrapMode)i;
                break;
            }
        }

        float speed = value.Get("speed").GetFloat();
        SetAttributeAnimation(name, attributeAnimation, wrapMode, speed);

        it++;
    }

    return true;
}
Example #2
0
void Localization::LoadJSON(const JSONValue& source)
{
    for (JSONObject::const_iterator i = source.GetObject().begin(); i != source.GetObject().end(); ++i)
    {
        QString id = MAP_KEY(i);
        if (id.isEmpty())
        {
            URHO3D_LOGWARNING("Localization::LoadJSON(source): string ID is empty");
            continue;
        }
        const JSONObject& langs = MAP_VALUE(i).GetObject();
        for (JSONObject::const_iterator j = langs.begin(); j != langs.end(); ++j)
        {
            const QString& lang = MAP_KEY(j);
            if (lang.isEmpty())
            {
                URHO3D_LOGWARNING("Localization::LoadJSON(source): language name is empty, string ID=\"" + id + "\"");
                continue;
            }
            const QString stringRef = MAP_VALUE(j).GetString();
            if (stringRef.isEmpty())
            {
                URHO3D_LOGWARNING(
                    "Localization::LoadJSON(source): translation is empty, string ID=\"" + id + "\", language=\"" + lang + "\"");
                continue;
            }
            if (strings_[StringHash(lang)][StringHash(id)] != s_dummy)
            {
                URHO3D_LOGWARNING(
                    "Localization::LoadJSON(source): override translation, string ID=\"" + id + "\", language=\"" + lang + "\"");
            }
            strings_[StringHash(lang)][StringHash(id)] = stringRef;
            if (!languages_.contains(lang))
                languages_.push_back(lang);
            if (languageIndex_ == -1)
                languageIndex_ = 0;
        }
    }
}
Example #3
0
bool ObjectAnimation::LoadJSON(const JSONValue& source)
{
    attributeAnimationInfos_.Clear();

    JSONValue attributeAnimationsValue = source.Get("attributeanimations");
    if (attributeAnimationsValue.IsNull())
        return true;
    if (!attributeAnimationsValue.IsObject())
        return true;

    const JSONObject& attributeAnimationsObject = attributeAnimationsValue.GetObject();

    for (JSONObject::ConstIterator it = attributeAnimationsObject.Begin(); it != attributeAnimationsObject.End(); it++)
    {
        String name = it->first_;
        JSONValue value = it->second_;
        SharedPtr<ValueAnimation> animation(new ValueAnimation(context_));
        if (!animation->LoadJSON(value))
            return false;

        String wrapModeString = value.Get("wrapmode").GetString();
        WrapMode wrapMode = WM_LOOP;
        for (int i = 0; i <= WM_CLAMP; ++i)
        {
            if (wrapModeString == wrapModeNames[i])
            {
                wrapMode = (WrapMode)i;
                break;
            }
        }

        float speed = value.Get("speed").GetFloat();
        AddAttributeAnimation(name, animation, wrapMode, speed);
    }

    return true;
}
void Server::Update(float frametime)
{
    TundraLogic* tundraLogic = framework_->Module<TundraLogic>();
    ::Server* tundraServer = tundraLogic->Server();

    // Clean dead requestedConnections
    if (!connections_.Empty())
    {
        WebSocket::UserConnectionList::Iterator cleanupIter = connections_.Begin();
        while (cleanupIter != connections_.End())
        {
            WebSocket::UserConnectionPtr connection = *cleanupIter;
            if (!connection)
            {
                cleanupIter = connections_.Erase(cleanupIter);
            }
            else if (connection->webSocketConnection.expired())
            {
                // If user was already registered to the Tundra server, remove from there
                tundraServer->RemoveExternalUser(Urho3D::StaticCast<::UserConnection>(connection));
                if (!connection->userID)
                    LogDebug(LC + "Removing non-logged in WebSocket connection.");
                else
                    LogInfo(LC + "Removing expired WebSocket connection with ID " + String(connection->userID));
                cleanupIter = connections_.Erase(cleanupIter);
            }
            else
            {
                ++cleanupIter;
            }
        }
    }
    
    Vector<SocketEvent*> processEvents;
    {
        Urho3D::MutexLock lockEvents(mutexEvents_);
        if (events_.Size() == 0)
            return;
        // Make copy of current event queue for processing
        processEvents = events_;
        events_.Clear();
    }

    Vector<UserConnectionPtr> toDisconnect;

    // Process events pushed from the websocket thread(s)
    for (uint i=0; i < processEvents.Size(); ++i)
    {
        SocketEvent *event = processEvents[i];
        if (!event)
            continue;

        // User connected
        if (event->type == SocketEvent::Connected)
        {
            if (!UserConnection(event->connection))
            {
                WebSocket::UserConnectionPtr userConnection(new WebSocket::UserConnection(context_, event->connection));
                connections_.Push(userConnection);

                // The connection does not yet have an ID assigned. Tundra server will assign on login
                LogDebug(LC + String("New WebSocket connection."));
            }
        }
        // User disconnected
        else if (event->type == SocketEvent::Disconnected)
        {
            for(UserConnectionList::Iterator iter = connections_.Begin(); iter != connections_.End(); ++iter)
            {
                if ((*iter) && (*iter)->WebSocketConnection() == event->connection)
                {
                    tundraServer->RemoveExternalUser(Urho3D::StaticCast<::UserConnection>(*iter));
                    if (!(*iter)->userID)
                        LogDebug(LC + "Removing non-logged in WebSocket connection.");
                    else
                        LogInfo(LC + "Removing WebSocket connection with ID " + String((*iter)->userID));
                    connections_.Erase(iter);
                    break;
                }
            }
        }
        // Data message
        else if (event->type == SocketEvent::Data && event->data.get())
        {
            WebSocket::UserConnectionPtr userConnection = UserConnection(event->connection);
            if (userConnection)
            {
                kNet::DataDeserializer dd(event->data->GetData(), event->data->BytesFilled());
                u16 messageId = dd.Read<u16>();

                // LoginMessage
                if (messageId == cLoginMessage)
                {
                    bool ok = false;
                    String loginDataString = ReadUtf8String(dd);
                    // Read optional protocol version
                    if (dd.BytesLeft())
                        userConnection->protocolVersion = (NetworkProtocolVersion)dd.ReadVLE<kNet::VLE8_16_32>();

                    JSONValue json;
                    ok = json.FromString(loginDataString);
                    if (ok)
                    {
                        JSONObject jsonObj = json.GetObject();
                        for (JSONObject::ConstIterator i = jsonObj.Begin(); i != jsonObj.End(); ++i)
                            userConnection->properties[i->first_] = i->second_.ToVariant();
                        userConnection->properties["authenticated"] = true;
                        bool success = tundraServer->AddExternalUser(Urho3D::StaticCast<::UserConnection>(userConnection));
                        if (!success)
                        {
                            LogInfo(LC + "Connection ID " + String(userConnection->userID) + " login refused");
                            toDisconnect.Push(userConnection);
                        }
                        else
                            LogInfo(LC + "Connection ID " + String(userConnection->userID) + " login successful");
                    }
                }
                else
                {
                    // Only signal messages from authenticated users
                    if (userConnection->properties["authenticated"].GetBool() == true)
                    {
                        // Signal network message. As per kNet tradition the message ID is given separately in addition with the rest of the data
                        NetworkMessageReceived.Emit(userConnection.Get(), messageId, event->data->GetData() + sizeof(u16), event->data->BytesFilled() - sizeof(u16));
                        // Signal network message on the Tundra server so that it can be globally picked up
                        tundraServer->EmitNetworkMessageReceived(userConnection.Get(), 0, messageId, event->data->GetData() + sizeof(u16), event->data->BytesFilled() - sizeof(u16));
                    }
                }
            }
            else
                LogError(LC + "Received message from unauthorized connection, ignoring.");

            event->data.reset();
        }
        else
            event->data.reset();
        
        SAFE_DELETE(event);
    }

    for (uint i = 0; i < toDisconnect.Size(); ++i)
    {
        if (toDisconnect[i])
            toDisconnect[i]->Disconnect();
    }
}
Example #5
0
static void ToRapidjsonValue(rapidjson::Value& rapidjsonValue, const JSONValue& jsonValue, rapidjson::MemoryPoolAllocator<>& allocator)
{
    switch (jsonValue.GetValueType())
    {
    case JSON_NULL:
        rapidjsonValue.SetNull();
        break;

    case JSON_BOOL:
        rapidjsonValue.SetBool(jsonValue.GetBool());
        break;

    case JSON_NUMBER:
        {
            switch (jsonValue.GetNumberType())
            {
            case JSONNT_INT:
                rapidjsonValue.SetInt(jsonValue.GetInt());
                break;

            case JSONNT_UINT:
                rapidjsonValue.SetUint(jsonValue.GetUInt());
                break;

            default:
                rapidjsonValue.SetDouble(jsonValue.GetDouble());
                break;
            }
        }
        break;

    case JSON_STRING:
        rapidjsonValue.SetString(jsonValue.GetCString(), allocator);
        break;

    case JSON_ARRAY:
        {
            const JSONArray& jsonArray = jsonValue.GetArray();

            rapidjsonValue.SetArray();
            rapidjsonValue.Reserve(jsonArray.Size(), allocator);

            for (unsigned i = 0; i < jsonArray.Size(); ++i)
            {
                rapidjson::Value value;
                rapidjsonValue.PushBack(value, allocator);
                ToRapidjsonValue(rapidjsonValue[i], jsonArray[i], allocator);
            }
        }
        break;

    case JSON_OBJECT:
        {
            const JSONObject& jsonObject = jsonValue.GetObject();

            rapidjsonValue.SetObject();
            for (JSONObject::ConstIterator i = jsonObject.Begin(); i != jsonObject.End(); ++i)
            {
                const char* name = i->first_.CString();
                rapidjson::Value value;
                rapidjsonValue.AddMember(name, value, allocator);
                ToRapidjsonValue(rapidjsonValue[name], i->second_, allocator);
            }
        }
        break;

    default:
        break;
    }
}