core/logic/smn_timers.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Thu May 22 09:49:27 2014 -0400 (2014-05-22)
changeset 4378 568f4497ef4a
parent 3998 a40a92c903be
permissions -rw-r--r--
Update CS:GO team score gamedata.
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
dvander@2989
    32
#include "common_logic.h"
dvander@2989
    33
#include <string.h>
dvander@2989
    34
#include <IHandleSys.h>
dvander@2989
    35
#include <ITimerSystem.h>
dvander@2989
    36
#include <IPluginSys.h>
dvander@2989
    37
#include <sh_stack.h>
dvander@3792
    38
#include "DebugReporter.h"
dvander@2989
    39
dvander@2989
    40
using namespace SourceHook;
faluco@612
    41
fyrenmoo@2639
    42
#define TIMER_DATA_HNDL_CLOSE	(1<<9)
fyrenmoo@2639
    43
#define TIMER_HNDL_CLOSE		(1<<9)
faluco@612
    44
faluco@612
    45
HandleType_t g_TimerType;
faluco@612
    46
faluco@612
    47
struct TimerInfo 
faluco@612
    48
{
faluco@612
    49
	ITimer *Timer;
faluco@612
    50
	IPluginFunction *Hook;
faluco@612
    51
	IPluginContext *pContext;
faluco@612
    52
	Handle_t TimerHandle;
faluco@612
    53
	int UserData;
faluco@612
    54
	int Flags;
faluco@612
    55
};
faluco@612
    56
faluco@612
    57
class TimerNatives :
faluco@612
    58
	public SMGlobalClass,
faluco@612
    59
	public IHandleTypeDispatch,
faluco@612
    60
	public ITimedEvent
faluco@612
    61
{
faluco@641
    62
public:
faluco@641
    63
	~TimerNatives();
faluco@612
    64
public: //ITimedEvent
faluco@612
    65
	ResultType OnTimer(ITimer *pTimer, void *pData);
faluco@612
    66
	void OnTimerEnd(ITimer *pTimer, void *pData);
faluco@612
    67
public: //IHandleTypeDispatch
faluco@612
    68
	void OnHandleDestroy(HandleType_t type, void *object);
faluco@612
    69
public: //SMGlobalClass
faluco@612
    70
	void OnSourceModAllInitialized();
faluco@612
    71
	void OnSourceModShutdown();
faluco@612
    72
public:
faluco@612
    73
	TimerInfo *CreateTimerInfo();
faluco@612
    74
	void DeleteTimerInfo(TimerInfo *pInfo);
faluco@612
    75
private:
faluco@612
    76
	CStack<TimerInfo *> m_FreeTimers;
faluco@612
    77
};
faluco@612
    78
faluco@641
    79
TimerNatives::~TimerNatives()
faluco@641
    80
{
faluco@641
    81
	CStack<TimerInfo *>::iterator iter;
faluco@641
    82
	for (iter=m_FreeTimers.begin(); iter!=m_FreeTimers.end(); iter++)
faluco@641
    83
	{
faluco@641
    84
		delete (*iter);
faluco@641
    85
	}
faluco@641
    86
	m_FreeTimers.popall();
faluco@641
    87
}
faluco@641
    88
faluco@612
    89
void TimerNatives::OnSourceModAllInitialized()
faluco@612
    90
{
faluco@612
    91
	HandleAccess sec;
faluco@1397
    92
dvander@2989
    93
	handlesys->InitAccessDefaults(NULL, &sec);
faluco@1397
    94
	sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
faluco@612
    95
dvander@2989
    96
	g_TimerType = handlesys->CreateType("Timer", this, 0, NULL, &sec, g_pCoreIdent, NULL);
faluco@612
    97
}
faluco@612
    98
faluco@612
    99
void TimerNatives::OnSourceModShutdown()
faluco@612
   100
{
dvander@2989
   101
	handlesys->RemoveType(g_TimerType, g_pCoreIdent);
faluco@612
   102
	g_TimerType = 0;
faluco@612
   103
}
faluco@612
   104
faluco@612
   105
void TimerNatives::OnHandleDestroy(HandleType_t type, void *object)
faluco@612
   106
{
faluco@612
   107
	TimerInfo *pTimer = reinterpret_cast<TimerInfo *>(object);
faluco@612
   108
dvander@2989
   109
	timersys->KillTimer(pTimer->Timer);
faluco@612
   110
}
faluco@612
   111
faluco@612
   112
TimerInfo *TimerNatives::CreateTimerInfo()
faluco@612
   113
{
faluco@612
   114
	TimerInfo *pInfo;
faluco@612
   115
faluco@612
   116
	if (m_FreeTimers.empty())
faluco@612
   117
	{
faluco@612
   118
		pInfo = new TimerInfo;
faluco@612
   119
	} else {
faluco@612
   120
		pInfo = m_FreeTimers.front();
faluco@612
   121
		m_FreeTimers.pop();
faluco@612
   122
	}
faluco@612
   123
faluco@612
   124
	return pInfo;
faluco@612
   125
}
faluco@612
   126
faluco@612
   127
void TimerNatives::DeleteTimerInfo(TimerInfo *pInfo)
faluco@612
   128
{
faluco@612
   129
	m_FreeTimers.push(pInfo);
faluco@612
   130
}
faluco@612
   131
faluco@612
   132
ResultType TimerNatives::OnTimer(ITimer *pTimer, void *pData)
faluco@612
   133
{
faluco@612
   134
	TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
faluco@612
   135
	IPluginFunction *pFunc = pInfo->Hook;
faluco@612
   136
	cell_t res = static_cast<ResultType>(Pl_Continue);
faluco@612
   137
faluco@612
   138
	pFunc->PushCell(pInfo->TimerHandle);
faluco@612
   139
	pFunc->PushCell(pInfo->UserData);
faluco@612
   140
	pFunc->Execute(&res);
faluco@612
   141
faluco@612
   142
	return static_cast<ResultType>(res);
faluco@612
   143
}
faluco@612
   144
faluco@612
   145
void TimerNatives::OnTimerEnd(ITimer *pTimer, void *pData)
faluco@612
   146
{
faluco@612
   147
	HandleSecurity sec;
faluco@612
   148
	HandleError herr;
faluco@612
   149
	TimerInfo *pInfo = reinterpret_cast<TimerInfo *>(pData);
faluco@612
   150
	Handle_t usrhndl = static_cast<Handle_t>(pInfo->UserData);
faluco@612
   151
faluco@612
   152
	sec.pOwner = pInfo->pContext->GetIdentity();
faluco@612
   153
	sec.pIdentity = g_pCoreIdent;
faluco@612
   154
fyrenmoo@2639
   155
	if (pInfo->Flags & TIMER_DATA_HNDL_CLOSE)
faluco@612
   156
	{
dvander@2989
   157
		if ((herr=handlesys->FreeHandle(usrhndl, &sec)) != HandleError_None)
faluco@612
   158
		{
dvander@3792
   159
			g_DbgReporter.GenerateError(pInfo->pContext, pInfo->Hook->GetFunctionID(), 
dvander@2989
   160
							  	 SP_ERROR_NATIVE, 
dvander@2989
   161
							  	 "Invalid data handle %x (error %d) passed during timer end with TIMER_DATA_HNDL_CLOSE",
dvander@2989
   162
							  	 usrhndl, herr);
faluco@612
   163
		}
faluco@612
   164
	}
faluco@612
   165
dvander@2379
   166
	if (pInfo->TimerHandle != BAD_HANDLE)
faluco@1674
   167
	{
dvander@2989
   168
		if ((herr=handlesys->FreeHandle(pInfo->TimerHandle, &sec)) != HandleError_None)
dvander@2379
   169
		{
dvander@3792
   170
			g_DbgReporter.GenerateError(pInfo->pContext, pInfo->Hook->GetFunctionID(), 
dvander@2379
   171
				SP_ERROR_NATIVE, 
dvander@2379
   172
				"Invalid timer handle %x (error %d) during timer end, displayed function is timer callback, not the stack trace", 
dvander@2989
   173
				pInfo->TimerHandle, 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();
dvander@2989
   203
	pTimer = timersys->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
januk@3998
   211
	pInfo->UserData = params[3];
januk@3998
   212
	pInfo->Flags = flags;
januk@3998
   213
	pInfo->Hook = pFunc;
januk@3998
   214
	pInfo->Timer = pTimer;
januk@3998
   215
	pInfo->pContext = pCtx;
januk@3998
   216
dvander@2989
   217
	hndl = handlesys->CreateHandle(g_TimerType, pInfo, pCtx->GetIdentity(), g_pCoreIdent, NULL);
faluco@612
   218
dvander@2379
   219
	/* If we can't get a handle, the timer isn't refcounted against the plugin and 
dvander@2379
   220
	 * we need to bail out to prevent a crash.
dvander@2379
   221
	 */
dvander@2379
   222
	if (hndl == BAD_HANDLE)
dvander@2379
   223
	{
januk@3998
   224
		pInfo->TimerHandle = BAD_HANDLE;
dvander@2989
   225
		timersys->KillTimer(pTimer);
dvander@2379
   226
dvander@2379
   227
		return pCtx->ThrowNativeError("Could not create timer, no more handles");
dvander@2379
   228
	}
dvander@2379
   229
faluco@612
   230
	pInfo->TimerHandle = hndl;
faluco@612
   231
faluco@612
   232
	return hndl;
faluco@612
   233
}
faluco@612
   234
faluco@612
   235
static cell_t smn_KillTimer(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
dvander@1008
   242
	sec.pOwner = pCtx->GetIdentity();
faluco@612
   243
	sec.pIdentity = g_pCoreIdent;
faluco@612
   244
dvander@2989
   245
	if ((herr=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
dvander@2989
   251
	timersys->KillTimer(pInfo->Timer);
faluco@612
   252
fyrenmoo@2639
   253
	if (params[2] && !(pInfo->Flags & TIMER_DATA_HNDL_CLOSE))
faluco@612
   254
	{
faluco@612
   255
		sec.pOwner = pInfo->pContext->GetIdentity();
faluco@612
   256
		sec.pIdentity = g_pCoreIdent;
faluco@612
   257
dvander@2989
   258
		if ((herr=handlesys->FreeHandle(static_cast<Handle_t>(pInfo->UserData), &sec)) != HandleError_None)
faluco@612
   259
		{
fyrenmoo@2639
   260
			return pCtx->ThrowNativeError("Invalid data handle %x (error %d) on timer kill with TIMER_DATA_HNDL_CLOSE", hndl, herr);
faluco@612
   261
		}
faluco@612
   262
	}
faluco@612
   263
faluco@612
   264
	return 1;
faluco@612
   265
}
faluco@612
   266
faluco@612
   267
static cell_t smn_TriggerTimer(IPluginContext *pCtx, const cell_t *params)
faluco@612
   268
{
faluco@612
   269
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@612
   270
	HandleError herr;
faluco@612
   271
	HandleSecurity sec;
faluco@612
   272
	TimerInfo *pInfo;
faluco@612
   273
faluco@1397
   274
	sec.pOwner = pCtx->GetIdentity();
faluco@612
   275
	sec.pIdentity = g_pCoreIdent;
faluco@612
   276
dvander@2989
   277
	if ((herr=handlesys->ReadHandle(hndl, g_TimerType, &sec, (void **)&pInfo))
faluco@612
   278
		!= HandleError_None)
faluco@612
   279
	{
faluco@612
   280
		return pCtx->ThrowNativeError("Invalid timer handle %x (error %d)", hndl, herr);
faluco@612
   281
	}
faluco@612
   282
dvander@2989
   283
	timersys->FireTimerOnce(pInfo->Timer, params[2] ? true : false);
faluco@612
   284
faluco@612
   285
	return 1;
faluco@612
   286
}
faluco@612
   287
dvander@822
   288
static cell_t smn_GetTickedTime(IPluginContext *pContext, const cell_t *params)
dvander@822
   289
{
dvander@2989
   290
	return sp_ftoc(*serverGlobals.universalTime);
dvander@822
   291
}
dvander@822
   292
dvander@1415
   293
static cell_t smn_GetMapTimeLeft(IPluginContext *pContext, const cell_t *params)
dvander@1415
   294
{
dvander@1459
   295
	float time_left;
dvander@2989
   296
	if (!timersys->GetMapTimeLeft(&time_left))
dvander@1415
   297
	{
dvander@1462
   298
		return 0;
dvander@1415
   299
	}
dvander@1415
   300
dvander@1462
   301
	cell_t *addr;
dvander@1462
   302
	pContext->LocalToPhysAddr(params[1], &addr);
dvander@1462
   303
	*addr = (int)time_left;
dvander@1415
   304
dvander@1462
   305
	return 1;
dvander@1415
   306
}
dvander@1415
   307
dvander@1415
   308
static cell_t smn_GetMapTimeLimit(IPluginContext *pContext, const cell_t *params)
dvander@1415
   309
{
dvander@2989
   310
	IMapTimer *pMapTimer = timersys->GetMapTimer();
dvander@1415
   311
dvander@1415
   312
	if (!pMapTimer)
dvander@1415
   313
	{
dvander@1415
   314
		return 0;
dvander@1415
   315
	}
dvander@1415
   316
dvander@1415
   317
	cell_t *addr;
dvander@1415
   318
	pContext->LocalToPhysAddr(params[1], &addr);
dvander@1415
   319
dvander@1415
   320
	*addr = pMapTimer->GetMapTimeLimit();
dvander@1415
   321
dvander@1415
   322
	return 1;
dvander@1415
   323
}
dvander@1415
   324
dvander@1415
   325
static cell_t smn_ExtendMapTimeLimit(IPluginContext *pContext, const cell_t *params)
dvander@1415
   326
{
dvander@2989
   327
	IMapTimer *pMapTimer = timersys->GetMapTimer();
dvander@1415
   328
dvander@1415
   329
	if (!pMapTimer)
dvander@1415
   330
	{
dvander@1415
   331
		return 0;
dvander@1415
   332
	}
dvander@1415
   333
dvander@1415
   334
	pMapTimer->ExtendMapTimeLimit(params[1]);
dvander@1415
   335
dvander@1415
   336
	return 1;
dvander@1415
   337
}
dvander@1415
   338
dvander@1461
   339
static cell_t smn_IsServerProcessing(IPluginContext *pContext, const cell_t *params)
dvander@1459
   340
{
dvander@2989
   341
	return (*serverGlobals.frametime > 0.0f) ? 1 : 0;
dvander@1459
   342
}
dvander@1459
   343
dvander@2063
   344
static cell_t smn_GetTickInterval(IPluginContext *pContext, const cell_t *params)
dvander@2063
   345
{
dvander@2989
   346
	return sp_ftoc(*serverGlobals.interval_per_tick);
dvander@2063
   347
}
dvander@2063
   348
faluco@612
   349
REGISTER_NATIVES(timernatives)
faluco@612
   350
{
faluco@612
   351
	{"CreateTimer",				smn_CreateTimer},
faluco@612
   352
	{"KillTimer",				smn_KillTimer},
faluco@612
   353
	{"TriggerTimer",			smn_TriggerTimer},
dvander@822
   354
	{"GetTickedTime",			smn_GetTickedTime},
dvander@1415
   355
	{"GetMapTimeLeft",			smn_GetMapTimeLeft},
dvander@1415
   356
	{"GetMapTimeLimit",			smn_GetMapTimeLimit},
dvander@1415
   357
	{"ExtendMapTimeLimit",		smn_ExtendMapTimeLimit},
dvander@1461
   358
	{"IsServerProcessing",		smn_IsServerProcessing},
dvander@2063
   359
	{"GetTickInterval",			smn_GetTickInterval},
faluco@612
   360
	{NULL,						NULL}
faluco@612
   361
};
dvander@2063
   362