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 bool SamplePlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
132 GET_V_IFACE_ANY(serverFactory, m_ServerDll, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
133 GET_V_IFACE_CURRENT(engineFactory, m_Engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
134 GET_V_IFACE_ANY(serverFactory, m_ServerClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
135 GET_V_IFACE_CURRENT(engineFactory, m_GameEventManager, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
137 META_LOG(g_PLAPI, "Starting plugin.\n");
139 ismm->AddListener(this, &g_Listener);
141 //Init our cvars/concmds
142 ConCommandBaseMgr::OneTimeInit(&g_Accessor);
144 //We're hooking the following things as POST, in order to seem like Server Plugins.
145 //However, I don't actually know if Valve has done server plugins as POST or not.
146 //Change the last parameter to 'false' in order to change this to PRE.
147 //SH_ADD_HOOK_MEMFUNC means "SourceHook, Add Hook, Member Function".
149 //Hook LevelInit to our function
150 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelInit, true);
151 //Hook ServerActivate to our function
152 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, m_ServerDll, &g_SamplePlugin, &SamplePlugin::ServerActivate, true);
153 //Hook GameFrame to our function
154 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, m_ServerDll, &g_SamplePlugin, &SamplePlugin::GameFrame, true);
155 //Hook LevelShutdown to our function -- this makes more sense as pre I guess
156 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelShutdown, false);
157 //Hook ClientActivate to our function
158 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientActive, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientActive, true);
159 //Hook ClientDisconnect to our function
160 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientDisconnect, true);
161 //Hook ClientPutInServer to our function
162 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientPutInServer, true);
163 //Hook SetCommandClient to our function
164 SH_ADD_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, m_ServerClients, &g_SamplePlugin, &SamplePlugin::SetCommandClient, true);
165 //Hook ClientSettingsChanged to our function
166 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientSettingsChanged, true);
168 //The following functions are pre handled, because that's how they are in IServerPluginCallbacks
170 //Hook ClientConnect to our function
171 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientConnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientConnect, false);
172 //Hook ClientCommand to our function
173 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientCommand, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientCommand, false);
175 //This hook is a static hook, no member function
176 SH_ADD_HOOK_STATICFUNC(IGameEventManager2, FireEvent, m_GameEventManager, FireEvent_Handler, false);
178 //Get the call class for IVServerEngine so we can safely call functions without
179 // invoking their hooks (when needed).
180 m_Engine_CC = SH_GET_CALLCLASS(m_Engine);
182 SH_CALL(m_Engine_CC, &IVEngineServer::LogPrint)("All hooks started!\n");
184 g_SMAPI->AddListener(g_PLAPI, this);
189 bool SamplePlugin::Unload(char *error, size_t maxlen)
191 //IT IS CRUCIAL THAT YOU REMOVE CVARS.
192 //As of Metamod:Source 1.00-RC2, it will automatically remove them for you.
193 //But this is only if you've registered them correctly!
195 //Make sure we remove any hooks we did... this may not be necessary since
196 //SourceHook is capable of unloading plugins' hooks itself, but just to be safe.
198 SH_REMOVE_HOOK_STATICFUNC(IGameEventManager2, FireEvent, m_GameEventManager, FireEvent_Handler, false);
199 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelInit, true);
200 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, m_ServerDll, &g_SamplePlugin, &SamplePlugin::ServerActivate, true);
201 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, m_ServerDll, &g_SamplePlugin, &SamplePlugin::GameFrame, true);
202 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelShutdown, false);
203 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientActive, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientActive, true);
204 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientDisconnect, true);
205 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientPutInServer, true);
206 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, m_ServerClients, &g_SamplePlugin, &SamplePlugin::SetCommandClient, true);
207 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientSettingsChanged, true);
208 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientConnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientConnect, false);
209 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientCommand, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientCommand, false);
211 //this, sourcehook does not keep track of. we must do this.
212 SH_RELEASE_CALLCLASS(m_Engine_CC);
217 void SamplePlugin::AllPluginsLoaded()
219 //we don't really need this for anything other than interplugin communication
220 //and that's not used in this plugin.
221 //If we really wanted, we could override the factories so other plugins can request
222 // interfaces we make. In this callback, the plugin could be assured that either
223 // the interfaces it requires were either loaded in another plugin or not.
226 void *MyListener::OnMetamodQuery(const char *iface, int *ret)
228 if (strcmp(iface, "SamplePlugin")==0)
232 return static_cast<void *>(&g_SamplePlugin);