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"
36 #define TIMER_HNDL_CLOSE (1<<9)
38 HandleType_t g_TimerType;
43 IPluginFunction *Hook;
44 IPluginContext *pContext;
52 public IHandleTypeDispatch,
58 ResultType OnTimer(ITimer *pTimer, void *pData);
59 void OnTimerEnd(ITimer *pTimer, void *pData);
60 public: //IHandleTypeDispatch
61 void OnHandleDestroy(HandleType_t type, void *object);
62 public: //SMGlobalClass
63 void OnSourceModAllInitialized();
64 void OnSourceModShutdown();
66 TimerInfo *CreateTimerInfo();
67 void DeleteTimerInfo(TimerInfo *pInfo);
69 CStack<TimerInfo *> m_FreeTimers;
72 TimerNatives::~TimerNatives()
74 CStack<TimerInfo *>::iterator iter;
75 for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
79 m_FreeTimers.popall();
82 void TimerNatives::OnSourceModAllInitialized()
86 g_HandleSys.InitAccessDefaults(NULL, &sec);
87 sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
89 g_TimerType = g_HandleSys.CreateType("Timer", this, 0, NULL, &sec, g_pCoreIdent, NULL);
92 void TimerNatives::OnSourceModShutdown()
94 g_HandleSys.RemoveType(g_TimerType, g_pCoreIdent);
98 void TimerNatives::OnHandleDestroy(HandleType_t type, void *object)
100 TimerInfo *pTimer = reinterpret_cast<TimerInfo *>(object);
102 g_Timers.KillTimer(pTimer->Timer);
105 TimerInfo *TimerNatives::CreateTimerInfo()
109 if (m_FreeTimers.empty())
111 pInfo = new TimerInfo;
113 pInfo = m_FreeTimers.front();
120 void TimerNatives::DeleteTimerInfo(TimerInfo *pInfo)
122 m_FreeTimers.push(pInfo);
125 ResultType TimerNatives::OnTimer(ITimer *pTimer, void *pData)
127 TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
128 IPluginFunction *pFunc = pInfo->Hook;
129 cell_t res = static_cast<ResultType>(Pl_Continue);
131 pFunc->PushCell(pInfo->TimerHandle);
132 pFunc->PushCell(pInfo->UserData);
133 pFunc->Execute(&res);
135 return static_cast<ResultType>(res);
138 void TimerNatives::OnTimerEnd(ITimer *pTimer, void *pData)
142 TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
143 Handle_t usrhndl = static_cast<Handle_t>(pInfo->UserData);
145 sec.pOwner = pInfo->pContext->GetIdentity();
146 sec.pIdentity = g_pCoreIdent;
148 if (pInfo->Flags & TIMER_HNDL_CLOSE)
150 if ((herr=g_HandleSys.FreeHandle(usrhndl, &sec)) != HandleError_None)
152 g_Logger.LogError("Invalid data handle %x (error %d) passed during timer end", usrhndl, herr);
156 g_HandleSys.FreeHandle(pInfo->TimerHandle, &sec);
157 DeleteTimerInfo(pInfo);
160 /*******************************
162 * TIMER NATIVE IMPLEMENTATIONS *
164 ********************************/
166 static TimerNatives s_TimerNatives;
168 static cell_t smn_CreateTimer(IPluginContext *pCtx, const cell_t *params)
170 IPluginFunction *pFunc;
174 int flags = params[4];
176 pFunc = pCtx->GetFunctionById(params[2]);
179 return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
182 pInfo = s_TimerNatives.CreateTimerInfo();
183 pTimer = g_Timers.CreateTimer(&s_TimerNatives, sp_ctof(params[1]), pInfo, flags);
184 hndl = g_HandleSys.CreateHandle(g_TimerType, pInfo, pCtx->GetIdentity(), g_pCoreIdent, NULL);
186 pInfo->UserData = params[3];
187 pInfo->Flags = flags;
188 pInfo->TimerHandle = hndl;
190 pInfo->Timer = pTimer;
191 pInfo->pContext = pCtx;
196 static cell_t smn_KillTimer(IPluginContext *pCtx, const cell_t *params)
198 Handle_t hndl = static_cast<Handle_t>(params[1]);
203 sec.pOwner = pCtx->GetIdentity();
204 sec.pIdentity = g_pCoreIdent;
206 if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
209 return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
212 g_Timers.KillTimer(pInfo->Timer);
214 if (params[2] && !(pInfo->Flags & TIMER_HNDL_CLOSE))
216 sec.pOwner = pInfo->pContext->GetIdentity();
217 sec.pIdentity = g_pCoreIdent;
219 if ((herr=g_HandleSys.FreeHandle(static_cast<Handle_t>(pInfo->UserData), &sec)) != HandleError_None)
221 return pCtx->ThrowNativeError("Invalid data handle %x (error %d)", hndl, herr);
228 static cell_t smn_TriggerTimer(IPluginContext *pCtx, const cell_t *params)
230 Handle_t hndl = static_cast<Handle_t>(params[1]);
235 sec.pOwner = pCtx->GetIdentity();
236 sec.pIdentity = g_pCoreIdent;
238 if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
241 return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
244 g_Timers.FireTimerOnce(pInfo->Timer, params[2] ? true : false);
249 static cell_t smn_GetTickedTime(IPluginContext *pContext, const cell_t *params)
251 float t = engine->Time();
255 REGISTER_NATIVES(timernatives)
257 {"CreateTimer", smn_CreateTimer},
258 {"KillTimer", smn_KillTimer},
259 {"TriggerTimer", smn_TriggerTimer},
260 {"GetTickedTime", smn_GetTickedTime},