static void callback(struct timer_list* timer, void *data) { int res; lua_State *L = (lua_State *)data; lua_pushtimer(L, timer); if (!lua_istable(L, -1)) { syslog(LOG_ERR, "CRON: system error: unable to get cron timer\n"); return; } unregister_timer(L, timer); lua_pushstring(L, "callback"); lua_gettable(L, -2); luaL_checktype(L, -1, LUA_TFUNCTION); lua_pushvalue(L, -2); res = lua_pcall(L, 1, 0, 0); if (res) { char const *name = "?"; char const *err = luaL_checkstring(L, -1); lua_pushstring(L, "name"); lua_gettable(L, -2); if (!lua_isnil(L, -1)) name = lua_tostring(L, -1); syslog(LOG_NOTICE, "CRON: timer [%s] callback error: %s\n", name, err); lua_pop(L, 2); } lua_poptimer(L); }
int CLuaTimerDefs::GetTimers ( lua_State* luaVM ) { // table getTimers ( [ time ] ) double dTime; CScriptArgReader argStream ( luaVM ); argStream.ReadNumber ( dTime, 0 ); if ( !argStream.HasErrors () ) { // Find our VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { // Create a new table lua_newtable ( luaVM ); // Add all the timers with less than ulTime left CLuaTimerManager* pLuaTimerManager = pLuaMain->GetTimerManager (); CTickCount llCurrentTime = CTickCount::Now (); unsigned int uiIndex = 0; CFastList < CLuaTimer* > ::const_iterator iter = pLuaTimerManager->IterBegin (); for ( ; iter != pLuaTimerManager->IterEnd (); iter++ ) { CLuaTimer* pLuaTimer = *iter; // If the time left is less than the time specified, or the time specifed is 0 CTickCount llTimeLeft = ( pLuaTimer->GetStartTime () + pLuaTimer->GetDelay () ) - llCurrentTime; if ( dTime == 0 || llTimeLeft.ToDouble () <= dTime ) { // Add it to the table lua_pushnumber ( luaVM, ++uiIndex ); lua_pushtimer ( luaVM, pLuaTimer ); lua_settable ( luaVM, -3 ); } } return 1; } } else 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; }
void lua_pushuserdata ( lua_State* luaVM, void* pData ) { if ( CClientEntity* pEntity = UserDataCast < CClientEntity > ( ( CClientEntity* ) NULL, pData, luaVM ) ) return lua_pushelement ( luaVM, pEntity ); else if ( CResource* pResource = UserDataCast < CResource > ( ( CResource* ) NULL, pData, luaVM ) ) return lua_pushresource ( luaVM, pResource ); else if ( CXMLNode* pNode = UserDataCast < CXMLNode > ( ( CXMLNode* ) NULL, pData, luaVM ) ) return lua_pushxmlnode ( luaVM, pNode ); else if ( CLuaTimer* pTimer = UserDataCast < CLuaTimer > ( ( CLuaTimer* ) NULL, pData, luaVM ) ) return lua_pushtimer ( luaVM, pTimer ); else if ( CLuaVector2D* pVector = UserDataCast < CLuaVector2D > ( (CLuaVector2D*) NULL, pData, luaVM ) ) return lua_pushvector ( luaVM,* pVector ); else if ( CLuaVector3D* pVector = UserDataCast < CLuaVector3D > ( (CLuaVector3D*) NULL, pData, luaVM ) ) return lua_pushvector ( luaVM, *pVector ); else if ( CLuaVector4D* pVector = UserDataCast < CLuaVector4D > ( (CLuaVector4D*) NULL, pData, luaVM ) ) return lua_pushvector ( luaVM, *pVector ); else if ( CLuaMatrix* pMatrix = UserDataCast < CLuaMatrix > ( (CLuaMatrix*) NULL, pData, luaVM ) ) return lua_pushmatrix ( luaVM, *pMatrix ); lua_pushobject ( luaVM, NULL, pData ); }
void CLuaTimerManager::GetTimers ( CTickCount llTime, lua_State* luaVM ) { assert ( luaVM ); CTickCount llCurrentTime = CTickCount::Now (); // Add all the timers to the table unsigned int uiIndex = 0; list < CLuaTimer* > ::const_iterator iter = m_TimerList.begin (); for ( ; iter != m_TimerList.end () ; iter++ ) { // If the time left is less than the time specified, or the time specifed is 0 CTickCount llTimeLeft = ( (*iter)->GetStartTime () + (*iter)->GetDelay () ) - llCurrentTime; if ( llTime.ToLongLong () == 0 || llTimeLeft <= llTime ) { // Add it to the table lua_pushnumber ( luaVM, ++uiIndex ); lua_pushtimer ( luaVM, *iter ); lua_settable ( luaVM, -3 ); } } }
void CLuaTimerManager::GetTimers ( unsigned long ulTime, CLuaMain* pLuaMain ) { assert ( pLuaMain ); unsigned long ulCurrentTime = GetTime(); // Add all the timers to the table unsigned int uiIndex = 0; list < CLuaTimer* > ::iterator iter = m_TimerList.begin (); for ( ; iter != m_TimerList.end () ; iter++ ) { // If the time left is less than the time specified, or the time specifed is 0 unsigned long ulTimeLeft = ( (*iter)->GetStartTime () + (*iter)->GetDelay () ) - ulCurrentTime; if ( ulTime == 0 || ulTimeLeft <= ulTime ) { // Add it to the table lua_State* luaVM = pLuaMain->GetVirtualMachine (); lua_pushnumber ( luaVM, ++uiIndex ); lua_pushtimer ( luaVM, *iter ); lua_settable ( luaVM, -3 ); } } }
void lua_pushuserdata ( lua_State* luaVM, void* pData ) { if ( CElement* pEntity = UserDataCast < CElement > ( ( CElement* ) NULL, pData, NULL ) ) return lua_pushelement ( luaVM, pEntity ); if ( CPlayer* pEntity = UserDataCast < CPlayer > ( ( CPlayer* ) NULL, pData, NULL ) ) return lua_pushelement ( luaVM, pEntity ); else if ( CResource* pResource = UserDataCast < CResource > ( ( CResource* ) NULL, pData, NULL ) ) return lua_pushresource ( luaVM, pResource ); else if ( CXMLNode* pNode = UserDataCast < CXMLNode > ( ( CXMLNode* ) NULL, pData, NULL ) ) return lua_pushxmlnode ( luaVM, pNode ); else if ( CLuaTimer* pTimer = UserDataCast < CLuaTimer > ( ( CLuaTimer* ) NULL, pData, luaVM ) ) return lua_pushtimer ( luaVM, pTimer ); else if ( CLuaVector2D* pVector = UserDataCast < CLuaVector2D > ( (CLuaVector2D*) NULL, pData, luaVM ) ) return lua_pushvector ( luaVM, *pVector ); else if ( CLuaVector3D* pVector = UserDataCast < CLuaVector3D > ( (CLuaVector3D*) NULL, pData, luaVM ) ) return lua_pushvector ( luaVM, *pVector ); else if ( CLuaVector4D* pVector = UserDataCast < CLuaVector4D > ( (CLuaVector4D*) NULL, pData, luaVM ) ) return lua_pushvector ( luaVM, *pVector ); else if ( CLuaMatrix* pMatrix = UserDataCast < CLuaMatrix > ( (CLuaMatrix*) NULL, pData, luaVM ) ) return lua_pushmatrix ( luaVM, *pMatrix ); else if ( CAccount* pAccount = UserDataCast < CAccount > ( (CAccount*) NULL, pData, luaVM ) ) return lua_pushaccount ( luaVM, pAccount ); else if ( CAccessControlList* pACL = UserDataCast < CAccessControlList > ( (CAccessControlList*) NULL, pData, luaVM ) ) return lua_pushacl ( luaVM, pACL ); else if ( CAccessControlListGroup* pACLGroup = UserDataCast < CAccessControlListGroup > ( (CAccessControlListGroup*) NULL, pData, luaVM ) ) return lua_pushaclgroup ( luaVM, pACLGroup ); else if ( CBan* pBan = UserDataCast < CBan > ( (CBan*) NULL, pData, luaVM ) ) return lua_pushban ( luaVM, pBan ); else if ( CTextDisplay* pTextDisplay = UserDataCast < CTextDisplay > ( (CTextDisplay*) NULL, pData, luaVM ) ) return lua_pushtextdisplay ( luaVM, pTextDisplay ); else if ( CTextItem* pTextItem = UserDataCast < CTextItem > ( (CTextItem*) NULL, pData, luaVM ) ) return lua_pushtextitem ( luaVM, pTextItem ); else if ( CDbJobData* pQuery = UserDataCast < CDbJobData > ( (CDbJobData*) NULL, pData, luaVM ) ) return lua_pushquery ( luaVM, pQuery ); lua_pushobject ( luaVM, NULL, pData ); }