core/smn_timers.cpp
author Borja Ferrer <faluco@alliedmods.net>
Sat Feb 23 11:40:45 2008 +0000 (2008-02-23)
changeset 1892 e1bc71f50865
parent 1674 cd474b45a045
child 1894 e6efc8ffa47a
permissions -rw-r--r--
improved timer error message (amb1474)
faluco@612
     1
/**
ds@653
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
dvander@1231
     5
 * Copyright (C) 2004-2007 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@612
    36
faluco@612
    37
#define TIMER_HNDL_CLOSE	(1<<9)
faluco@612
    38
faluco@612
    39
HandleType_t g_TimerType;
faluco@612
    40
faluco@612
    41
struct TimerInfo 
faluco@612
    42
{
faluco@612
    43
	ITimer *Timer;
faluco@612
    44
	IPluginFunction *Hook;
faluco@612
    45
	IPluginContext *pContext;
faluco@612
    46
	Handle_t TimerHandle;
faluco@612
    47
	int UserData;
faluco@612
    48
	int Flags;
faluco@612
    49
};
faluco@612
    50
faluco@612
    51
class TimerNatives :
faluco@612
    52
	public SMGlobalClass,
faluco@612
    53
	public IHandleTypeDispatch,
faluco@612
    54
	public ITimedEvent
faluco@612
    55
{
faluco@641
    56
public:
faluco@641
    57
	~TimerNatives();
faluco@612
    58
public: //ITimedEvent
faluco@612
    59
	ResultType OnTimer(ITimer *pTimer, void *pData);
faluco@612
    60
	void OnTimerEnd(ITimer *pTimer, void *pData);
faluco@612
    61
public: //IHandleTypeDispatch
faluco@612
    62
	void OnHandleDestroy(HandleType_t type, void *object);
faluco@612
    63
public: //SMGlobalClass
faluco@612
    64
	void OnSourceModAllInitialized();
faluco@612
    65
	void OnSourceModShutdown();
faluco@612
    66
public:
faluco@612
    67
	TimerInfo *CreateTimerInfo();
faluco@612
    68
	void DeleteTimerInfo(TimerInfo *pInfo);
faluco@612
    69
private:
faluco@612
    70
	CStack<TimerInfo *> m_FreeTimers;
faluco@612
    71
};
faluco@612
    72
faluco@641
    73
TimerNatives::~TimerNatives()
faluco@641
    74
{
faluco@641
    75
	CStack<TimerInfo *>::iterator iter;
faluco@641
    76
	for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
faluco@641
    77
	{
faluco@641
    78
		delete (*iter);
faluco@641
    79
	}
faluco@641
    80
	m_FreeTimers.popall();
faluco@641
    81
}
faluco@641
    82
faluco@612
    83
void TimerNatives::OnSourceModAllInitialized()
faluco@612
    84
{
faluco@612
    85
	HandleAccess sec;
faluco@1397
    86
faluco@1397
    87
	g_HandleSys.InitAccessDefaults(NULL, &sec);
faluco@1397
    88
	sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
faluco@612
    89
faluco@612
    90
	g_TimerType = g_HandleSys.CreateType("Timer", this, 0, NULL, &sec, g_pCoreIdent, NULL);
faluco@612
    91
}
faluco@612
    92
faluco@612
    93
void TimerNatives::OnSourceModShutdown()
faluco@612
    94
{
faluco@612
    95
	g_HandleSys.RemoveType(g_TimerType, g_pCoreIdent);
faluco@612
    96
	g_TimerType = 0;
faluco@612
    97
}
faluco@612
    98
faluco@612
    99
void TimerNatives::OnHandleDestroy(HandleType_t type, void *object)
faluco@612
   100
{
faluco@612
   101
	TimerInfo *pTimer = reinterpret_cast<TimerInfo *>(object);
faluco@612
   102
faluco@612
   103
	g_Timers.KillTimer(pTimer->Timer);
faluco@612
   104
}
faluco@612
   105
faluco@612
   106
TimerInfo *TimerNatives::CreateTimerInfo()
faluco@612
   107
{
faluco@612
   108
	TimerInfo *pInfo;
faluco@612
   109
faluco@612
   110
	if (m_FreeTimers.empty())
faluco@612
   111
	{
faluco@612
   112
		pInfo = new TimerInfo;
faluco@612
   113
	} else {
faluco@612
   114
		pInfo = m_FreeTimers.front();
faluco@612
   115
		m_FreeTimers.pop();
faluco@612
   116
	}
faluco@612
   117
faluco@612
   118
	return pInfo;
faluco@612
   119
}
faluco@612
   120
faluco@612
   121
void TimerNatives::DeleteTimerInfo(TimerInfo *pInfo)
faluco@612
   122
{
faluco@612
   123
	m_FreeTimers.push(pInfo);
faluco@612
   124
}
faluco@612
   125
faluco@612
   126
ResultType TimerNatives::OnTimer(ITimer *pTimer, void *pData)
faluco@612
   127
{
faluco@612
   128
	TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
faluco@612
   129
	IPluginFunction *pFunc = pInfo->Hook;
faluco@612
   130
	cell_t res = static_cast<ResultType>(Pl_Continue);
faluco@612
   131
faluco@612
   132
	pFunc->PushCell(pInfo->TimerHandle);
faluco@612
   133
	pFunc->PushCell(pInfo->UserData);
faluco@612
   134
	pFunc->Execute(&res);
faluco@612
   135
faluco@612
   136
	return static_cast<ResultType>(res);
faluco@612
   137
}
faluco@612
   138
faluco@612
   139
void TimerNatives::OnTimerEnd(ITimer *pTimer, void *pData)
faluco@612
   140
{
faluco@612
   141
	HandleSecurity sec;
faluco@612
   142
	HandleError herr;
faluco@612
   143
	TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
faluco@612
   144
	Handle_t usrhndl = static_cast<Handle_t>(pInfo->UserData);
faluco@612
   145
faluco@612
   146
	sec.pOwner = pInfo->pContext->GetIdentity();
faluco@612
   147
	sec.pIdentity = g_pCoreIdent;
faluco@612
   148
faluco@612
   149
	if (pInfo->Flags & TIMER_HNDL_CLOSE)
faluco@612
   150
	{
faluco@612
   151
		if ((herr=g_HandleSys.FreeHandle(usrhndl, &sec)) != HandleError_None)
faluco@612
   152
		{
faluco@1892
   153
			g_Logger.LogError("Invalid data handle %x (error %d) passed during timer end on plugin %s", 
faluco@1892
   154
								usrhndl, 
faluco@1892
   155
								herr, 
faluco@1892
   156
								g_PluginSys.FindPluginByContext(pInfo->pContext->GetContext())->GetFilename());
faluco@612
   157
		}
faluco@612
   158
	}
faluco@612
   159
faluco@1674
   160
	if ((herr=g_HandleSys.FreeHandle(pInfo->TimerHandle, &sec)) != HandleError_None)
faluco@1674
   161
	{
faluco@1674
   162
		g_Logger.LogError("Invalid timer handle %x (error %d) on plugin %s during timer end", 
faluco@1674
   163
							pInfo->TimerHandle, 
faluco@1674
   164
							herr, 
faluco@1674
   165
							g_PluginSys.FindPluginByContext(pInfo->pContext->GetContext())->GetFilename());
faluco@1674
   166
		return;
faluco@1674
   167
	}
faluco@613
   168
	DeleteTimerInfo(pInfo);
faluco@612
   169
}
faluco@612
   170
faluco@612
   171
/*******************************
faluco@612
   172
*                              *
faluco@612
   173
* TIMER NATIVE IMPLEMENTATIONS *
faluco@612
   174
*                              *
faluco@612
   175
********************************/
faluco@612
   176
faluco@612
   177
static TimerNatives s_TimerNatives;
faluco@612
   178
faluco@612
   179
static cell_t smn_CreateTimer(IPluginContext *pCtx, const cell_t *params)
faluco@612
   180
{
faluco@612
   181
	IPluginFunction *pFunc;
faluco@612
   182
	TimerInfo *pInfo;
faluco@612
   183
	ITimer *pTimer;
faluco@612
   184
	Handle_t hndl;
faluco@612
   185
	int flags = params[4];
faluco@612
   186
faluco@612
   187
	pFunc = pCtx->GetFunctionById(params[2]);
faluco@612
   188
	if (!pFunc)
faluco@612
   189
	{
faluco@612
   190
		return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
faluco@612
   191
	}
faluco@612
   192
faluco@612
   193
	pInfo = s_TimerNatives.CreateTimerInfo();
faluco@612
   194
	pTimer = g_Timers.CreateTimer(&s_TimerNatives, sp_ctof(params[1]), pInfo, flags);
dvander@1409
   195
dvander@1409
   196
	if (!pTimer)
dvander@1409
   197
	{
dvander@1409
   198
		s_TimerNatives.DeleteTimerInfo(pInfo);
dvander@1409
   199
		return 0;
dvander@1409
   200
	}
dvander@1409
   201
faluco@612
   202
	hndl = g_HandleSys.CreateHandle(g_TimerType, pInfo, pCtx->GetIdentity(), g_pCoreIdent, NULL);
faluco@612
   203
faluco@612
   204
	pInfo->UserData = params[3];
faluco@612
   205
	pInfo->Flags = flags;
faluco@612
   206
	pInfo->TimerHandle = hndl;
faluco@612
   207
	pInfo->Hook = pFunc;
faluco@612
   208
	pInfo->Timer = pTimer;
faluco@612
   209
	pInfo->pContext = pCtx;
faluco@612
   210
faluco@612
   211
	return hndl;
faluco@612
   212
}
faluco@612
   213
faluco@612
   214
static cell_t smn_KillTimer(IPluginContext *pCtx, const cell_t *params)
faluco@612
   215
{
faluco@612
   216
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@612
   217
	HandleError herr;
faluco@612
   218
	HandleSecurity sec;
faluco@612
   219
	TimerInfo *pInfo;
faluco@612
   220
dvander@1008
   221
	sec.pOwner = pCtx->GetIdentity();
faluco@612
   222
	sec.pIdentity = g_pCoreIdent;
faluco@612
   223
faluco@612
   224
	if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
faluco@612
   225
		!= HandleError_None)
faluco@612
   226
	{
faluco@612
   227
		return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
faluco@612
   228
	}
faluco@612
   229
faluco@612
   230
	g_Timers.KillTimer(pInfo->Timer);
faluco@612
   231
faluco@612
   232
	if (params[2] && !(pInfo->Flags & TIMER_HNDL_CLOSE))
faluco@612
   233
	{
faluco@612
   234
		sec.pOwner = pInfo->pContext->GetIdentity();
faluco@612
   235
		sec.pIdentity = g_pCoreIdent;
faluco@612
   236
faluco@612
   237
		if ((herr=g_HandleSys.FreeHandle(static_cast<Handle_t>(pInfo->UserData), &sec)) != HandleError_None)
faluco@612
   238
		{
faluco@612
   239
			return pCtx->ThrowNativeError("Invalid data handle %x (error %d)", hndl, herr);
faluco@612
   240
		}
faluco@612
   241
	}
faluco@612
   242
faluco@612
   243
	return 1;
faluco@612
   244
}
faluco@612
   245
faluco@612
   246
static cell_t smn_TriggerTimer(IPluginContext *pCtx, const cell_t *params)
faluco@612
   247
{
faluco@612
   248
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@612
   249
	HandleError herr;
faluco@612
   250
	HandleSecurity sec;
faluco@612
   251
	TimerInfo *pInfo;
faluco@612
   252
faluco@1397
   253
	sec.pOwner = pCtx->GetIdentity();
faluco@612
   254
	sec.pIdentity = g_pCoreIdent;
faluco@612
   255
faluco@612
   256
	if ((herr=g_HandleSys.ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
faluco@612
   257
		!= HandleError_None)
faluco@612
   258
	{
faluco@612
   259
		return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
faluco@612
   260
	}
faluco@612
   261
faluco@612
   262
	g_Timers.FireTimerOnce(pInfo->Timer, params[2] ? true : false);
faluco@612
   263
faluco@612
   264
	return 1;
faluco@612
   265
}
faluco@612
   266
dvander@822
   267
static cell_t smn_GetTickedTime(IPluginContext *pContext, const cell_t *params)
dvander@822
   268
{
dvander@1412
   269
	return sp_ftoc(*g_pUniversalTime);
dvander@822
   270
}
dvander@822
   271
dvander@1415
   272
static cell_t smn_GetMapTimeLeft(IPluginContext *pContext, const cell_t *params)
dvander@1415
   273
{
dvander@1459
   274
	float time_left;
dvander@1459
   275
	if (!g_Timers.GetMapTimeLeft(&time_left))
dvander@1415
   276
	{
dvander@1462
   277
		return 0;
dvander@1415
   278
	}
dvander@1415
   279
dvander@1462
   280
	cell_t *addr;
dvander@1462
   281
	pContext->LocalToPhysAddr(params[1], &addr);
dvander@1462
   282
	*addr = (int)time_left;
dvander@1415
   283
dvander@1462
   284
	return 1;
dvander@1415
   285
}
dvander@1415
   286
dvander@1415
   287
static cell_t smn_GetMapTimeLimit(IPluginContext *pContext, const cell_t *params)
dvander@1415
   288
{
dvander@1415
   289
	IMapTimer *pMapTimer = g_Timers.GetMapTimer();
dvander@1415
   290
dvander@1415
   291
	if (!pMapTimer)
dvander@1415
   292
	{
dvander@1415
   293
		return 0;
dvander@1415
   294
	}
dvander@1415
   295
dvander@1415
   296
	cell_t *addr;
dvander@1415
   297
	pContext->LocalToPhysAddr(params[1], &addr);
dvander@1415
   298
dvander@1415
   299
	*addr = pMapTimer->GetMapTimeLimit();
dvander@1415
   300
dvander@1415
   301
	return 1;
dvander@1415
   302
}
dvander@1415
   303
dvander@1415
   304
static cell_t smn_ExtendMapTimeLimit(IPluginContext *pContext, const cell_t *params)
dvander@1415
   305
{
dvander@1415
   306
	IMapTimer *pMapTimer = g_Timers.GetMapTimer();
dvander@1415
   307
dvander@1415
   308
	if (!pMapTimer)
dvander@1415
   309
	{
dvander@1415
   310
		return 0;
dvander@1415
   311
	}
dvander@1415
   312
dvander@1415
   313
	pMapTimer->ExtendMapTimeLimit(params[1]);
dvander@1415
   314
dvander@1415
   315
	return 1;
dvander@1415
   316
}
dvander@1415
   317
dvander@1461
   318
static cell_t smn_IsServerProcessing(IPluginContext *pContext, const cell_t *params)
dvander@1459
   319
{
dvander@1459
   320
	return (gpGlobals->frametime > 0.0f) ? 1 : 0;
dvander@1459
   321
}
dvander@1459
   322
faluco@612
   323
REGISTER_NATIVES(timernatives)
faluco@612
   324
{
faluco@612
   325
	{"CreateTimer",				smn_CreateTimer},
faluco@612
   326
	{"KillTimer",				smn_KillTimer},
faluco@612
   327
	{"TriggerTimer",			smn_TriggerTimer},
dvander@822
   328
	{"GetTickedTime",			smn_GetTickedTime},
dvander@1415
   329
	{"GetMapTimeLeft",			smn_GetMapTimeLeft},
dvander@1415
   330
	{"GetMapTimeLimit",			smn_GetMapTimeLimit},
dvander@1415
   331
	{"ExtendMapTimeLimit",		smn_ExtendMapTimeLimit},
dvander@1461
   332
	{"IsServerProcessing",		smn_IsServerProcessing},
faluco@612
   333
	{NULL,						NULL}
faluco@612
   334
};
dvander@1462
   335