int CLuaFunctionDefs::AddCommandHandler ( lua_State* luaVM )
{
    // Grab our VM
    CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
    if ( pLuaMain )
    {
        // Got two strings?
        int iArgument1 = lua_type ( luaVM, 1 );
        int iArgument2 = lua_type ( luaVM, 2 );
        if ( iArgument1 == LUA_TSTRING && iArgument2 == LUA_TFUNCTION )
        {
            // Check if the command name is case sensitive
            bool bCaseSensitive = true;
            if ( lua_type ( luaVM, 3 ) == LUA_TBOOLEAN )
                bCaseSensitive = ( ( lua_toboolean ( luaVM, 3 ) == 0 ) ? false : true );

            // Grab the strings. Valid?
            const char* szKey = lua_tostring ( luaVM, 1 );
            int iLuaFunction = luaM_toref ( luaVM, 2 );
            if ( szKey [0] != 0 && VERIFY_FUNCTION ( iLuaFunction ) )
            {
                // Add them to our list over command handlers
                if ( m_pRegisteredCommands->AddCommand ( pLuaMain, szKey, iLuaFunction, bCaseSensitive ) )
                {
                    lua_pushboolean ( luaVM, true );
                    return 1;
                }
            }
            else
                m_pScriptDebugging->LogWarning ( luaVM, "Empty key or handler strings sent to addCommandHandler" );
        }
        else
            m_pScriptDebugging->LogBadType ( luaVM, "addCommandHandler" );
    }

    lua_pushboolean ( luaVM, false );
    return 1;
}
示例#2
0
CLuaTimer* CLuaTimerManager::AddTimer ( lua_State* luaVM )
{
    if ( luaVM )
    {
        int iArgument2 = lua_type ( luaVM, 2 );
        int iArgument3 = lua_type ( luaVM, 3 );
        if ( lua_type ( luaVM, 1 ) &&
             (( iArgument2 == LUA_TNUMBER || iArgument2 == LUA_TSTRING ) ||
              ( iArgument3 == LUA_TNUMBER || iArgument3 == LUA_TSTRING ) ))
        {
            // Grab the string argument, start-time, delay and repeats
            unsigned long ulTimeDelay = static_cast < unsigned long > ( lua_tonumber ( luaVM, 2 ) );
            unsigned int uiRepeats = static_cast < unsigned int > ( lua_tonumber ( luaVM, 3 ) );

            // Check for the minimum interval
            if ( ulTimeDelay < LUA_TIMER_MIN_INTERVAL ) return NULL;

            // Grab the arguments from argument 4 and up
            CLuaArguments Arguments;
            Arguments.ReadArguments ( luaVM, 4 );

            int iLuaFunction = luaM_toref ( luaVM, 1 );

            if ( iLuaFunction != LUA_REFNIL )
            {
                // Add the timer
                CLuaTimer* pLuaTimer = new CLuaTimer ( iLuaFunction, Arguments );
                pLuaTimer->SetStartTime ( GetTime () );
                pLuaTimer->SetDelay ( ulTimeDelay );
                pLuaTimer->SetRepeats ( uiRepeats );
                m_TimerList.push_back ( pLuaTimer );
                return pLuaTimer;
            }
        }
    }
    return false;
}