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