コード例 #1
0
int CLuaFunctionDefs::GetKeyBoundToFunction ( lua_State* luaVM )
{
    SString strKey = "", strHitState = "";
    CScriptArgReader argStream ( luaVM );

    CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
    if ( pLuaMain )
    {
        if ( argStream.NextIsFunction ( ) )
        {
            CLuaFunctionRef iLuaFunction = luaM_toref ( luaVM, 1 );
            // get the key
            list < CScriptKeyBind* > ::const_iterator iter =  m_pClientGame->GetScriptKeyBinds ()->IterBegin ();
            for ( ; iter !=  m_pClientGame->GetScriptKeyBinds ()->IterEnd (); iter++ )
            {
                CScriptKeyBind* pScriptKeyBind = *iter;
                if ( !pScriptKeyBind->IsBeingDeleted () )
                {
                    switch ( pScriptKeyBind->GetType () )
                    {
                        case SCRIPT_KEY_BIND_FUNCTION:
                        {
                            CScriptKeyFunctionBind* pBind = static_cast < CScriptKeyFunctionBind* > ( pScriptKeyBind );
                            // ACHTUNG: DOES IT FIND THE CORRECT LUA REF HERE?
                            if ( iLuaFunction == pBind->m_iLuaFunction )
                            {
                                lua_pushstring ( luaVM, pBind->boundKey->szKey );
                                return 1;
                            }
                            break;
                        }
                        case SCRIPT_KEY_BIND_CONTROL_FUNCTION:
                        {
                            CScriptControlFunctionBind* pBind = static_cast < CScriptControlFunctionBind* > ( pScriptKeyBind );
                            // ACHTUNG: DOES IT FIND THE CORRECT LUA REF HERE?
                            if ( iLuaFunction == pBind->m_iLuaFunction )
                            {
                                lua_pushstring ( luaVM, pBind->boundControl->szControl );
                                return 1;
                            }
                            break;
                        }
                        default:
                            break;
                    }
                }
            }
            lua_pushboolean ( luaVM, false );
            return 1;
        }
        else
            m_pScriptDebugging->LogBadType ( luaVM );
    }
    lua_pushboolean ( luaVM, false );
    return 1;
}
コード例 #2
0
int CLuaDatabaseDefs::OOP_DbQuery ( lua_State* luaVM )
{
    //  handle dbQuery ( [ function callbackFunction, [ table callbackArguments, ] ] element connection, string query, ... )
    CLuaFunctionRef iLuaFunction; CLuaArguments callbackArgs; CDatabaseConnectionElement* pElement; SString strQuery; CLuaArguments Args;

    CScriptArgReader argStream ( luaVM );

    argStream.ReadUserData ( pElement );
    if ( argStream.NextIsFunction () )
    {
        argStream.ReadFunction ( iLuaFunction );
        if ( argStream.NextIsTable () )
        {
            argStream.ReadLuaArgumentsTable( callbackArgs );
        }
    }
    argStream.ReadString ( strQuery );
    argStream.ReadLuaArguments ( Args );
    argStream.ReadFunctionComplete ();

    if ( !argStream.HasErrors () )
    {
        // Start async query
        CDbJobData* pJobData = g_pGame->GetDatabaseManager ()->QueryStart ( pElement->GetConnectionHandle (), strQuery, &Args );
        if ( !pJobData )
        {
            if ( !g_pGame->GetDatabaseManager ()->IsLastErrorSuppressed () )
                m_pScriptDebugging->LogWarning ( luaVM, "%s failed; %s", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ), *g_pGame->GetDatabaseManager ()->GetLastErrorMessage () );
            lua_pushboolean ( luaVM, false );
            return 1;
        }
        // Make callback function if required
        if ( VERIFY_FUNCTION ( iLuaFunction ) )
        {
            CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
            if ( pLuaMain )
            {
                CLuaArguments Arguments;
                Arguments.PushDbQuery ( pJobData );
                Arguments.PushArguments ( callbackArgs );
                pJobData->SetCallback ( CLuaDatabaseDefs::DbQueryCallback, g_pGame->GetLuaCallbackManager ()->CreateCallback ( pLuaMain, iLuaFunction, Arguments ) );
            }
        }
        // Add debug info incase query result does not get collected
        pJobData->SetLuaDebugInfo ( g_pGame->GetScriptDebugging ()->GetLuaDebugInfo ( luaVM ) );
        lua_pushquery ( luaVM, pJobData );
        return 1;
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
コード例 #3
0
int CLuaFunctionDefs::Reference ( lua_State* luaVM )
{
    CScriptArgReader argStream ( luaVM );
    if ( argStream.NextIsTable ( ) || argStream.NextIsFunction ( ) ||
        argStream.NextIsUserData ( ) )
    {
        int iPointer = lua_ref ( luaVM, 1 );
        lua_pushnumber ( luaVM, iPointer );
        return 1;
    }
    lua_pushboolean ( luaVM, false );
    return 1;
}
コード例 #4
0
// Call a function on a remote server
int CLuaFunctionDefs::CallRemote ( lua_State* luaVM )
{
    CScriptArgReader argStream ( luaVM );
    if ( !argStream.NextIsFunction ( 1 ) && !argStream.NextIsFunction ( 2 ) )
    {
        // Call type 1
        //  bool callRemote ( string host [, int connectionAttempts = 10, int connectTimeout = 10000 ], string resourceName, string functionName, callback callbackFunction, [ arguments... ] )
        SString strHost; uint uiConnectionAttempts; uint uiConnectTimeoutMs; SString strResourceName; SString strFunctionName; CLuaFunctionRef iLuaFunction; CLuaArguments args;

        argStream.ReadString ( strHost );
        if ( argStream.NextIsNumber () )
            MinServerReqCheck ( argStream, MIN_SERVER_REQ_CALLREMOTE_CONNECTION_ATTEMPTS, "'connection attempts' is being used" );
        argStream.ReadIfNextIsNumber ( uiConnectionAttempts, 10 );
        if ( argStream.NextIsNumber () )
            MinServerReqCheck ( argStream, MIN_SERVER_REQ_CALLREMOTE_CONNECT_TIMEOUT, "'connect timeout' is being used" );
        argStream.ReadIfNextIsNumber ( uiConnectTimeoutMs, 10000 );
        argStream.ReadString ( strResourceName );
        argStream.ReadString ( strFunctionName );
        argStream.ReadFunction ( iLuaFunction );
        argStream.ReadLuaArguments ( args );
        argStream.ReadFunctionComplete ();

        if ( !argStream.HasErrors () )
        {
            CLuaMain * luaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
            if ( luaMain )
            {
                g_pGame->GetRemoteCalls ()->Call ( strHost, strResourceName, strFunctionName, &args, luaMain, iLuaFunction, uiConnectionAttempts, uiConnectTimeoutMs );
                lua_pushboolean ( luaVM, true );
                return 1;
            }
        }
    }
    else
    {
        // Call type 2
        //  bool callRemote ( string URL [, int connectionAttempts = 10, int connectTimeout = 10000 ], callback callbackFunction, [ arguments... ] )
        SString strURL; uint uiConnectionAttempts; uint uiConnectTimeoutMs; CLuaFunctionRef iLuaFunction; CLuaArguments args;

        argStream.ReadString ( strURL );
        if ( argStream.NextIsNumber () )
            MinServerReqCheck ( argStream, MIN_SERVER_REQ_CALLREMOTE_CONNECTION_ATTEMPTS, "'connection attempts' is being used" );
        argStream.ReadIfNextIsNumber ( uiConnectionAttempts, 10 );
        if ( argStream.NextIsNumber () )
            MinServerReqCheck ( argStream, MIN_SERVER_REQ_CALLREMOTE_CONNECT_TIMEOUT, "'connect timeout' is being used" );
        argStream.ReadIfNextIsNumber ( uiConnectTimeoutMs, 10000 );
        argStream.ReadFunction ( iLuaFunction );
        argStream.ReadLuaArguments ( args );
        argStream.ReadFunctionComplete ();

        if ( !argStream.HasErrors () )
        {
            CLuaMain * luaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
            if ( luaMain )
            {
                g_pGame->GetRemoteCalls ()->Call ( strURL, &args, luaMain, iLuaFunction, uiConnectionAttempts, uiConnectTimeoutMs );
                lua_pushboolean ( luaVM, true );
                return 1;
            }
        }
    }

    if ( argStream.HasErrors () )
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}