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