3 * =============================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include "HandleSys.h"
34 #include "PluginSys.h"
36 #include "DebugReporter.h"
38 #define TIMER_HNDL_CLOSE (1<<9)
40 HandleType_t g_TimerType;
45 IPluginFunction *Hook;
46 IPluginContext *pContext;
54 public IHandleTypeDispatch,
60 ResultType OnTimer(ITimer *pTimer, void *pData);
61 void OnTimerEnd(ITimer *pTimer, void *pData);
62 public: //IHandleTypeDispatch
63 void OnHandleDestroy(HandleType_t type, void *object);
64 public: //SMGlobalClass
65 void OnSourceModAllInitialized();
66 void OnSourceModShutdown();
68 TimerInfo *CreateTimerInfo();
69 void DeleteTimerInfo(TimerInfo *pInfo);
71 CStack<TimerInfo *> m_FreeTimers;
74 TimerNatives::~TimerNatives()
76 CStack<TimerInfo *>::iterator iter;
77 for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
81 m_FreeTimers.popall();
84 void TimerNatives::OnSourceModAllInitialized()
88 g_HandleSys.InitAccessDefaults(NULL, &sec);
89 sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
91 g_TimerType = g_HandleSys.CreateType("Timer", this, 0, NULL, &sec, g_pCoreIdent, NULL);
94 void TimerNatives::OnSourceModShutdown()
96 g_HandleSys.RemoveType(g_TimerType, g_pCoreIdent);
100 void TimerNatives::OnHandleDestroy(HandleType_t type, void *object)
102 TimerInfo *pTimer = reinterpret_cast<TimerInfo *>(object);
104 g_Timers.KillTimer(pTimer->Timer);
107 TimerInfo *TimerNatives::CreateTimerInfo()
111 if (m_FreeTimers.empty())
113 pInfo = new TimerInfo;
115 pInfo = m_FreeTimers.front();
122 void TimerNatives::DeleteTimerInfo(TimerInfo *pInfo)
124 m_FreeTimers.push(pInfo);
127 ResultType TimerNatives::OnTimer(ITimer *pTimer, void *pData)
129 TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
130 IPluginFunction *pFunc = pInfo->Hook;
131 cell_t res = static_cast<ResultType>(Pl_Continue);
133 pFunc->PushCell(pInfo->TimerHandle);
134 pFunc->PushCell(pInfo->UserData);
135 pFunc->Execute(&res);
137 return static_cast<ResultType>(res);
140 void TimerNatives::OnTimerEnd(ITimer *pTimer, void *pData)
144 TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
145 Handle_t usrhndl = static_cast<Handle_t>(pInfo->UserData);
147 sec.pOwner = pInfo->pContext->GetIdentity();
148 sec.pIdentity = g_pCoreIdent;
150 if (pInfo->Flags & TIMER_HNDL_CLOSE)
152 if ((herr=g_HandleSys.FreeHandle(usrhndl, &sec)) != HandleError_None)
154 g_DbgReporter.GenerateError(pInfo->pContext,
155 pInfo->Hook->GetFunctionID(),
157 "Invalid data handle %x (error %d) passed during timer end",
163 if ((herr=g_HandleSys.FreeHandle(pInfo->TimerHandle, &sec)) != HandleError_None)
165 g_DbgReporter.GenerateError(pInfo->pContext,
166 pInfo->Hook->GetFunctionID(),
168 "Invalid timer handle %x (error %d) during timer end, displayed function is timer callback, not the stack trace",
173 DeleteTimerInfo(pInfo);
176 /*******************************
178 * TIMER NATIVE IMPLEMENTATIONS *
180 ********************************/
182 static TimerNatives s_TimerNatives;
184 static cell_t smn_CreateTimer(IPluginContext *pCtx, const cell_t *params)
186 IPluginFunction *pFunc;
190 int flags = params[4];
192 pFunc = pCtx->GetFunctionById(params[2]);
195 return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
198 pInfo = s_TimerNatives.CreateTimerInfo();
199 pTimer = g_Timers.CreateTimer(&s_TimerNatives, sp_ctof(params[1]), pInfo, flags);
203 s_TimerNatives.DeleteTimerInfo(pInfo);
207 hndl = g_HandleSys.CreateHandle(g_TimerType, pInfo, pCtx->GetIdentity(), g_pCoreIdent, NULL);
209 pInfo->UserData = params[3];
210 pInfo->Flags = flags;
211 pInfo->TimerHandle = hndl;
213 pInfo->Timer = pTimer;
214 pInfo->pContext = pCtx;
219 static cell_t smn_KillTimer(IPluginContext *pCtx, const cell_t *params)
221 Handle_t hndl = static_cast<Handle_t>(params[1]);
226 sec.pOwner = pCtx->GetIdentity();
227 sec.pIdentity = g_pCoreIdent;
229 if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
232 return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
235 g_Timers.KillTimer(pInfo->Timer);
237 if (params[2] && !(pInfo->Flags & TIMER_HNDL_CLOSE))
239 sec.pOwner = pInfo->pContext->GetIdentity();
240 sec.pIdentity = g_pCoreIdent;
242 if ((herr=g_HandleSys.FreeHandle(static_cast<Handle_t>(pInfo->UserData), &sec)) != HandleError_None)
244 return pCtx->ThrowNativeError("Invalid data handle %x (error %d)", hndl, herr);
251 static cell_t smn_TriggerTimer(IPluginContext *pCtx, const cell_t *params)
253 Handle_t hndl = static_cast<Handle_t>(params[1]);
258 sec.pOwner = pCtx->GetIdentity();
259 sec.pIdentity = g_pCoreIdent;
261 if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
264 return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
267 g_Timers.FireTimerOnce(pInfo->Timer, params[2] ? true : false);
272 static cell_t smn_GetTickedTime(IPluginContext *pContext, const cell_t *params)
274 return sp_ftoc(*g_pUniversalTime);
277 static cell_t smn_GetMapTimeLeft(IPluginContext *pContext, const cell_t *params)
280 if (!g_Timers.GetMapTimeLeft(&time_left))
286 pContext->LocalToPhysAddr(params[1], &addr);
287 *addr = (int)time_left;
292 static cell_t smn_GetMapTimeLimit(IPluginContext *pContext, const cell_t *params)
294 IMapTimer *pMapTimer = g_Timers.GetMapTimer();
302 pContext->LocalToPhysAddr(params[1], &addr);
304 *addr = pMapTimer->GetMapTimeLimit();
309 static cell_t smn_ExtendMapTimeLimit(IPluginContext *pContext, const cell_t *params)
311 IMapTimer *pMapTimer = g_Timers.GetMapTimer();
318 pMapTimer->ExtendMapTimeLimit(params[1]);
323 static cell_t smn_IsServerProcessing(IPluginContext *pContext, const cell_t *params)
325 return (gpGlobals->frametime > 0.0f) ? 1 : 0;
328 REGISTER_NATIVES(timernatives)
330 {"CreateTimer", smn_CreateTimer},
331 {"KillTimer", smn_KillTimer},
332 {"TriggerTimer", smn_TriggerTimer},
333 {"GetTickedTime", smn_GetTickedTime},
334 {"GetMapTimeLeft", smn_GetMapTimeLeft},
335 {"GetMapTimeLimit", smn_GetMapTimeLimit},
336 {"ExtendMapTimeLimit", smn_ExtendMapTimeLimit},
337 {"IsServerProcessing", smn_IsServerProcessing},