예제 #1
0
//////////////////////////////////////////////////////////////////////
//
// Fetch multiple values for a named setting from the server config
//
//  <module src="module_test.dll" />
//  <resource src="admin" startup="1" protected="0" />
//
//////////////////////////////////////////////////////////////////////
bool CMainConfig::GetSettingTable ( const SString& strName, const char** szAttribNames, uint uiNumAttribNames, CLuaArguments* outTable )
{
    uint uiXMLIndex = 0;
    uint uiLuaIndex = 1;
    CXMLNode* pNode = NULL;
    do
    {
        // Grab the current script node
        pNode = m_pRootNode->FindSubNode ( strName, uiXMLIndex++ );
        if ( pNode )
        {
            CLuaArguments resultLine;
            CXMLAttributes& attributes = pNode->GetAttributes();
            for ( uint i = 0 ; i < attributes.Count() ; i++ )
            {
                CXMLAttribute* pAttribute = attributes.Get( i );
                resultLine.PushString( pAttribute->GetName() );
                resultLine.PushString( pAttribute->GetValue() );
            }

            if ( resultLine.Count() != 0 )
            {
                outTable->PushNumber( uiLuaIndex++ );
                outTable->PushTable( &resultLine );
            }
        }
    }
    while( pNode );

    return outTable->Count() != 0;
}
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;
}
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;
}
예제 #4
0
int CLuaFunctionDefs::Load( lua_State* luaVM )
{
//  func,err load( callback callbackFunction[, string name] )
    CLuaFunctionRef iLuaFunction; SString strName;

    CScriptArgReader argStream( luaVM );
    argStream.ReadFunction( iLuaFunction );
    argStream.ReadString( strName, "=(load)" );
    argStream.ReadFunctionComplete();

    if ( !argStream.HasErrors() )
    {
        // Call supplied function to get all the bits
        // Should apply some limit here?
        SString strInput;
        CLuaArguments callbackArguments;
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine( luaVM );
        while( pLuaMain )
        {
            CLuaArguments returnValues;
            callbackArguments.Call( pLuaMain, iLuaFunction, &returnValues );
            if ( returnValues.Count() )
            {
                CLuaArgument* returnedValue = *returnValues.IterBegin();
                if ( returnedValue->GetType() == LUA_TSTRING )
                {
                    strInput += returnedValue->GetString();
                    continue;
                }
            }
            break;
        }

        const char* szChunkname = *strName;
        const char* cpInBuffer = strInput;
        uint uiInSize = strInput.length();

        // Decrypt if required
        const char* cpBuffer;
        uint uiSize;
        if ( !g_pNet->DecryptScript( cpInBuffer, uiInSize, &cpBuffer, &uiSize, m_pResourceManager->GetResourceName( luaVM ) + "/load" ) )
        {
            SString strMessage( "argument 2 is invalid. Please re-compile at http://luac.mtasa.com/", 0 ); 
            argStream.SetCustomError( strMessage );
            cpBuffer = NULL;
            g_pCore->GetConsole()->Print( argStream.GetFullErrorMessage() );
            g_pClientGame->TellServerSomethingImportant( 1005, argStream.GetFullErrorMessage(), true );
        }

        if ( !argStream.HasErrors() )
        {
            CLuaShared::CheckUTF8BOMAndUpdate ( &cpBuffer, &uiSize );
            if ( !CLuaMain::LuaLoadBuffer( luaVM, cpBuffer, uiSize, szChunkname ) )
            {
                // Ok
                return 1;
            }
            else
            {
                lua_pushnil( luaVM );
                lua_insert( luaVM, -2 );  /* put before error message */
                return 2;  /* return nil plus error message */
            }
        }
    }
    if ( argStream.HasErrors() )
        m_pScriptDebugging->LogCustom( luaVM, argStream.GetFullErrorMessage() );

    lua_pushboolean( luaVM, false );
    return 1;
}
예제 #5
0
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;
}
예제 #6
0
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;
}
int CLuaFunctionDefs::SetBrowserAjaxHandler ( lua_State* luaVM )
{
    //  bool setBrowserAjaxHandler ( browser browser, string URL[, function callback] )
    CClientWebBrowser* pWebBrowser; SString strURL; CLuaFunctionRef callbackFunction;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadUserData ( pWebBrowser );
    argStream.ReadString ( strURL );

    if ( argStream.NextIsNil () || argStream.NextIsNone () )
    {
        if ( !argStream.HasErrors () )
        {
            lua_pushboolean ( luaVM, pWebBrowser->RemoveAjaxHandler ( strURL ) );
            return 1;
        }
        else
            m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );
    }
    else
    {
        argStream.ReadFunction ( callbackFunction );
        argStream.ReadFunctionComplete ();
        if ( !argStream.HasErrors () )
        {
            CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
            if ( pLuaMain && VERIFY_FUNCTION ( callbackFunction ) )
            {
                CResource* pResource = pLuaMain->GetResource ();
                CResourceManager * pResourceManager = m_pResourceManager;
                auto netId = pResource->GetNetID ();

                bool bResult = pWebBrowser->AddAjaxHandler ( strURL, 
                [=] ( std::vector<SString>& vecGet, std::vector<SString>& vecPost ) -> const SString
                {
                    // Make sure the resource is still running
                    if ( !pResourceManager->Exists ( pResource ) || pResource->GetNetID() != netId )
                    {
                        return "";
                    }

                    // Make sure the function is valid
                    if ( VERIFY_FUNCTION ( callbackFunction ) )
                    {
                        CLuaArguments arguments;
                        CLuaArguments getArguments;
                        CLuaArguments postArguments;

                        for ( auto&& param : vecGet )
                            getArguments.PushString ( param );

                        for ( auto&& param : vecPost )
                            postArguments.PushString ( param );

                        arguments.PushTable ( &getArguments );
                        arguments.PushTable ( &postArguments );

                        CLuaArguments result;
                        
                        arguments.Call ( pLuaMain, callbackFunction, &result );

                        if ( result.Count () == 0 )
                            return "";


                        CLuaArgument* returnedValue = *result.IterBegin ();
                        if ( returnedValue->GetType () == LUA_TSTRING )                       
                            return returnedValue->GetString ();
                        else
                            return "";
                    }
                    else
                        return "";

                } );

                lua_pushboolean ( luaVM, bResult );
                return 1;
            }
        }
        else
            m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );
    }

    lua_pushboolean ( luaVM, false );
    return 1;
}