core/smn_events.cpp
author Scott Ehlert <ds@alliedmods.net>
Mon May 14 21:29:17 2007 +0000 (2007-05-14)
changeset 794 5947b0ee60bb
parent 713 7a8ac1106ace
child 801 712a6e20b3e1
permissions -rw-r--r--
Implemented amb286 - .smx extension no longer required with "sm plugins load"
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@576
    41
static cell_t sm_UnhookEvent(IPluginContext *pContext, const cell_t *params)
ds@576
    42
{
ds@576
    43
	char *name;
ds@576
    44
	IPluginFunction *pFunction;
ds@576
    45
ds@576
    46
	pContext->LocalToString(params[1], &name);
ds@576
    47
	pFunction = pContext->GetFunctionById(params[2]);
ds@576
    48
ds@576
    49
	if (!pFunction)
ds@576
    50
	{
ds@576
    51
		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
ds@576
    52
	}
ds@576
    53
ds@576
    54
	EventHookError err = g_EventManager.UnhookEvent(name, pFunction, static_cast<EventHookMode>(params[3]));
ds@576
    55
ds@576
    56
	/* Possible errors that UnhookGameEvent can return */
ds@576
    57
	if (err == EventHookErr_NotActive)
ds@576
    58
	{
ds@576
    59
		return pContext->ThrowNativeError("Game event \"%s\" has no active hook", name);
ds@576
    60
	} else if (err == EventHookErr_InvalidCallback) {
ds@576
    61
		return pContext->ThrowNativeError("Invalid hook callback specified for game event \"%s\"", name);
ds@576
    62
	}
ds@576
    63
ds@576
    64
	return 1;
ds@576
    65
}
ds@576
    66
ds@576
    67
static cell_t sm_CreateEvent(IPluginContext *pContext, const cell_t *params)
ds@576
    68
{
ds@577
    69
	char *name;
ds@576
    70
	EventInfo *pInfo;
ds@576
    71
ds@576
    72
	pContext->LocalToString(params[1], &name);
ds@576
    73
ds@713
    74
	pInfo = g_EventManager.CreateEvent(pContext, name, params[2] ? true : false);
ds@576
    75
ds@577
    76
	if (pInfo)
ds@576
    77
	{
ds@576
    78
		return g_HandleSys.CreateHandle(g_EventManager.GetHandleType(), pInfo, pContext->GetIdentity(), g_pCoreIdent, NULL);
ds@576
    79
	}
ds@576
    80
ds@576
    81
	return BAD_HANDLE;
ds@576
    82
}
ds@576
    83
ds@576
    84
static cell_t sm_FireEvent(IPluginContext *pContext, const cell_t *params)
ds@576
    85
{
ds@576
    86
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
    87
	HandleError err;
ds@576
    88
	EventInfo *pInfo;
ds@576
    89
ds@576
    90
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
    91
		!= HandleError_None)
ds@576
    92
	{
ds@581
    93
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
    94
	}
ds@576
    95
ds@577
    96
	/* If identities do not match, don't fire event */
ds@577
    97
	if (pContext->GetIdentity() != pInfo->pOwner)
ds@576
    98
	{
ds@577
    99
		return pContext->ThrowNativeError("Game event \"%s\" could not be fired because it was not created by this plugin", pInfo->pEvent->GetName());
ds@576
   100
	}
ds@576
   101
ds@712
   102
	g_EventManager.FireEvent(pInfo, params[2] ? true : false);
ds@576
   103
ds@576
   104
	/* Free handle on game event */
ds@577
   105
	HandleSecurity sec = {pContext->GetIdentity(), g_pCoreIdent};
ds@576
   106
	g_HandleSys.FreeHandle(hndl, &sec);
ds@576
   107
ds@576
   108
	return 1;
ds@576
   109
}
ds@576
   110
ds@576
   111
static cell_t sm_CancelCreatedEvent(IPluginContext *pContext, const cell_t *params)
ds@576
   112
{
ds@576
   113
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   114
	HandleError err;
ds@576
   115
	EventInfo *pInfo;
ds@576
   116
ds@576
   117
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   118
		!= HandleError_None)
ds@576
   119
	{
ds@581
   120
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   121
	}
ds@576
   122
ds@577
   123
	/* If identities do not match, don't cancel event */
ds@577
   124
	if (pContext->GetIdentity() != pInfo->pOwner)
ds@577
   125
	{
ds@577
   126
		return pContext->ThrowNativeError("Game event \"%s\" could not be canceled because it was not created by this plugin", pInfo->pEvent->GetName());
ds@577
   127
	}
ds@576
   128
ds@687
   129
	g_EventManager.CancelCreatedEvent(pInfo);
ds@687
   130
ds@577
   131
	/* Free handle on game event */
ds@577
   132
	HandleSecurity sec = {pContext->GetIdentity(), g_pCoreIdent};
ds@576
   133
	g_HandleSys.FreeHandle(hndl, &sec);
ds@576
   134
ds@576
   135
	return 1;
ds@576
   136
}
ds@576
   137
ds@576
   138
static cell_t sm_GetEventName(IPluginContext *pContext, const cell_t *params)
ds@576
   139
{
ds@576
   140
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   141
	HandleError err;
ds@576
   142
	EventInfo *pInfo;
ds@576
   143
ds@576
   144
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   145
		!= HandleError_None)
ds@576
   146
	{
ds@581
   147
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   148
	}
ds@576
   149
ds@576
   150
	pContext->StringToLocalUTF8(params[2], params[3], pInfo->pEvent->GetName(), NULL);
ds@576
   151
ds@576
   152
	return 1;
ds@576
   153
}
ds@576
   154
ds@576
   155
static cell_t sm_GetEventBool(IPluginContext *pContext, const cell_t *params)
ds@576
   156
{
ds@576
   157
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   158
	HandleError err;
ds@576
   159
	EventInfo *pInfo;
ds@576
   160
ds@576
   161
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   162
		!= HandleError_None)
ds@576
   163
	{
ds@581
   164
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   165
	}
ds@576
   166
ds@576
   167
	char *key;
ds@576
   168
	pContext->LocalToString(params[2], &key);
ds@576
   169
ds@576
   170
	return pInfo->pEvent->GetBool(key);
ds@576
   171
}
ds@576
   172
ds@576
   173
static cell_t sm_GetEventInt(IPluginContext *pContext, const cell_t *params)
ds@576
   174
{
ds@576
   175
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   176
	HandleError err;
ds@576
   177
	EventInfo *pInfo;
ds@576
   178
ds@576
   179
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   180
		!= HandleError_None)
ds@576
   181
	{
ds@581
   182
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   183
	}
ds@576
   184
ds@576
   185
	char *key;
ds@576
   186
	pContext->LocalToString(params[2], &key);
ds@576
   187
ds@576
   188
	return pInfo->pEvent->GetInt(key);
ds@576
   189
}
ds@576
   190
ds@576
   191
static cell_t sm_GetEventFloat(IPluginContext *pContext, const cell_t *params)
ds@576
   192
{
ds@576
   193
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   194
	HandleError err;
ds@576
   195
	EventInfo *pInfo;
ds@576
   196
ds@576
   197
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   198
		!= HandleError_None)
ds@576
   199
	{
ds@581
   200
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   201
	}
ds@576
   202
ds@576
   203
	char *key;
ds@576
   204
	pContext->LocalToString(params[2], &key);
ds@576
   205
ds@576
   206
	float value = pInfo->pEvent->GetFloat(key);
ds@576
   207
ds@576
   208
	return sp_ftoc(value);
ds@576
   209
}
ds@576
   210
ds@576
   211
static cell_t sm_GetEventString(IPluginContext *pContext, const cell_t *params)
ds@576
   212
{
ds@576
   213
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   214
	HandleError err;
ds@576
   215
	EventInfo *pInfo;
ds@576
   216
ds@576
   217
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   218
		!= HandleError_None)
ds@576
   219
	{
ds@581
   220
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   221
	}
ds@576
   222
ds@576
   223
	char *key;
ds@576
   224
	pContext->LocalToString(params[2], &key);
ds@576
   225
ds@576
   226
	pContext->StringToLocalUTF8(params[3], params[4], pInfo->pEvent->GetString(key), NULL);
ds@576
   227
ds@576
   228
	return 1;
ds@576
   229
}
ds@576
   230
ds@576
   231
static cell_t sm_SetEventBool(IPluginContext *pContext, const cell_t *params)
ds@576
   232
{
ds@576
   233
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   234
	HandleError err;
ds@576
   235
	EventInfo *pInfo;
ds@576
   236
ds@576
   237
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   238
		!= HandleError_None)
ds@576
   239
	{
ds@581
   240
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   241
	}
ds@576
   242
ds@576
   243
	char *key;
ds@576
   244
	pContext->LocalToString(params[2], &key);
ds@576
   245
ds@576
   246
	pInfo->pEvent->SetBool(key, params[3] ? true : false);
ds@576
   247
ds@576
   248
	return 1;
ds@576
   249
}
ds@576
   250
ds@576
   251
static cell_t sm_SetEventInt(IPluginContext *pContext, const cell_t *params)
ds@576
   252
{
ds@576
   253
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   254
	HandleError err;
ds@576
   255
	EventInfo *pInfo;
ds@576
   256
ds@576
   257
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   258
		!= HandleError_None)
ds@576
   259
	{
ds@581
   260
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   261
	}
ds@576
   262
ds@576
   263
	char *key;
ds@576
   264
	pContext->LocalToString(params[2], &key);
ds@576
   265
ds@576
   266
	pInfo->pEvent->SetInt(key, params[3]);
ds@576
   267
ds@576
   268
	return 1;
ds@576
   269
}
ds@576
   270
ds@576
   271
static cell_t sm_SetEventFloat(IPluginContext *pContext, const cell_t *params)
ds@576
   272
{
ds@576
   273
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@576
   274
	HandleError err;
ds@576
   275
	EventInfo *pInfo;
ds@576
   276
ds@576
   277
	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
ds@576
   278
		!= HandleError_None)
ds@576
   279
	{
ds@581
   280
		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
ds@576
   281
	}
ds@576
   282
ds@576
   283
	char *key;
ds@576
   284
	pContext->LocalToString(params[2], &key);
ds@576
   285
ds@576
   286
	float value = sp_ctof(params[3]);
ds@576
   287
	pInfo->pEvent->SetFloat(key, value);
ds@576
   288
ds@576
   289
	return 1;
ds@576
   290
}
ds@576
   291
ds@576
   292
static cell_t sm_SetEventString(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, *value;
ds@576
   305
	pContext->LocalToString(params[2], &key);
ds@576
   306
	pContext->LocalToString(params[3], &value);
ds@576
   307
ds@576
   308
	pInfo->pEvent->SetString(key, value);
ds@576
   309
ds@576
   310
	return 1;
ds@576
   311
}
ds@576
   312
ds@576
   313
REGISTER_NATIVES(gameEventNatives)
ds@576
   314
{
ds@576
   315
	{"HookEvent",			sm_HookEvent},
ds@576
   316
	{"UnhookEvent",			sm_UnhookEvent},
ds@576
   317
	{"CreateEvent",			sm_CreateEvent},
ds@576
   318
	{"FireEvent",			sm_FireEvent},
ds@576
   319
	{"CancelCreatedEvent",	sm_CancelCreatedEvent},
ds@576
   320
	{"GetEventName",		sm_GetEventName},
ds@576
   321
	{"GetEventBool",		sm_GetEventBool},
ds@576
   322
	{"GetEventInt",			sm_GetEventInt},
ds@576
   323
	{"GetEventFloat",		sm_GetEventFloat},
ds@576
   324
	{"GetEventString",		sm_GetEventString},
ds@576
   325
	{"SetEventBool",		sm_SetEventBool},
ds@576
   326
	{"SetEventInt",			sm_SetEventInt},
ds@576
   327
	{"SetEventFloat",		sm_SetEventFloat},
ds@576
   328
	{"SetEventString",		sm_SetEventString},
ds@576
   329
	{NULL,					NULL}
ds@576
   330
};