int CLuaFunctionDefs::SetWeaponTarget ( lua_State* luaVM ) { CClientWeapon * pWeapon; CClientEntity * pTarget; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pWeapon ); if ( argStream.NextIsUserData() ) { int targetBone; argStream.ReadUserData ( pTarget ); argStream.ReadNumber ( targetBone, 255 ); if ( !argStream.HasErrors () ) { if ( CStaticFunctionDefinitions::SetWeaponTarget ( pWeapon, pTarget, targetBone ) ) { lua_pushboolean ( luaVM, true ); return 1; } } } else if ( argStream.NextIsNumber() || argStream.NextIsUserDataOfType < CLuaVector3D > () ) { CVector vecTarget; argStream.ReadVector3D ( vecTarget ); if ( !argStream.HasErrors () ) { if ( CStaticFunctionDefinitions::SetWeaponTarget ( pWeapon, vecTarget ) ) { lua_pushboolean ( luaVM, true ); return 1; } } } else if ( argStream.NextIsNil() ) { if ( !argStream.HasErrors () ) { if ( CStaticFunctionDefinitions::ClearWeaponTarget ( pWeapon ) ) { lua_pushboolean ( luaVM, true ); return 1; } } } else argStream.SetCustomError( "Expected element, number or nil at argument 2" ); if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFunctionDefs::Split ( lua_State* luaVM ) { SString strInput = ""; unsigned int uiDelimiter = 0; SString strDelimiter; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strInput ); if ( argStream.NextIsNumber () ) { argStream.ReadNumber ( uiDelimiter ); wchar_t wUNICODE[2] = { uiDelimiter, '\0' }; strDelimiter = UTF16ToMbUTF8 ( wUNICODE ); } else // It's already a string argStream.ReadString ( strDelimiter ); if ( !argStream.HasErrors () ) { // Copy the string char* strText = new char[strInput.length () + 1]; strcpy ( strText, strInput ); unsigned int uiCount = 0; char* szToken = strtok ( strText, strDelimiter ); // Create a new table lua_newtable ( luaVM ); // Add our first token lua_pushnumber ( luaVM, ++uiCount ); lua_pushstring ( luaVM, szToken ); lua_settable ( luaVM, -3 ); // strtok until we're out of tokens while ( ( szToken = strtok ( NULL, strDelimiter ) ) ) { // Add the token to the table lua_pushnumber ( luaVM, ++uiCount ); lua_pushstring ( luaVM, szToken ); lua_settable ( luaVM, -3 ); } // Delete the text delete[] strText; } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); return 1; }
// Call a function on a remote server int CLuaFunctionDefs::FetchRemote ( lua_State* luaVM ) { // bool fetchRemote ( string URL [, string queueName ][, int connectionAttempts = 10, int connectTimeout = 10000 ], callback callbackFunction, [ string postData, bool bPostBinary, arguments... ] ) CScriptArgReader argStream ( luaVM ); SString strURL; SString strQueueName; CLuaFunctionRef iLuaFunction; SString strPostData; bool bPostBinary; CLuaArguments args; uint uiConnectionAttempts; uint uiConnectTimeoutMs; argStream.ReadString ( strURL ); if ( argStream.NextIsString () ) MinServerReqCheck ( argStream, MIN_SERVER_REQ_CALLREMOTE_QUEUE_NAME, "'queue name' is being used" ); argStream.ReadIfNextIsString ( strQueueName, CALL_REMOTE_DEFAULT_QUEUE_NAME ); if ( argStream.NextIsNumber () ) MinServerReqCheck ( argStream, MIN_SERVER_REQ_CALLREMOTE_CONNECTION_ATTEMPTS, "'connection attempts' is being used" ); argStream.ReadIfNextIsNumber ( uiConnectionAttempts, 10 ); if ( argStream.NextIsNumber () ) MinServerReqCheck ( argStream, MIN_SERVER_REQ_CALLREMOTE_CONNECT_TIMEOUT, "'connect timeout' is being used" ); argStream.ReadIfNextIsNumber ( uiConnectTimeoutMs, 10000 ); argStream.ReadFunction ( iLuaFunction ); argStream.ReadString ( strPostData, "" ); argStream.ReadBool ( bPostBinary, false ); argStream.ReadLuaArguments ( args ); argStream.ReadFunctionComplete (); if ( !argStream.HasErrors () ) { CLuaMain * luaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( luaMain ) { g_pGame->GetRemoteCalls ()->Call ( strURL, &args, strPostData, bPostBinary, luaMain, iLuaFunction, strQueueName, uiConnectionAttempts, uiConnectTimeoutMs ); lua_pushboolean ( luaVM, true ); return 1; } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFunctionDefs::OOP_OutputChatBox ( lua_State * luaVM ) { // bool Player:outputChat ( string text [, int r=231, int g=217, int b=176, bool colorCoded=false ] ) CElement* pElement; SString strText; uchar ucRed = 231; uchar ucGreen = 217; uchar ucBlue = 176; bool bColorCoded; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pElement ); argStream.ReadString ( strText ); if ( argStream.NextIsNumber ( 0 ) && argStream.NextIsNumber ( 1 ) && argStream.NextIsNumber ( 2 ) ) { argStream.ReadNumber ( ucRed ); argStream.ReadNumber ( ucGreen ); argStream.ReadNumber ( ucBlue ); } else argStream.Skip ( 3 ); argStream.ReadBool ( bColorCoded, false ); if ( !argStream.HasErrors () ) { CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { CStaticFunctionDefinitions::OutputChatBox ( strText, pElement, ucRed, ucGreen, ucBlue, bColorCoded, pLuaMain ); lua_pushboolean ( luaVM, true ); return 1; } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaVector4Defs::Pow ( lua_State* luaVM ) { CLuaVector4D* pVector1 = NULL; CLuaVector4D* pVector2 = NULL; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pVector1 ); if ( argStream.NextIsNumber () ) { float fValue = 0.0f; argStream.ReadNumber ( fValue ); CVector4D vector ( *pVector1 ); vector.fX = pow ( vector.fX, fValue ); vector.fY = pow ( vector.fY, fValue ); vector.fZ = pow ( vector.fZ, fValue ); vector.fW = pow ( vector.fW, fValue ); lua_pushvector ( luaVM, vector ); return 1; } else { argStream.ReadUserData ( pVector2 ); if ( !argStream.HasErrors () ) { CVector4D vector ( *pVector1 ); vector.fX = pow ( vector.fX, pVector2->fX ); vector.fY = pow ( vector.fY, pVector2->fY ); vector.fZ = pow ( vector.fZ, pVector2->fZ ); vector.fW = pow ( vector.fW, pVector2->fW ); lua_pushvector ( luaVM, vector ); return 1; } else { m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() ); } } lua_pushboolean ( luaVM, false ); return 1; }
// // Read next as preg option flags // void ReadPregFlags( CScriptArgReader& argStream, pcrecpp::RE_Options& pOptions ) { if ( argStream.NextIsNumber() ) { uint uiFlags = 0; argStream.ReadNumber ( uiFlags ); pOptions.set_caseless ( ( uiFlags & 1 ) != 0 ); pOptions.set_multiline ( ( uiFlags & 2 ) != 0 ); pOptions.set_dotall ( ( uiFlags & 4 ) != 0 ); pOptions.set_extended ( ( uiFlags & 8 ) != 0 ); pOptions.set_utf8 ( ( uiFlags & 16 ) != 0 ); } else if ( argStream.NextIsString() ) { SString strFlags; argStream.ReadString ( strFlags ); for( uint i = 0 ; i < strFlags.length() ; i++ ) { switch ( strFlags[i] ) { case 'i': pOptions.set_caseless ( true ); break; case 'm': pOptions.set_multiline ( true ); break; case 'd': pOptions.set_dotall ( true ); break; case 'e': pOptions.set_extended ( true ); break; case 'u': pOptions.set_utf8 ( true ); break; default: argStream.SetCustomError( "Flags all wrong" ); return; } } } }
// Call a function on a remote server int CLuaFunctionDefs::CallRemote ( lua_State* luaVM ) { CScriptArgReader argStream ( luaVM ); if ( !argStream.NextIsFunction ( 1 ) && !argStream.NextIsFunction ( 2 ) ) { // Call type 1 // bool callRemote ( string host [, int connectionAttempts = 10, int connectTimeout = 10000 ], string resourceName, string functionName, callback callbackFunction, [ arguments... ] ) SString strHost; uint uiConnectionAttempts; uint uiConnectTimeoutMs; SString strResourceName; SString strFunctionName; CLuaFunctionRef iLuaFunction; CLuaArguments args; argStream.ReadString ( strHost ); if ( argStream.NextIsNumber () ) MinServerReqCheck ( argStream, MIN_SERVER_REQ_CALLREMOTE_CONNECTION_ATTEMPTS, "'connection attempts' is being used" ); argStream.ReadIfNextIsNumber ( uiConnectionAttempts, 10 ); if ( argStream.NextIsNumber () ) MinServerReqCheck ( argStream, MIN_SERVER_REQ_CALLREMOTE_CONNECT_TIMEOUT, "'connect timeout' is being used" ); argStream.ReadIfNextIsNumber ( uiConnectTimeoutMs, 10000 ); argStream.ReadString ( strResourceName ); argStream.ReadString ( strFunctionName ); argStream.ReadFunction ( iLuaFunction ); argStream.ReadLuaArguments ( args ); argStream.ReadFunctionComplete (); if ( !argStream.HasErrors () ) { CLuaMain * luaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( luaMain ) { g_pGame->GetRemoteCalls ()->Call ( strHost, strResourceName, strFunctionName, &args, luaMain, iLuaFunction, uiConnectionAttempts, uiConnectTimeoutMs ); lua_pushboolean ( luaVM, true ); return 1; } } } else { // Call type 2 // bool callRemote ( string URL [, int connectionAttempts = 10, int connectTimeout = 10000 ], callback callbackFunction, [ arguments... ] ) SString strURL; uint uiConnectionAttempts; uint uiConnectTimeoutMs; CLuaFunctionRef iLuaFunction; CLuaArguments args; argStream.ReadString ( strURL ); if ( argStream.NextIsNumber () ) MinServerReqCheck ( argStream, MIN_SERVER_REQ_CALLREMOTE_CONNECTION_ATTEMPTS, "'connection attempts' is being used" ); argStream.ReadIfNextIsNumber ( uiConnectionAttempts, 10 ); if ( argStream.NextIsNumber () ) MinServerReqCheck ( argStream, MIN_SERVER_REQ_CALLREMOTE_CONNECT_TIMEOUT, "'connect timeout' is being used" ); argStream.ReadIfNextIsNumber ( uiConnectTimeoutMs, 10000 ); argStream.ReadFunction ( iLuaFunction ); argStream.ReadLuaArguments ( args ); argStream.ReadFunctionComplete (); if ( !argStream.HasErrors () ) { CLuaMain * luaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( luaMain ) { g_pGame->GetRemoteCalls ()->Call ( strURL, &args, luaMain, iLuaFunction, uiConnectionAttempts, uiConnectTimeoutMs ); lua_pushboolean ( luaVM, true ); return 1; } } } if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaVector4Defs::Create ( lua_State* luaVM ) { CVector4D vector; CScriptArgReader argStream ( luaVM ); if ( argStream.NextIsTable () ) { lua_pushvalue ( luaVM, 1 ); lua_pushstring ( luaVM, "x" ); lua_rawget ( luaVM, -2 ); if ( lua_isnumber ( luaVM, -1 ) ) { vector.fX = ( float ) lua_tonumber ( luaVM, -1 ); lua_pop ( luaVM, 1 ); } else { lua_pop ( luaVM, 1 ); lua_rawgeti ( luaVM, -1, 1 ); if ( lua_isnumber ( luaVM, -1 ) ) vector.fX = ( float ) lua_tonumber ( luaVM, -1 ); lua_pop ( luaVM, 1 ); } lua_pushstring ( luaVM, "y" ); lua_rawget ( luaVM, -2 ); if ( lua_isnumber ( luaVM, -1 ) ) { vector.fY = ( float ) lua_tonumber ( luaVM, -1 ); lua_pop ( luaVM, 1 ); } else { lua_pop ( luaVM, 1 ); lua_rawgeti ( luaVM, -1, 2 ); if ( lua_isnumber ( luaVM, -1 ) ) vector.fY = ( float ) lua_tonumber ( luaVM, -1 ); lua_pop ( luaVM, 1 ); } lua_pushstring ( luaVM, "z" ); lua_rawget ( luaVM, -2 ); if ( lua_isnumber ( luaVM, -1 ) ) { vector.fZ = ( float ) lua_tonumber ( luaVM, -1 ); lua_pop ( luaVM, 1 ); } else { lua_pop ( luaVM, 1 ); lua_rawgeti ( luaVM, -1, 3 ); if ( lua_isnumber ( luaVM, -1 ) ) vector.fZ = ( float ) lua_tonumber ( luaVM, -1 ); lua_pop ( luaVM, 1 ); } lua_pushstring ( luaVM, "w" ); lua_rawget ( luaVM, -2 ); if ( lua_isnumber ( luaVM, -1 ) ) { vector.fW = ( float ) lua_tonumber ( luaVM, -1 ); lua_pop ( luaVM, 1 ); } else { lua_pop ( luaVM, 1 ); lua_rawgeti ( luaVM, -1, 3 ); if ( lua_isnumber ( luaVM, -1 ) ) vector.fW = ( float ) lua_tonumber ( luaVM, -1 ); lua_pop ( luaVM, 1 ); } } else if ( argStream.NextIsNumber () ) { argStream.ReadNumber ( vector.fX ); if ( argStream.NextIsNumber () ) { argStream.ReadNumber ( vector.fY ); if ( argStream.NextIsNumber () ) argStream.ReadNumber ( vector.fZ ); if ( argStream.NextIsNumber () ) argStream.ReadNumber ( vector.fW ); } } lua_pushvector ( luaVM, vector ); return 1; }
int CLuaWorldDefs::setTrafficLightState ( lua_State* luaVM ) { // bool setTrafficLightState ( int state ) // bool setTrafficLightState ( string state ) // bool setTrafficLightState ( string colorNS, string colorEW ) CScriptArgReader argStream ( luaVM ); // Determine which version to parse if ( argStream.NextIsNumber () ) { // bool setTrafficLightState ( int state ) int iState; argStream.ReadNumber ( iState ); if ( !argStream.HasErrors () ) { if ( CStaticFunctionDefinitions::SetTrafficLightState ( iState ) ) { lua_pushboolean ( luaVM, true ); return 1; } } } else if ( !argStream.NextIsString ( 1 ) ) { // bool setTrafficLightState ( string state ) TrafficLight::EState eState; argStream.ReadEnumString ( eState ); if ( !argStream.HasErrors () ) { if ( eState == TrafficLight::AUTO ) { bool bOk = CStaticFunctionDefinitions::SetTrafficLightsLocked ( false ) && CStaticFunctionDefinitions::SetTrafficLightState ( 0 ); lua_pushboolean ( luaVM, bOk ); return 1; } else { bool bOk = CStaticFunctionDefinitions::SetTrafficLightsLocked ( true ) && CStaticFunctionDefinitions::SetTrafficLightState ( 9 ); lua_pushboolean ( luaVM, bOk ); return 1; } } } else { // bool setTrafficLightState ( string colorNS, string colorEW ) TrafficLight::EColor eColorNS; TrafficLight::EColor eColorEW; argStream.ReadEnumString ( eColorNS ); argStream.ReadEnumString ( eColorEW ); if ( !argStream.HasErrors () ) { unsigned char ucState = SharedUtil::GetTrafficLightStateFromColors ( eColorNS, eColorEW ); // Change it. bool bOk = CStaticFunctionDefinitions::SetTrafficLightsLocked ( true ) && CStaticFunctionDefinitions::SetTrafficLightState ( ucState ); lua_pushboolean ( luaVM, bOk ); return 1; } } if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFunctionDefs::GetTok ( lua_State* luaVM ) { SString strInput = ""; unsigned int uiToken = 0; unsigned int uiDelimiter = 0; SString strDelimiter; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strInput ); argStream.ReadNumber ( uiToken ); if ( argStream.NextIsNumber ( ) ) { argStream.ReadNumber ( uiDelimiter ); wchar_t wUNICODE[2] = { uiDelimiter, '\0' }; strDelimiter = UTF16ToMbUTF8(wUNICODE); } else // It's already a string argStream.ReadString ( strDelimiter ); if ( !argStream.HasErrors ( ) ) { unsigned int uiCount = 0; if ( uiToken > 0 && uiToken < 1024 ) { unsigned int uiCount = 1; char* szText = new char [ strInput.length ( ) + 1 ]; strcpy ( szText, strInput ); char* szToken = strtok ( szText, strDelimiter ); // We're looking for the first part? if ( uiToken != 1 ) { // strtok count number of times do { uiCount++; szToken = strtok ( NULL, strDelimiter ); } while ( uiCount != uiToken ); } // Found it? if ( szToken ) { // Return it lua_pushstring ( luaVM, szToken ); delete [] szText; return 1; } // Delete the text delete [] szText; } else m_pScriptDebugging->LogWarning ( luaVM, "Token parameter sent to split must be greater than 0 and smaller than 1024" ); } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() ); lua_pushboolean ( luaVM, false ); return 1; }