1 /* ======== sample_mm ========
2 * Copyright (C) 2004-2006 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
12 #include "SamplePlugin.h"
15 //Declare the hooks we will be using in this file. Hooking will not compile without these.
16 //The macro naming scheme is SH_DECL_HOOKn[_void].
17 //If you have 5 parameters, it would be HOOK5. If the function is void, add _void.
18 //It stands for "SourceHook, Declare Hook".
19 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, 0, bool, char const *, char const *, char const *, char const *, bool, bool);
20 SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int, int);
21 SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, 0, bool);
22 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, 0);
23 SH_DECL_HOOK2_void(IServerGameClients, ClientActive, SH_NOATTRIB, 0, edict_t *, bool);
24 SH_DECL_HOOK1_void(IServerGameClients, ClientDisconnect, SH_NOATTRIB, 0, edict_t *);
25 SH_DECL_HOOK2_void(IServerGameClients, ClientPutInServer, SH_NOATTRIB, 0, edict_t *, char const *);
26 SH_DECL_HOOK1_void(IServerGameClients, SetCommandClient, SH_NOATTRIB, 0, int);
27 SH_DECL_HOOK1_void(IServerGameClients, ClientSettingsChanged, SH_NOATTRIB, 0, edict_t *);
28 SH_DECL_HOOK5(IServerGameClients, ClientConnect, SH_NOATTRIB, 0, bool, edict_t *, const char*, const char *, char *, int);
29 SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
30 SH_DECL_HOOK2(IGameEventManager2, FireEvent, SH_NOATTRIB, 0, bool, IGameEvent *, bool);
32 SamplePlugin g_SamplePlugin;
33 MyListener g_Listener;
35 PLUGIN_EXPOSE(SamplePlugin, g_SamplePlugin);
37 bool SamplePlugin::LevelInit(const char *pMapName, const char *pMapEntities, const char *pOldLevel, const char *pLandmarkName, bool loadGame, bool background)
39 META_LOG(g_PLAPI, "LevelInit() called: pMapName=%s", pMapName);
40 RETURN_META_VALUE(MRES_IGNORED, true);
43 void SamplePlugin::OnLevelShutdown()
45 META_LOG(g_PLAPI, "OnLevelShutdown() called from listener");
48 void SamplePlugin::ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
50 META_LOG(g_PLAPI, "ServerActivate() called: edictCount=%d, clientMax=%d", edictCount, clientMax);
51 RETURN_META(MRES_IGNORED);
54 void SamplePlugin::GameFrame(bool simulating)
56 //don't log this, it just pumps stuff to the screen ;]
57 //META_LOG(g_PLAPI, "GameFrame() called: simulating=%d", simulating);
58 RETURN_META(MRES_IGNORED);
61 void SamplePlugin::LevelShutdown( void )
63 META_LOG(g_PLAPI, "LevelShutdown() called");
64 RETURN_META(MRES_IGNORED);
67 void SamplePlugin::ClientActive(edict_t *pEntity, bool bLoadGame)
69 META_LOG(g_PLAPI, "ClientActive called: pEntity=%d", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0);
70 RETURN_META(MRES_IGNORED);
73 void SamplePlugin::ClientDisconnect(edict_t *pEntity)
75 META_LOG(g_PLAPI, "ClientDisconnect called: pEntity=%d", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0);
76 RETURN_META(MRES_IGNORED);
79 void SamplePlugin::ClientPutInServer(edict_t *pEntity, char const *playername)
81 META_LOG(g_PLAPI, "ClientPutInServer called: pEntity=%d, playername=%s", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, playername);
82 RETURN_META(MRES_IGNORED);
85 void SamplePlugin::SetCommandClient(int index)
87 META_LOG(g_PLAPI, "SetCommandClient() called: index=%d", index);
88 RETURN_META(MRES_IGNORED);
91 void SamplePlugin::ClientSettingsChanged(edict_t *pEdict)
93 META_LOG(g_PLAPI, "ClientSettingsChanged called: pEdict=%d", pEdict ? m_Engine->IndexOfEdict(pEdict) : 0);
94 RETURN_META(MRES_IGNORED);
97 bool SamplePlugin::ClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen)
99 META_LOG(g_PLAPI, "ClientConnect called: pEntity=%d, pszName=%s, pszAddress=%s", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, pszName, pszAddress);
100 RETURN_META_VALUE(MRES_IGNORED, true);
103 void SamplePlugin::ClientCommand(edict_t *pEntity)
105 META_LOG(g_PLAPI, "ClientCommand called: pEntity=%d (commandString=%s)", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, m_Engine->Cmd_Args() ? m_Engine->Cmd_Args() : "");
106 RETURN_META(MRES_IGNORED);
109 bool FireEvent_Handler(IGameEvent *event, bool bDontBroadcast)
111 if (!event || !event->GetName())
112 RETURN_META_VALUE(MRES_IGNORED, false);
115 * Note that this will only fire on game events that are already being listened to.
116 * For events that are not firing (such as item_pickup), you must actually
117 * register an event listener in the event manager. This hook is provided
118 * as an example for non-eiface hooks only.
121 const char *name = event->GetName();
123 META_LOG(g_PLAPI, "FireGameEvent called: name=%s", name);
125 RETURN_META_VALUE(MRES_IGNORED, true);
128 #define GET_V_IFACE(v_factory, v_var, v_type, v_name) \
129 v_var = (v_type *)ismm->VInterfaceMatch(ismm->v_factory(), v_name); \
132 if (error && maxlen) \
134 snprintf(error, maxlen, "Could not find interface: %s", v_name); \
140 bool SamplePlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
144 GET_V_IFACE(serverFactory, m_ServerDll, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
145 GET_V_IFACE(engineFactory, m_Engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
146 GET_V_IFACE(serverFactory, m_ServerClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
147 GET_V_IFACE(engineFactory, m_GameEventManager, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
149 META_LOG(g_PLAPI, "Starting plugin.\n");
151 ismm->AddListener(this, &g_Listener);
153 //Init our cvars/concmds
154 ConCommandBaseMgr::OneTimeInit(&g_Accessor);
156 //We're hooking the following things as POST, in order to seem like Server Plugins.
157 //However, I don't actually know if Valve has done server plugins as POST or not.
158 //Change the last parameter to 'false' in order to change this to PRE.
159 //SH_ADD_HOOK_MEMFUNC means "SourceHook, Add Hook, Member Function".
161 //Hook LevelInit to our function
162 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelInit, true);
163 //Hook ServerActivate to our function
164 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, m_ServerDll, &g_SamplePlugin, &SamplePlugin::ServerActivate, true);
165 //Hook GameFrame to our function
166 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, m_ServerDll, &g_SamplePlugin, &SamplePlugin::GameFrame, true);
167 //Hook LevelShutdown to our function -- this makes more sense as pre I guess
168 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelShutdown, false);
169 //Hook ClientActivate to our function
170 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientActive, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientActive, true);
171 //Hook ClientDisconnect to our function
172 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientDisconnect, true);
173 //Hook ClientPutInServer to our function
174 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientPutInServer, true);
175 //Hook SetCommandClient to our function
176 SH_ADD_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, m_ServerClients, &g_SamplePlugin, &SamplePlugin::SetCommandClient, true);
177 //Hook ClientSettingsChanged to our function
178 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientSettingsChanged, true);
180 //The following functions are pre handled, because that's how they are in IServerPluginCallbacks
182 //Hook ClientConnect to our function
183 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientConnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientConnect, false);
184 //Hook ClientCommand to our function
185 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientCommand, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientCommand, false);
187 //This hook is a static hook, no member function
188 SH_ADD_HOOK_STATICFUNC(IGameEventManager2, FireEvent, m_GameEventManager, FireEvent_Handler, false);
190 //Get the call class for IVServerEngine so we can safely call functions without
191 // invoking their hooks (when needed).
192 m_Engine_CC = SH_GET_CALLCLASS(m_Engine);
194 SH_CALL(m_Engine_CC, &IVEngineServer::LogPrint)("All hooks started!\n");
196 g_SMAPI->AddListener(g_PLAPI, this);
201 bool SamplePlugin::Unload(char *error, size_t maxlen)
203 //IT IS CRUCIAL THAT YOU REMOVE CVARS.
204 //As of Metamod:Source 1.00-RC2, it will automatically remove them for you.
205 //But this is only if you've registered them correctly!
207 //Make sure we remove any hooks we did... this may not be necessary since
208 //SourceHook is capable of unloading plugins' hooks itself, but just to be safe.
210 SH_REMOVE_HOOK_STATICFUNC(IGameEventManager2, FireEvent, m_GameEventManager, FireEvent_Handler, false);
211 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelInit, true);
212 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, m_ServerDll, &g_SamplePlugin, &SamplePlugin::ServerActivate, true);
213 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, m_ServerDll, &g_SamplePlugin, &SamplePlugin::GameFrame, true);
214 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelShutdown, false);
215 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientActive, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientActive, true);
216 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientDisconnect, true);
217 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientPutInServer, true);
218 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, m_ServerClients, &g_SamplePlugin, &SamplePlugin::SetCommandClient, true);
219 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientSettingsChanged, true);
220 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientConnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientConnect, false);
221 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientCommand, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientCommand, false);
223 //this, sourcehook does not keep track of. we must do this.
224 SH_RELEASE_CALLCLASS(m_Engine_CC);
229 void SamplePlugin::AllPluginsLoaded()
231 //we don't really need this for anything other than interplugin communication
232 //and that's not used in this plugin.
233 //If we really wanted, we could override the factories so other plugins can request
234 // interfaces we make. In this callback, the plugin could be assured that either
235 // the interfaces it requires were either loaded in another plugin or not.
238 void *MyListener::OnMetamodQuery(const char *iface, int *ret)
240 if (strcmp(iface, "SamplePlugin")==0)
244 return static_cast<void *>(&g_SamplePlugin);