Added ISDKHooks interface with entity listeners (bug 5602, r=psychonic).
Added ISDKHooks interface with entity listeners (
bug 5602, r=psychonic).
1.1 --- a/extensions/sdkhooks/extension.cpp Sat Aug 03 19:20:03 2013 -0400
1.2 +++ b/extensions/sdkhooks/extension.cpp Sun Aug 04 10:03:40 2013 -0400
1.3 @@ -185,6 +185,7 @@
1.4 sharesys->AddDependency(myself, "bintools.ext", true, true);
1.5 sharesys->AddNatives(myself, g_Natives);
1.6 sharesys->RegisterLibrary(myself, "sdkhooks");
1.7 + sharesys->AddInterface(myself, &g_Interface);
1.8 sharesys->AddCapabilityProvider(myself, this, "SDKHook_DmgCustomInOTD");
1.9 sharesys->AddCapabilityProvider(myself, this, "SDKHook_LogicalEntSupport");
1.10
1.11 @@ -391,18 +392,36 @@
1.12
1.13 void SDKHooks::OnClientPutInServer(int client)
1.14 {
1.15 - g_pOnEntityCreated->PushCell(client);
1.16 + CBaseEntity *pPlayer = gamehelpers->ReferenceToEntity(client);
1.17 + const char *pName = gamehelpers->GetEntityClassname(pPlayer);
1.18
1.19 - CBaseEntity *pPlayer = gamehelpers->ReferenceToEntity(client);
1.20 + // Send OnEntityCreated to SM listeners
1.21 + SourceHook::List<ISMEntityListener *>::iterator iter;
1.22 + ISMEntityListener *pListener = NULL;
1.23 + for (iter=m_EntListeners.begin(); iter!=m_EntListeners.end(); iter++)
1.24 + {
1.25 + pListener = (*iter);
1.26 + pListener->OnEntityCreated(pPlayer, pName ? pName : "");
1.27 + }
1.28
1.29 - const char * pName = gamehelpers->GetEntityClassname(pPlayer);
1.30 -
1.31 + // Call OnEntityCreated forward
1.32 + g_pOnEntityCreated->PushCell(client);
1.33 g_pOnEntityCreated->PushString(pName ? pName : "");
1.34 g_pOnEntityCreated->Execute(NULL);
1.35
1.36 m_EntityExists.Set(client);
1.37 }
1.38
1.39 +void SDKHooks::AddEntityListener(ISMEntityListener *listener)
1.40 +{
1.41 + m_EntListeners.push_back(listener);
1.42 +}
1.43 +
1.44 +void SDKHooks::RemoveEntityListener(ISMEntityListener *listener)
1.45 +{
1.46 + m_EntListeners.remove(listener);
1.47 +}
1.48 +
1.49 bool SDKHooks::RegisterConCommandBase(ConCommandBase *pVar)
1.50 {
1.51 /* Always call META_REGCVAR instead of going through the engine. */
1.52 @@ -824,11 +843,19 @@
1.53 return;
1.54 }
1.55
1.56 - g_pOnEntityCreated->PushCell(gamehelpers->EntityToBCompatRef(pEntity));
1.57 + const char *pName = gamehelpers->GetEntityClassname(pEntity);
1.58
1.59 - const char * pName = gamehelpers->GetEntityClassname(pEntity);
1.60 + // Send OnEntityCreated to SM listeners
1.61 + SourceHook::List<ISMEntityListener *>::iterator iter;
1.62 + ISMEntityListener *pListener = NULL;
1.63 + for (iter=m_EntListeners.begin(); iter!=m_EntListeners.end(); iter++)
1.64 + {
1.65 + pListener = (*iter);
1.66 + pListener->OnEntityCreated(pEntity, pName ? pName : "");
1.67 + }
1.68 +
1.69 + g_pOnEntityCreated->PushCell(gamehelpers->EntityToBCompatRef(pEntity));
1.70 g_pOnEntityCreated->PushString(pName ? pName : "");
1.71 -
1.72 g_pOnEntityCreated->Execute(NULL);
1.73
1.74 m_EntityExists.Set(entity);
1.75 @@ -1394,6 +1421,15 @@
1.76
1.77 void SDKHooks::OnEntityDeleted(CBaseEntity *pEntity)
1.78 {
1.79 + // Send OnEntityDestroyed to SM listeners
1.80 + SourceHook::List<ISMEntityListener *>::iterator iter;
1.81 + ISMEntityListener *pListener = NULL;
1.82 + for (iter=m_EntListeners.begin(); iter!=m_EntListeners.end(); iter++)
1.83 + {
1.84 + pListener = (*iter);
1.85 + pListener->OnEntityDestroyed(pEntity);
1.86 + }
1.87 +
1.88 int entity = gamehelpers->EntityToBCompatRef(pEntity);
1.89
1.90 // Call OnEntityDestroyed forward
2.1 --- a/extensions/sdkhooks/extension.h Sat Aug 03 19:20:03 2013 -0400
2.2 +++ b/extensions/sdkhooks/extension.h Sun Aug 04 10:03:40 2013 -0400
2.3 @@ -2,8 +2,10 @@
2.4 #define _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_
2.5
2.6 #include "smsdk_ext.h"
2.7 +#include <ISDKHooks.h>
2.8 #include <IBinTools.h>
2.9 #include <convar.h>
2.10 +#include <sh_list.h>
2.11
2.12 #include <iplayerinfo.h>
2.13 #include <shareddefs.h>
2.14 @@ -120,7 +122,8 @@
2.15 public IPluginsListener,
2.16 public IFeatureProvider,
2.17 public IEntityListener,
2.18 - public IClientListener
2.19 + public IClientListener,
2.20 + public ISDKHooks
2.21 {
2.22 public:
2.23 /**
2.24 @@ -215,6 +218,13 @@
2.25 public: // IClientListener
2.26 virtual void OnClientPutInServer(int client);
2.27
2.28 +public: // ISDKHooks
2.29 + virtual void AddEntityListener(ISMEntityListener *listener);
2.30 + virtual void RemoveEntityListener(ISMEntityListener *listener);
2.31 +
2.32 +private:
2.33 + SourceHook::List<ISMEntityListener *> m_EntListeners;
2.34 +
2.35 public:
2.36 /**
2.37 * Functions
3.1 --- a/extensions/sdkhooks/msvc10/sdkhooks.vcxproj Sat Aug 03 19:20:03 2013 -0400
3.2 +++ b/extensions/sdkhooks/msvc10/sdkhooks.vcxproj Sun Aug 04 10:03:40 2013 -0400
3.3 @@ -1300,6 +1300,7 @@
3.4 <ClCompile Include="..\sdk\smsdk_ext.cpp" />
3.5 </ItemGroup>
3.6 <ItemGroup>
3.7 + <ClInclude Include="..\..\..\public\extensions\ISDKHooks.h" />
3.8 <ClInclude Include="..\extension.h" />
3.9 <ClInclude Include="..\macros.h" />
3.10 <ClInclude Include="..\natives.h" />
4.1 --- a/extensions/sdkhooks/msvc10/sdkhooks.vcxproj.filters Sat Aug 03 19:20:03 2013 -0400
4.2 +++ b/extensions/sdkhooks/msvc10/sdkhooks.vcxproj.filters Sun Aug 04 10:03:40 2013 -0400
4.3 @@ -56,6 +56,9 @@
4.4 <ClInclude Include="..\sdk\smsdk_ext.h">
4.5 <Filter>SourceMod SDK</Filter>
4.6 </ClInclude>
4.7 + <ClInclude Include="..\..\..\public\extensions\ISDKHooks.h">
4.8 + <Filter>Header Files</Filter>
4.9 + </ClInclude>
4.10 </ItemGroup>
4.11 <ItemGroup>
4.12 <ResourceCompile Include="..\version.rc">
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/public/extensions/ISDKHooks.h Sun Aug 04 10:03:40 2013 -0400
5.3 @@ -0,0 +1,107 @@
5.4 +/**
5.5 + * vim: set ts=4 :
5.6 + * =============================================================================
5.7 + * Source SDK Hooks Extension
5.8 + * Copyright (C) 2010-2012 Nicholas Hastings
5.9 + * Copyright (C) 2009-2010 Erik Minekus
5.10 + * =============================================================================
5.11 + *
5.12 + * This program is free software; you can redistribute it and/or modify it under
5.13 + * the terms of the GNU General Public License, version 3.0, as published by the
5.14 + * Free Software Foundation.
5.15 + *
5.16 + * This program is distributed in the hope that it will be useful, but WITHOUT
5.17 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
5.18 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
5.19 + * details.
5.20 + *
5.21 + * You should have received a copy of the GNU General Public License along with
5.22 + * this program. If not, see <http://www.gnu.org/licenses/>.
5.23 + *
5.24 + * As a special exception, AlliedModders LLC gives you permission to link the
5.25 + * code of this program (as well as its derivative works) to "Half-Life 2," the
5.26 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
5.27 + * by the Valve Corporation. You must obey the GNU General Public License in
5.28 + * all respects for all other code used. Additionally, AlliedModders LLC grants
5.29 + * this exception to all derivative works. AlliedModders LLC defines further
5.30 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
5.31 + * or <http://www.sourcemod.net/license.php>.
5.32 + *
5.33 + * Version: $Id$
5.34 + */
5.35 +
5.36 +#ifndef _INCLUDE_SMEXT_I_SDKHOOKS_H_
5.37 +#define _INCLUDE_SMEXT_I_SDKHOOKS_H_
5.38 +
5.39 +#include <IShareSys.h>
5.40 +
5.41 +#define SMINTERFACE_SDKHOOKS_NAME "ISDKHooks"
5.42 +#define SMINTERFACE_SDKHOOKS_VERSION 1
5.43 +
5.44 +class CBaseEntity;
5.45 +
5.46 +/**
5.47 + * @brief SDKHooks shared API
5.48 + * @file ISDKHooks.h
5.49 + */
5.50 +
5.51 +namespace SourceMod
5.52 +{
5.53 + /**
5.54 + * @brief Provides callbacks for entity events.
5.55 + */
5.56 + class ISMEntityListener
5.57 + {
5.58 + public:
5.59 + /**
5.60 + * @brief When an entity is created
5.61 + *
5.62 + * @param pEntity CBaseEntity entity.
5.63 + * @param classname Entity classname.
5.64 + */
5.65 + virtual void OnEntityCreated(CBaseEntity *pEntity, const char *classname)
5.66 + {
5.67 + }
5.68 +
5.69 + /**
5.70 + * @brief When an entity is destroyed
5.71 + *
5.72 + * @param pEntity CBaseEntity entity.
5.73 + */
5.74 + virtual void OnEntityDestroyed(CBaseEntity *pEntity)
5.75 + {
5.76 + }
5.77 + };
5.78 +
5.79 + /**
5.80 + * @brief SDKHooks API.
5.81 + */
5.82 + class ISDKHooks : public SMInterface
5.83 + {
5.84 + public:
5.85 + virtual const char *GetInterfaceName()
5.86 + {
5.87 + return SMINTERFACE_SDKHOOKS_NAME;
5.88 + }
5.89 + virtual unsigned int GetInterfaceVersion()
5.90 + {
5.91 + return SMINTERFACE_SDKHOOKS_VERSION;
5.92 + }
5.93 + public:
5.94 + /**
5.95 + * @brief Adds an entity listener.
5.96 + *
5.97 + * @param listener Pointer to an ISMEntityListener.
5.98 + */
5.99 + virtual void AddEntityListener(ISMEntityListener *listener) =0;
5.100 +
5.101 + /**
5.102 + * @brief Removes an entity listener.
5.103 + *
5.104 + * @param listener Pointer to an ISMEntityListener.
5.105 + */
5.106 + virtual void RemoveEntityListener(ISMEntityListener *listener) =0;
5.107 + };
5.108 +}
5.109 +
5.110 +#endif /* _INCLUDE_SMEXT_I_SDKHOOKS_H_ */