core/smn_timers.cpp
author David Anderson <dvander@alliedmods.net>
Thu Mar 05 02:06:59 2009 -0500 (2009-03-05)
changeset 2653 0ae7d8ab1de7
parent 2639 021fbd7a8cf6
permissions -rw-r--r--
Removed -dev tag for release.
faluco@612
     1
/**
ds@653
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
ds@1947
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
ds@653
     7
 *
ds@1330
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1330
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330
    10
 * Free Software Foundation.
dvander@1231
    11
 * 
ds@1330
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1330
    15
 * details.
dvander@1231
    16
 *
ds@1330
    17
 * You should have received a copy of the GNU General Public License along with
ds@1330
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1231
    19
 *
ds@1330
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1330
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1330
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1330
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330
    27
 * or <http://www.sourcemod.net/license.php>.
ds@653
    28
 *
ds@653
    29
 * Version: $Id$
ds@653
    30
 */
faluco@612
    31
faluco@612
    32
#include "HandleSys.h"
faluco@612
    33
#include "TimerSys.h"
faluco@1674
    34
#include "PluginSys.h"
faluco@612
    35
#include "Logger.h"
faluco@1894
    36
#include "DebugReporter.h"
faluco@612
    37
fyrenmoo@2639
    38
#define TIMER_DATA_HNDL_CLOSE	(1<<9)
fyrenmoo@2639
    39
#define TIMER_HNDL_CLOSE		(1<<9)
faluco@612
    40
faluco@612
    41
HandleType_t g_TimerType;
faluco@612
    42
faluco@612
    43
struct TimerInfo 
faluco@612
    44
{
faluco@612
    45
	ITimer *Timer;
faluco@612
    46
	IPluginFunction *Hook;
faluco@612
    47
	IPluginContext *pContext;
faluco@612
    48
	Handle_t TimerHandle;
faluco@612
    49
	int UserData;
faluco@612
    50
	int Flags;
faluco@612
    51
};
faluco@612
    52
faluco@612
    53
class TimerNatives :
faluco@612
    54
	public SMGlobalClass,
faluco@612
    55
	public IHandleTypeDispatch,
faluco@612
    56
	public ITimedEvent
faluco@612
    57
{
faluco@641
    58
public:
faluco@641
    59
	~TimerNatives();
faluco@612
    60
public: //ITimedEvent
faluco@612
    61
	ResultType OnTimer(ITimer *pTimer, void *pData);
faluco@612
    62
	void OnTimerEnd(ITimer *pTimer, void *pData);
faluco@612
    63
public: //IHandleTypeDispatch
faluco@612
    64
	void OnHandleDestroy(HandleType_t type, void *object);
faluco@612
    65
public: //SMGlobalClass
faluco@612
    66
	void OnSourceModAllInitialized();
faluco@612
    67
	void OnSourceModShutdown();
faluco@612
    68
public:
faluco@612
    69
	TimerInfo *CreateTimerInfo();
faluco@612
    70
	void DeleteTimerInfo(TimerInfo *pInfo);
faluco@612
    71
private:
faluco@612
    72
	CStack<TimerInfo *> m_FreeTimers;
faluco@612
    73
};
faluco@612
    74
faluco@641
    75
TimerNatives::~TimerNatives()
faluco@641
    76
{
faluco@641
    77
	CStack<TimerInfo *>::iterator iter;
faluco@641
    78
	for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
faluco@641
    79
	{
faluco@641
    80
		delete (*iter);
faluco@641
    81
	}
faluco@641
    82
	m_FreeTimers.popall();
faluco@641
    83
}
faluco@641
    84
faluco@612
    85
void TimerNatives::OnSourceModAllInitialized()
faluco@612
    86
{
faluco@612
    87
	HandleAccess sec;
faluco@1397
    88
faluco@1397
    89
	g_HandleSys.InitAccessDefaults(NULL, &sec);
faluco@1397
    90
	sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
faluco@612
    91
faluco@612
    92
	g_TimerType = g_HandleSys.CreateType("Timer", this, 0, NULL, &sec, g_pCoreIdent, NULL);
faluco@612
    93
}
faluco@612
    94
faluco@612
    95
void TimerNatives::OnSourceModShutdown()
faluco@612
    96
{
faluco@612
    97
	g_HandleSys.RemoveType(g_TimerType, g_pCoreIdent);
faluco@612
    98
	g_TimerType = 0;
faluco@612
    99
}
faluco@612
   100
faluco@612
   101
void TimerNatives::OnHandleDestroy(HandleType_t type, void *object)
faluco@612
   102
{
faluco@612
   103
	TimerInfo *pTimer = reinterpret_cast<TimerInfo *>(object);
faluco@612
   104
faluco@612
   105
	g_Timers.KillTimer(pTimer->Timer);
faluco@612
   106
}
faluco@612
   107
faluco@612
   108
TimerInfo *TimerNatives::CreateTimerInfo()
faluco@612
   109
{
faluco@612
   110
	TimerInfo *pInfo;
faluco@612
   111
faluco@612
   112
	if (m_FreeTimers.empty())
faluco@612
   113
	{
faluco@612
   114
		pInfo = new TimerInfo;
faluco@612
   115
	} else {
faluco@612
   116
		pInfo = m_FreeTimers.front();
faluco@612
   117
		m_FreeTimers.pop();
faluco@612
   118
	}
faluco@612
   119
faluco@612
   120
	return pInfo;
faluco@612
   121
}
faluco@612
   122
faluco@612
   123
void TimerNatives::DeleteTimerInfo(TimerInfo *pInfo)
faluco@612
   124
{
faluco@612
   125
	m_FreeTimers.push(pInfo);
faluco@612
   126
}
faluco@612
   127
faluco@612
   128
ResultType TimerNatives::OnTimer(ITimer *pTimer, void *pData)
faluco@612
   129
{
faluco@612
   130
	TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
faluco@612
   131
	IPluginFunction *pFunc = pInfo->Hook;
faluco@612
   132
	cell_t res = static_cast<ResultType>(Pl_Continue);
faluco@612
   133
faluco@612
   134
	pFunc->PushCell(pInfo->TimerHandle);
faluco@612
   135
	pFunc->PushCell(pInfo->UserData);
faluco@612
   136
	pFunc->Execute(&res);
faluco@612
   137
faluco@612
   138
	return static_cast<ResultType>(res);
faluco@612
   139
}
faluco@612
   140
faluco@612
   141
void TimerNatives::OnTimerEnd(ITimer *pTimer, void *pData)
faluco@612
   142
{
faluco@612
   143
	HandleSecurity sec;
faluco@612
   144
	HandleError herr;
faluco@612
   145
	TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
faluco@612
   146
	Handle_t usrhndl = static_cast<Handle_t>(pInfo->UserData);
faluco@612
   147
faluco@612
   148
	sec.pOwner = pInfo->pContext->GetIdentity();
faluco@612
   149
	sec.pIdentity = g_pCoreIdent;
faluco@612
   150
fyrenmoo@2639
   151
	if (pInfo->Flags & TIMER_DATA_HNDL_CLOSE)
faluco@612
   152
	{
faluco@612
   153
		if ((herr=g_HandleSys.FreeHandle(usrhndl, &sec)) != HandleError_None)
faluco@612
   154
		{
faluco@1894
   155
			g_DbgReporter.GenerateError(pInfo->pContext,
faluco@1894
   156
										pInfo->Hook->GetFunctionID(), 
faluco@1894
   157
										SP_ERROR_NATIVE, 
fyrenmoo@2639
   158
										"Invalid data handle %x (error %d) passed during timer end with TIMER_DATA_HNDL_CLOSE",
faluco@1894
   159
										usrhndl, 
faluco@1894
   160
										herr);
faluco@612
   161
		}
faluco@612
   162
	}
faluco@612
   163
dvander@2379
   164
	if (pInfo->TimerHandle != BAD_HANDLE)
faluco@1674
   165
	{
dvander@2379
   166
		if ((herr=g_HandleSys.FreeHandle(pInfo->TimerHandle, &sec)) != HandleError_None)
dvander@2379
   167
		{
dvander@2379
   168
			g_DbgReporter.GenerateError(pInfo->pContext, 
dvander@2379
   169
				pInfo->Hook->GetFunctionID(), 
dvander@2379
   170
				SP_ERROR_NATIVE, 
dvander@2379
   171
				"Invalid timer handle %x (error %d) during timer end, displayed function is timer callback, not the stack trace", 
dvander@2379
   172
				pInfo->TimerHandle, 
dvander@2379
   173
				herr);
dvander@2379
   174
		}
faluco@1674
   175
	}
dvander@2379
   176
faluco@613
   177
	DeleteTimerInfo(pInfo);
faluco@612
   178
}
faluco@612
   179
faluco@612
   180
/*******************************
faluco@612
   181
*                              *
faluco@612
   182
* TIMER NATIVE IMPLEMENTATIONS *
faluco@612
   183
*                              *
faluco@612
   184
********************************/
faluco@612
   185
faluco@612
   186
static TimerNatives s_TimerNatives;
faluco@612
   187
faluco@612
   188
static cell_t smn_CreateTimer(IPluginContext *pCtx, const cell_t *params)
faluco@612
   189
{
faluco@612
   190
	IPluginFunction *pFunc;
faluco@612
   191
	TimerInfo *pInfo;
faluco@612
   192
	ITimer *pTimer;
faluco@612
   193
	Handle_t hndl;
faluco@612
   194
	int flags = params[4];
faluco@612
   195
faluco@612
   196
	pFunc = pCtx->GetFunctionById(params[2]);
faluco@612
   197
	if (!pFunc)
faluco@612
   198
	{
faluco@612
   199
		return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
faluco@612
   200
	}
faluco@612
   201
faluco@612
   202
	pInfo = s_TimerNatives.CreateTimerInfo();
faluco@612
   203
	pTimer = g_Timers.CreateTimer(&s_TimerNatives, sp_ctof(params[1]), pInfo, flags);
dvander@1409
   204
dvander@1409
   205
	if (!pTimer)
dvander@1409
   206
	{
dvander@1409
   207
		s_TimerNatives.DeleteTimerInfo(pInfo);
dvander@1409
   208
		return 0;
dvander@1409
   209
	}
dvander@1409
   210
faluco@612
   211
	hndl = g_HandleSys.CreateHandle(g_TimerType, pInfo, pCtx->GetIdentity(), g_pCoreIdent, NULL);
faluco@612
   212
dvander@2379
   213
	/* If we can't get a handle, the timer isn't refcounted against the plugin and 
dvander@2379
   214
	 * we need to bail out to prevent a crash.
dvander@2379
   215
	 */
dvander@2379
   216
	if (hndl == BAD_HANDLE)
dvander@2379
   217
	{
dvander@2379
   218
		/* Free this for completeness. */
fyrenmoo@2639
   219
		if (flags & TIMER_DATA_HNDL_CLOSE)
dvander@2379
   220
		{
dvander@2379
   221
			HandleSecurity sec(pCtx->GetIdentity(), g_pCoreIdent);
dvander@2379
   222
			g_HandleSys.FreeHandle(params[3], &sec);
dvander@2379
   223
		}
dvander@2379
   224
		/* Zero everything so there's no conflicts */
dvander@2379
   225
		memset(pInfo, 0, sizeof(TimerInfo));
dvander@2379
   226
		g_Timers.KillTimer(pTimer);
dvander@2379
   227
dvander@2379
   228
		return pCtx->ThrowNativeError("Could not create timer, no more handles");
dvander@2379
   229
	}
dvander@2379
   230
faluco@612
   231
	pInfo->UserData = params[3];
faluco@612
   232
	pInfo->Flags = flags;
faluco@612
   233
	pInfo->TimerHandle = hndl;
faluco@612
   234
	pInfo->Hook = pFunc;
faluco@612
   235
	pInfo->Timer = pTimer;
faluco@612
   236
	pInfo->pContext = pCtx;
faluco@612
   237
faluco@612
   238
	return hndl;
faluco@612
   239
}
faluco@612
   240
faluco@612
   241
static cell_t smn_KillTimer(IPluginContext *pCtx, const cell_t *params)
faluco@612
   242
{
faluco@612
   243
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@612
   244
	HandleError herr;
faluco@612
   245
	HandleSecurity sec;
faluco@612
   246
	TimerInfo *pInfo;
faluco@612
   247
dvander@1008
   248
	sec.pOwner = pCtx->GetIdentity();
faluco@612
   249
	sec.pIdentity = g_pCoreIdent;
faluco@612
   250
faluco@612
   251
	if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
faluco@612
   252
		!= HandleError_None)
faluco@612
   253
	{
faluco@612
   254
		return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
faluco@612
   255
	}
faluco@612
   256
faluco@612
   257
	g_Timers.KillTimer(pInfo->Timer);
faluco@612
   258
fyrenmoo@2639
   259
	if (params[2] && !(pInfo->Flags & TIMER_DATA_HNDL_CLOSE))
faluco@612
   260
	{
faluco@612
   261
		sec.pOwner = pInfo->pContext->GetIdentity();
faluco@612
   262
		sec.pIdentity = g_pCoreIdent;
faluco@612
   263
faluco@612
   264
		if ((herr=g_HandleSys.FreeHandle(static_cast<Handle_t>(pInfo->UserData), &sec)) != HandleError_None)
faluco@612
   265
		{
fyrenmoo@2639
   266
			return pCtx->ThrowNativeError("Invalid data handle %x (error %d) on timer kill with TIMER_DATA_HNDL_CLOSE", hndl, herr);
faluco@612
   267
		}
faluco@612
   268
	}
faluco@612
   269
faluco@612
   270
	return 1;
faluco@612
   271
}
faluco@612
   272
faluco@612
   273
static cell_t smn_TriggerTimer(IPluginContext *pCtx, const cell_t *params)
faluco@612
   274
{
faluco@612
   275
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@612
   276
	HandleError herr;
faluco@612
   277
	HandleSecurity sec;
faluco@612
   278
	TimerInfo *pInfo;
faluco@612
   279
faluco@1397
   280
	sec.pOwner = pCtx->GetIdentity();
faluco@612
   281
	sec.pIdentity = g_pCoreIdent;
faluco@612
   282
faluco@612
   283
	if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
faluco@612
   284
		!= HandleError_None)
faluco@612
   285
	{
faluco@612
   286
		return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
faluco@612
   287
	}
faluco@612
   288
faluco@612
   289
	g_Timers.FireTimerOnce(pInfo->Timer, params[2] ? true : false);
faluco@612
   290
faluco@612
   291
	return 1;
faluco@612
   292
}
faluco@612
   293
dvander@822
   294
static cell_t smn_GetTickedTime(IPluginContext *pContext, const cell_t *params)
dvander@822
   295
{
dvander@1412
   296
	return sp_ftoc(*g_pUniversalTime);
dvander@822
   297
}
dvander@822
   298
dvander@1415
   299
static cell_t smn_GetMapTimeLeft(IPluginContext *pContext, const cell_t *params)
dvander@1415
   300
{
dvander@1459
   301
	float time_left;
dvander@1459
   302
	if (!g_Timers.GetMapTimeLeft(&time_left))
dvander@1415
   303
	{
dvander@1462
   304
		return 0;
dvander@1415
   305
	}
dvander@1415
   306
dvander@1462
   307
	cell_t *addr;
dvander@1462
   308
	pContext->LocalToPhysAddr(params[1], &addr);
dvander@1462
   309
	*addr = (int)time_left;
dvander@1415
   310
dvander@1462
   311
	return 1;
dvander@1415
   312
}
dvander@1415
   313
dvander@1415
   314
static cell_t smn_GetMapTimeLimit(IPluginContext *pContext, const cell_t *params)
dvander@1415
   315
{
dvander@1415
   316
	IMapTimer *pMapTimer = g_Timers.GetMapTimer();
dvander@1415
   317
dvander@1415
   318
	if (!pMapTimer)
dvander@1415
   319
	{
dvander@1415
   320
		return 0;
dvander@1415
   321
	}
dvander@1415
   322
dvander@1415
   323
	cell_t *addr;
dvander@1415
   324
	pContext->LocalToPhysAddr(params[1], &addr);
dvander@1415
   325
dvander@1415
   326
	*addr = pMapTimer->GetMapTimeLimit();
dvander@1415
   327
dvander@1415
   328
	return 1;
dvander@1415
   329
}
dvander@1415
   330
dvander@1415
   331
static cell_t smn_ExtendMapTimeLimit(IPluginContext *pContext, const cell_t *params)
dvander@1415
   332
{
dvander@1415
   333
	IMapTimer *pMapTimer = g_Timers.GetMapTimer();
dvander@1415
   334
dvander@1415
   335
	if (!pMapTimer)
dvander@1415
   336
	{
dvander@1415
   337
		return 0;
dvander@1415
   338
	}
dvander@1415
   339
dvander@1415
   340
	pMapTimer->ExtendMapTimeLimit(params[1]);
dvander@1415
   341
dvander@1415
   342
	return 1;
dvander@1415
   343
}
dvander@1415
   344
dvander@1461
   345
static cell_t smn_IsServerProcessing(IPluginContext *pContext, const cell_t *params)
dvander@1459
   346
{
dvander@1459
   347
	return (gpGlobals->frametime > 0.0f) ? 1 : 0;
dvander@1459
   348
}
dvander@1459
   349
dvander@2063
   350
static cell_t smn_GetTickInterval(IPluginContext *pContext, const cell_t *params)
dvander@2063
   351
{
dvander@2063
   352
	return sp_ftoc(gpGlobals->interval_per_tick);
dvander@2063
   353
}
dvander@2063
   354
faluco@612
   355
REGISTER_NATIVES(timernatives)
faluco@612
   356
{
faluco@612
   357
	{"CreateTimer",				smn_CreateTimer},
faluco@612
   358
	{"KillTimer",				smn_KillTimer},
faluco@612
   359
	{"TriggerTimer",			smn_TriggerTimer},
dvander@822
   360
	{"GetTickedTime",			smn_GetTickedTime},
dvander@1415
   361
	{"GetMapTimeLeft",			smn_GetMapTimeLeft},
dvander@1415
   362
	{"GetMapTimeLimit",			smn_GetMapTimeLimit},
dvander@1415
   363
	{"ExtendMapTimeLimit",		smn_ExtendMapTimeLimit},
dvander@1461
   364
	{"IsServerProcessing",		smn_IsServerProcessing},
dvander@2063
   365
	{"GetTickInterval",			smn_GetTickInterval},
faluco@612
   366
	{NULL,						NULL}
faluco@612
   367
};
dvander@2063
   368