core/smn_events.cpp
author Matt Woodrow <pred@alliedmods.net>
Tue Mar 11 23:52:56 2008 +0000 (2008-03-11)
changeset 1921 f993e100e5a3
parent 1330 ab94316bb52b
child 1947 e73a1e51833b
permissions -rw-r--r--
Fixed bug in GetAdjustedTime()
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 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 "HandleSys.h"
    35 #include "EventManager.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 g_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 
   128 	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
   129 		!= HandleError_None)
   130 	{
   131 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   132 	}
   133 
   134 	/* If identities do not match, don't fire event */
   135 	if (pContext->GetIdentity() != pInfo->pOwner)
   136 	{
   137 		return pContext->ThrowNativeError("Game event \"%s\" could not be fired because it was not created by this plugin", pInfo->pEvent->GetName());
   138 	}
   139 
   140 	g_EventManager.FireEvent(pInfo, params[2] ? true : false);
   141 
   142 	/* Free handle on game event */
   143 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   144 	g_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 
   155 	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
   156 		!= HandleError_None)
   157 	{
   158 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   159 	}
   160 
   161 	/* If identities do not match, don't cancel event */
   162 	if (pContext->GetIdentity() != pInfo->pOwner)
   163 	{
   164 		return pContext->ThrowNativeError("Game event \"%s\" could not be canceled because it was not created by this plugin", pInfo->pEvent->GetName());
   165 	}
   166 
   167 	g_EventManager.CancelCreatedEvent(pInfo);
   168 
   169 	/* Free handle on game event */
   170 	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   171 	g_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 
   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 	pContext->StringToLocalUTF8(params[2], params[3], pInfo->pEvent->GetName(), NULL);
   189 
   190 	return 1;
   191 }
   192 
   193 static cell_t sm_GetEventBool(IPluginContext *pContext, const cell_t *params)
   194 {
   195 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   196 	HandleError err;
   197 	EventInfo *pInfo;
   198 
   199 	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
   200 		!= HandleError_None)
   201 	{
   202 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   203 	}
   204 
   205 	char *key;
   206 	pContext->LocalToString(params[2], &key);
   207 
   208 	return pInfo->pEvent->GetBool(key);
   209 }
   210 
   211 static cell_t sm_GetEventInt(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 	return pInfo->pEvent->GetInt(key);
   227 }
   228 
   229 static cell_t sm_GetEventFloat(IPluginContext *pContext, const cell_t *params)
   230 {
   231 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   232 	HandleError err;
   233 	EventInfo *pInfo;
   234 
   235 	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
   236 		!= HandleError_None)
   237 	{
   238 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   239 	}
   240 
   241 	char *key;
   242 	pContext->LocalToString(params[2], &key);
   243 
   244 	float value = pInfo->pEvent->GetFloat(key);
   245 
   246 	return sp_ftoc(value);
   247 }
   248 
   249 static cell_t sm_GetEventString(IPluginContext *pContext, const cell_t *params)
   250 {
   251 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   252 	HandleError err;
   253 	EventInfo *pInfo;
   254 
   255 	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
   256 		!= HandleError_None)
   257 	{
   258 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   259 	}
   260 
   261 	char *key;
   262 	pContext->LocalToString(params[2], &key);
   263 
   264 	pContext->StringToLocalUTF8(params[3], params[4], pInfo->pEvent->GetString(key), NULL);
   265 
   266 	return 1;
   267 }
   268 
   269 static cell_t sm_SetEventBool(IPluginContext *pContext, const cell_t *params)
   270 {
   271 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   272 	HandleError err;
   273 	EventInfo *pInfo;
   274 
   275 	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
   276 		!= HandleError_None)
   277 	{
   278 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   279 	}
   280 
   281 	char *key;
   282 	pContext->LocalToString(params[2], &key);
   283 
   284 	pInfo->pEvent->SetBool(key, params[3] ? true : false);
   285 
   286 	return 1;
   287 }
   288 
   289 static cell_t sm_SetEventInt(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;
   302 	pContext->LocalToString(params[2], &key);
   303 
   304 	pInfo->pEvent->SetInt(key, params[3]);
   305 
   306 	return 1;
   307 }
   308 
   309 static cell_t sm_SetEventFloat(IPluginContext *pContext, const cell_t *params)
   310 {
   311 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   312 	HandleError err;
   313 	EventInfo *pInfo;
   314 
   315 	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
   316 		!= HandleError_None)
   317 	{
   318 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   319 	}
   320 
   321 	char *key;
   322 	pContext->LocalToString(params[2], &key);
   323 
   324 	float value = sp_ctof(params[3]);
   325 	pInfo->pEvent->SetFloat(key, value);
   326 
   327 	return 1;
   328 }
   329 
   330 static cell_t sm_SetEventString(IPluginContext *pContext, const cell_t *params)
   331 {
   332 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   333 	HandleError err;
   334 	EventInfo *pInfo;
   335 
   336 	if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
   337 		!= HandleError_None)
   338 	{
   339 		return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
   340 	}
   341 
   342 	char *key, *value;
   343 	pContext->LocalToString(params[2], &key);
   344 	pContext->LocalToString(params[3], &value);
   345 
   346 	pInfo->pEvent->SetString(key, value);
   347 
   348 	return 1;
   349 }
   350 
   351 REGISTER_NATIVES(gameEventNatives)
   352 {
   353 	{"HookEvent",			sm_HookEvent},
   354 	{"HookEventEx",			sm_HookEventEx},
   355 	{"UnhookEvent",			sm_UnhookEvent},
   356 	{"CreateEvent",			sm_CreateEvent},
   357 	{"FireEvent",			sm_FireEvent},
   358 	{"CancelCreatedEvent",	sm_CancelCreatedEvent},
   359 	{"GetEventName",		sm_GetEventName},
   360 	{"GetEventBool",		sm_GetEventBool},
   361 	{"GetEventInt",			sm_GetEventInt},
   362 	{"GetEventFloat",		sm_GetEventFloat},
   363 	{"GetEventString",		sm_GetEventString},
   364 	{"SetEventBool",		sm_SetEventBool},
   365 	{"SetEventInt",			sm_SetEventInt},
   366 	{"SetEventFloat",		sm_SetEventFloat},
   367 	{"SetEventString",		sm_SetEventString},
   368 	{NULL,					NULL}
   369 };