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