core/smn_events.cpp
author David Anderson <dvander@alliedmods.net>
Tue Jul 03 20:46:17 2007 +0000 (2007-07-03)
changeset 1036 2a073d5b3454
parent 857 11471aa66e06
child 1231 17badaf66ce4
permissions -rw-r--r--
added amb505
ds@576
     1
/**
ds@653
     2
 * vim: set ts=4 :
dvander@600
     3
 * ===============================================================
dvander@600
     4
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
dvander@600
     5
 * ===============================================================
dvander@600
     6
 *
dvander@600
     7
 * This file is not open source and may not be copied without explicit
dvander@600
     8
 * written permission of AlliedModders LLC.  This file may not be redistributed 
dvander@600
     9
 * in whole or significant part.
dvander@600
    10
 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
dvander@600
    11
 *
dvander@600
    12
 * Version: $Id$
dvander@600
    13
 */
ds@576
    14
ds@576
    15
#include "sm_globals.h"
ds@576
    16
#include "sourcemm_api.h"
ds@576
    17
#include "HandleSys.h"
ds@576
    18
#include "EventManager.h"
ds@576
    19
ds@576
    20
static cell_t sm_HookEvent(IPluginContext *pContext, const cell_t *params)
ds@576
    21
{
ds@576
    22
	char *name;
ds@576
    23
	IPluginFunction *pFunction;
ds@576
    24
ds@576
    25
	pContext->LocalToString(params[1], &name);
ds@576
    26
	pFunction = pContext->GetFunctionById(params[2]);
ds@576
    27
ds@576
    28
	if (!pFunction)
ds@576
    29
	{
ds@576
    30
		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
ds@576
    31
	}
ds@576
    32
ds@576
    33
	if (g_EventManager.HookEvent(name, pFunction, static_cast<EventHookMode>(params[3])) == EventHookErr_InvalidEvent)
ds@576
    34
	{
ds@576
    35
		return pContext->ThrowNativeError("Game event \"%s\" does not exist", name);
ds@576
    36
	}
ds@576
    37
ds@576
    38
	return 1;
ds@576
    39
}
ds@576
    40
ds@801
    41
static cell_t sm_HookEventEx(IPluginContext *pContext, const cell_t *params)
ds@801
    42
{
ds@801
    43
	char *name;
ds@801
    44
	IPluginFunction *pFunction;
ds@801
    45
ds@801
    46
	pContext->LocalToString(params[1], &name);
ds@802
    47
	pFunction = pContext->GetFunctionById(params[2]);
ds@801
    48
ds@801
    49
	if (!pFunction)
ds@801
    50
	{
ds@801
    51
		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
ds@801
    52
	}
ds@801
    53
ds@801
    54
	if (g_EventManager.HookEvent(name, pFunction, static_cast<EventHookMode>(params[3])) == EventHookErr_InvalidEvent)
ds@801
    55
	{
ds@801
    56
		return 0;
ds@801
    57
	}
ds@801
    58
ds@801
    59
	return 1;
ds@801
    60
}
ds@801
    61
ds@576
    62
static cell_t sm_UnhookEvent(IPluginContext *pContext, const cell_t *params)
ds@576
    63
{
ds@576
    64
	char *name;
ds@576
    65
	IPluginFunction *pFunction;
ds@576
    66
ds@576
    67
	pContext->LocalToString(params[1], &name);
ds@576
    68
	pFunction = pContext->GetFunctionById(params[2]);
ds@576
    69
ds@576
    70
	if (!pFunction)
ds@576
    71
	{
ds@576
    72
		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
ds@576
    73
	}
ds@576
    74
ds@576
    75
	EventHookError err = g_EventManager.UnhookEvent(name, pFunction, static_cast<EventHookMode>(params[3]));
ds@576
    76
ds@576
    77
	/* Possible errors that UnhookGameEvent can return */
ds@576
    78
	if (err == EventHookErr_NotActive)
ds@576
    79
	{
ds@576
    80
		return pContext->ThrowNativeError("Game event \"%s\" has no active hook", name);
ds@576
    81
	} else if (err == EventHookErr_InvalidCallback) {
ds@576
    82
		return pContext->ThrowNativeError("Invalid hook callback specified for game event \"%s\"", name);
ds@576
    83
	}
ds@576
    84
ds@576
    85
	return 1;
ds@576
    86
}
ds@576
    87
ds@576
    88
static cell_t sm_CreateEvent(IPluginContext *pContext, const cell_t *params)
ds@576
    89
{
ds@577
    90
	char *name;
ds@576
    91
	EventInfo *pInfo;
ds@576
    92
ds@576
    93
	pContext->LocalToString(params[1], &name);
ds@576
    94
ds@713
    95
	pInfo = g_EventManager.CreateEvent(pContext, name, params[2] ? true : false);
ds@576
    96
ds@577
    97
	if (pInfo)
ds@576
    98
	{
ds@576
    99
		return g_HandleSys.CreateHandle(g_EventManager.GetHandleType(), pInfo, pContext->GetIdentity(), g_pCoreIdent, NULL);
ds@576
   100
	}
ds@576
   101
ds@576
   102
	return BAD_HANDLE;
ds@576
   103
}
ds@576
   104
ds@576
   105
static cell_t sm_FireEvent(IPluginContext *pContext, const cell_t *params)
ds@576
   106
{
ds@576
   107
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   108
	HandleError err;
ds@576
   109
	EventInfo *pInfo;
ds@576
   110
ds@576
   111
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   112
		!= HandleError_None)
ds@576
   113
	{
ds@581
   114
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   115
	}
ds@576
   116
ds@577
   117
	/* If identities do not match, don't fire event */
ds@577
   118
	if (pContext->GetIdentity() != pInfo->pOwner)
ds@576
   119
	{
ds@577
   120
		return pContext->ThrowNativeError("Game event \"%s\" could not be fired because it was not created by this plugin", pInfo->pEvent->GetName());
ds@576
   121
	}
ds@576
   122
ds@712
   123
	g_EventManager.FireEvent(pInfo, params[2] ? true : false);
ds@576
   124
ds@576
   125
	/* Free handle on game event */
dvander@857
   126
	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
ds@576
   127
	g_HandleSys.FreeHandle(hndl, &sec);
ds@576
   128
ds@576
   129
	return 1;
ds@576
   130
}
ds@576
   131
ds@576
   132
static cell_t sm_CancelCreatedEvent(IPluginContext *pContext, const cell_t *params)
ds@576
   133
{
ds@576
   134
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   135
	HandleError err;
ds@576
   136
	EventInfo *pInfo;
ds@576
   137
ds@576
   138
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   139
		!= HandleError_None)
ds@576
   140
	{
ds@581
   141
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   142
	}
ds@576
   143
ds@577
   144
	/* If identities do not match, don't cancel event */
ds@577
   145
	if (pContext->GetIdentity() != pInfo->pOwner)
ds@577
   146
	{
ds@577
   147
		return pContext->ThrowNativeError("Game event \"%s\" could not be canceled because it was not created by this plugin", pInfo->pEvent->GetName());
ds@577
   148
	}
ds@576
   149
ds@687
   150
	g_EventManager.CancelCreatedEvent(pInfo);
ds@687
   151
ds@577
   152
	/* Free handle on game event */
dvander@857
   153
	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
ds@576
   154
	g_HandleSys.FreeHandle(hndl, &sec);
ds@576
   155
ds@576
   156
	return 1;
ds@576
   157
}
ds@576
   158
ds@576
   159
static cell_t sm_GetEventName(IPluginContext *pContext, const cell_t *params)
ds@576
   160
{
ds@576
   161
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   162
	HandleError err;
ds@576
   163
	EventInfo *pInfo;
ds@576
   164
ds@576
   165
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   166
		!= HandleError_None)
ds@576
   167
	{
ds@581
   168
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   169
	}
ds@576
   170
ds@576
   171
	pContext->StringToLocalUTF8(params[2], params[3], pInfo->pEvent->GetName(), NULL);
ds@576
   172
ds@576
   173
	return 1;
ds@576
   174
}
ds@576
   175
ds@576
   176
static cell_t sm_GetEventBool(IPluginContext *pContext, const cell_t *params)
ds@576
   177
{
ds@576
   178
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   179
	HandleError err;
ds@576
   180
	EventInfo *pInfo;
ds@576
   181
ds@576
   182
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   183
		!= HandleError_None)
ds@576
   184
	{
ds@581
   185
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   186
	}
ds@576
   187
ds@576
   188
	char *key;
ds@576
   189
	pContext->LocalToString(params[2], &key);
ds@576
   190
ds@576
   191
	return pInfo->pEvent->GetBool(key);
ds@576
   192
}
ds@576
   193
ds@576
   194
static cell_t sm_GetEventInt(IPluginContext *pContext, const cell_t *params)
ds@576
   195
{
ds@576
   196
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   197
	HandleError err;
ds@576
   198
	EventInfo *pInfo;
ds@576
   199
ds@576
   200
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   201
		!= HandleError_None)
ds@576
   202
	{
ds@581
   203
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   204
	}
ds@576
   205
ds@576
   206
	char *key;
ds@576
   207
	pContext->LocalToString(params[2], &key);
ds@576
   208
ds@576
   209
	return pInfo->pEvent->GetInt(key);
ds@576
   210
}
ds@576
   211
ds@576
   212
static cell_t sm_GetEventFloat(IPluginContext *pContext, const cell_t *params)
ds@576
   213
{
ds@576
   214
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   215
	HandleError err;
ds@576
   216
	EventInfo *pInfo;
ds@576
   217
ds@576
   218
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   219
		!= HandleError_None)
ds@576
   220
	{
ds@581
   221
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   222
	}
ds@576
   223
ds@576
   224
	char *key;
ds@576
   225
	pContext->LocalToString(params[2], &key);
ds@576
   226
ds@576
   227
	float value = pInfo->pEvent->GetFloat(key);
ds@576
   228
ds@576
   229
	return sp_ftoc(value);
ds@576
   230
}
ds@576
   231
ds@576
   232
static cell_t sm_GetEventString(IPluginContext *pContext, const cell_t *params)
ds@576
   233
{
ds@576
   234
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   235
	HandleError err;
ds@576
   236
	EventInfo *pInfo;
ds@576
   237
ds@576
   238
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   239
		!= HandleError_None)
ds@576
   240
	{
ds@581
   241
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   242
	}
ds@576
   243
ds@576
   244
	char *key;
ds@576
   245
	pContext->LocalToString(params[2], &key);
ds@576
   246
ds@576
   247
	pContext->StringToLocalUTF8(params[3], params[4], pInfo->pEvent->GetString(key), NULL);
ds@576
   248
ds@576
   249
	return 1;
ds@576
   250
}
ds@576
   251
ds@576
   252
static cell_t sm_SetEventBool(IPluginContext *pContext, const cell_t *params)
ds@576
   253
{
ds@576
   254
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   255
	HandleError err;
ds@576
   256
	EventInfo *pInfo;
ds@576
   257
ds@576
   258
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   259
		!= HandleError_None)
ds@576
   260
	{
ds@581
   261
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   262
	}
ds@576
   263
ds@576
   264
	char *key;
ds@576
   265
	pContext->LocalToString(params[2], &key);
ds@576
   266
ds@576
   267
	pInfo->pEvent->SetBool(key, params[3] ? true : false);
ds@576
   268
ds@576
   269
	return 1;
ds@576
   270
}
ds@576
   271
ds@576
   272
static cell_t sm_SetEventInt(IPluginContext *pContext, const cell_t *params)
ds@576
   273
{
ds@576
   274
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   275
	HandleError err;
ds@576
   276
	EventInfo *pInfo;
ds@576
   277
ds@576
   278
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   279
		!= HandleError_None)
ds@576
   280
	{
ds@581
   281
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   282
	}
ds@576
   283
ds@576
   284
	char *key;
ds@576
   285
	pContext->LocalToString(params[2], &key);
ds@576
   286
ds@576
   287
	pInfo->pEvent->SetInt(key, params[3]);
ds@576
   288
ds@576
   289
	return 1;
ds@576
   290
}
ds@576
   291
ds@576
   292
static cell_t sm_SetEventFloat(IPluginContext *pContext, const cell_t *params)
ds@576
   293
{
ds@576
   294
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   295
	HandleError err;
ds@576
   296
	EventInfo *pInfo;
ds@576
   297
ds@576
   298
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   299
		!= HandleError_None)
ds@576
   300
	{
ds@581
   301
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   302
	}
ds@576
   303
ds@576
   304
	char *key;
ds@576
   305
	pContext->LocalToString(params[2], &key);
ds@576
   306
ds@576
   307
	float value = sp_ctof(params[3]);
ds@576
   308
	pInfo->pEvent->SetFloat(key, value);
ds@576
   309
ds@576
   310
	return 1;
ds@576
   311
}
ds@576
   312
ds@576
   313
static cell_t sm_SetEventString(IPluginContext *pContext, const cell_t *params)
ds@576
   314
{
ds@576
   315
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   316
	HandleError err;
ds@576
   317
	EventInfo *pInfo;
ds@576
   318
ds@576
   319
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   320
		!= HandleError_None)
ds@576
   321
	{
ds@581
   322
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   323
	}
ds@576
   324
ds@576
   325
	char *key, *value;
ds@576
   326
	pContext->LocalToString(params[2], &key);
ds@576
   327
	pContext->LocalToString(params[3], &value);
ds@576
   328
ds@576
   329
	pInfo->pEvent->SetString(key, value);
ds@576
   330
ds@576
   331
	return 1;
ds@576
   332
}
ds@576
   333
ds@576
   334
REGISTER_NATIVES(gameEventNatives)
ds@576
   335
{
ds@576
   336
	{"HookEvent",			sm_HookEvent},
ds@801
   337
	{"HookEventEx",			sm_HookEventEx},
ds@576
   338
	{"UnhookEvent",			sm_UnhookEvent},
ds@576
   339
	{"CreateEvent",			sm_CreateEvent},
ds@576
   340
	{"FireEvent",			sm_FireEvent},
ds@576
   341
	{"CancelCreatedEvent",	sm_CancelCreatedEvent},
ds@576
   342
	{"GetEventName",		sm_GetEventName},
ds@576
   343
	{"GetEventBool",		sm_GetEventBool},
ds@576
   344
	{"GetEventInt",			sm_GetEventInt},
ds@576
   345
	{"GetEventFloat",		sm_GetEventFloat},
ds@576
   346
	{"GetEventString",		sm_GetEventString},
ds@576
   347
	{"SetEventBool",		sm_SetEventBool},
ds@576
   348
	{"SetEventInt",			sm_SetEventInt},
ds@576
   349
	{"SetEventFloat",		sm_SetEventFloat},
ds@576
   350
	{"SetEventString",		sm_SetEventString},
ds@576
   351
	{NULL,					NULL}
ds@576
   352
};