core/smn_events.cpp
author Matt Woodrow <pred@alliedmods.net>
Thu May 01 23:48:27 2008 +0000 (2008-05-01)
changeset 2035 941abff6deed
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Added msvc9 Project files to sdktools. Badly.
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 "sm_globals.h"
ds@1952
    33
#include "sourcemm_api.h"
ds@1952
    34
#include "HandleSys.h"
ds@1952
    35
#include "EventManager.h"
ds@1952
    36
ds@1952
    37
static cell_t sm_HookEvent(IPluginContext *pContext, const cell_t *params)
ds@1952
    38
{
ds@1952
    39
	char *name;
ds@1952
    40
	IPluginFunction *pFunction;
ds@1952
    41
ds@1952
    42
	pContext->LocalToString(params[1], &name);
ds@1952
    43
	pFunction = pContext->GetFunctionById(params[2]);
ds@1952
    44
ds@1952
    45
	if (!pFunction)
ds@1952
    46
	{
ds@1952
    47
		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
ds@1952
    48
	}
ds@1952
    49
ds@1952
    50
	if (g_EventManager.HookEvent(name, pFunction, static_cast<EventHookMode>(params[3])) == EventHookErr_InvalidEvent)
ds@1952
    51
	{
ds@1952
    52
		return pContext->ThrowNativeError("Game event \"%s\" does not exist", name);
ds@1952
    53
	}
ds@1952
    54
ds@1952
    55
	return 1;
ds@1952
    56
}
ds@1952
    57
ds@1952
    58
static cell_t sm_HookEventEx(IPluginContext *pContext, const cell_t *params)
ds@1952
    59
{
ds@1952
    60
	char *name;
ds@1952
    61
	IPluginFunction *pFunction;
ds@1952
    62
ds@1952
    63
	pContext->LocalToString(params[1], &name);
ds@1952
    64
	pFunction = pContext->GetFunctionById(params[2]);
ds@1952
    65
ds@1952
    66
	if (!pFunction)
ds@1952
    67
	{
ds@1952
    68
		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
ds@1952
    69
	}
ds@1952
    70
ds@1952
    71
	if (g_EventManager.HookEvent(name, pFunction, static_cast<EventHookMode>(params[3])) == EventHookErr_InvalidEvent)
ds@1952
    72
	{
ds@1952
    73
		return 0;
ds@1952
    74
	}
ds@1952
    75
ds@1952
    76
	return 1;
ds@1952
    77
}
ds@1952
    78
ds@1952
    79
static cell_t sm_UnhookEvent(IPluginContext *pContext, const cell_t *params)
ds@1952
    80
{
ds@1952
    81
	char *name;
ds@1952
    82
	IPluginFunction *pFunction;
ds@1952
    83
ds@1952
    84
	pContext->LocalToString(params[1], &name);
ds@1952
    85
	pFunction = pContext->GetFunctionById(params[2]);
ds@1952
    86
ds@1952
    87
	if (!pFunction)
ds@1952
    88
	{
ds@1952
    89
		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
ds@1952
    90
	}
ds@1952
    91
ds@1952
    92
	EventHookError err = g_EventManager.UnhookEvent(name, pFunction, static_cast<EventHookMode>(params[3]));
ds@1952
    93
ds@1952
    94
	/* Possible errors that UnhookGameEvent can return */
ds@1952
    95
	if (err == EventHookErr_NotActive)
ds@1952
    96
	{
ds@1952
    97
		return pContext->ThrowNativeError("Game event \"%s\" has no active hook", name);
ds@1952
    98
	} else if (err == EventHookErr_InvalidCallback) {
ds@1952
    99
		return pContext->ThrowNativeError("Invalid hook callback specified for game event \"%s\"", name);
ds@1952
   100
	}
ds@1952
   101
ds@1952
   102
	return 1;
ds@1952
   103
}
ds@1952
   104
ds@1952
   105
static cell_t sm_CreateEvent(IPluginContext *pContext, const cell_t *params)
ds@1952
   106
{
ds@1952
   107
	char *name;
ds@1952
   108
	EventInfo *pInfo;
ds@1952
   109
ds@1952
   110
	pContext->LocalToString(params[1], &name);
ds@1952
   111
ds@1952
   112
	pInfo = g_EventManager.CreateEvent(pContext, name, params[2] ? true : false);
ds@1952
   113
ds@1952
   114
	if (pInfo)
ds@1952
   115
	{
ds@1952
   116
		return g_HandleSys.CreateHandle(g_EventManager.GetHandleType(), pInfo, pContext->GetIdentity(), g_pCoreIdent, NULL);
ds@1952
   117
	}
ds@1952
   118
ds@1952
   119
	return BAD_HANDLE;
ds@1952
   120
}
ds@1952
   121
ds@1952
   122
static cell_t sm_FireEvent(IPluginContext *pContext, const cell_t *params)
ds@1952
   123
{
ds@1952
   124
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   125
	HandleError err;
ds@1952
   126
	EventInfo *pInfo;
ds@1952
   127
ds@1952
   128
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@1952
   129
		!= HandleError_None)
ds@1952
   130
	{
ds@1952
   131
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@1952
   132
	}
ds@1952
   133
ds@1952
   134
	/* If identities do not match, don't fire event */
ds@1952
   135
	if (pContext->GetIdentity() != pInfo->pOwner)
ds@1952
   136
	{
ds@1952
   137
		return pContext->ThrowNativeError("Game event \"%s\" could not be fired because it was not created by this plugin", pInfo->pEvent->GetName());
ds@1952
   138
	}
ds@1952
   139
ds@1952
   140
	g_EventManager.FireEvent(pInfo, params[2] ? true : false);
ds@1952
   141
ds@1952
   142
	/* Free handle on game event */
ds@1952
   143
	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
ds@1952
   144
	g_HandleSys.FreeHandle(hndl, &sec);
ds@1952
   145
ds@1952
   146
	return 1;
ds@1952
   147
}
ds@1952
   148
ds@1952
   149
static cell_t sm_CancelCreatedEvent(IPluginContext *pContext, const cell_t *params)
ds@1952
   150
{
ds@1952
   151
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   152
	HandleError err;
ds@1952
   153
	EventInfo *pInfo;
ds@1952
   154
ds@1952
   155
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@1952
   156
		!= HandleError_None)
ds@1952
   157
	{
ds@1952
   158
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@1952
   159
	}
ds@1952
   160
ds@1952
   161
	/* If identities do not match, don't cancel event */
ds@1952
   162
	if (pContext->GetIdentity() != pInfo->pOwner)
ds@1952
   163
	{
ds@1952
   164
		return pContext->ThrowNativeError("Game event \"%s\" could not be canceled because it was not created by this plugin", pInfo->pEvent->GetName());
ds@1952
   165
	}
ds@1952
   166
ds@1952
   167
	g_EventManager.CancelCreatedEvent(pInfo);
ds@1952
   168
ds@1952
   169
	/* Free handle on game event */
ds@1952
   170
	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
ds@1952
   171
	g_HandleSys.FreeHandle(hndl, &sec);
ds@1952
   172
ds@1952
   173
	return 1;
ds@1952
   174
}
ds@1952
   175
ds@1952
   176
static cell_t sm_GetEventName(IPluginContext *pContext, const cell_t *params)
ds@1952
   177
{
ds@1952
   178
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   179
	HandleError err;
ds@1952
   180
	EventInfo *pInfo;
ds@1952
   181
ds@1952
   182
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@1952
   183
		!= HandleError_None)
ds@1952
   184
	{
ds@1952
   185
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@1952
   186
	}
ds@1952
   187
ds@1952
   188
	pContext->StringToLocalUTF8(params[2], params[3], pInfo->pEvent->GetName(), NULL);
ds@1952
   189
ds@1952
   190
	return 1;
ds@1952
   191
}
ds@1952
   192
ds@1952
   193
static cell_t sm_GetEventBool(IPluginContext *pContext, const cell_t *params)
ds@1952
   194
{
ds@1952
   195
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   196
	HandleError err;
ds@1952
   197
	EventInfo *pInfo;
ds@1952
   198
ds@1952
   199
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@1952
   200
		!= HandleError_None)
ds@1952
   201
	{
ds@1952
   202
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@1952
   203
	}
ds@1952
   204
ds@1952
   205
	char *key;
ds@1952
   206
	pContext->LocalToString(params[2], &key);
ds@1952
   207
ds@1952
   208
	return pInfo->pEvent->GetBool(key);
ds@1952
   209
}
ds@1952
   210
ds@1952
   211
static cell_t sm_GetEventInt(IPluginContext *pContext, const cell_t *params)
ds@1952
   212
{
ds@1952
   213
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   214
	HandleError err;
ds@1952
   215
	EventInfo *pInfo;
ds@1952
   216
ds@1952
   217
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@1952
   218
		!= HandleError_None)
ds@1952
   219
	{
ds@1952
   220
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@1952
   221
	}
ds@1952
   222
ds@1952
   223
	char *key;
ds@1952
   224
	pContext->LocalToString(params[2], &key);
ds@1952
   225
ds@1952
   226
	return pInfo->pEvent->GetInt(key);
ds@1952
   227
}
ds@1952
   228
ds@1952
   229
static cell_t sm_GetEventFloat(IPluginContext *pContext, const cell_t *params)
ds@1952
   230
{
ds@1952
   231
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   232
	HandleError err;
ds@1952
   233
	EventInfo *pInfo;
ds@1952
   234
ds@1952
   235
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@1952
   236
		!= HandleError_None)
ds@1952
   237
	{
ds@1952
   238
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@1952
   239
	}
ds@1952
   240
ds@1952
   241
	char *key;
ds@1952
   242
	pContext->LocalToString(params[2], &key);
ds@1952
   243
ds@1952
   244
	float value = pInfo->pEvent->GetFloat(key);
ds@1952
   245
ds@1952
   246
	return sp_ftoc(value);
ds@1952
   247
}
ds@1952
   248
ds@1952
   249
static cell_t sm_GetEventString(IPluginContext *pContext, const cell_t *params)
ds@1952
   250
{
ds@1952
   251
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   252
	HandleError err;
ds@1952
   253
	EventInfo *pInfo;
ds@1952
   254
ds@1952
   255
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@1952
   256
		!= HandleError_None)
ds@1952
   257
	{
ds@1952
   258
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@1952
   259
	}
ds@1952
   260
ds@1952
   261
	char *key;
ds@1952
   262
	pContext->LocalToString(params[2], &key);
ds@1952
   263
ds@1952
   264
	pContext->StringToLocalUTF8(params[3], params[4], pInfo->pEvent->GetString(key), NULL);
ds@1952
   265
ds@1952
   266
	return 1;
ds@1952
   267
}
ds@1952
   268
ds@1952
   269
static cell_t sm_SetEventBool(IPluginContext *pContext, const cell_t *params)
ds@1952
   270
{
ds@1952
   271
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   272
	HandleError err;
ds@1952
   273
	EventInfo *pInfo;
ds@1952
   274
ds@1952
   275
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@1952
   276
		!= HandleError_None)
ds@1952
   277
	{
ds@1952
   278
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@1952
   279
	}
ds@1952
   280
ds@1952
   281
	char *key;
ds@1952
   282
	pContext->LocalToString(params[2], &key);
ds@1952
   283
ds@1952
   284
	pInfo->pEvent->SetBool(key, params[3] ? true : false);
ds@1952
   285
ds@1952
   286
	return 1;
ds@1952
   287
}
ds@1952
   288
ds@1952
   289
static cell_t sm_SetEventInt(IPluginContext *pContext, const cell_t *params)
ds@1952
   290
{
ds@1952
   291
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   292
	HandleError err;
ds@1952
   293
	EventInfo *pInfo;
ds@1952
   294
ds@1952
   295
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@1952
   296
		!= HandleError_None)
ds@1952
   297
	{
ds@1952
   298
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@1952
   299
	}
ds@1952
   300
ds@1952
   301
	char *key;
ds@1952
   302
	pContext->LocalToString(params[2], &key);
ds@1952
   303
ds@1952
   304
	pInfo->pEvent->SetInt(key, params[3]);
ds@1952
   305
ds@1952
   306
	return 1;
ds@1952
   307
}
ds@1952
   308
ds@1952
   309
static cell_t sm_SetEventFloat(IPluginContext *pContext, const cell_t *params)
ds@1952
   310
{
ds@1952
   311
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   312
	HandleError err;
ds@1952
   313
	EventInfo *pInfo;
ds@1952
   314
ds@1952
   315
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@1952
   316
		!= HandleError_None)
ds@1952
   317
	{
ds@1952
   318
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@1952
   319
	}
ds@1952
   320
ds@1952
   321
	char *key;
ds@1952
   322
	pContext->LocalToString(params[2], &key);
ds@1952
   323
ds@1952
   324
	float value = sp_ctof(params[3]);
ds@1952
   325
	pInfo->pEvent->SetFloat(key, value);
ds@1952
   326
ds@1952
   327
	return 1;
ds@1952
   328
}
ds@1952
   329
ds@1952
   330
static cell_t sm_SetEventString(IPluginContext *pContext, const cell_t *params)
ds@1952
   331
{
ds@1952
   332
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@1952
   333
	HandleError err;
ds@1952
   334
	EventInfo *pInfo;
ds@1952
   335
ds@1952
   336
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@1952
   337
		!= HandleError_None)
ds@1952
   338
	{
ds@1952
   339
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@1952
   340
	}
ds@1952
   341
ds@1952
   342
	char *key, *value;
ds@1952
   343
	pContext->LocalToString(params[2], &key);
ds@1952
   344
	pContext->LocalToString(params[3], &value);
ds@1952
   345
ds@1952
   346
	pInfo->pEvent->SetString(key, value);
ds@1952
   347
ds@1952
   348
	return 1;
ds@1952
   349
}
ds@1952
   350
ds@1952
   351
REGISTER_NATIVES(gameEventNatives)
ds@1952
   352
{
ds@1952
   353
	{"HookEvent",			sm_HookEvent},
ds@1952
   354
	{"HookEventEx",			sm_HookEventEx},
ds@1952
   355
	{"UnhookEvent",			sm_UnhookEvent},
ds@1952
   356
	{"CreateEvent",			sm_CreateEvent},
ds@1952
   357
	{"FireEvent",			sm_FireEvent},
ds@1952
   358
	{"CancelCreatedEvent",	sm_CancelCreatedEvent},
ds@1952
   359
	{"GetEventName",		sm_GetEventName},
ds@1952
   360
	{"GetEventBool",		sm_GetEventBool},
ds@1952
   361
	{"GetEventInt",			sm_GetEventInt},
ds@1952
   362
	{"GetEventFloat",		sm_GetEventFloat},
ds@1952
   363
	{"GetEventString",		sm_GetEventString},
ds@1952
   364
	{"SetEventBool",		sm_SetEventBool},
ds@1952
   365
	{"SetEventInt",			sm_SetEventInt},
ds@1952
   366
	{"SetEventFloat",		sm_SetEventFloat},
ds@1952
   367
	{"SetEventString",		sm_SetEventString},
ds@1952
   368
	{NULL,					NULL}
ds@1952
   369
};