3 * =============================================================================
5 * Copyright (C) 2004-2008 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_DATA_HNDL_CLOSE (1<<9)
39 #define TIMER_HNDL_CLOSE (1<<9)
41 HandleType_t g_TimerType;
46 IPluginFunction *Hook;
47 IPluginContext *pContext;
55 public IHandleTypeDispatch,
61 ResultType OnTimer(ITimer *pTimer, void *pData);
62 void OnTimerEnd(ITimer *pTimer, void *pData);
63 public: //IHandleTypeDispatch
64 void OnHandleDestroy(HandleType_t type, void *object);
65 public: //SMGlobalClass
66 void OnSourceModAllInitialized();
67 void OnSourceModShutdown();
69 TimerInfo *CreateTimerInfo();
70 void DeleteTimerInfo(TimerInfo *pInfo);
72 CStack<TimerInfo *> m_FreeTimers;
75 TimerNatives::~TimerNatives()
77 CStack<TimerInfo *>::iterator iter;
78 for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
82 m_FreeTimers.popall();
85 void TimerNatives::OnSourceModAllInitialized()
89 g_HandleSys.InitAccessDefaults(NULL, &sec);
90 sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
92 g_TimerType = g_HandleSys.CreateType("Timer", this, 0, NULL, &sec, g_pCoreIdent, NULL);
95 void TimerNatives::OnSourceModShutdown()
97 g_HandleSys.RemoveType(g_TimerType, g_pCoreIdent);
101 void TimerNatives::OnHandleDestroy(HandleType_t type, void *object)
103 TimerInfo *pTimer = reinterpret_cast<TimerInfo *>(object);
105 g_Timers.KillTimer(pTimer->Timer);
108 TimerInfo *TimerNatives::CreateTimerInfo()
112 if (m_FreeTimers.empty())
114 pInfo = new TimerInfo;
116 pInfo = m_FreeTimers.front();
123 void TimerNatives::DeleteTimerInfo(TimerInfo *pInfo)
125 m_FreeTimers.push(pInfo);
128 ResultType TimerNatives::OnTimer(ITimer *pTimer, void *pData)
130 TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
131 IPluginFunction *pFunc = pInfo->Hook;
132 cell_t res = static_cast<ResultType>(Pl_Continue);
134 pFunc->PushCell(pInfo->TimerHandle);
135 pFunc->PushCell(pInfo->UserData);
136 pFunc->Execute(&res);
138 return static_cast<ResultType>(res);
141 void TimerNatives::OnTimerEnd(ITimer *pTimer, void *pData)
145 TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
146 Handle_t usrhndl = static_cast<Handle_t>(pInfo->UserData);
148 sec.pOwner = pInfo->pContext->GetIdentity();
149 sec.pIdentity = g_pCoreIdent;
151 if (pInfo->Flags & TIMER_DATA_HNDL_CLOSE)
153 if ((herr=g_HandleSys.FreeHandle(usrhndl, &sec)) != HandleError_None)
155 g_DbgReporter.GenerateError(pInfo->pContext,
156 pInfo->Hook->GetFunctionID(),
158 "Invalid data handle %x (error %d) passed during timer end with TIMER_DATA_HNDL_CLOSE",
164 if (pInfo->TimerHandle != BAD_HANDLE)
166 if ((herr=g_HandleSys.FreeHandle(pInfo->TimerHandle, &sec)) != HandleError_None)
168 g_DbgReporter.GenerateError(pInfo->pContext,
169 pInfo->Hook->GetFunctionID(),
171 "Invalid timer handle %x (error %d) during timer end, displayed function is timer callback, not the stack trace",
177 DeleteTimerInfo(pInfo);
180 /*******************************
182 * TIMER NATIVE IMPLEMENTATIONS *
184 ********************************/
186 static TimerNatives s_TimerNatives;
188 static cell_t smn_CreateTimer(IPluginContext *pCtx, const cell_t *params)
190 IPluginFunction *pFunc;
194 int flags = params[4];
196 pFunc = pCtx->GetFunctionById(params[2]);
199 return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
202 pInfo = s_TimerNatives.CreateTimerInfo();
203 pTimer = g_Timers.CreateTimer(&s_TimerNatives, sp_ctof(params[1]), pInfo, flags);
207 s_TimerNatives.DeleteTimerInfo(pInfo);
211 hndl = g_HandleSys.CreateHandle(g_TimerType, pInfo, pCtx->GetIdentity(), g_pCoreIdent, NULL);
213 /* If we can't get a handle, the timer isn't refcounted against the plugin and
214 * we need to bail out to prevent a crash.
216 if (hndl == BAD_HANDLE)
218 /* Free this for completeness. */
219 if (flags & TIMER_DATA_HNDL_CLOSE)
221 HandleSecurity sec(pCtx->GetIdentity(), g_pCoreIdent);
222 g_HandleSys.FreeHandle(params[3], &sec);
224 /* Zero everything so there's no conflicts */
225 memset(pInfo, 0, sizeof(TimerInfo));
226 g_Timers.KillTimer(pTimer);
228 return pCtx->ThrowNativeError("Could not create timer, no more handles");
231 pInfo->UserData = params[3];
232 pInfo->Flags = flags;
233 pInfo->TimerHandle = hndl;
235 pInfo->Timer = pTimer;
236 pInfo->pContext = pCtx;
241 static cell_t smn_KillTimer(IPluginContext *pCtx, const cell_t *params)
243 Handle_t hndl = static_cast<Handle_t>(params[1]);
248 sec.pOwner = pCtx->GetIdentity();
249 sec.pIdentity = g_pCoreIdent;
251 if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
254 return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
257 g_Timers.KillTimer(pInfo->Timer);
259 if (params[2] && !(pInfo->Flags & TIMER_DATA_HNDL_CLOSE))
261 sec.pOwner = pInfo->pContext->GetIdentity();
262 sec.pIdentity = g_pCoreIdent;
264 if ((herr=g_HandleSys.FreeHandle(static_cast<Handle_t>(pInfo->UserData), &sec)) != HandleError_None)
266 return pCtx->ThrowNativeError("Invalid data handle %x (error %d) on timer kill with TIMER_DATA_HNDL_CLOSE", hndl, herr);
273 static cell_t smn_TriggerTimer(IPluginContext *pCtx, const cell_t *params)
275 Handle_t hndl = static_cast<Handle_t>(params[1]);
280 sec.pOwner = pCtx->GetIdentity();
281 sec.pIdentity = g_pCoreIdent;
283 if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
286 return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
289 g_Timers.FireTimerOnce(pInfo->Timer, params[2] ? true : false);
294 static cell_t smn_GetTickedTime(IPluginContext *pContext, const cell_t *params)
296 return sp_ftoc(*g_pUniversalTime);
299 static cell_t smn_GetMapTimeLeft(IPluginContext *pContext, const cell_t *params)
302 if (!g_Timers.GetMapTimeLeft(&time_left))
308 pContext->LocalToPhysAddr(params[1], &addr);
309 *addr = (int)time_left;
314 static cell_t smn_GetMapTimeLimit(IPluginContext *pContext, const cell_t *params)
316 IMapTimer *pMapTimer = g_Timers.GetMapTimer();
324 pContext->LocalToPhysAddr(params[1], &addr);
326 *addr = pMapTimer->GetMapTimeLimit();
331 static cell_t smn_ExtendMapTimeLimit(IPluginContext *pContext, const cell_t *params)
333 IMapTimer *pMapTimer = g_Timers.GetMapTimer();
340 pMapTimer->ExtendMapTimeLimit(params[1]);
345 static cell_t smn_IsServerProcessing(IPluginContext *pContext, const cell_t *params)
347 return (gpGlobals->frametime > 0.0f) ? 1 : 0;
350 static cell_t smn_GetTickInterval(IPluginContext *pContext, const cell_t *params)
352 return sp_ftoc(gpGlobals->interval_per_tick);
355 REGISTER_NATIVES(timernatives)
357 {"CreateTimer", smn_CreateTimer},
358 {"KillTimer", smn_KillTimer},
359 {"TriggerTimer", smn_TriggerTimer},
360 {"GetTickedTime", smn_GetTickedTime},
361 {"GetMapTimeLeft", smn_GetMapTimeLeft},
362 {"GetMapTimeLimit", smn_GetMapTimeLimit},
363 {"ExtendMapTimeLimit", smn_ExtendMapTimeLimit},
364 {"IsServerProcessing", smn_IsServerProcessing},
365 {"GetTickInterval", smn_GetTickInterval},