3 * ================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ================================================================
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License,
10 * version 3.0, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to
21 * link the code of this program (as well as its derivative works) to
22 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any
23 * Game MODs that run on software by the Valve Corporation. You must
24 * obey the GNU General Public License in all respects for all other
25 * code used. Additionally, AlliedModders LLC grants this exception
26 * to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version
28 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
33 #include "HandleSys.h"
37 #define TIMER_HNDL_CLOSE (1<<9)
39 HandleType_t g_TimerType;
44 IPluginFunction *Hook;
45 IPluginContext *pContext;
53 public IHandleTypeDispatch,
59 ResultType OnTimer(ITimer *pTimer, void *pData);
60 void OnTimerEnd(ITimer *pTimer, void *pData);
61 public: //IHandleTypeDispatch
62 void OnHandleDestroy(HandleType_t type, void *object);
63 public: //SMGlobalClass
64 void OnSourceModAllInitialized();
65 void OnSourceModShutdown();
67 TimerInfo *CreateTimerInfo();
68 void DeleteTimerInfo(TimerInfo *pInfo);
70 CStack<TimerInfo *> m_FreeTimers;
73 TimerNatives::~TimerNatives()
75 CStack<TimerInfo *>::iterator iter;
76 for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
80 m_FreeTimers.popall();
83 void TimerNatives::OnSourceModAllInitialized()
86 sec.access[HandleAccess_Clone] |= HANDLE_RESTRICT_IDENTITY;
88 g_TimerType = g_HandleSys.CreateType("Timer", this, 0, NULL, &sec, g_pCoreIdent, NULL);
91 void TimerNatives::OnSourceModShutdown()
93 g_HandleSys.RemoveType(g_TimerType, g_pCoreIdent);
97 void TimerNatives::OnHandleDestroy(HandleType_t type, void *object)
99 TimerInfo *pTimer = reinterpret_cast<TimerInfo *>(object);
101 g_Timers.KillTimer(pTimer->Timer);
104 TimerInfo *TimerNatives::CreateTimerInfo()
108 if (m_FreeTimers.empty())
110 pInfo = new TimerInfo;
112 pInfo = m_FreeTimers.front();
119 void TimerNatives::DeleteTimerInfo(TimerInfo *pInfo)
121 m_FreeTimers.push(pInfo);
124 ResultType TimerNatives::OnTimer(ITimer *pTimer, void *pData)
126 TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
127 IPluginFunction *pFunc = pInfo->Hook;
128 cell_t res = static_cast<ResultType>(Pl_Continue);
130 pFunc->PushCell(pInfo->TimerHandle);
131 pFunc->PushCell(pInfo->UserData);
132 pFunc->Execute(&res);
134 return static_cast<ResultType>(res);
137 void TimerNatives::OnTimerEnd(ITimer *pTimer, void *pData)
141 TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
142 Handle_t usrhndl = static_cast<Handle_t>(pInfo->UserData);
144 sec.pOwner = pInfo->pContext->GetIdentity();
145 sec.pIdentity = g_pCoreIdent;
147 if (pInfo->Flags & TIMER_HNDL_CLOSE)
149 if ((herr=g_HandleSys.FreeHandle(usrhndl, &sec)) != HandleError_None)
151 g_Logger.LogError("Invalid data handle %x (error %d) passed during timer end", usrhndl, herr);
155 g_HandleSys.FreeHandle(pInfo->TimerHandle, &sec);
156 DeleteTimerInfo(pInfo);
159 /*******************************
161 * TIMER NATIVE IMPLEMENTATIONS *
163 ********************************/
165 static TimerNatives s_TimerNatives;
167 static cell_t smn_CreateTimer(IPluginContext *pCtx, const cell_t *params)
169 IPluginFunction *pFunc;
173 int flags = params[4];
175 pFunc = pCtx->GetFunctionById(params[2]);
178 return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
181 pInfo = s_TimerNatives.CreateTimerInfo();
182 pTimer = g_Timers.CreateTimer(&s_TimerNatives, sp_ctof(params[1]), pInfo, flags);
183 hndl = g_HandleSys.CreateHandle(g_TimerType, pInfo, pCtx->GetIdentity(), g_pCoreIdent, NULL);
185 pInfo->UserData = params[3];
186 pInfo->Flags = flags;
187 pInfo->TimerHandle = hndl;
189 pInfo->Timer = pTimer;
190 pInfo->pContext = pCtx;
195 static cell_t smn_KillTimer(IPluginContext *pCtx, const cell_t *params)
197 Handle_t hndl = static_cast<Handle_t>(params[1]);
202 sec.pOwner = pCtx->GetIdentity();
203 sec.pIdentity = g_pCoreIdent;
205 if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
208 return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
211 g_Timers.KillTimer(pInfo->Timer);
213 if (params[2] && !(pInfo->Flags & TIMER_HNDL_CLOSE))
215 sec.pOwner = pInfo->pContext->GetIdentity();
216 sec.pIdentity = g_pCoreIdent;
218 if ((herr=g_HandleSys.FreeHandle(static_cast<Handle_t>(pInfo->UserData), &sec)) != HandleError_None)
220 return pCtx->ThrowNativeError("Invalid data handle %x (error %d)", hndl, herr);
227 static cell_t smn_TriggerTimer(IPluginContext *pCtx, const cell_t *params)
229 Handle_t hndl = static_cast<Handle_t>(params[1]);
235 sec.pIdentity = g_pCoreIdent;
237 if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
240 return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
243 g_Timers.FireTimerOnce(pInfo->Timer, params[2] ? true : false);
248 static cell_t smn_GetTickedTime(IPluginContext *pContext, const cell_t *params)
251 pContext->LocalToPhysAddr(params[1], &simulating);
253 *simulating = g_SimTicks.ticking ? 0 : 1;
255 float t = g_SimTicks.ticking ? gpGlobals->curtime : g_SimTicks.ticktime;
259 REGISTER_NATIVES(timernatives)
261 {"CreateTimer", smn_CreateTimer},
262 {"KillTimer", smn_KillTimer},
263 {"TriggerTimer", smn_TriggerTimer},
264 {"GetTickedTime", smn_GetTickedTime},