ds@1952: /** ds@1952: * vim: set ts=4 : ds@1952: * ============================================================================= ds@1952: * SourceMod ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. ds@1952: * ============================================================================= ds@1952: * ds@1952: * This program is free software; you can redistribute it and/or modify it under ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the ds@1952: * Free Software Foundation. ds@1952: * ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1952: * details. ds@1952: * ds@1952: * You should have received a copy of the GNU General Public License along with ds@1952: * this program. If not, see . ds@1952: * ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1952: * this exception to all derivative works. AlliedModders LLC defines further ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1952: * or . ds@1952: * ds@1952: * Version: $Id$ ds@1952: */ ds@1952: ds@1952: #include "HandleSys.h" ds@1952: #include "TimerSys.h" ds@1952: #include "PluginSys.h" ds@1952: #include "Logger.h" ds@1952: #include "DebugReporter.h" ds@1952: ds@1952: #define TIMER_HNDL_CLOSE (1<<9) ds@1952: ds@1952: HandleType_t g_TimerType; ds@1952: ds@1952: struct TimerInfo ds@1952: { ds@1952: ITimer *Timer; ds@1952: IPluginFunction *Hook; ds@1952: IPluginContext *pContext; ds@1952: Handle_t TimerHandle; ds@1952: int UserData; ds@1952: int Flags; ds@1952: }; ds@1952: ds@1952: class TimerNatives : ds@1952: public SMGlobalClass, ds@1952: public IHandleTypeDispatch, ds@1952: public ITimedEvent ds@1952: { ds@1952: public: ds@1952: ~TimerNatives(); ds@1952: public: //ITimedEvent ds@1952: ResultType OnTimer(ITimer *pTimer, void *pData); ds@1952: void OnTimerEnd(ITimer *pTimer, void *pData); ds@1952: public: //IHandleTypeDispatch ds@1952: void OnHandleDestroy(HandleType_t type, void *object); ds@1952: public: //SMGlobalClass ds@1952: void OnSourceModAllInitialized(); ds@1952: void OnSourceModShutdown(); ds@1952: public: ds@1952: TimerInfo *CreateTimerInfo(); ds@1952: void DeleteTimerInfo(TimerInfo *pInfo); ds@1952: private: ds@1952: CStack m_FreeTimers; ds@1952: }; ds@1952: ds@1952: TimerNatives::~TimerNatives() ds@1952: { ds@1952: CStack::iterator iter; ds@1952: for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++) ds@1952: { ds@1952: delete (*iter); ds@1952: } ds@1952: m_FreeTimers.popall(); ds@1952: } ds@1952: ds@1952: void TimerNatives::OnSourceModAllInitialized() ds@1952: { ds@1952: HandleAccess sec; ds@1952: ds@1952: g_HandleSys.InitAccessDefaults(NULL, &sec); ds@1952: sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY; ds@1952: ds@1952: g_TimerType = g_HandleSys.CreateType("Timer", this, 0, NULL, &sec, g_pCoreIdent, NULL); ds@1952: } ds@1952: ds@1952: void TimerNatives::OnSourceModShutdown() ds@1952: { ds@1952: g_HandleSys.RemoveType(g_TimerType, g_pCoreIdent); ds@1952: g_TimerType = 0; ds@1952: } ds@1952: ds@1952: void TimerNatives::OnHandleDestroy(HandleType_t type, void *object) ds@1952: { ds@1952: TimerInfo *pTimer = reinterpret_cast(object); ds@1952: ds@1952: g_Timers.KillTimer(pTimer->Timer); ds@1952: } ds@1952: ds@1952: TimerInfo *TimerNatives::CreateTimerInfo() ds@1952: { ds@1952: TimerInfo *pInfo; ds@1952: ds@1952: if (m_FreeTimers.empty()) ds@1952: { ds@1952: pInfo = new TimerInfo; ds@1952: } else { ds@1952: pInfo = m_FreeTimers.front(); ds@1952: m_FreeTimers.pop(); ds@1952: } ds@1952: ds@1952: return pInfo; ds@1952: } ds@1952: ds@1952: void TimerNatives::DeleteTimerInfo(TimerInfo *pInfo) ds@1952: { ds@1952: m_FreeTimers.push(pInfo); ds@1952: } ds@1952: ds@1952: ResultType TimerNatives::OnTimer(ITimer *pTimer, void *pData) ds@1952: { ds@1952: TimerInfo *pInfo = reinterpret_cast(pData); ds@1952: IPluginFunction *pFunc = pInfo->Hook; ds@1952: cell_t res = static_cast(Pl_Continue); ds@1952: ds@1952: pFunc->PushCell(pInfo->TimerHandle); ds@1952: pFunc->PushCell(pInfo->UserData); ds@1952: pFunc->Execute(&res); ds@1952: ds@1952: return static_cast(res); ds@1952: } ds@1952: ds@1952: void TimerNatives::OnTimerEnd(ITimer *pTimer, void *pData) ds@1952: { ds@1952: HandleSecurity sec; ds@1952: HandleError herr; ds@1952: TimerInfo *pInfo = reinterpret_cast(pData); ds@1952: Handle_t usrhndl = static_cast(pInfo->UserData); ds@1952: ds@1952: sec.pOwner = pInfo->pContext->GetIdentity(); ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if (pInfo->Flags & TIMER_HNDL_CLOSE) ds@1952: { ds@1952: if ((herr=g_HandleSys.FreeHandle(usrhndl, &sec)) != HandleError_None) ds@1952: { ds@1952: g_DbgReporter.GenerateError(pInfo->pContext, ds@1952: pInfo->Hook->GetFunctionID(), ds@1952: SP_ERROR_NATIVE, ds@1952: "Invalid data handle %x (error %d) passed during timer end", ds@1952: usrhndl, ds@1952: herr); ds@1952: } ds@1952: } ds@1952: ds@1952: if ((herr=g_HandleSys.FreeHandle(pInfo->TimerHandle, &sec)) != HandleError_None) ds@1952: { ds@1952: g_DbgReporter.GenerateError(pInfo->pContext, ds@1952: pInfo->Hook->GetFunctionID(), ds@1952: SP_ERROR_NATIVE, ds@1952: "Invalid timer handle %x (error %d) during timer end, displayed function is timer callback, not the stack trace", ds@1952: pInfo->TimerHandle, ds@1952: herr); ds@1952: return; ds@1952: } ds@1952: DeleteTimerInfo(pInfo); ds@1952: } ds@1952: ds@1952: /******************************* ds@1952: * * ds@1952: * TIMER NATIVE IMPLEMENTATIONS * ds@1952: * * ds@1952: ********************************/ ds@1952: ds@1952: static TimerNatives s_TimerNatives; ds@1952: ds@1952: static cell_t smn_CreateTimer(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: IPluginFunction *pFunc; ds@1952: TimerInfo *pInfo; ds@1952: ITimer *pTimer; ds@1952: Handle_t hndl; ds@1952: int flags = params[4]; ds@1952: ds@1952: pFunc = pCtx->GetFunctionById(params[2]); ds@1952: if (!pFunc) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]); ds@1952: } ds@1952: ds@1952: pInfo = s_TimerNatives.CreateTimerInfo(); ds@1952: pTimer = g_Timers.CreateTimer(&s_TimerNatives, sp_ctof(params[1]), pInfo, flags); ds@1952: ds@1952: if (!pTimer) ds@1952: { ds@1952: s_TimerNatives.DeleteTimerInfo(pInfo); ds@1952: return 0; ds@1952: } ds@1952: ds@1952: hndl = g_HandleSys.CreateHandle(g_TimerType, pInfo, pCtx->GetIdentity(), g_pCoreIdent, NULL); ds@1952: ds@1952: pInfo->UserData = params[3]; ds@1952: pInfo->Flags = flags; ds@1952: pInfo->TimerHandle = hndl; ds@1952: pInfo->Hook = pFunc; ds@1952: pInfo->Timer = pTimer; ds@1952: pInfo->pContext = pCtx; ds@1952: ds@1952: return hndl; ds@1952: } ds@1952: ds@1952: static cell_t smn_KillTimer(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: TimerInfo *pInfo; ds@1952: ds@1952: sec.pOwner = pCtx->GetIdentity(); ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: g_Timers.KillTimer(pInfo->Timer); ds@1952: ds@1952: if (params[2] && !(pInfo->Flags & TIMER_HNDL_CLOSE)) ds@1952: { ds@1952: sec.pOwner = pInfo->pContext->GetIdentity(); ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.FreeHandle(static_cast(pInfo->UserData), &sec)) != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid data handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: } ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_TriggerTimer(IPluginContext *pCtx, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: TimerInfo *pInfo; ds@1952: ds@1952: sec.pOwner = pCtx->GetIdentity(); ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: g_Timers.FireTimerOnce(pInfo->Timer, params[2] ? true : false); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_GetTickedTime(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: return sp_ftoc(*g_pUniversalTime); ds@1952: } ds@1952: ds@1952: static cell_t smn_GetMapTimeLeft(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: float time_left; ds@1952: if (!g_Timers.GetMapTimeLeft(&time_left)) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: cell_t *addr; ds@1952: pContext->LocalToPhysAddr(params[1], &addr); ds@1952: *addr = (int)time_left; ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_GetMapTimeLimit(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: IMapTimer *pMapTimer = g_Timers.GetMapTimer(); ds@1952: ds@1952: if (!pMapTimer) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: cell_t *addr; ds@1952: pContext->LocalToPhysAddr(params[1], &addr); ds@1952: ds@1952: *addr = pMapTimer->GetMapTimeLimit(); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_ExtendMapTimeLimit(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: IMapTimer *pMapTimer = g_Timers.GetMapTimer(); ds@1952: ds@1952: if (!pMapTimer) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: pMapTimer->ExtendMapTimeLimit(params[1]); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t smn_IsServerProcessing(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: return (gpGlobals->frametime > 0.0f) ? 1 : 0; ds@1952: } ds@1952: dvander@2063: static cell_t smn_GetTickInterval(IPluginContext *pContext, const cell_t *params) dvander@2063: { dvander@2063: return sp_ftoc(gpGlobals->interval_per_tick); dvander@2063: } dvander@2063: ds@1952: REGISTER_NATIVES(timernatives) ds@1952: { ds@1952: {"CreateTimer", smn_CreateTimer}, ds@1952: {"KillTimer", smn_KillTimer}, ds@1952: {"TriggerTimer", smn_TriggerTimer}, ds@1952: {"GetTickedTime", smn_GetTickedTime}, ds@1952: {"GetMapTimeLeft", smn_GetMapTimeLeft}, ds@1952: {"GetMapTimeLimit", smn_GetMapTimeLimit}, ds@1952: {"ExtendMapTimeLimit", smn_ExtendMapTimeLimit}, ds@1952: {"IsServerProcessing", smn_IsServerProcessing}, dvander@2063: {"GetTickInterval", smn_GetTickInterval}, ds@1952: {NULL, NULL} ds@1952: }; dvander@2063: