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