CLuaArgument* CLuaArguments::PushUserData ( void* pUserData ) { CLuaArgument* pArgument = new CLuaArgument; pArgument->ReadUserData ( pUserData ); m_Arguments.push_back ( pArgument ); return pArgument; }
CLuaArgument* CLuaArguments::PushTable ( CLuaArguments * table ) { CLuaArgument* pArgument = new CLuaArgument ( ); pArgument->Read(table); m_Arguments.push_back ( pArgument ); return pArgument; }
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; }
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 ); }
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; }
bool CLuaArguments::ReadFromJSONString(const char* szJSON) { // Fast isJSON check: Check first non-white space character is '[' or '{' for (const char* ptr = szJSON; true;) { char c = *ptr++; if (c == '[' || c == '{') break; if (isspace((uchar)c)) continue; return false; } json_object* object = json_tokener_parse(szJSON); if (!is_error(object)) { if (json_object_get_type(object) == json_type_array) { bool bSuccess = true; std::vector<CLuaArguments*> knownTables; for (int i = 0; i < json_object_array_length(object); i++) { json_object* arrayObject = json_object_array_get_idx(object, i); CLuaArgument* pArgument = new CLuaArgument(); bSuccess = pArgument->ReadFromJSONObject(arrayObject, &knownTables); m_Arguments.push_back(pArgument); // then the value if (!bSuccess) break; } json_object_put(object); // dereference return bSuccess; } else if (json_object_get_type(object) == json_type_object) { std::vector<CLuaArguments*> knownTables; CLuaArgument* pArgument = new CLuaArgument(); bool bSuccess = pArgument->ReadFromJSONObject(object, &knownTables); m_Arguments.push_back(pArgument); // value json_object_put(object); return bSuccess; } json_object_put(object); // dereference } // else // g_pGame->GetScriptDebugging()->LogError ( "Could not parse invalid JSON object."); // else // g_pGame->GetScriptDebugging()->LogError ( "Could not parse HTTP POST request, ensure data uses JSON."); return false; }
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; }
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; }
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; }
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; }
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; }
void CElementRPCs::SetElementData ( CClientEntity* pSource, NetBitStreamInterface& bitStream ) { unsigned short usNameLength; if ( bitStream.ReadCompressed ( usNameLength ) ) { // We should never receive an illegal name length from the server if ( usNameLength > MAX_CUSTOMDATA_NAME_LENGTH ) { CLogger::ErrorPrintf ( "RPC SetElementData name length > MAX_CUSTOMDATA_NAME_LENGTH" ); return; } SString strName; CLuaArgument Argument; if ( bitStream.ReadStringCharacters ( strName, usNameLength ) && Argument.ReadFromBitStream ( bitStream ) ) { pSource->SetCustomData ( strName, Argument, NULL ); } } }
json_object * CLuaArguments::WriteToJSONArray ( bool bSerialize ) { json_object * my_array = json_object_new_array(); vector < CLuaArgument* > ::const_iterator iter = m_Arguments.begin (); for ( ; iter != m_Arguments.end () ; iter++ ) { CLuaArgument* pArgument = *iter; json_object * object = pArgument->WriteToJSONObject ( bSerialize ); if ( object ) { json_object_array_add(my_array, object); } else { break; } } return my_array; }
bool CLuaArguments::WriteToBitStream(NetBitStreamInterface& bitStream, CFastHashMap<CLuaArguments*, unsigned long>* pKnownTables) const { bool bKnownTablesCreated = false; if (!pKnownTables) { pKnownTables = new CFastHashMap<CLuaArguments*, unsigned long>(); bKnownTablesCreated = true; } bool bSuccess = true; pKnownTables->insert(make_pair((CLuaArguments*)this, pKnownTables->size())); #if MTA_DM_VERSION >= 0x150 bitStream.WriteCompressed(static_cast<unsigned int>(m_Arguments.size())); #else if (ExtractVersionStringBuildNumber(g_pGame->GetPlayerManager()->GetLowestConnectedPlayerVersion()) < ExtractVersionStringBuildNumber("1.4.0-9.06858") && MTASA_VERSION_TYPE != VERSION_TYPE_CUSTOM) bitStream.WriteCompressed(static_cast<unsigned short>(m_Arguments.size())); else { // Send 0xFFFF to indicate that we're using the new version | TODO: Remove this in 1.5 bitStream.WriteCompressed(static_cast<unsigned short>(0xFFFF)); bitStream.WriteCompressed(static_cast<unsigned int>(m_Arguments.size())); } #endif vector<CLuaArgument*>::const_iterator iter = m_Arguments.begin(); for (; iter != m_Arguments.end(); ++iter) { CLuaArgument* pArgument = *iter; if (!pArgument->WriteToBitStream(bitStream, pKnownTables)) { bSuccess = false; } } if (bKnownTablesCreated) delete pKnownTables; return bSuccess; }
void CElementRPCs::SetElementData ( NetBitStreamInterface& bitStream ) { ElementID ID; unsigned short usNameLength; if ( bitStream.ReadCompressed ( ID ) && bitStream.ReadCompressed ( usNameLength ) ) { char* szName = new char [ usNameLength + 1 ]; szName [ usNameLength ] = NULL; CLuaArgument Argument; if ( bitStream.Read ( szName, usNameLength ) && Argument.ReadFromBitStream ( bitStream ) ) { CClientEntity* pEntity = CElementIDs::GetElement ( ID ); if ( pEntity ) { pEntity->SetCustomData ( szName, Argument, NULL ); } } delete [] szName; } }
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; }
CLuaArgument* CAccountManager::GetAccountData( CAccount* pAccount, const char* szKey ) { //Get the user ID int iUserID = pAccount->GetID(); //create a new registry result for the query return CRegistryResult result; //Select the value and type from the database where the user is our user and the key is the required key m_pDatabaseManager->QueryWithResultf ( m_hDbConnection, &result, "SELECT value,type from userdata where userid=? and key=? LIMIT 1", SQLITE_INTEGER, iUserID, SQLITE_TEXT, szKey ); // Default result is nil CLuaArgument* pResult = new CLuaArgument (); //Do we have any results? if ( result->nRows > 0 ) { const CRegistryResultRow& row = result->Data.front(); int iType = static_cast < int > ( row[1].nVal ); //Account data is stored as text so we don't need to check what type it is just return it if ( iType == LUA_TBOOLEAN ) { SString strResult = (const char *)row[0].pVal; pResult->ReadBool ( strResult == "true" ); } else if ( iType == LUA_TNUMBER ) pResult->ReadNumber ( strtod ( (const char *)row[0].pVal, NULL ) ); else pResult->ReadString ( (const char *)row[0].pVal ); } else { //No results pResult->ReadBool ( false ); } return pResult; }
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; }
/////////////////////////////////////////////////////////////// // // 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; }
bool CLuaArguments::WriteToBitStream(NetBitStreamInterface& bitStream, CFastHashMap<CLuaArguments*, unsigned long>* pKnownTables) const { bool bKnownTablesCreated = false; if (!pKnownTables) { pKnownTables = new CFastHashMap<CLuaArguments*, unsigned long>(); bKnownTablesCreated = true; } bool bSuccess = true; pKnownTables->insert(make_pair((CLuaArguments*)this, pKnownTables->size())); #if MTA_DM_VERSION >= 0x150 bitStream.WriteCompressed(static_cast<unsigned int>(m_Arguments.size())); #else if (bitStream.Version() < 0x05B) bitStream.WriteCompressed(static_cast<unsigned short>(m_Arguments.size())); else bitStream.WriteCompressed(static_cast<unsigned int>(m_Arguments.size())); #endif vector<CLuaArgument*>::const_iterator iter = m_Arguments.begin(); for (; iter != m_Arguments.end(); iter++) { CLuaArgument* pArgument = *iter; if (!pArgument->WriteToBitStream(bitStream, pKnownTables)) { bSuccess = false; } } if (bKnownTablesCreated) delete pKnownTables; return bSuccess; }
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::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; }
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; } }
bool CMapEventManager::Call ( const char* szName, const CLuaArguments& Arguments, class CClientEntity* pSource, class CClientEntity* pThis ) { // Call all the events with matching names bool bCalled = false; CMapEvent* pMapEvent; bool bIsAlreadyIterating = m_bIteratingList; m_bIteratingList = true; list < CMapEvent* > ::const_iterator iter = m_Events.begin (); for ( ; iter != m_Events.end (); iter++ ) { pMapEvent = *iter; // If it's not being destroyed if ( !pMapEvent->IsBeingDestroyed () ) { // Compare the names if ( strcmp ( pMapEvent->GetName (), szName ) == 0 ) { // Call if propagated? if ( pSource == pThis || pMapEvent->IsPropagated () ) { // Grab the current VM lua_State* pState = pMapEvent->GetVM ()->GetVM (); #if MTA_DEBUG int luaStackPointer = lua_gettop ( pState ); #endif // Store the current values of the globals lua_getglobal ( pState, "source" ); CLuaArgument OldSource ( pState, -1 ); lua_pop( pState, 1 ); lua_getglobal ( pState, "this" ); CLuaArgument OldThis ( pState, -1 ); lua_pop( pState, 1 ); lua_getglobal ( pState, "sourceResource" ); CLuaArgument OldResource ( pState, -1 ); lua_pop( pState, 1 ); lua_getglobal ( pState, "sourceResourceRoot" ); CLuaArgument OldResourceRoot ( pState, -1 ); lua_pop( pState, 1 ); lua_getglobal ( pState, "eventName" ); CLuaArgument OldEventName ( pState, -1 ); lua_pop( pState, 1 ); // Set the "source", "this", "sourceResource" and the "sourceResourceRoot" globals on that VM lua_pushelement ( pState, pSource ); lua_setglobal ( pState, "source" ); lua_pushelement ( pState, pThis ); lua_setglobal ( pState, "this" ); lua_pushresource ( pState, pMapEvent->GetVM()->GetResource() ); lua_setglobal ( pState, "sourceResource" ); lua_pushelement ( pState, pMapEvent->GetVM()->GetResource()->GetResourceDynamicEntity() ); lua_setglobal ( pState, "sourceResourceRoot" ); lua_pushstring ( pState, szName ); lua_setglobal ( pState, "eventName" ); // Call it pMapEvent->Call ( Arguments ); bCalled = true; // Reset the globals on that VM OldSource.Push ( pState ); lua_setglobal ( pState, "source" ); OldThis.Push ( pState ); lua_setglobal ( pState, "this" ); OldResource.Push ( pState ); lua_setglobal ( pState, "sourceResource" ); OldResourceRoot.Push ( pState ); lua_setglobal ( pState, "sourceResourceRoot" ); OldEventName.Push ( pState ); lua_setglobal ( pState, "eventName" ); #if MTA_DEBUG assert ( lua_gettop ( pState ) == luaStackPointer ); #endif } } } } // Clean out the trash if we're no longer calling events. if ( !bIsAlreadyIterating )\ { TakeOutTheTrash (); // We're no longer iterating the list m_bIteratingList = false; } // Return whether we called atleast one func or not return bCalled; }
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; }
bool CMapEventManager::Call ( const char* szName, const CLuaArguments& Arguments, class CClientEntity* pSource, class CClientEntity* pThis ) { // Check if no events if ( !m_bHasEvents ) return false; // Check if no events with a name match EventsIterPair itPair = m_EventsMap.equal_range ( szName ); if ( itPair.first == itPair.second ) return false; TIMEUS startTimeCall = GetTimeUs (); SString strStatus; // Check for multi-threading slipups assert ( IsMainThread () ); // Call all the events with matching names bool bCalled = false; bool bIsAlreadyIterating = m_bIteratingList; m_bIteratingList = true; // Copy the results into a array in case m_EventsMap is modified during the call std::vector< CMapEvent* > matchingEvents; for ( EventsIter iter = itPair.first ; iter != itPair.second ; ++iter ) matchingEvents.push_back(iter->second); for ( std::vector< CMapEvent* >::iterator iter = matchingEvents.begin() ; iter != matchingEvents.end() ; ++iter ) { CMapEvent* pMapEvent = *iter; // If it's not being destroyed if ( !pMapEvent->IsBeingDestroyed () ) { // Compare the names dassert ( strcmp ( pMapEvent->GetName (), szName ) == 0 ); { // Call if propagated? if ( pSource == pThis || pMapEvent->IsPropagated () ) { // Grab the current VM lua_State* pState = pMapEvent->GetVM ()->GetVM (); LUA_CHECKSTACK ( pState, 1 ); // Ensure some room #if MTA_DEBUG int luaStackPointer = lua_gettop ( pState ); #endif TIMEUS startTime = GetTimeUs(); // Aspect ratio adjustment bodges if ( pMapEvent->ShouldAllowAspectRatioAdjustment() ) { g_bAllowAspectRatioAdjustment = true; if ( pMapEvent->ShouldForceAspectRatioAdjustment() ) g_pCore->GetGraphics()->SetAspectRatioAdjustmentEnabled( true ); } // Record event for the crash dump writer static bool bEnabled = ( g_pCore->GetDiagnosticDebug () == EDiagnosticDebug::LUA_TRACE_0000 ); if ( bEnabled ) g_pCore->LogEvent ( 0, "Lua Event", pMapEvent->GetVM ()->GetScriptName (), szName ); // Store the current values of the globals lua_getglobal ( pState, "source" ); CLuaArgument OldSource ( pState, -1 ); lua_pop( pState, 1 ); lua_getglobal ( pState, "this" ); CLuaArgument OldThis ( pState, -1 ); lua_pop( pState, 1 ); lua_getglobal ( pState, "sourceResource" ); CLuaArgument OldResource ( pState, -1 ); lua_pop( pState, 1 ); lua_getglobal ( pState, "sourceResourceRoot" ); CLuaArgument OldResourceRoot ( pState, -1 ); lua_pop( pState, 1 ); lua_getglobal ( pState, "eventName" ); CLuaArgument OldEventName ( pState, -1 ); lua_pop( pState, 1 ); // Set the "source", "this", "sourceResource" and the "sourceResourceRoot" globals on that VM lua_pushelement ( pState, pSource ); lua_setglobal ( pState, "source" ); lua_pushelement ( pState, pThis ); lua_setglobal ( pState, "this" ); CLuaMain* pLuaMain = g_pClientGame->GetScriptDebugging()->GetTopLuaMain(); CResource* pSourceResource = pLuaMain ? pLuaMain->GetResource() : NULL; if ( pSourceResource ) { lua_pushresource ( pState, pSourceResource ); lua_setglobal ( pState, "sourceResource" ); lua_pushelement ( pState, pSourceResource->GetResourceDynamicEntity() ); lua_setglobal ( pState, "sourceResourceRoot" ); } else { lua_pushnil ( pState ); lua_setglobal ( pState, "sourceResource" ); lua_pushnil ( pState ); lua_setglobal ( pState, "sourceResourceRoot" ); } lua_pushstring ( pState, szName ); lua_setglobal ( pState, "eventName" ); // Call it pMapEvent->Call ( Arguments ); bCalled = true; // Reset the globals on that VM OldSource.Push ( pState ); lua_setglobal ( pState, "source" ); OldThis.Push ( pState ); lua_setglobal ( pState, "this" ); OldResource.Push ( pState ); lua_setglobal ( pState, "sourceResource" ); OldResourceRoot.Push ( pState ); lua_setglobal ( pState, "sourceResourceRoot" ); OldEventName.Push ( pState ); lua_setglobal ( pState, "eventName" ); #if MTA_DEBUG assert ( lua_gettop ( pState ) == luaStackPointer ); #endif // Aspect ratio adjustment bodges if ( pMapEvent->ShouldAllowAspectRatioAdjustment() ) { g_pCore->GetGraphics()->SetAspectRatioAdjustmentEnabled( false ); g_bAllowAspectRatioAdjustment = false; } TIMEUS deltaTimeUs = GetTimeUs() - startTime; if ( deltaTimeUs > 3000 ) if ( IS_TIMING_CHECKPOINTS() ) strStatus += SString ( " (%s %d ms)", pMapEvent->GetVM ()->GetScriptName (), deltaTimeUs / 1000 ); CClientPerfStatLuaTiming::GetSingleton ()->UpdateLuaTiming ( pMapEvent->GetVM (), szName, deltaTimeUs ); } } } } // Clean out the trash if we're no longer calling events. if ( !bIsAlreadyIterating ) { TakeOutTheTrash (); // We're no longer iterating the list m_bIteratingList = false; } if ( IS_TIMING_CHECKPOINTS() ) { TIMEUS deltaTimeUs = GetTimeUs() - startTimeCall; if ( deltaTimeUs > 5000 ) TIMING_DETAIL( SString ( "CMapEventManager::Call ( %s, ... ) took %d ms ( %s )", szName, deltaTimeUs / 1000, *strStatus ) ); } // Return whether we called atleast one func or not return bCalled; }
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; }
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 ); }
bool CClientTask::ReadParameters ( lua_State* luaVM, int iTableIndex, bool bClear ) { // Clear the old parameters first if ( bClear ) { m_Keys.clear (); m_Values.clear (); } // Grab the stack position for the table after we've pushed data to it. // This is table index - 1 if the index is negative, otherwize it stays // the same. int iNewTableIndex; if ( iTableIndex < 0 ) iNewTableIndex = iTableIndex - 1; else iNewTableIndex = iTableIndex; // Not a table? Bad if ( !lua_istable ( luaVM, iTableIndex ) ) { return false; } // Loop through our table, beginning at the first key lua_pushnil ( luaVM ); while ( lua_next ( luaVM, iNewTableIndex ) != 0 ) { // Get the key and value const char* szKey = lua_tostring ( luaVM, -2 ); CLuaArgument Value; Value.Read ( luaVM, -1 ); // Got both a key and a value? if ( szKey ) { // If we cleared, just add them if ( bClear ) { // Store it m_Keys.push_back ( szKey ); m_Values.push_back ( Value ); } else { // Otherwize merge it in SetParameter ( szKey, Value ); } // Remove the value and keep the key for the next iteration lua_pop ( luaVM, 1 ); } else { // Remove the value and keep the key for the next iteration lua_pop ( luaVM, 1 ); return false; } } // Success return true; }