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