1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcemm/sample_mm/SamplePlugin.cpp Tue Sep 18 20:03:47 2007 +0000
1.3 @@ -0,0 +1,237 @@
1.4 +/* ======== sample_mm ========
1.5 + * Copyright (C) 2004-2007 Metamod:Source Development Team
1.6 + * No warranties of any kind
1.7 + *
1.8 + * License: zlib/libpng
1.9 + *
1.10 + * Author(s): David "BAILOPAN" Anderson
1.11 + * ============================
1.12 + */
1.13 +
1.14 +#include <oslink.h>
1.15 +#include "SamplePlugin.h"
1.16 +#include "cvars.h"
1.17 +
1.18 +//Declare the hooks we will be using in this file. Hooking will not compile without these.
1.19 +//The macro naming scheme is SH_DECL_HOOKn[_void].
1.20 +//If you have 5 parameters, it would be HOOK5. If the function is void, add _void.
1.21 +//It stands for "SourceHook, Declare Hook".
1.22 +SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, 0, bool, char const *, char const *, char const *, char const *, bool, bool);
1.23 +SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int, int);
1.24 +SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, 0, bool);
1.25 +SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, 0);
1.26 +SH_DECL_HOOK2_void(IServerGameClients, ClientActive, SH_NOATTRIB, 0, edict_t *, bool);
1.27 +SH_DECL_HOOK1_void(IServerGameClients, ClientDisconnect, SH_NOATTRIB, 0, edict_t *);
1.28 +SH_DECL_HOOK2_void(IServerGameClients, ClientPutInServer, SH_NOATTRIB, 0, edict_t *, char const *);
1.29 +SH_DECL_HOOK1_void(IServerGameClients, SetCommandClient, SH_NOATTRIB, 0, int);
1.30 +SH_DECL_HOOK1_void(IServerGameClients, ClientSettingsChanged, SH_NOATTRIB, 0, edict_t *);
1.31 +SH_DECL_HOOK5(IServerGameClients, ClientConnect, SH_NOATTRIB, 0, bool, edict_t *, const char*, const char *, char *, int);
1.32 +SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
1.33 +SH_DECL_HOOK2(IGameEventManager2, FireEvent, SH_NOATTRIB, 0, bool, IGameEvent *, bool);
1.34 +
1.35 +SamplePlugin g_SamplePlugin;
1.36 +MyListener g_Listener;
1.37 +
1.38 +PLUGIN_EXPOSE(SamplePlugin, g_SamplePlugin);
1.39 +
1.40 +bool SamplePlugin::LevelInit(const char *pMapName, const char *pMapEntities, const char *pOldLevel, const char *pLandmarkName, bool loadGame, bool background)
1.41 +{
1.42 + META_LOG(g_PLAPI, "LevelInit() called: pMapName=%s", pMapName);
1.43 + RETURN_META_VALUE(MRES_IGNORED, true);
1.44 +}
1.45 +
1.46 +void SamplePlugin::OnLevelShutdown()
1.47 +{
1.48 + META_LOG(g_PLAPI, "OnLevelShutdown() called from listener");
1.49 +}
1.50 +
1.51 +void SamplePlugin::ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
1.52 +{
1.53 + META_LOG(g_PLAPI, "ServerActivate() called: edictCount=%d, clientMax=%d", edictCount, clientMax);
1.54 + RETURN_META(MRES_IGNORED);
1.55 +}
1.56 +
1.57 +void SamplePlugin::GameFrame(bool simulating)
1.58 +{
1.59 + //don't log this, it just pumps stuff to the screen ;]
1.60 + //META_LOG(g_PLAPI, "GameFrame() called: simulating=%d", simulating);
1.61 + RETURN_META(MRES_IGNORED);
1.62 +}
1.63 +
1.64 +void SamplePlugin::LevelShutdown( void )
1.65 +{
1.66 + META_LOG(g_PLAPI, "LevelShutdown() called");
1.67 + RETURN_META(MRES_IGNORED);
1.68 +}
1.69 +
1.70 +void SamplePlugin::ClientActive(edict_t *pEntity, bool bLoadGame)
1.71 +{
1.72 + META_LOG(g_PLAPI, "ClientActive called: pEntity=%d", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0);
1.73 + RETURN_META(MRES_IGNORED);
1.74 +}
1.75 +
1.76 +void SamplePlugin::ClientDisconnect(edict_t *pEntity)
1.77 +{
1.78 + META_LOG(g_PLAPI, "ClientDisconnect called: pEntity=%d", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0);
1.79 + RETURN_META(MRES_IGNORED);
1.80 +}
1.81 +
1.82 +void SamplePlugin::ClientPutInServer(edict_t *pEntity, char const *playername)
1.83 +{
1.84 + META_LOG(g_PLAPI, "ClientPutInServer called: pEntity=%d, playername=%s", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, playername);
1.85 + RETURN_META(MRES_IGNORED);
1.86 +}
1.87 +
1.88 +void SamplePlugin::SetCommandClient(int index)
1.89 +{
1.90 + META_LOG(g_PLAPI, "SetCommandClient() called: index=%d", index);
1.91 + RETURN_META(MRES_IGNORED);
1.92 +}
1.93 +
1.94 +void SamplePlugin::ClientSettingsChanged(edict_t *pEdict)
1.95 +{
1.96 + META_LOG(g_PLAPI, "ClientSettingsChanged called: pEdict=%d", pEdict ? m_Engine->IndexOfEdict(pEdict) : 0);
1.97 + RETURN_META(MRES_IGNORED);
1.98 +}
1.99 +
1.100 +bool SamplePlugin::ClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen)
1.101 +{
1.102 + META_LOG(g_PLAPI, "ClientConnect called: pEntity=%d, pszName=%s, pszAddress=%s", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, pszName, pszAddress);
1.103 + RETURN_META_VALUE(MRES_IGNORED, true);
1.104 +}
1.105 +
1.106 +void SamplePlugin::ClientCommand(edict_t *pEntity)
1.107 +{
1.108 + META_LOG(g_PLAPI, "ClientCommand called: pEntity=%d (commandString=%s)", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, m_Engine->Cmd_Args() ? m_Engine->Cmd_Args() : "");
1.109 + RETURN_META(MRES_IGNORED);
1.110 +}
1.111 +
1.112 +bool FireEvent_Handler(IGameEvent *event, bool bDontBroadcast)
1.113 +{
1.114 + if (!event || !event->GetName())
1.115 + RETURN_META_VALUE(MRES_IGNORED, false);
1.116 +
1.117 + /**
1.118 + * Note that this will only fire on game events that are already being listened to.
1.119 + * For events that are not firing (such as item_pickup), you must actually
1.120 + * register an event listener in the event manager. This hook is provided
1.121 + * as an example for non-eiface hooks only.
1.122 + */
1.123 +
1.124 + const char *name = event->GetName();
1.125 +
1.126 + META_LOG(g_PLAPI, "FireGameEvent called: name=%s", name);
1.127 +
1.128 + RETURN_META_VALUE(MRES_IGNORED, true);
1.129 +}
1.130 +
1.131 +bool SamplePlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
1.132 +{
1.133 + PLUGIN_SAVEVARS();
1.134 +
1.135 + GET_V_IFACE_ANY(serverFactory, m_ServerDll, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
1.136 + GET_V_IFACE_CURRENT(engineFactory, m_Engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
1.137 + GET_V_IFACE_ANY(serverFactory, m_ServerClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
1.138 + GET_V_IFACE_CURRENT(engineFactory, m_GameEventManager, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
1.139 +
1.140 + META_LOG(g_PLAPI, "Starting plugin.\n");
1.141 +
1.142 + ismm->AddListener(this, &g_Listener);
1.143 +
1.144 + //Init our cvars/concmds
1.145 + ConCommandBaseMgr::OneTimeInit(&g_Accessor);
1.146 +
1.147 + //We're hooking the following things as POST, in order to seem like Server Plugins.
1.148 + //However, I don't actually know if Valve has done server plugins as POST or not.
1.149 + //Change the last parameter to 'false' in order to change this to PRE.
1.150 + //SH_ADD_HOOK_MEMFUNC means "SourceHook, Add Hook, Member Function".
1.151 +
1.152 + //Hook LevelInit to our function
1.153 + SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelInit, true);
1.154 + //Hook ServerActivate to our function
1.155 + SH_ADD_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, m_ServerDll, &g_SamplePlugin, &SamplePlugin::ServerActivate, true);
1.156 + //Hook GameFrame to our function
1.157 + SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, m_ServerDll, &g_SamplePlugin, &SamplePlugin::GameFrame, true);
1.158 + //Hook LevelShutdown to our function -- this makes more sense as pre I guess
1.159 + SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelShutdown, false);
1.160 + //Hook ClientActivate to our function
1.161 + SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientActive, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientActive, true);
1.162 + //Hook ClientDisconnect to our function
1.163 + SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientDisconnect, true);
1.164 + //Hook ClientPutInServer to our function
1.165 + SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientPutInServer, true);
1.166 + //Hook SetCommandClient to our function
1.167 + SH_ADD_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, m_ServerClients, &g_SamplePlugin, &SamplePlugin::SetCommandClient, true);
1.168 + //Hook ClientSettingsChanged to our function
1.169 + SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientSettingsChanged, true);
1.170 +
1.171 + //The following functions are pre handled, because that's how they are in IServerPluginCallbacks
1.172 +
1.173 + //Hook ClientConnect to our function
1.174 + SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientConnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientConnect, false);
1.175 + //Hook ClientCommand to our function
1.176 + SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientCommand, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientCommand, false);
1.177 +
1.178 + //This hook is a static hook, no member function
1.179 + SH_ADD_HOOK_STATICFUNC(IGameEventManager2, FireEvent, m_GameEventManager, FireEvent_Handler, false);
1.180 +
1.181 + //Get the call class for IVServerEngine so we can safely call functions without
1.182 + // invoking their hooks (when needed).
1.183 + m_Engine_CC = SH_GET_CALLCLASS(m_Engine);
1.184 +
1.185 + SH_CALL(m_Engine_CC, &IVEngineServer::LogPrint)("All hooks started!\n");
1.186 +
1.187 + return true;
1.188 +}
1.189 +
1.190 +bool SamplePlugin::Unload(char *error, size_t maxlen)
1.191 +{
1.192 + //IT IS CRUCIAL THAT YOU REMOVE CVARS.
1.193 + //As of Metamod:Source 1.00-RC2, it will automatically remove them for you.
1.194 + //But this is only if you've registered them correctly!
1.195 +
1.196 + //Make sure we remove any hooks we did... this may not be necessary since
1.197 + //SourceHook is capable of unloading plugins' hooks itself, but just to be safe.
1.198 +
1.199 + SH_REMOVE_HOOK_STATICFUNC(IGameEventManager2, FireEvent, m_GameEventManager, FireEvent_Handler, false);
1.200 + SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelInit, true);
1.201 + SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, m_ServerDll, &g_SamplePlugin, &SamplePlugin::ServerActivate, true);
1.202 + SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, m_ServerDll, &g_SamplePlugin, &SamplePlugin::GameFrame, true);
1.203 + SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelShutdown, false);
1.204 + SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientActive, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientActive, true);
1.205 + SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientDisconnect, true);
1.206 + SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientPutInServer, true);
1.207 + SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, m_ServerClients, &g_SamplePlugin, &SamplePlugin::SetCommandClient, true);
1.208 + SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientSettingsChanged, true);
1.209 + SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientConnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientConnect, false);
1.210 + SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientCommand, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientCommand, false);
1.211 +
1.212 + //this, sourcehook does not keep track of. we must do this.
1.213 + SH_RELEASE_CALLCLASS(m_Engine_CC);
1.214 +
1.215 + return true;
1.216 +}
1.217 +
1.218 +void SamplePlugin::AllPluginsLoaded()
1.219 +{
1.220 + //we don't really need this for anything other than interplugin communication
1.221 + //and that's not used in this plugin.
1.222 + //If we really wanted, we could override the factories so other plugins can request
1.223 + // interfaces we make. In this callback, the plugin could be assured that either
1.224 + // the interfaces it requires were either loaded in another plugin or not.
1.225 +}
1.226 +
1.227 +void *MyListener::OnMetamodQuery(const char *iface, int *ret)
1.228 +{
1.229 + if (strcmp(iface, "SamplePlugin")==0)
1.230 + {
1.231 + if (ret)
1.232 + *ret = IFACE_OK;
1.233 + return static_cast<void *>(&g_SamplePlugin);
1.234 + }
1.235 +
1.236 + if (ret)
1.237 + *ret = IFACE_FAILED;
1.238 +
1.239 + return NULL;
1.240 +}