Esempio n. 1
0
bool CClientEntity::GetCustomDataFloat ( const char* szName, float& fOut, bool bInheritData )
{
    // Grab the custom data variable
    CLuaArgument* pData = GetCustomData ( szName, bInheritData );
    if ( pData )
    {
        // Write the content depending on what type it is
        int iType = pData->GetType ();
        if ( iType == LUA_TSTRING )
        {
            fOut = static_cast < float > ( atof ( pData->GetString () ) );
        }
        else if ( iType == LUA_TNUMBER )
        {
            fOut = static_cast < float > ( pData->GetNumber () );
        }
        else
        {
            return false;
        }

        return true;
    }

    return false;
}
Esempio n. 2
0
bool CClientEntity::GetCustomDataString ( const char* szName, SString& strOut, bool bInheritData )
{
    // Grab the custom data variable
    CLuaArgument* pData = GetCustomData ( szName, bInheritData );
    if ( pData )
    {
        // Write the content depending on what type it is
        int iType = pData->GetType ();
        if ( iType == LUA_TSTRING )
        {
            strOut = pData->GetString ();
        }
        else if ( iType == LUA_TNUMBER )
        {
            strOut.Format ( "%f", pData->GetNumber () );
        }
        else if ( iType == LUA_TBOOLEAN )
        {
            strOut.Format ( "%u", pData->GetBoolean () );
        }
        else if ( iType == LUA_TNIL )
        {
            strOut = "";
        }
        else
        {
            return false;
        }

        return true;
    }

    return false;
}
Esempio n. 3
0
CXMLNode * CCustomData::OutputToXML ( CXMLNode * pNode )
{
    std::map < std::string, SCustomData > :: const_iterator iter = m_Data.begin ();
    for ( ; iter != m_Data.end (); iter++ )
    {
        CLuaArgument* arg = (CLuaArgument *)&iter->second.Variable;
        
        switch ( arg->GetType() )
        {
        case LUA_TSTRING:
            {
                CXMLAttribute* attr = pNode->GetAttributes().Create( iter->first.c_str () );
                attr->SetValue ( arg->GetString ().c_str () );
                break;
            }
        case LUA_TNUMBER:
            {
                CXMLAttribute* attr = pNode->GetAttributes().Create( iter->first.c_str () );
                attr->SetValue ( (float)arg->GetNumber () );
                break;
            }
        case LUA_TBOOLEAN:
            {
                CXMLAttribute* attr = pNode->GetAttributes().Create( iter->first.c_str () );
                attr->SetValue ( arg->GetBoolean () );
                break;
            }
        }
    }
    return pNode;   
}
Esempio n. 4
0
bool CRegistry::Query ( std::string strQuery, CLuaArguments *pArgs, CRegistryResult* pResult )
{
    std::string strParsedQuery = "";

    if ( m_bOpened == false ) {
        m_strLastError = "SQLite3 was not opened, cannot perform query!";
        return false;
    }

    // Walk through the query and replace the variable placeholders with the actual variables
    unsigned int uiLen = strQuery.length ();
    unsigned int a = 0, type = 0;
    const char *szContent = NULL;
    char szBuffer[32] = {0};
    for ( unsigned int i = 0; i < uiLen; i++ )
    {
        if ( strQuery.at(i) == SQL_VARIABLE_PLACEHOLDER ) {
            // If the placeholder is found, replace it with the variable
            CLuaArgument *pArgument = (*pArgs)[a++];

            // Check the type of the argument and convert it to a string we can process
            if ( pArgument ) {
                type = pArgument->GetType ();
                if ( type == LUA_TBOOLEAN ) {
                    szContent = ( pArgument->GetBoolean() ) ? "true" : "false";
                } else if ( type == LUA_TNUMBER ) {
                    _snprintf ( szBuffer, 31, "%f", pArgument->GetNumber () );
                    szContent = szBuffer;
                } else if ( type == LUA_TSTRING ) {
                    szContent = pArgument->GetString ().c_str ();

                    // If we have a string, add a quote at the beginning too
                    strParsedQuery += '\'';
                }
            }

            // Copy the string into the query, and escape the single quotes as well
            if ( szContent ) {
                for ( unsigned int k = 0; k < strlen ( szContent ); k++ ) {
                    if ( szContent[k] == '\'' )
                        strParsedQuery += '\'';
                    strParsedQuery += szContent[k];
                }
                // If we have a string, add a quote at the end too
                if ( type == LUA_TSTRING ) strParsedQuery += '\'';
            } else {
                // If we don't have any content, put just output 2 quotes to indicate an empty variable
                strParsedQuery += "\'\'";
            }

        } else {
            // If we found a normal character, copy it into the destination buffer
            strParsedQuery += strQuery[i];
        }
    }

    return QueryInternal ( strParsedQuery.c_str (), pResult );
}
///////////////////////////////////////////////////////////////
//
// InsertQueryArgumentsSqlite
//
// Insert arguments and apply Sqlite escapement
//
///////////////////////////////////////////////////////////////
SString InsertQueryArgumentsSqlite ( const SString& strQuery, CLuaArguments* pArgs )
{
    SString strParsedQuery;

    // Walk through the query and replace the variable placeholders with the actual variables
    unsigned int uiLen = strQuery.length ();
    unsigned int a = 0;
    for ( unsigned int i = 0 ; i < uiLen ; i++ )
    {
        if ( strQuery[i] != SQL_VARIABLE_PLACEHOLDER )
        {
            // If we found a normal character, copy it into the destination buffer
            strParsedQuery += strQuery[i];
        }
        else
        {
            // Use ?? for unquoted strings
            bool bUnquotedStrings = strQuery[i+1] == SQL_VARIABLE_PLACEHOLDER;
            if ( bUnquotedStrings )
                i++;

            // If the placeholder is found, replace it with the variable
            CLuaArgument* pArgument = (*pArgs)[a++];

            // Check the type of the argument and convert it to a string we can process
            uint type = pArgument ? pArgument->GetType () : LUA_TNONE;
            if ( type == LUA_TBOOLEAN )
            {
                strParsedQuery += ( pArgument->GetBoolean() ) ? "1" : "0";
            }
            else
            if ( type == LUA_TNUMBER )
            {
                double dNumber = pArgument->GetNumber ();
                if ( dNumber == floor ( dNumber ) )
                    strParsedQuery += SString ( "%" PRId64, (long long)dNumber );
                else
                    strParsedQuery += SString ( "%f", dNumber );
            }
            else
            if ( type == LUA_TSTRING )
            {
                // Copy the string into the query, and escape '
                if ( !bUnquotedStrings ) strParsedQuery += '\'';
                SqliteEscape ( strParsedQuery, pArgument->GetString ().c_str (), pArgument->GetString ().length () );
                if ( !bUnquotedStrings ) strParsedQuery += '\'';
            }
            else
            {
                // If we don't have any content, put just output 2 quotes to indicate an empty variable
                strParsedQuery += "\'\'";
            }
        }
    }

    return strParsedQuery;
}
Esempio n. 6
0
bool CClientEntity::GetCustomDataBool ( const char* szName, bool& bOut, bool bInheritData )
{
    // Grab the custom data variable
    CLuaArgument* pData = GetCustomData ( szName, bInheritData );
    if ( pData )
    {
        // Write the content depending on what type it is
        int iType = pData->GetType ();
        if ( iType == LUA_TSTRING )
        {
            const char* szString = pData->GetString ();
            if ( strcmp ( szString, "true" ) == 0 || strcmp ( szString, "1" ) == 0 )
            {
                bOut = true;
            }
            else if ( strcmp ( szString, "false" ) == 0 || strcmp ( szString, "0" ) == 0 )
            {
                bOut = false;
            }
            else
            {
                return false;
            }
        }
        else if ( iType == LUA_TNUMBER )
        {
            int iNumber = static_cast < int > ( pData->GetNumber () );
            if ( iNumber == 1 )
            {
                bOut = true;
            }
            else if ( iNumber == 0 )
            {
                bOut = false;
            }
            else
            {
                return false;
            }
        }
        else if ( iType == LUA_TBOOLEAN )
        {
            bOut = pData->GetBoolean ();
        }
        else
        {
            return false;
        }

        return true;
    }

    return false;
}
Esempio n. 7
0
const char* CClientTask::GetParameterString ( const char* szKey )
{
    // Grab the parameter and check its type
    CLuaArgument* pArgument = GetParameter ( szKey );
    if ( pArgument && pArgument->GetType () == LUA_TSTRING )
    {
        // Return the string
        return pArgument->GetString ();
    }

    // Non-existing
    return NULL;
}
Esempio n. 8
0
bool CClientTask::GetParameterNumber ( const char* szKey, float& Number )
{
    // Grab the parameter and check its type
    CLuaArgument* pArgument = GetParameter ( szKey );
    if ( pArgument && pArgument->GetType () == LUA_TNUMBER )
    {
        // Return the number
        Number = static_cast < float > ( pArgument->GetNumber () );
        return true;
    }

    // Non-existing
    return false;
}
Esempio n. 9
0
bool CClientTask::GetParameterBool ( const char* szKey, bool& Bool )
{
    // Grab the parameter and check its type
    CLuaArgument* pArgument = GetParameter ( szKey );
    if ( pArgument && pArgument->GetType () == LUA_TBOOLEAN )
    {
        // Return the bool
        Bool = pArgument->GetBoolean ();
        return true;
    }

    // Non-existing
    return false;
}
Esempio n. 10
0
CVehicle* CClientTask::GetParameterVehicle ( const char* szKey )
{
    // Grab the parameter, is it userdata?
    CLuaArgument* pArgument = GetParameter ( szKey );
    if ( pArgument && pArgument->GetType () == LUA_TLIGHTUSERDATA )
    {
        // Grab the player and verify it
        CClientVehicle* pVehicle = reinterpret_cast < CClientVehicle* > ( pArgument->GetLightUserData () );
        if ( VERIFY_VEHICLE ( pVehicle ) )
        {
            // Return the game vehicle
            return pVehicle->GetGameVehicle ();
        }
    }

    // Non-existing
    return NULL;
}
Esempio n. 11
0
CPed* CClientTask::GetParameterPed ( const char* szKey )
{
    // Grab the parameter, is it userdata?
    CLuaArgument* pArgument = GetParameter ( szKey );
    if ( pArgument && pArgument->GetType () == LUA_TLIGHTUSERDATA )
    {
        // Grab the player and verify it
        CClientPlayer* pPlayer = reinterpret_cast < CClientPlayer* > ( pArgument->GetLightUserData () );
        if ( VERIFY_PLAYER ( pPlayer ) )
        {
            // Return his game player
            return pPlayer->GetGamePlayer ();
        }
    }

    // Non-existing
    return NULL;
}
Esempio n. 12
0
bool CElement::GetCustomDataString ( const char* szName, char* pOut, size_t sizeBuffer, bool bInheritData )
{
    // Grab the custom data variable
    CLuaArgument* pData = GetCustomData ( szName, bInheritData );
    if ( pData )
    {
        // Make sure it gets 0 terminated
        sizeBuffer -= 1;
        pOut [sizeBuffer] = 0;

        // Write the content depending on what type it is
        int iType = pData->GetType ();
        if ( iType == LUA_TSTRING )
        {
            strncpy ( pOut, pData->GetString ().c_str (), sizeBuffer );
        }
        else if ( iType == LUA_TNUMBER )
        {
            snprintf ( pOut, sizeBuffer, "%f", pData->GetNumber () );
        }
        else if ( iType == LUA_TBOOLEAN )
        {
            snprintf ( pOut, sizeBuffer, "%u", pData->GetBoolean () );
        }
        else if ( iType == LUA_TNIL )
        {
            pOut [0] = 0;
        }
        else
        {
            return false;
        }

        return true;
    }

    return false;
}
Esempio n. 13
0
bool CClientEntity::GetCustomDataInt ( const char* szName, int& iOut, bool bInheritData )
{
    // Grab the custom data variable
    CLuaArgument* pData = GetCustomData ( szName, bInheritData );
    if ( pData )
    {
        // Write the content depending on what type it is
        int iType = pData->GetType ();
        if ( iType == LUA_TSTRING )
        {
            iOut = atoi ( pData->GetString () );
        }
        else if ( iType == LUA_TNUMBER )
        {
            iOut = static_cast < int > ( pData->GetNumber () );
        }
        else if ( iType == LUA_TBOOLEAN )
        {
            if ( pData->GetBoolean () )
            {
                iOut = 1;
            }
            else
            {
                iOut = 0;
            }
        }
        else
        {
            return false;
        }

        return true;
    }

    return false;
}
Esempio n. 14
0
bool CRegistry::Query ( const std::string& strQuery, CLuaArguments *pArgs, CRegistryResult* pResult )
{
    std::string strParsedQuery = "";

    if ( m_bOpened == false )
	{
        SetLastErrorMessage ( "SQLite3 was not opened, cannot perform query!", strQuery );
        return false;
    }

    // Walk through the query and replace the variable placeholders with the actual variables
    unsigned int uiLen = strQuery.length ();
    unsigned int a = 0, type = 0;
    const char *szContent = NULL;
    char szBuffer[32] = {0};
    for ( unsigned int i = 0; i < uiLen; i++ )
    {
        if ( strQuery.at(i) == SQL_VARIABLE_PLACEHOLDER ) {
            // If the placeholder is found, replace it with the variable
            CLuaArgument *pArgument = (*pArgs)[a++];

            // Check the type of the argument and convert it to a string we can process
            if ( pArgument ) {
                type = pArgument->GetType ();
                if ( type == LUA_TBOOLEAN ) {
                    szContent = ( pArgument->GetBoolean() ) ? "true" : "false";
                } else if ( type == LUA_TNUMBER ) {
                    snprintf ( szBuffer, 31, "%f", pArgument->GetNumber () );
                    szContent = szBuffer;
                } else if ( type == LUA_TSTRING ) {
                    szContent = pArgument->GetString ().c_str ();

                    // If we have a string, add a quote at the beginning too
                    strParsedQuery += '\'';
                }
            }

            // Copy the string into the query, and escape the single quotes as well
            if ( szContent ) {
                for ( unsigned int k = 0; szContent[k] != '\0'; k++ ) {
                    if ( szContent[k] == '\'' )
                        strParsedQuery += '\'';
                    strParsedQuery += szContent[k];
                }
                // If we have a string, add a quote at the end too
                if ( type == LUA_TSTRING ) strParsedQuery += '\'';
            } else {
                // If we don't have any content, put just output 2 quotes to indicate an empty variable
                strParsedQuery += "\'\'";
            }

        } else {
            // If we found a normal character, copy it into the destination buffer
            strParsedQuery += strQuery[i];
        }
    }

    // Catch BEGIN/END/COMMIT TRANSACTION and ignore
    SString strTest = SString ( strParsedQuery ).ToUpper ();
    if ( strTest.find ( "TRANSACTION" ) != std::string::npos )
    {
        strTest = strTest.Replace ( "\t", " " ).Replace ( "  ", " ", true ).TrimStart ( " " ).TrimEnd ( " " );
        if ( strTest.find ( "BEGIN" ) == 0 || strTest.find ( "END" ) == 0 || strTest.find ( "COMMIT" ) == 0 )
        {
            return true;
        }
    }

    BeginAutomaticTransaction ();
    return QueryInternal ( strParsedQuery.c_str (), pResult );
}
Esempio n. 15
0
json_object * CLuaArguments::WriteTableToJSONObject ( bool bSerialize, std::map < CLuaArguments*, unsigned long > * pKnownTables )
{
    bool bKnownTablesCreated = false;
    if ( !pKnownTables )
    {
        pKnownTables = new std::map < CLuaArguments*, unsigned long > ();
        bKnownTablesCreated = true;
    }

    pKnownTables->insert ( std::make_pair ( this, pKnownTables->size () ) );

    bool bIsArray = true;
    unsigned int iArrayPos = 1; // lua arrays are 1 based
    vector < CLuaArgument* > ::const_iterator iter = m_Arguments.begin ();
    for ( ; iter != m_Arguments.end () ; iter+=2 )
    {
        CLuaArgument* pArgument = *iter;
        if ( pArgument->GetType() == LUA_TNUMBER )
        {
            double num = pArgument->GetNumber();
            unsigned int iNum = static_cast < unsigned int > ( num );
            if ( num == iNum )
            {
                if ( iArrayPos != iNum ) // check if the value matches its index in the table
                {
                    bIsArray = false;
                    break;
                }
            }
            else
            {
                bIsArray = false;
                break;
            }
        }
        else
        {
            bIsArray = false;
            break;
        }
        iArrayPos++;
    }
    
    if ( bIsArray )
    {
        json_object * my_array = json_object_new_array();
        vector < CLuaArgument* > ::const_iterator iter = m_Arguments.begin ();
        for ( ; iter != m_Arguments.end () ; iter++ ) 
        {
            iter++; // skip the key values
            CLuaArgument* pArgument = *iter;
            json_object * object = pArgument->WriteToJSONObject ( bSerialize, pKnownTables );
            if ( object )
            {
                json_object_array_add(my_array, object);
            }
            else
            {
                break;
            }
        }
        if ( bKnownTablesCreated )
            delete pKnownTables;
        return my_array;
    }
    else
    {
        json_object * my_object = json_object_new_object();
        iter = m_Arguments.begin ();
        for ( ; iter != m_Arguments.end () ; iter++ )
        {
            char szKey[255];
            szKey[0] = '\0';
            CLuaArgument* pArgument = *iter;
            if ( !pArgument->WriteToString(szKey, 255) ) // index
                break;
            iter++;
            pArgument = *iter;
            json_object * object = pArgument->WriteToJSONObject ( bSerialize, pKnownTables ); // value

            if ( object )
            {
                json_object_object_add(my_object, szKey, object);
            }
            else
            {            
                break;
            }
        }
        if ( bKnownTablesCreated )
            delete pKnownTables;
        return my_object;
    }
}
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;
}
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;
}