core/smn_timers.cpp
author David Anderson <dvander@alliedmods.net>
Tue Sep 11 18:08:49 2007 +0000 (2007-09-11)
changeset 1408 2fd63cc4bd9c
parent 1407 bb6516a50eea
child 1409 84e9d3494603
permissions -rw-r--r--
timelimit function now gets initialized
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "HandleSys.h"
    33 #include "TimerSys.h"
    34 #include "Logger.h"
    35 
    36 #define TIMER_HNDL_CLOSE	(1<<9)
    37 
    38 HandleType_t g_TimerType;
    39 
    40 struct TimerInfo 
    41 {
    42 	ITimer *Timer;
    43 	IPluginFunction *Hook;
    44 	IPluginContext *pContext;
    45 	Handle_t TimerHandle;
    46 	int UserData;
    47 	int Flags;
    48 };
    49 
    50 class TimerNatives :
    51 	public SMGlobalClass,
    52 	public IHandleTypeDispatch,
    53 	public ITimedEvent
    54 {
    55 public:
    56 	~TimerNatives();
    57 public: //ITimedEvent
    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();
    65 public:
    66 	TimerInfo *CreateTimerInfo();
    67 	void DeleteTimerInfo(TimerInfo *pInfo);
    68 private:
    69 	CStack<TimerInfo *> m_FreeTimers;
    70 };
    71 
    72 TimerNatives::~TimerNatives()
    73 {
    74 	CStack<TimerInfo *>::iterator iter;
    75 	for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
    76 	{
    77 		delete (*iter);
    78 	}
    79 	m_FreeTimers.popall();
    80 }
    81 
    82 void TimerNatives::OnSourceModAllInitialized()
    83 {
    84 	HandleAccess sec;
    85 
    86 	g_HandleSys.InitAccessDefaults(NULL, &sec);
    87 	sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
    88 
    89 	g_TimerType = g_HandleSys.CreateType("Timer", this, 0, NULL, &sec, g_pCoreIdent, NULL);
    90 }
    91 
    92 void TimerNatives::OnSourceModShutdown()
    93 {
    94 	g_HandleSys.RemoveType(g_TimerType, g_pCoreIdent);
    95 	g_TimerType = 0;
    96 }
    97 
    98 void TimerNatives::OnHandleDestroy(HandleType_t type, void *object)
    99 {
   100 	TimerInfo *pTimer = reinterpret_cast<TimerInfo *>(object);
   101 
   102 	g_Timers.KillTimer(pTimer->Timer);
   103 }
   104 
   105 TimerInfo *TimerNatives::CreateTimerInfo()
   106 {
   107 	TimerInfo *pInfo;
   108 
   109 	if (m_FreeTimers.empty())
   110 	{
   111 		pInfo = new TimerInfo;
   112 	} else {
   113 		pInfo = m_FreeTimers.front();
   114 		m_FreeTimers.pop();
   115 	}
   116 
   117 	return pInfo;
   118 }
   119 
   120 void TimerNatives::DeleteTimerInfo(TimerInfo *pInfo)
   121 {
   122 	m_FreeTimers.push(pInfo);
   123 }
   124 
   125 ResultType TimerNatives::OnTimer(ITimer *pTimer, void *pData)
   126 {
   127 	TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
   128 	IPluginFunction *pFunc = pInfo->Hook;
   129 	cell_t res = static_cast<ResultType>(Pl_Continue);
   130 
   131 	pFunc->PushCell(pInfo->TimerHandle);
   132 	pFunc->PushCell(pInfo->UserData);
   133 	pFunc->Execute(&res);
   134 
   135 	return static_cast<ResultType>(res);
   136 }
   137 
   138 void TimerNatives::OnTimerEnd(ITimer *pTimer, void *pData)
   139 {
   140 	HandleSecurity sec;
   141 	HandleError herr;
   142 	TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
   143 	Handle_t usrhndl = static_cast<Handle_t>(pInfo->UserData);
   144 
   145 	sec.pOwner = pInfo->pContext->GetIdentity();
   146 	sec.pIdentity = g_pCoreIdent;
   147 
   148 	if (pInfo->Flags & TIMER_HNDL_CLOSE)
   149 	{
   150 		if ((herr=g_HandleSys.FreeHandle(usrhndl, &sec)) != HandleError_None)
   151 		{
   152 			g_Logger.LogError("Invalid data handle %x (error %d) passed during timer end", usrhndl, herr);
   153 		}
   154 	}
   155 
   156 	g_HandleSys.FreeHandle(pInfo->TimerHandle, &sec);
   157 	DeleteTimerInfo(pInfo);
   158 }
   159 
   160 /*******************************
   161 *                              *
   162 * TIMER NATIVE IMPLEMENTATIONS *
   163 *                              *
   164 ********************************/
   165 
   166 static TimerNatives s_TimerNatives;
   167 
   168 static cell_t smn_CreateTimer(IPluginContext *pCtx, const cell_t *params)
   169 {
   170 	IPluginFunction *pFunc;
   171 	TimerInfo *pInfo;
   172 	ITimer *pTimer;
   173 	Handle_t hndl;
   174 	int flags = params[4];
   175 
   176 	pFunc = pCtx->GetFunctionById(params[2]);
   177 	if (!pFunc)
   178 	{
   179 		return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
   180 	}
   181 
   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);
   185 
   186 	pInfo->UserData = params[3];
   187 	pInfo->Flags = flags;
   188 	pInfo->TimerHandle = hndl;
   189 	pInfo->Hook = pFunc;
   190 	pInfo->Timer = pTimer;
   191 	pInfo->pContext = pCtx;
   192 
   193 	return hndl;
   194 }
   195 
   196 static cell_t smn_KillTimer(IPluginContext *pCtx, const cell_t *params)
   197 {
   198 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   199 	HandleError herr;
   200 	HandleSecurity sec;
   201 	TimerInfo *pInfo;
   202 
   203 	sec.pOwner = pCtx->GetIdentity();
   204 	sec.pIdentity = g_pCoreIdent;
   205 
   206 	if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
   207 		!= HandleError_None)
   208 	{
   209 		return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
   210 	}
   211 
   212 	g_Timers.KillTimer(pInfo->Timer);
   213 
   214 	if (params[2] && !(pInfo->Flags & TIMER_HNDL_CLOSE))
   215 	{
   216 		sec.pOwner = pInfo->pContext->GetIdentity();
   217 		sec.pIdentity = g_pCoreIdent;
   218 
   219 		if ((herr=g_HandleSys.FreeHandle(static_cast<Handle_t>(pInfo->UserData), &sec)) != HandleError_None)
   220 		{
   221 			return pCtx->ThrowNativeError("Invalid data handle %x (error %d)", hndl, herr);
   222 		}
   223 	}
   224 
   225 	return 1;
   226 }
   227 
   228 static cell_t smn_TriggerTimer(IPluginContext *pCtx, const cell_t *params)
   229 {
   230 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   231 	HandleError herr;
   232 	HandleSecurity sec;
   233 	TimerInfo *pInfo;
   234 
   235 	sec.pOwner = pCtx->GetIdentity();
   236 	sec.pIdentity = g_pCoreIdent;
   237 
   238 	if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
   239 		!= HandleError_None)
   240 	{
   241 		return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
   242 	}
   243 
   244 	g_Timers.FireTimerOnce(pInfo->Timer, params[2] ? true : false);
   245 
   246 	return 1;
   247 }
   248 
   249 static cell_t smn_GetTickedTime(IPluginContext *pContext, const cell_t *params)
   250 {
   251 	float t = engine->Time();
   252 	return sp_ftoc(t);
   253 }
   254 
   255 REGISTER_NATIVES(timernatives)
   256 {
   257 	{"CreateTimer",				smn_CreateTimer},
   258 	{"KillTimer",				smn_KillTimer},
   259 	{"TriggerTimer",			smn_TriggerTimer},
   260 	{"GetTickedTime",			smn_GetTickedTime},
   261 	{NULL,						NULL}
   262 };