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