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