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"
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()
87 g_HandleSys.InitAccessDefaults(NULL, &sec);
88 sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
90 g_TimerType = g_HandleSys.CreateType("Timer", this, 0, NULL, &sec, g_pCoreIdent, NULL);
93 void TimerNatives::OnSourceModShutdown()
95 g_HandleSys.RemoveType(g_TimerType, g_pCoreIdent);
99 void TimerNatives::OnHandleDestroy(HandleType_t type, void *object)
101 TimerInfo *pTimer = reinterpret_cast<TimerInfo *>(object);
103 g_Timers.KillTimer(pTimer->Timer);
106 TimerInfo *TimerNatives::CreateTimerInfo()
110 if (m_FreeTimers.empty())
112 pInfo = new TimerInfo;
114 pInfo = m_FreeTimers.front();
121 void TimerNatives::DeleteTimerInfo(TimerInfo *pInfo)
123 m_FreeTimers.push(pInfo);
126 ResultType TimerNatives::OnTimer(ITimer *pTimer, void *pData)
128 TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
129 IPluginFunction *pFunc = pInfo->Hook;
130 cell_t res = static_cast<ResultType>(Pl_Continue);
132 pFunc->PushCell(pInfo->TimerHandle);
133 pFunc->PushCell(pInfo->UserData);
134 pFunc->Execute(&res);
136 return static_cast<ResultType>(res);
139 void TimerNatives::OnTimerEnd(ITimer *pTimer, void *pData)
143 TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
144 Handle_t usrhndl = static_cast<Handle_t>(pInfo->UserData);
146 sec.pOwner = pInfo->pContext->GetIdentity();
147 sec.pIdentity = g_pCoreIdent;
149 if (pInfo->Flags & TIMER_HNDL_CLOSE)
151 if ((herr=g_HandleSys.FreeHandle(usrhndl, &sec)) != HandleError_None)
153 g_Logger.LogError("Invalid data handle %x (error %d) passed during timer end on plugin %s",
156 g_PluginSys.FindPluginByContext(pInfo->pContext->GetContext())->GetFilename());
160 if ((herr=g_HandleSys.FreeHandle(pInfo->TimerHandle, &sec)) != HandleError_None)
162 g_Logger.LogError("Invalid timer handle %x (error %d) on plugin %s during timer end",
165 g_PluginSys.FindPluginByContext(pInfo->pContext->GetContext())->GetFilename());
168 DeleteTimerInfo(pInfo);
171 /*******************************
173 * TIMER NATIVE IMPLEMENTATIONS *
175 ********************************/
177 static TimerNatives s_TimerNatives;
179 static cell_t smn_CreateTimer(IPluginContext *pCtx, const cell_t *params)
181 IPluginFunction *pFunc;
185 int flags = params[4];
187 pFunc = pCtx->GetFunctionById(params[2]);
190 return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
193 pInfo = s_TimerNatives.CreateTimerInfo();
194 pTimer = g_Timers.CreateTimer(&s_TimerNatives, sp_ctof(params[1]), pInfo, flags);
198 s_TimerNatives.DeleteTimerInfo(pInfo);
202 hndl = g_HandleSys.CreateHandle(g_TimerType, pInfo, pCtx->GetIdentity(), g_pCoreIdent, NULL);
204 pInfo->UserData = params[3];
205 pInfo->Flags = flags;
206 pInfo->TimerHandle = hndl;
208 pInfo->Timer = pTimer;
209 pInfo->pContext = pCtx;
214 static cell_t smn_KillTimer(IPluginContext *pCtx, const cell_t *params)
216 Handle_t hndl = static_cast<Handle_t>(params[1]);
221 sec.pOwner = pCtx->GetIdentity();
222 sec.pIdentity = g_pCoreIdent;
224 if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
227 return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
230 g_Timers.KillTimer(pInfo->Timer);
232 if (params[2] && !(pInfo->Flags & TIMER_HNDL_CLOSE))
234 sec.pOwner = pInfo->pContext->GetIdentity();
235 sec.pIdentity = g_pCoreIdent;
237 if ((herr=g_HandleSys.FreeHandle(static_cast<Handle_t>(pInfo->UserData), &sec)) != HandleError_None)
239 return pCtx->ThrowNativeError("Invalid data handle %x (error %d)", hndl, herr);
246 static cell_t smn_TriggerTimer(IPluginContext *pCtx, const cell_t *params)
248 Handle_t hndl = static_cast<Handle_t>(params[1]);
253 sec.pOwner = pCtx->GetIdentity();
254 sec.pIdentity = g_pCoreIdent;
256 if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
259 return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
262 g_Timers.FireTimerOnce(pInfo->Timer, params[2] ? true : false);
267 static cell_t smn_GetTickedTime(IPluginContext *pContext, const cell_t *params)
269 return sp_ftoc(*g_pUniversalTime);
272 static cell_t smn_GetMapTimeLeft(IPluginContext *pContext, const cell_t *params)
275 if (!g_Timers.GetMapTimeLeft(&time_left))
281 pContext->LocalToPhysAddr(params[1], &addr);
282 *addr = (int)time_left;
287 static cell_t smn_GetMapTimeLimit(IPluginContext *pContext, const cell_t *params)
289 IMapTimer *pMapTimer = g_Timers.GetMapTimer();
297 pContext->LocalToPhysAddr(params[1], &addr);
299 *addr = pMapTimer->GetMapTimeLimit();
304 static cell_t smn_ExtendMapTimeLimit(IPluginContext *pContext, const cell_t *params)
306 IMapTimer *pMapTimer = g_Timers.GetMapTimer();
313 pMapTimer->ExtendMapTimeLimit(params[1]);
318 static cell_t smn_IsServerProcessing(IPluginContext *pContext, const cell_t *params)
320 return (gpGlobals->frametime > 0.0f) ? 1 : 0;
323 REGISTER_NATIVES(timernatives)
325 {"CreateTimer", smn_CreateTimer},
326 {"KillTimer", smn_KillTimer},
327 {"TriggerTimer", smn_TriggerTimer},
328 {"GetTickedTime", smn_GetTickedTime},
329 {"GetMapTimeLeft", smn_GetMapTimeLeft},
330 {"GetMapTimeLimit", smn_GetMapTimeLimit},
331 {"ExtendMapTimeLimit", smn_ExtendMapTimeLimit},
332 {"IsServerProcessing", smn_IsServerProcessing},