core/smn_events.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Fri Aug 30 14:15:02 2013 -0400 (2013-08-30)
changeset 4102 0eb66c45a884
parent 3955 763d59fe6d77
permissions -rw-r--r--
Reserved slot fixes for OB games when SourceTV or Replay are enabled (bug 5499, r=drifter).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "sm_globals.h"
    33 #include "sourcemm_api.h"
    34 #include "EventManager.h"
    35 #include "logic_bridge.h"
    36 
    37 static cell_t sm_HookEvent(IPluginContext *pContext, const cell_t *params)
    38 {
    39 	char *name;
    40 	IPluginFunction *pFunction;
    41 
    42 	pContext->LocalToString(params[1], &name);
    43 	pFunction = pContext->GetFunctionById(params[2]);
    44 
    45 	if (!pFunction)
    46 	{
    47 		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
    48 	}
    49 
    50 	if (g_EventManager.HookEvent(name, pFunction, static_cast<EventHookMode>(params[3])) == EventHookErr_InvalidEvent)
    51 	{
    52 		return pContext->ThrowNativeError("Game event \"%s\" does not exist", name);
    53 	}
    54 
    55 	return 1;
    56 }
    57 
    58 static cell_t sm_HookEventEx(IPluginContext *pContext, const cell_t *params)
    59 {
    60 	char *name;
    61 	IPluginFunction *pFunction;
    62 
    63 	pContext->LocalToString(params[1], &name);
    64 	pFunction = pContext->GetFunctionById(params[2]);
    65 
    66 	if (!pFunction)
    67 	{
    68 		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
    69 	}
    70 
    71 	if (g_EventManager.HookEvent(name, pFunction, static_cast<EventHookMode>(params[3])) == EventHookErr_InvalidEvent)
    72 	{
    73 		return 0;
    74 	}
    75 
    76 	return 1;
    77 }
    78 
    79 static cell_t sm_UnhookEvent(IPluginContext *pContext, const cell_t *params)
    80 {
    81 	char *name;
    82 	IPluginFunction *pFunction;
    83 
    84 	pContext->LocalToString(params[1], &name);
    85 	pFunction = pContext->GetFunctionById(params[2]);
    86 
    87 	if (!pFunction)
    88 	{
    89 		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
    90 	}
    91 
    92 	EventHookError err = g_EventManager.UnhookEvent(name, pFunction, static_cast<EventHookMode>(params[3]));
    93 
    94 	/* Possible errors that UnhookGameEvent can return */
    95 	if (err == EventHookErr_NotActive)
    96 	{
    97 		return pContext->ThrowNativeError("Game event \"%s\" has no active hook", name);
    98 	} else if (err == EventHookErr_InvalidCallback) {
    99 		return pContext->ThrowNativeError("Invalid hook callback specified for game event \"%s\"", name);
   100 	}
   101 
   102 	return 1;
   103 }
   104 
   105 static cell_t sm_CreateEvent(IPluginContext *pContext, const cell_t *params)
   106 {
   107 	char *name;
   108 	EventInfo *pInfo;
   109 
   110 	pContext->LocalToString(params[1], &name);
   111 
   112 	pInfo = g_EventManager.CreateEvent(pContext, name, params[2] ? true : false);
   113 
   114 	if (pInfo)
   115 	{
   116 		return handlesys->CreateHandle(g_EventManager.GetHandleType(), pInfo, pContext->GetIdentity(), g_pCoreIdent, NULL);
   117 	}
   118 
   119 	return BAD_HANDLE;
   120 }
   121 
   122 static cell_t sm_FireEvent(IPluginContext *pContext, const cell_t *params)
   123 {
   124 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   125 	HandleError err;
   126 	EventInfo *pInfo;
   127         HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   128 
   129 	if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
   130 		!= HandleError_None)
   131 	{
   132 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   133 	}
   134 
   135 	/* If identities do not match, don't fire event */
   136 	if (pContext->GetIdentity() != pInfo->pOwner)
   137 	{
   138 		return pContext->ThrowNativeError("Game event \"%s\" could not be fired because it was not created by this plugin", pInfo->pEvent->GetName());
   139 	}
   140 
   141 	g_EventManager.FireEvent(pInfo, params[2] ? true : false);
   142 
   143 	/* Free handle on game event */
   144 	handlesys->FreeHandle(hndl, &sec);
   145 
   146 	return 1;
   147 }
   148 
   149 static cell_t sm_CancelCreatedEvent(IPluginContext *pContext, const cell_t *params)
   150 {
   151 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   152 	HandleError err;
   153 	EventInfo *pInfo;
   154         HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   155 
   156 	if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
   157 		!= HandleError_None)
   158 	{
   159 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   160 	}
   161 
   162 	/* If identities do not match, don't cancel event */
   163 	if (pContext->GetIdentity() != pInfo->pOwner)
   164 	{
   165 		return pContext->ThrowNativeError("Game event \"%s\" could not be canceled because it was not created by this plugin", pInfo->pEvent->GetName());
   166 	}
   167 
   168 	g_EventManager.CancelCreatedEvent(pInfo);
   169 
   170 	/* Free handle on game event */
   171 	handlesys->FreeHandle(hndl, &sec);
   172 
   173 	return 1;
   174 }
   175 
   176 static cell_t sm_GetEventName(IPluginContext *pContext, const cell_t *params)
   177 {
   178 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   179 	HandleError err;
   180 	EventInfo *pInfo;
   181 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   182 
   183 	if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
   184 		!= HandleError_None)
   185 	{
   186 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   187 	}
   188 
   189 	pContext->StringToLocalUTF8(params[2], params[3], pInfo->pEvent->GetName(), NULL);
   190 
   191 	return 1;
   192 }
   193 
   194 static cell_t sm_GetEventBool(IPluginContext *pContext, const cell_t *params)
   195 {
   196 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   197 	HandleError err;
   198 	EventInfo *pInfo;
   199 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   200 
   201 	if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
   202 		!= HandleError_None)
   203 	{
   204 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   205 	}
   206 
   207 	char *key;
   208 	pContext->LocalToString(params[2], &key);
   209 
   210 	return pInfo->pEvent->GetBool(key);
   211 }
   212 
   213 static cell_t sm_GetEventInt(IPluginContext *pContext, const cell_t *params)
   214 {
   215 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   216 	HandleError err;
   217 	EventInfo *pInfo;
   218 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   219 
   220 	if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
   221 		!= HandleError_None)
   222 	{
   223 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   224 	}
   225 
   226 	char *key;
   227 	pContext->LocalToString(params[2], &key);
   228 
   229 	return pInfo->pEvent->GetInt(key);
   230 }
   231 
   232 static cell_t sm_GetEventFloat(IPluginContext *pContext, const cell_t *params)
   233 {
   234 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   235 	HandleError err;
   236 	EventInfo *pInfo;
   237 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   238 
   239 	if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
   240 		!= HandleError_None)
   241 	{
   242 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   243 	}
   244 
   245 	char *key;
   246 	pContext->LocalToString(params[2], &key);
   247 
   248 	float value = pInfo->pEvent->GetFloat(key);
   249 
   250 	return sp_ftoc(value);
   251 }
   252 
   253 static cell_t sm_GetEventString(IPluginContext *pContext, const cell_t *params)
   254 {
   255 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   256 	HandleError err;
   257 	EventInfo *pInfo;
   258 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   259 
   260 	if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
   261 		!= HandleError_None)
   262 	{
   263 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   264 	}
   265 
   266 	char *key;
   267 	pContext->LocalToString(params[2], &key);
   268 
   269 	pContext->StringToLocalUTF8(params[3], params[4], pInfo->pEvent->GetString(key), NULL);
   270 
   271 	return 1;
   272 }
   273 
   274 static cell_t sm_SetEventBool(IPluginContext *pContext, const cell_t *params)
   275 {
   276 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   277 	HandleError err;
   278 	EventInfo *pInfo;
   279 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   280 
   281 	if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
   282 		!= HandleError_None)
   283 	{
   284 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   285 	}
   286 
   287 	char *key;
   288 	pContext->LocalToString(params[2], &key);
   289 
   290 	pInfo->pEvent->SetBool(key, params[3] ? true : false);
   291 
   292 	return 1;
   293 }
   294 
   295 static cell_t sm_SetEventInt(IPluginContext *pContext, const cell_t *params)
   296 {
   297 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   298 	HandleError err;
   299 	EventInfo *pInfo;
   300 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   301 
   302 	if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
   303 		!= HandleError_None)
   304 	{
   305 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   306 	}
   307 
   308 	char *key;
   309 	pContext->LocalToString(params[2], &key);
   310 
   311 	pInfo->pEvent->SetInt(key, params[3]);
   312 
   313 	return 1;
   314 }
   315 
   316 static cell_t sm_SetEventFloat(IPluginContext *pContext, const cell_t *params)
   317 {
   318 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   319 	HandleError err;
   320 	EventInfo *pInfo;
   321 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   322 
   323 	if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
   324 		!= HandleError_None)
   325 	{
   326 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   327 	}
   328 
   329 	char *key;
   330 	pContext->LocalToString(params[2], &key);
   331 
   332 	float value = sp_ctof(params[3]);
   333 	pInfo->pEvent->SetFloat(key, value);
   334 
   335 	return 1;
   336 }
   337 
   338 static cell_t sm_SetEventString(IPluginContext *pContext, const cell_t *params)
   339 {
   340 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   341 	HandleError err;
   342 	EventInfo *pInfo;
   343 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   344 
   345 	if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
   346 		!= HandleError_None)
   347 	{
   348 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   349 	}
   350 
   351 	char *key, *value;
   352 	pContext->LocalToString(params[2], &key);
   353 	pContext->LocalToString(params[3], &value);
   354 
   355 	pInfo->pEvent->SetString(key, value);
   356 
   357 	return 1;
   358 }
   359 
   360 static cell_t sm_SetEventBroadcast(IPluginContext *pContext, const cell_t *params)
   361 {
   362 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   363 	HandleError err;
   364 	EventInfo *pInfo;
   365 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   366 
   367 	if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
   368 		!= HandleError_None)
   369 	{
   370 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   371 	}
   372 
   373 	pInfo->bDontBroadcast = params[2] ? true : false;
   374 
   375 	return 1;
   376 }
   377 
   378 REGISTER_NATIVES(gameEventNatives)
   379 {
   380 	{"HookEvent",			sm_HookEvent},
   381 	{"HookEventEx",			sm_HookEventEx},
   382 	{"UnhookEvent",			sm_UnhookEvent},
   383 	{"CreateEvent",			sm_CreateEvent},
   384 	{"FireEvent",			sm_FireEvent},
   385 	{"CancelCreatedEvent",	sm_CancelCreatedEvent},
   386 	{"GetEventName",		sm_GetEventName},
   387 	{"GetEventBool",		sm_GetEventBool},
   388 	{"GetEventInt",			sm_GetEventInt},
   389 	{"GetEventFloat",		sm_GetEventFloat},
   390 	{"GetEventString",		sm_GetEventString},
   391 	{"SetEventBool",		sm_SetEventBool},
   392 	{"SetEventInt",			sm_SetEventInt},
   393 	{"SetEventFloat",		sm_SetEventFloat},
   394 	{"SetEventString",		sm_SetEventString},
   395 	{"SetEventBroadcast",   sm_SetEventBroadcast},
   396 	{NULL,					NULL}
   397 };
   398