// Call a function on a remote server int CLuaFunctionDefs::FetchRemote ( lua_State* luaVM ) { // bool fetchRemote ( string URL [, int connectionAttempts = 10, int connectTimeout = 10000 ], callback callbackFunction, [ string postData, bool bPostBinary, arguments... ] ) CScriptArgReader argStream ( luaVM ); SString strURL; CLuaFunctionRef iLuaFunction; SString strPostData; bool bPostBinary; CLuaArguments args; uint uiConnectionAttempts; uint uiConnectTimeoutMs; 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.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, uiConnectionAttempts, uiConnectTimeoutMs ); lua_pushboolean ( luaVM, true ); return 1; } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFunctionDefs::Set ( lua_State* luaVM ) { CResource* pResource = m_pLuaManager->GetVirtualMachine ( luaVM )->GetResource (); SString strSetting; CLuaArguments Args; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strSetting ); argStream.ReadLuaArguments ( Args ); if ( !argStream.HasErrors () ) { std::string strResourceName = pResource->GetName (); std::string strJSON; Args.WriteToJSONString ( strJSON ); if ( g_pGame->GetSettings ()->Set ( strResourceName.c_str (), strSetting.c_str (), strJSON.c_str () ) ) { lua_pushboolean ( luaVM, true ); return 1; } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaDatabaseDefs::DbPrepareString ( lua_State* luaVM ) { // string dbPrepareString ( element connection, string query, ... ) CDatabaseConnectionElement* pElement; SString strQuery; CLuaArguments Args; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pElement ); argStream.ReadString ( strQuery ); argStream.ReadLuaArguments ( Args ); if ( !argStream.HasErrors () ) { SString strResult = g_pGame->GetDatabaseManager ()->PrepareString ( pElement->GetConnectionHandle (), strQuery, &Args ); SString strError = g_pGame->GetDatabaseManager ()->GetLastErrorMessage (); if ( !strResult.empty () || strError.empty () ) { lua_pushstring ( luaVM, strResult ); return 1; } if ( !g_pGame->GetDatabaseManager ()->IsLastErrorSuppressed () ) argStream.SetCustomError ( strError ); } if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaDatabaseDefs::DbExec ( lua_State* luaVM ) { // bool dbExec ( element connection, string query, ... ) CDatabaseConnectionElement* pElement; SString strQuery; CLuaArguments Args; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pElement ); argStream.ReadString ( strQuery ); argStream.ReadLuaArguments ( Args ); if ( !argStream.HasErrors () ) { // Start async query CDbJobData* pJobData = g_pGame->GetDatabaseManager ()->Exec ( pElement->GetConnectionHandle (), strQuery, &Args ); if ( !pJobData ) { if ( !g_pGame->GetDatabaseManager ()->IsLastErrorSuppressed () ) m_pScriptDebugging->LogError ( luaVM, "%s failed: %s", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ), *g_pGame->GetDatabaseManager ()->GetLastErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; } // Add callback for tracking errors pJobData->SetCallback ( DbExecCallback, NULL ); pJobData->SetLuaDebugInfo ( g_pGame->GetScriptDebugging ()->GetLuaDebugInfo ( luaVM ) ); lua_pushboolean ( luaVM, true ); return 1; } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFunctionDefs::TriggerServerEvent ( lua_State* luaVM ) { // bool triggerServerEvent ( string event, element theElement, [arguments...] ) SString strName; CClientEntity* pCallWithEntity; CLuaArguments Arguments; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strName ); argStream.ReadUserData ( pCallWithEntity ); argStream.ReadLuaArguments ( Arguments ); if ( !argStream.HasErrors () ) { // Trigger it if ( CStaticFunctionDefinitions::TriggerServerEvent ( strName, *pCallWithEntity, Arguments ) ) { lua_pushboolean ( luaVM, true ); return 1; } } else m_pScriptDebugging->LogCustom ( luaVM, SString ( "Bad argument @ '%s' [%s]", "triggerServerEvent", *argStream.GetErrorMessage () ) ); // Failed lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFunctionDefs::TriggerEvent ( lua_State* luaVM ) { // bool triggerEvent ( string eventName, element baseElement, [ var argument1, ... ] ) SString strName; CClientEntity* pEntity; CLuaArguments Arguments; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strName ); argStream.ReadUserData ( pEntity ); argStream.ReadLuaArguments ( Arguments ); if ( !argStream.HasErrors () ) { // Trigger it bool bWasCancelled; if ( CStaticFunctionDefinitions::TriggerEvent ( strName, *pEntity, Arguments, bWasCancelled ) ) { lua_pushboolean ( luaVM, !bWasCancelled ); return 1; } } else m_pScriptDebugging->LogCustom ( luaVM, SString ( "Bad argument @ '%s' [%s]", "triggerEvent", *argStream.GetErrorMessage () ) ); // Error lua_pushnil ( luaVM ); return 1; }
int CLuaDatabaseDefs::OOP_DbQuery ( lua_State* luaVM ) { // handle dbQuery ( [ function callbackFunction, [ table callbackArguments, ] ] element connection, string query, ... ) CLuaFunctionRef iLuaFunction; CLuaArguments callbackArgs; CDatabaseConnectionElement* pElement; SString strQuery; CLuaArguments Args; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pElement ); if ( argStream.NextIsFunction () ) { argStream.ReadFunction ( iLuaFunction ); if ( argStream.NextIsTable () ) { argStream.ReadLuaArgumentsTable( callbackArgs ); } } argStream.ReadString ( strQuery ); argStream.ReadLuaArguments ( Args ); argStream.ReadFunctionComplete (); if ( !argStream.HasErrors () ) { // Start async query CDbJobData* pJobData = g_pGame->GetDatabaseManager ()->QueryStart ( pElement->GetConnectionHandle (), strQuery, &Args ); if ( !pJobData ) { if ( !g_pGame->GetDatabaseManager ()->IsLastErrorSuppressed () ) m_pScriptDebugging->LogWarning ( luaVM, "%s failed; %s", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ), *g_pGame->GetDatabaseManager ()->GetLastErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; } // Make callback function if required if ( VERIFY_FUNCTION ( iLuaFunction ) ) { CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { CLuaArguments Arguments; Arguments.PushDbQuery ( pJobData ); Arguments.PushArguments ( callbackArgs ); pJobData->SetCallback ( CLuaDatabaseDefs::DbQueryCallback, g_pGame->GetLuaCallbackManager ()->CreateCallback ( pLuaMain, iLuaFunction, Arguments ) ); } } // Add debug info incase query result does not get collected pJobData->SetLuaDebugInfo ( g_pGame->GetScriptDebugging ()->GetLuaDebugInfo ( luaVM ) ); lua_pushquery ( luaVM, pJobData ); return 1; } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFunctionDefs::BindKey ( lua_State* luaVM ) { SString strKey = "", strHitState = "", strCommand = "", strArguments = ""; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strKey ); argStream.ReadString ( strHitState ); if ( !argStream.HasErrors ( ) ) { CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { if ( argStream.NextIsString ( ) ) { // bindKey ( string key, string keyState, string commandName, [ string arguments ] ) SString strResource = pLuaMain->GetResource()->GetName(); argStream.ReadString ( strCommand ); argStream.ReadString ( strArguments, "" ); if ( !argStream.HasErrors ( ) ) { if ( CStaticFunctionDefinitions::BindKey ( strKey, strHitState, strCommand, strArguments, strResource ) ) { lua_pushboolean ( luaVM, true ); return 1; } } } else { // bindKey ( string key, string keyState, function handlerFunction, [ var arguments, ... ] ) CLuaFunctionRef iLuaFunction; CLuaArguments Arguments; argStream.ReadFunction ( iLuaFunction ); argStream.ReadLuaArguments ( Arguments ); argStream.ReadFunctionComplete (); if ( !argStream.HasErrors ( ) ) { if ( CStaticFunctionDefinitions::BindKey ( strKey, strHitState, pLuaMain, iLuaFunction, Arguments ) ) { lua_pushboolean ( luaVM, true ); return 1; } } } } } if ( argStream.HasErrors ( ) ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaTimerDefs::SetTimer ( lua_State* luaVM ) { // timer setTimer ( function theFunction, int timeInterval, int timesToExecute, [ var arguments... ] ) CLuaFunctionRef iLuaFunction; double dTimeInterval; uint uiTimesToExecute; CLuaArguments Arguments; CScriptArgReader argStream ( luaVM ); argStream.ReadFunction ( iLuaFunction ); argStream.ReadNumber ( dTimeInterval ); argStream.ReadNumber ( uiTimesToExecute ); argStream.ReadLuaArguments ( Arguments ); argStream.ReadFunctionComplete (); if ( !argStream.HasErrors () ) { CLuaMain * luaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( luaMain ) { // Check for the minimum interval if ( dTimeInterval < LUA_TIMER_MIN_INTERVAL ) { argStream.SetCustomError ( "Interval is below 50" ); } else { CLuaTimer* pLuaTimer = luaMain->GetTimerManager ()->AddTimer ( iLuaFunction, CTickCount ( dTimeInterval ), uiTimesToExecute, Arguments ); if ( pLuaTimer ) { // Set our timer debug info (in case we don't have any debug info which is usually when you do setTimer(destroyElement, 50, 1) or such) pLuaTimer->SetLuaDebugInfo ( g_pClientGame->GetScriptDebugging ()->GetLuaDebugInfo ( luaVM ) ); lua_pushtimer ( luaVM, pLuaTimer ); return 1; } } } } if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
// 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; }