core/smn_timers.cpp
branchsourcemod-1.1.0
changeset 2283 03a67e41afbc
parent 1951 c7b4237fff50
child 2287 017a725c9d17
     1.1 --- a/core/smn_timers.cpp	Fri Mar 28 06:21:34 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,332 +0,0 @@
     1.4 -/**
     1.5 - * vim: set ts=4 :
     1.6 - * =============================================================================
     1.7 - * SourceMod
     1.8 - * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     1.9 - * =============================================================================
    1.10 - *
    1.11 - * This program is free software; you can redistribute it and/or modify it under
    1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 - * Free Software Foundation.
    1.14 - * 
    1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 - * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 - * details.
    1.19 - *
    1.20 - * You should have received a copy of the GNU General Public License along with
    1.21 - * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 - *
    1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 - * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 - * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 - * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 - * or <http://www.sourcemod.net/license.php>.
    1.31 - *
    1.32 - * Version: $Id$
    1.33 - */
    1.34 -
    1.35 -#include "HandleSys.h"
    1.36 -#include "TimerSys.h"
    1.37 -#include "PluginSys.h"
    1.38 -#include "Logger.h"
    1.39 -
    1.40 -#define TIMER_HNDL_CLOSE	(1<<9)
    1.41 -
    1.42 -HandleType_t g_TimerType;
    1.43 -
    1.44 -struct TimerInfo 
    1.45 -{
    1.46 -	ITimer *Timer;
    1.47 -	IPluginFunction *Hook;
    1.48 -	IPluginContext *pContext;
    1.49 -	Handle_t TimerHandle;
    1.50 -	int UserData;
    1.51 -	int Flags;
    1.52 -};
    1.53 -
    1.54 -class TimerNatives :
    1.55 -	public SMGlobalClass,
    1.56 -	public IHandleTypeDispatch,
    1.57 -	public ITimedEvent
    1.58 -{
    1.59 -public:
    1.60 -	~TimerNatives();
    1.61 -public: //ITimedEvent
    1.62 -	ResultType OnTimer(ITimer *pTimer, void *pData);
    1.63 -	void OnTimerEnd(ITimer *pTimer, void *pData);
    1.64 -public: //IHandleTypeDispatch
    1.65 -	void OnHandleDestroy(HandleType_t type, void *object);
    1.66 -public: //SMGlobalClass
    1.67 -	void OnSourceModAllInitialized();
    1.68 -	void OnSourceModShutdown();
    1.69 -public:
    1.70 -	TimerInfo *CreateTimerInfo();
    1.71 -	void DeleteTimerInfo(TimerInfo *pInfo);
    1.72 -private:
    1.73 -	CStack<TimerInfo *> m_FreeTimers;
    1.74 -};
    1.75 -
    1.76 -TimerNatives::~TimerNatives()
    1.77 -{
    1.78 -	CStack<TimerInfo *>::iterator iter;
    1.79 -	for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
    1.80 -	{
    1.81 -		delete (*iter);
    1.82 -	}
    1.83 -	m_FreeTimers.popall();
    1.84 -}
    1.85 -
    1.86 -void TimerNatives::OnSourceModAllInitialized()
    1.87 -{
    1.88 -	HandleAccess sec;
    1.89 -
    1.90 -	g_HandleSys.InitAccessDefaults(NULL, &sec);
    1.91 -	sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
    1.92 -
    1.93 -	g_TimerType = g_HandleSys.CreateType("Timer", this, 0, NULL, &sec, g_pCoreIdent, NULL);
    1.94 -}
    1.95 -
    1.96 -void TimerNatives::OnSourceModShutdown()
    1.97 -{
    1.98 -	g_HandleSys.RemoveType(g_TimerType, g_pCoreIdent);
    1.99 -	g_TimerType = 0;
   1.100 -}
   1.101 -
   1.102 -void TimerNatives::OnHandleDestroy(HandleType_t type, void *object)
   1.103 -{
   1.104 -	TimerInfo *pTimer = reinterpret_cast<TimerInfo *>(object);
   1.105 -
   1.106 -	g_Timers.KillTimer(pTimer->Timer);
   1.107 -}
   1.108 -
   1.109 -TimerInfo *TimerNatives::CreateTimerInfo()
   1.110 -{
   1.111 -	TimerInfo *pInfo;
   1.112 -
   1.113 -	if (m_FreeTimers.empty())
   1.114 -	{
   1.115 -		pInfo = new TimerInfo;
   1.116 -	} else {
   1.117 -		pInfo = m_FreeTimers.front();
   1.118 -		m_FreeTimers.pop();
   1.119 -	}
   1.120 -
   1.121 -	return pInfo;
   1.122 -}
   1.123 -
   1.124 -void TimerNatives::DeleteTimerInfo(TimerInfo *pInfo)
   1.125 -{
   1.126 -	m_FreeTimers.push(pInfo);
   1.127 -}
   1.128 -
   1.129 -ResultType TimerNatives::OnTimer(ITimer *pTimer, void *pData)
   1.130 -{
   1.131 -	TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
   1.132 -	IPluginFunction *pFunc = pInfo->Hook;
   1.133 -	cell_t res = static_cast<ResultType>(Pl_Continue);
   1.134 -
   1.135 -	pFunc->PushCell(pInfo->TimerHandle);
   1.136 -	pFunc->PushCell(pInfo->UserData);
   1.137 -	pFunc->Execute(&res);
   1.138 -
   1.139 -	return static_cast<ResultType>(res);
   1.140 -}
   1.141 -
   1.142 -void TimerNatives::OnTimerEnd(ITimer *pTimer, void *pData)
   1.143 -{
   1.144 -	HandleSecurity sec;
   1.145 -	HandleError herr;
   1.146 -	TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
   1.147 -	Handle_t usrhndl = static_cast<Handle_t>(pInfo->UserData);
   1.148 -
   1.149 -	sec.pOwner = pInfo->pContext->GetIdentity();
   1.150 -	sec.pIdentity = g_pCoreIdent;
   1.151 -
   1.152 -	if (pInfo->Flags & TIMER_HNDL_CLOSE)
   1.153 -	{
   1.154 -		if ((herr=g_HandleSys.FreeHandle(usrhndl, &sec)) != HandleError_None)
   1.155 -		{
   1.156 -			g_Logger.LogError("Invalid data handle %x (error %d) passed during timer end", usrhndl, herr);
   1.157 -		}
   1.158 -	}
   1.159 -
   1.160 -	if ((herr=g_HandleSys.FreeHandle(pInfo->TimerHandle, &sec)) != HandleError_None)
   1.161 -	{
   1.162 -		g_Logger.LogError("Invalid timer handle %x (error %d) on plugin %s during timer end", 
   1.163 -							pInfo->TimerHandle, 
   1.164 -							herr, 
   1.165 -							g_PluginSys.FindPluginByContext(pInfo->pContext->GetContext())->GetFilename());
   1.166 -		return;
   1.167 -	}
   1.168 -	DeleteTimerInfo(pInfo);
   1.169 -}
   1.170 -
   1.171 -/*******************************
   1.172 -*                              *
   1.173 -* TIMER NATIVE IMPLEMENTATIONS *
   1.174 -*                              *
   1.175 -********************************/
   1.176 -
   1.177 -static TimerNatives s_TimerNatives;
   1.178 -
   1.179 -static cell_t smn_CreateTimer(IPluginContext *pCtx, const cell_t *params)
   1.180 -{
   1.181 -	IPluginFunction *pFunc;
   1.182 -	TimerInfo *pInfo;
   1.183 -	ITimer *pTimer;
   1.184 -	Handle_t hndl;
   1.185 -	int flags = params[4];
   1.186 -
   1.187 -	pFunc = pCtx->GetFunctionById(params[2]);
   1.188 -	if (!pFunc)
   1.189 -	{
   1.190 -		return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
   1.191 -	}
   1.192 -
   1.193 -	pInfo = s_TimerNatives.CreateTimerInfo();
   1.194 -	pTimer = g_Timers.CreateTimer(&s_TimerNatives, sp_ctof(params[1]), pInfo, flags);
   1.195 -
   1.196 -	if (!pTimer)
   1.197 -	{
   1.198 -		s_TimerNatives.DeleteTimerInfo(pInfo);
   1.199 -		return 0;
   1.200 -	}
   1.201 -
   1.202 -	hndl = g_HandleSys.CreateHandle(g_TimerType, pInfo, pCtx->GetIdentity(), g_pCoreIdent, NULL);
   1.203 -
   1.204 -	pInfo->UserData = params[3];
   1.205 -	pInfo->Flags = flags;
   1.206 -	pInfo->TimerHandle = hndl;
   1.207 -	pInfo->Hook = pFunc;
   1.208 -	pInfo->Timer = pTimer;
   1.209 -	pInfo->pContext = pCtx;
   1.210 -
   1.211 -	return hndl;
   1.212 -}
   1.213 -
   1.214 -static cell_t smn_KillTimer(IPluginContext *pCtx, const cell_t *params)
   1.215 -{
   1.216 -	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.217 -	HandleError herr;
   1.218 -	HandleSecurity sec;
   1.219 -	TimerInfo *pInfo;
   1.220 -
   1.221 -	sec.pOwner = pCtx->GetIdentity();
   1.222 -	sec.pIdentity = g_pCoreIdent;
   1.223 -
   1.224 -	if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
   1.225 -		!= HandleError_None)
   1.226 -	{
   1.227 -		return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
   1.228 -	}
   1.229 -
   1.230 -	g_Timers.KillTimer(pInfo->Timer);
   1.231 -
   1.232 -	if (params[2] && !(pInfo->Flags & TIMER_HNDL_CLOSE))
   1.233 -	{
   1.234 -		sec.pOwner = pInfo->pContext->GetIdentity();
   1.235 -		sec.pIdentity = g_pCoreIdent;
   1.236 -
   1.237 -		if ((herr=g_HandleSys.FreeHandle(static_cast<Handle_t>(pInfo->UserData), &sec)) != HandleError_None)
   1.238 -		{
   1.239 -			return pCtx->ThrowNativeError("Invalid data handle %x (error %d)", hndl, herr);
   1.240 -		}
   1.241 -	}
   1.242 -
   1.243 -	return 1;
   1.244 -}
   1.245 -
   1.246 -static cell_t smn_TriggerTimer(IPluginContext *pCtx, const cell_t *params)
   1.247 -{
   1.248 -	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.249 -	HandleError herr;
   1.250 -	HandleSecurity sec;
   1.251 -	TimerInfo *pInfo;
   1.252 -
   1.253 -	sec.pOwner = pCtx->GetIdentity();
   1.254 -	sec.pIdentity = g_pCoreIdent;
   1.255 -
   1.256 -	if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
   1.257 -		!= HandleError_None)
   1.258 -	{
   1.259 -		return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
   1.260 -	}
   1.261 -
   1.262 -	g_Timers.FireTimerOnce(pInfo->Timer, params[2] ? true : false);
   1.263 -
   1.264 -	return 1;
   1.265 -}
   1.266 -
   1.267 -static cell_t smn_GetTickedTime(IPluginContext *pContext, const cell_t *params)
   1.268 -{
   1.269 -	return sp_ftoc(*g_pUniversalTime);
   1.270 -}
   1.271 -
   1.272 -static cell_t smn_GetMapTimeLeft(IPluginContext *pContext, const cell_t *params)
   1.273 -{
   1.274 -	float time_left;
   1.275 -	if (!g_Timers.GetMapTimeLeft(&time_left))
   1.276 -	{
   1.277 -		return 0;
   1.278 -	}
   1.279 -
   1.280 -	cell_t *addr;
   1.281 -	pContext->LocalToPhysAddr(params[1], &addr);
   1.282 -	*addr = (int)time_left;
   1.283 -
   1.284 -	return 1;
   1.285 -}
   1.286 -
   1.287 -static cell_t smn_GetMapTimeLimit(IPluginContext *pContext, const cell_t *params)
   1.288 -{
   1.289 -	IMapTimer *pMapTimer = g_Timers.GetMapTimer();
   1.290 -
   1.291 -	if (!pMapTimer)
   1.292 -	{
   1.293 -		return 0;
   1.294 -	}
   1.295 -
   1.296 -	cell_t *addr;
   1.297 -	pContext->LocalToPhysAddr(params[1], &addr);
   1.298 -
   1.299 -	*addr = pMapTimer->GetMapTimeLimit();
   1.300 -
   1.301 -	return 1;
   1.302 -}
   1.303 -
   1.304 -static cell_t smn_ExtendMapTimeLimit(IPluginContext *pContext, const cell_t *params)
   1.305 -{
   1.306 -	IMapTimer *pMapTimer = g_Timers.GetMapTimer();
   1.307 -
   1.308 -	if (!pMapTimer)
   1.309 -	{
   1.310 -		return 0;
   1.311 -	}
   1.312 -
   1.313 -	pMapTimer->ExtendMapTimeLimit(params[1]);
   1.314 -
   1.315 -	return 1;
   1.316 -}
   1.317 -
   1.318 -static cell_t smn_IsServerProcessing(IPluginContext *pContext, const cell_t *params)
   1.319 -{
   1.320 -	return (gpGlobals->frametime > 0.0f) ? 1 : 0;
   1.321 -}
   1.322 -
   1.323 -REGISTER_NATIVES(timernatives)
   1.324 -{
   1.325 -	{"CreateTimer",				smn_CreateTimer},
   1.326 -	{"KillTimer",				smn_KillTimer},
   1.327 -	{"TriggerTimer",			smn_TriggerTimer},
   1.328 -	{"GetTickedTime",			smn_GetTickedTime},
   1.329 -	{"GetMapTimeLeft",			smn_GetMapTimeLeft},
   1.330 -	{"GetMapTimeLimit",			smn_GetMapTimeLimit},
   1.331 -	{"ExtendMapTimeLimit",		smn_ExtendMapTimeLimit},
   1.332 -	{"IsServerProcessing",		smn_IsServerProcessing},
   1.333 -	{NULL,						NULL}
   1.334 -};
   1.335 -