Exemplo n.º 1
0
void ScriptRunner::bootstrap()
{
    if (QThread::currentThread() != m_thread) {
        bool success;
        success = QMetaObject::invokeMethod(this, "bootstrap", Qt::QueuedConnection);
        Q_ASSERT(success);
        return;
    }

    // initialize the MF object before we run any user code
    QScriptValue mf_obj = m_engine->newObject();
    m_engine->globalObject().setProperty("mf", mf_obj);

    // add utility functions
    mf_obj.setProperty("include", m_engine->newFunction(include));
    mf_obj.setProperty("exit", m_engine->newFunction(exit));
    mf_obj.setProperty("print", m_engine->newFunction(print));
    mf_obj.setProperty("debug", m_engine->newFunction(debug));
    mf_obj.setProperty("setTimeout", m_engine->newFunction(setTimeout));
    mf_obj.setProperty("clearTimeout", m_engine->newFunction(clearTimeout));
    mf_obj.setProperty("setInterval", m_engine->newFunction(setInterval));
    mf_obj.setProperty("clearInterval", m_engine->newFunction(clearTimeout));
    mf_obj.setProperty("currentTimestamp", m_engine->newFunction(currentTimestamp));
    mf_obj.setProperty("readFile", m_engine->newFunction(readFile));
    mf_obj.setProperty("writeFile", m_engine->newFunction(writeFile));
    mf_obj.setProperty("args", m_engine->newFunction(args));

    // init event handler framework
    {
        QString file_name = ":/js/create_handlers.js";
        m_handler_map = evalJsonContents(internalReadFile(file_name), file_name);
        checkEngine("creating event handlers");
    }
    // init builtin types
    {
        QString file_name = ":/js/builtin_types.js";
        m_engine->evaluate(internalReadFile(file_name), file_name);
        checkEngine("evaluating builtin type");
        m_point_class = mf_obj.property("Point");
        m_entity_class = mf_obj.property("Entity");
        m_item_class = mf_obj.property("Item");
        m_block_class = mf_obj.property("Block");
        m_health_status_class = mf_obj.property("HealthStatus");
        m_status_effect_class = mf_obj.property("StatusEffect");
    }
    // create the mf.ItemType enum
    {
        QScriptValue item_type_obj = m_engine->newObject();
        mf_obj.setProperty("ItemType", item_type_obj);

        const QHash<Item::ItemType, Item::ItemData*> * item_data_hash = Item::itemDataHash();
        for (QHash<Item::ItemType, Item::ItemData*>::const_iterator it = item_data_hash->constBegin();
             it != item_data_hash->constEnd(); ++it)
        {
            const Item::ItemData * item_data = it.value();
            item_type_obj.setProperty(item_data->name, item_data->id);
        }
    }

    // hook up mf functions
    mf_obj.setProperty("chat", m_engine->newFunction(chat));
    mf_obj.setProperty("timeOfDay", m_engine->newFunction(timeOfDay));
    mf_obj.setProperty("itemStackHeight", m_engine->newFunction(itemStackHeight));
    mf_obj.setProperty("isPhysical", m_engine->newFunction(isPhysical));
    mf_obj.setProperty("isSafe", m_engine->newFunction(isSafe));
    mf_obj.setProperty("isDiggable", m_engine->newFunction(isDiggable));
    mf_obj.setProperty("healthStatus", m_engine->newFunction(healthStatus));
    mf_obj.setProperty("blockAt", m_engine->newFunction(blockAt));
    mf_obj.setProperty("isBlockLoaded", m_engine->newFunction(isBlockLoaded));
    mf_obj.setProperty("signTextAt", m_engine->newFunction(signTextAt));
    mf_obj.setProperty("self", m_engine->newFunction(self));
    mf_obj.setProperty("setControlState", m_engine->newFunction(setControlState));
    mf_obj.setProperty("clearControlStates", m_engine->newFunction(clearControlStates));
    mf_obj.setProperty("lookAt", m_engine->newFunction(lookAt));
    mf_obj.setProperty("respawn", m_engine->newFunction(respawn));
    mf_obj.setProperty("entity", m_engine->newFunction(entity));
    mf_obj.setProperty("startDigging", m_engine->newFunction(startDigging));
    mf_obj.setProperty("stopDigging", m_engine->newFunction(stopDigging));
    mf_obj.setProperty("attackEntity", m_engine->newFunction(attackEntity));

    mf_obj.setProperty("selectEquipSlot", m_engine->newFunction(selectEquipSlot));
    mf_obj.setProperty("selectedEquipSlot", m_engine->newFunction(selectedEquipSlot));
    mf_obj.setProperty("openInventoryWindow", m_engine->newFunction(openInventoryWindow));
    mf_obj.setProperty("clickInventorySlot", m_engine->newFunction(clickInventorySlot));
    mf_obj.setProperty("clickUniqueSlot", m_engine->newFunction(clickUniqueSlot));
    mf_obj.setProperty("clickOutsideWindow", m_engine->newFunction(clickOutsideWindow));
    mf_obj.setProperty("closeWindow", m_engine->newFunction(closeWindow));
    mf_obj.setProperty("inventoryItem", m_engine->newFunction(inventoryItem));
    mf_obj.setProperty("uniqueWindowItem", m_engine->newFunction(uniqueWindowItem));
    mf_obj.setProperty("canPlaceBlock", m_engine->newFunction(canPlaceBlock));
    mf_obj.setProperty("activateItem", m_engine->newFunction(activateItem));
    mf_obj.setProperty("dimension", m_engine->newFunction(dimension));
    mf_obj.setProperty("onlinePlayers", m_engine->newFunction(onlinePlayers));


    // hook up hax functions
    QScriptValue hax_obj = m_engine->newObject();
    mf_obj.setProperty("hax", hax_obj);
    hax_obj.setProperty("setPosition", m_engine->newFunction(setPosition));
    hax_obj.setProperty("positionUpdateInterval", m_engine->newFunction(positionUpdateInterval));
    hax_obj.setProperty("setGravityEnabled", m_engine->newFunction(setGravityEnabled));
    hax_obj.setProperty("setJesusModeEnabled", m_engine->newFunction(setJesusModeEnabled));

    hax_obj.setProperty("placeBlock", m_engine->newFunction(placeBlock));
    hax_obj.setProperty("activateBlock", m_engine->newFunction(activateBlock));

    // run main script
    QString main_script_contents = internalReadFile(m_main_script_filename);
    if (main_script_contents.isNull()) {
        m_stderr << "file not found: " << m_main_script_filename << "\n";
        m_stderr.flush();
        QCoreApplication::instance()->exit(1);
        return;
    }
    m_engine->evaluate(main_script_contents, m_main_script_filename);
    checkEngine("evaluating main script");

    if (m_exiting)
        return;

    // connect to server
    bool success;

    success = connect(m_game, SIGNAL(entitySpawned(QSharedPointer<Game::Entity>)), this, SLOT(handleEntitySpawned(QSharedPointer<Game::Entity>)));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(entityMoved(QSharedPointer<Game::Entity>)), this, SLOT(handleEntityMoved(QSharedPointer<Game::Entity>)));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(entityDespawned(QSharedPointer<Game::Entity>)), this, SLOT(handleEntityDespawned(QSharedPointer<Game::Entity>)));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(animation(QSharedPointer<Game::Entity>,Message::AnimationType)), this, SLOT(handleAnimation(QSharedPointer<Game::Entity>,Message::AnimationType)));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(entityEffect(QSharedPointer<Game::Entity>,QSharedPointer<Game::StatusEffect>)), this, SLOT(handleEntityEffect(QSharedPointer<Game::Entity>,QSharedPointer<Game::StatusEffect>)));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(removeEntityEffect(QSharedPointer<Game::Entity>,QSharedPointer<Game::StatusEffect>)), this, SLOT(handleRemoveEntityEffect(QSharedPointer<Game::Entity>,QSharedPointer<Game::StatusEffect>)));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(chunkUpdated(Int3D,Int3D)), this, SLOT(handleChunkUpdated(Int3D,Int3D)));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(signUpdated(Int3D,QString)), this, SLOT(handleSignUpdated(Int3D,QString)));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(playerPositionUpdated()), this, SLOT(movePlayerPosition()));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(loginStatusUpdated(Server::LoginStatus)), this, SLOT(handleLoginStatusUpdated(Server::LoginStatus)));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(chatReceived(QString,QString)), this, SLOT(handleChatReceived(QString,QString)));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(nonSpokenChatReceived(QString)), this, SLOT(handleNonSpokenChatReceived(QString)));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(timeUpdated(double)), this, SLOT(handleTimeUpdated(double)));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(playerDied()), this, SLOT(handlePlayerDied()));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(playerSpawned(int)), this, SLOT(handlePlayerSpawned(int)));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(playerHealthStatusUpdated()), this, SLOT(handlePlayerHealthStatusUpdated()));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(inventoryUpdated()), this, SLOT(handleInventoryUpdated()));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(stoppedDigging(Game::StoppedDiggingReason)), this, SLOT(handleStoppedDigging(Game::StoppedDiggingReason)));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(windowOpened(Message::WindowType)), this, SLOT(handleWindowOpened(Message::WindowType)));
    Q_ASSERT(success);
    success = connect(m_game, SIGNAL(equippedItemChanged()), this, SLOT(handleEquippedItemChanged()));
    Q_ASSERT(success);


    success = connect(&m_stdin_reader, SIGNAL(readLine(QString)), this, SLOT(handleReadLine(QString)));
    Q_ASSERT(success);
    success = connect(&m_stdin_reader, SIGNAL(eof()), QCoreApplication::instance(), SLOT(quit()));
    Q_ASSERT(success);

    m_physics_doer = new PhysicsDoer(m_game);

    m_started_game = true;
    m_game->start();
    m_stdin_reader.start();
}
Exemplo n.º 2
0
void LauncherWindow::showEvent(QShowEvent *event)
{
    QMainWindow::showEvent(event);
    emit windowOpened();
    event->accept();
}