Beispiel #1
0
void CScriptKeyBinds::Call ( CScriptKeyBind* pKeyBind )
{
    if ( pKeyBind && !pKeyBind->IsBeingDeleted () )
    {
        switch ( pKeyBind->GetType () )
        {
            case SCRIPT_KEY_BIND_FUNCTION:
            {
                CScriptKeyFunctionBind* pBind = static_cast < CScriptKeyFunctionBind* > ( pKeyBind );
                if ( pBind->luaMain && VERIFY_FUNCTION( pBind->m_iLuaFunction ) )
                {
                    CLuaArguments Arguments;
                    Arguments.PushString ( pBind->boundKey->szKey );
                    Arguments.PushString ( ( pBind->bHitState ) ? "down" : "up" );
                    Arguments.PushArguments ( pBind->m_Arguments );
                    Arguments.Call ( pBind->luaMain, pBind->m_iLuaFunction );
                }
                break;
            }
            case SCRIPT_KEY_BIND_CONTROL_FUNCTION:
            {
                CScriptControlFunctionBind* pBind = static_cast < CScriptControlFunctionBind* > ( pKeyBind );
                if ( pBind->luaMain && VERIFY_FUNCTION( pBind->m_iLuaFunction ) )
                {
                    CLuaArguments Arguments;
                    Arguments.PushString ( pBind->boundControl->szControl );
                    Arguments.PushString ( ( pBind->bHitState ) ? "down" : "up" );
                    Arguments.PushArguments ( pBind->m_Arguments );
                    Arguments.Call ( pBind->luaMain, pBind->m_iLuaFunction );
                }
                break;
            }
        }
    }
}
int CLuaFunctionDefs::fromJSON ( lua_State* luaVM )
{
    // Got a string argument?
    SString strJsonString = "";
    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( strJsonString );

    if ( !argStream.HasErrors ( ) )
    {
        // Read it into lua arguments
        CLuaArguments Converted;
        if ( Converted.ReadFromJSONString ( strJsonString ) )
        {
            // Return it as data
            Converted.PushArguments ( luaVM );
            return Converted.Count ();
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() );

    // Failed
    lua_pushnil ( luaVM );
    return 1;
}
Beispiel #3
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;
}
Beispiel #4
0
void CKeyBinds::Call ( CKeyBind* pKeyBind )
{
    if ( pKeyBind && !pKeyBind->IsBeingDeleted () )
    {
        switch ( pKeyBind->GetType () )
        {
            case KEY_BIND_FUNCTION:
            {
                CKeyFunctionBind* pBind = static_cast < CKeyFunctionBind* > ( pKeyBind );
                if ( pBind->luaMain && pBind->m_iLuaFunction != LUA_REFNIL )
                {
                    CLuaArguments Arguments;
                    Arguments.PushElement ( m_pPlayer );
                    Arguments.PushString ( pBind->boundKey->szKey );
                    Arguments.PushString ( ( pBind->bHitState ) ? "down" : "up" );
                    Arguments.PushArguments ( pBind->m_Arguments );
                    Arguments.Call ( pBind->luaMain, pBind->m_iLuaFunction );
                }
                break;
            }
            case KEY_BIND_CONTROL_FUNCTION:
            {
                CControlFunctionBind* pBind = static_cast < CControlFunctionBind* > ( pKeyBind );
                if ( pBind->luaMain && pBind->m_iLuaFunction != LUA_REFNIL )
                {
                    CLuaArguments Arguments;
                    Arguments.PushElement ( m_pPlayer );
                    Arguments.PushString ( pBind->boundControl->szControl );
                    Arguments.PushString ( ( pBind->bHitState ) ? "down" : "up" );
                    Arguments.PushArguments ( pBind->m_Arguments );
                    Arguments.Call ( pBind->luaMain, pBind->m_iLuaFunction );
                }
                break;
            }
            default:
                break;
        }
    }
}
Beispiel #5
0
int CFunctions::HelloWorld ( lua_State* luaVM )
{
    if ( luaVM )
    {

    	int a = luaL_checkinteger ( luaVM, 1 );
        int b = luaL_checkinteger ( luaVM, 2 );

        CLuaArguments args;
        
        int n = sum(a,b);
        args.PushNumber(n);
        args.PushArguments(luaVM);
        return 1;
    }
    return 0;
}
Beispiel #6
0
int CFunctions::base64_encode( lua_State* luaVM )
{
	if( luaVM )
	{
		CLuaArguments args;
		if( lua_type( luaVM, 1 ) == LUA_TSTRING || lua_type( luaVM, 1 ) == LUA_TNUMBER )
		{
			args.PushString( base64::encode( lua_tostring( luaVM, 1 ) ).c_str( ) );
		}
		else
		{
			luaL_error( luaVM, "Bad argument @ 'base64_encode'" );
			args.PushBoolean( false );
		}
		args.PushArguments( luaVM );
		return 1;
	}
	return 0;
}
int CLuaFunctionDefs::fromJSON ( lua_State* luaVM )
{
    // Got a string argument?
    if ( lua_type ( luaVM, 1 ) == LUA_TSTRING )
    {
        // Grab the JSON string
        const char* szJSONString = lua_tostring ( luaVM, 1 );

        // Read it into lua arguments
        CLuaArguments Converted;
        if ( Converted.ReadFromJSONString ( szJSONString ) )
        {
            // Return it as data
            Converted.PushArguments ( luaVM );
            return Converted.Count ();
        }
    }

    // Failed
    lua_pushnil ( luaVM );
    return 1;
}
int CLuaFunctionDefs::Call ( lua_State* luaVM )
{
    CResource * pResource = NULL;
    SString strFunctionName = "";
    CScriptArgReader argStream ( luaVM );
    argStream.ReadUserData ( pResource );
    argStream.ReadString ( strFunctionName );
    if ( !argStream.HasErrors ( ) )
    {
        // Grab our VM
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            // Grab this resource
            CResource* pThisResource = pLuaMain->GetResource ();
            if ( pThisResource )
            {
                if ( pResource )
                {
                    //Get the target Lua VM
                    lua_State* targetLuaVM = pResource->GetVM()->GetVM();

                    // Read out the vargs
                    CLuaArguments args;
                    args.ReadArguments ( luaVM, 3 );
                    CLuaArguments returns;

                    LUA_CHECKSTACK ( targetLuaVM, 1 );   // Ensure some room

                    //Lets grab the original hidden variables so we can restore them later
                    lua_getglobal ( targetLuaVM, "sourceResource" );
                    CLuaArgument OldResource ( luaVM, -1 );
                    lua_pop( targetLuaVM, 1 );

                    lua_getglobal ( targetLuaVM, "sourceResourceRoot" );
                    CLuaArgument OldResourceRoot ( luaVM, -1 );
                    lua_pop( targetLuaVM, 1 );

                    //Set the new values for the current sourceResource, and sourceResourceRoot
                    lua_pushresource ( targetLuaVM, pThisResource );
                    lua_setglobal ( targetLuaVM, "sourceResource" );

                    lua_pushelement ( targetLuaVM, pThisResource->GetResourceEntity() );
                    lua_setglobal ( targetLuaVM, "sourceResourceRoot" );

                    // Call the exported function with the given name and the args
                    if ( pResource->CallExportedFunction ( strFunctionName, args, returns, *pThisResource ) )
                    {
                        // Push return arguments
                        returns.PushArguments ( luaVM );
                        //Restore the old variables
                        OldResource.Push ( targetLuaVM );
                        lua_setglobal ( targetLuaVM, "sourceResource" );

                        OldResourceRoot.Push ( targetLuaVM );
                        lua_setglobal ( targetLuaVM, "sourceResourceRoot" );

                        return returns.Count ();
                    }
                    else
                    {
                        //Restore the old variables
                        OldResource.Push ( targetLuaVM );
                        lua_setglobal ( targetLuaVM, "sourceResource" );

                        OldResourceRoot.Push ( targetLuaVM );
                        lua_setglobal ( targetLuaVM, "sourceResourceRoot" );
                        m_pScriptDebugging->LogError ( luaVM, "call: failed to call '%s:%s'", pResource->GetName (), *strFunctionName );
                    }
                }
                else
                {
                    m_pScriptDebugging->LogBadType ( luaVM );
                }
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() );


    // Failed
    lua_pushboolean ( luaVM, false );
    return 1;
}
int CLuaFunctionDefs::Get ( lua_State* luaVM )
{
    CResource* pResource = m_pLuaManager->GetVirtualMachine ( luaVM )->GetResource ();
    SString strSetting;
    CLuaArguments Args;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( strSetting );

    if ( !argStream.HasErrors () )
    {
        unsigned int uiIndex = 0;
        bool bDeleteNode;

        // Extract attribute name if setting to be gotten has three parts i.e. resname.settingname.attributename
        SString strAttribute = "value";
        vector < SString > Result;
        strSetting.Split ( ".", Result );
        if ( Result.size () == 3 && Result[2].length () )
        {
            strAttribute = Result[2];
        }

        // Get the setting
        CXMLNode *pSubNode, *pNode = g_pGame->GetSettings ()->Get ( pResource->GetName ().c_str (), strSetting.c_str (), bDeleteNode );

        // Only proceed if we have a valid node
        if ( pNode ) {
            // Argument count
            unsigned int uiArgCount = 1;

            // See if we need to return a table with single or multiple entries
            if ( pNode->GetSubNodeCount () == 0 ) {
                // See if required attribute exists
                CXMLAttribute *pAttribute = pNode->GetAttributes ().Find ( strAttribute.c_str () );
                if ( !pAttribute )
                {
                    if ( bDeleteNode )
                        delete pNode;
                    lua_pushboolean ( luaVM, false );
                    return 1;
                }
                // We only have a single entry for a specific setting, so output a string
                const std::string& strDataValue = pAttribute->GetValue ();
                if ( !Args.ReadFromJSONString ( strDataValue.c_str () ) ) {
                    // No valid JSON? Parse as plain text
                    Args.PushString ( strDataValue );
                }

                Args.PushArguments ( luaVM );
                uiArgCount = Args.Count ();

                /* Don't output a table because although it is more consistent with the multiple values output below,
                ** due to lua's implementation of associative arrays (assuming we use the "setting-name", "value" key-value pairs)
                ** it would require the scripter to walk through an array that only has a single entry which is a Bad Thing, performance wise.
                **
                PUSH_SETTING ( pNode );
                Args.PushAsTable ( luaVM );
                **/
            }
            else {
                // We need to return multiply entries, so push all subnodes
                while ( ( pSubNode = pNode->FindSubNode ( "setting", uiIndex++ ) ) )
                {
                    CXMLAttributes& attributes = pSubNode->GetAttributes ();
                    Args.PushString ( attributes.Find ( "name" )->GetValue () );
                    const std::string& strDataValue = attributes.Find ( "value" )->GetValue ();
                    if ( !Args.ReadFromJSONString ( strDataValue.c_str () ) )
                    {
                        Args.PushString ( strDataValue );
                    }
                }
                // Push a table and return
                Args.PushAsTable ( luaVM );
            }

            // Check if we have to delete the node
            if ( bDeleteNode )
                delete pNode;

            return uiArgCount;
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
int CLuaFunctionDefs::Call ( lua_State* luaVM )
{
    // Grab our VM
    CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
    if ( pLuaMain )
    {
        // Grab this resource
        CResource* pThisResource = pLuaMain->GetResource ();
        if ( pThisResource )
        {
            // Typechecking
            if ( lua_istype ( luaVM, 1, LUA_TLIGHTUSERDATA ) &&
                lua_istype ( luaVM, 2, LUA_TSTRING ) )
            {
                // Grab the resource
                CResource* pResource = lua_toresource ( luaVM, 1 );
                if ( pResource )
                {
                    //Get the target Lua VM
                    lua_State* targetLuaVM = pResource->GetVM()->GetVM();

                    // The function name
                    const char* szFunctionName = lua_tostring ( luaVM, 2 );

                    // Read out the vargs
                    CLuaArguments args;
                    args.ReadArguments ( luaVM, 3 );
                    CLuaArguments returns;

                    //Lets grab the original hidden variables so we can restore them later
                    lua_getglobal ( targetLuaVM, "sourceResource" );
                    CLuaArgument OldResource ( luaVM, -1 );

                    lua_getglobal ( targetLuaVM, "sourceResourceRoot" );
                    CLuaArgument OldResourceRoot ( luaVM, -1 );

                    //Set the new values for the current sourceResource, and sourceResourceRoot
                    lua_pushresource ( targetLuaVM, pThisResource );
                    lua_setglobal ( targetLuaVM, "sourceResource" );

                    lua_pushelement ( targetLuaVM, pThisResource->GetResourceEntity() );
                    lua_setglobal ( targetLuaVM, "sourceResourceRoot" );

                    // Call the exported function with the given name and the args
                    if ( pResource->CallExportedFunction ( szFunctionName, args, returns, *pThisResource ) )
                    {
                        // Push return arguments
                        returns.PushArguments ( luaVM );
                        //Restore the old variables
                        OldResource.Push ( targetLuaVM );
                        lua_setglobal ( targetLuaVM, "sourceResource" );

                        OldResourceRoot.Push ( targetLuaVM );
                        lua_setglobal ( targetLuaVM, "sourceResourceRoot" );

                        return returns.Count ();
                    }
                    else
                    {
                        //Restore the old variables
                        OldResource.Push ( targetLuaVM );
                        lua_setglobal ( targetLuaVM, "sourceResource" );

                        OldResourceRoot.Push ( targetLuaVM );
                        lua_setglobal ( targetLuaVM, "sourceResourceRoot" );
                        m_pScriptDebugging->LogError ( luaVM, "call: failed to call '%s:%s'", pResource->GetName (), szFunctionName );
                    }
                }
                else
                {
                    m_pScriptDebugging->LogBadPointer ( luaVM, "call", "resource", 1 );
                }
            }
            else
            {
                m_pScriptDebugging->LogBadType ( luaVM, "call" );
            }
        }
    }

    // Failed
    lua_pushboolean ( luaVM, false );
    return 1;
}