1 /* ======== sample_mm ========
2 * Copyright (C) 2004-2005 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
12 #include "SamplePlugin.h"
14 SamplePlugin g_SamplePlugin;
16 PLUGIN_EXPOSE(SamplePlugin, g_SamplePlugin);
18 //This has all of the necessary hook declarations. Read it!
19 #include "meta_hooks.h"
21 bool SamplePlugin::LevelInit(const char *pMapName, const char *pMapEntities, const char *pOldLevel, const char *pLandmarkName, bool loadGame, bool background)
23 META_LOG(g_PLAPI, "LevelInit() called: pMapName=%s", pMapName);
24 RETURN_META_VALUE(MRES_IGNORED, true);
27 void SamplePlugin::ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
29 META_LOG(g_PLAPI, "ServerActivate() called: edictCount=%d, clientMax=%d", edictCount, clientMax);
30 RETURN_META(MRES_IGNORED);
33 void SamplePlugin::GameFrame(bool simulating)
35 META_LOG(g_PLAPI, "GameFrame() called: simulating=%d", simulating);
36 RETURN_META(MRES_IGNORED);
39 void SamplePlugin::LevelShutdown( void )
41 META_LOG(g_PLAPI, "LevelShutdown() called");
42 RETURN_META(MRES_IGNORED);
45 void SamplePlugin::ClientActive(edict_t *pEntity, bool bLoadGame)
47 META_LOG(g_PLAPI, "ClientActive called: pEntity=%d", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0);
48 RETURN_META(MRES_IGNORED);
51 void SamplePlugin::ClientDisconnect(edict_t *pEntity)
53 META_LOG(g_PLAPI, "ClientDisconnect called: pEntity=%d", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0);
54 RETURN_META(MRES_IGNORED);
57 void SamplePlugin::ClientPutInServer(edict_t *pEntity, char const *playername)
59 META_LOG(g_PLAPI, "ClientPutInServer called: pEntity=%d, playername=%s", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, playername);
60 RETURN_META(MRES_IGNORED);
63 void SamplePlugin::SetCommandClient(int index)
65 META_LOG(g_PLAPI, "SetCommandClient() called: index=%d", index);
66 RETURN_META(MRES_IGNORED);
69 void SamplePlugin::ClientSettingsChanged(edict_t *pEdict)
71 META_LOG(g_PLAPI, "ClientSettingsChanged called: pEdict=%d", pEdict ? m_Engine->IndexOfEdict(pEdict) : 0);
72 RETURN_META(MRES_IGNORED);
75 bool SamplePlugin::ClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen)
77 META_LOG(g_PLAPI, "ClientConnect called: pEntity=%d, pszName=%s, pszAddress=%s", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, pszName, pszAddress);
78 RETURN_META_VALUE(MRES_IGNORED, true);
81 void SamplePlugin::ClientCommand(edict_t *pEntity)
83 META_LOG(g_PLAPI, "ClientCommand called: pEntity=%d (commandString=%s)", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, m_Engine->Cmd_Args() ? m_Engine->Cmd_Args() : "");
84 RETURN_META(MRES_IGNORED);
87 bool SamplePlugin::Load(PluginId id, ISmmAPI *ismm, factories *list, char *error, size_t maxlen)
91 m_ServerDll = (IServerGameDLL *)((ismm->serverFactory())(INTERFACEVERSION_SERVERGAMEDLL, NULL));
92 m_Engine = (IVEngineServer *)((ismm->engineFactory())(INTERFACEVERSION_VENGINESERVER, NULL));
93 m_ServerClients = (IServerGameClients *)((ismm->serverFactory())(INTERFACEVERSION_SERVERGAMECLIENTS, NULL));
97 snprintf(error, maxlen, "Could not find interface %s", INTERFACEVERSION_SERVERGAMEDLL);
102 snprintf(error, maxlen, "Could not find interface %s", INTERFACEVERSION_VENGINESERVER);
105 if (!m_ServerClients)
107 snprintf(error, maxlen, "Could not find interface %s", INTERFACEVERSION_SERVERGAMECLIENTS);
111 //We're hooking the following things as POST, in order to seem like Server Plugins.
112 //However, I don't actually know if Valve has done server plugins as POST or not.
113 //Change the last parameter to 'false' in order to change this to PRE.
114 //SH_ADD_HOOK_MEMFUNC means "SourceHook, Add Hook, Member Function".
116 //Hook LevelInit to our function
117 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, m_ServerDll, &g_SamplePlugin, LevelInit, true);
118 //Hook ServerActivate to our function
119 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, m_ServerDll, &g_SamplePlugin, ServerActivate, true);
120 //Hook GameFrame to our function
121 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, m_ServerDll, &g_SamplePlugin, GameFrame, true);
122 //Hook LevelShutdown to our function -- this makes more sense as pre I guess
123 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, m_ServerDll, &g_SamplePlugin, LevelShutdown, false);
124 //Hook ClientActivate to our function
125 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientActive, m_ServerClients, &g_SamplePlugin, ClientActive, true);
126 //Hook ClientDisconnect to our function
127 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, m_ServerClients, &g_SamplePlugin, ClientDisconnect, true);
128 //Hook ClientPutInServer to our function
129 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, m_ServerClients, &g_SamplePlugin, ClientPutInServer, true);
130 //Hook SetCommandClient to our function
131 SH_ADD_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, m_ServerClients, &g_SamplePlugin, SetCommandClient, true);
132 //Hook ClientSettingsChanged to our function
133 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, m_ServerClients, &g_SamplePlugin, ClientSettingsChanged, true);
135 //The following functions are pre handled, because that's how they are in IServerPluginCallbacks
137 //Hook ClientConnect to our function
138 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientConnect, m_ServerClients, &g_SamplePlugin, ClientConnect, false);
139 //Hook ClientCommand to our function
140 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientCommand, m_ServerClients, &g_SamplePlugin, ClientCommand, false);
145 bool SamplePlugin::Unload(char *error, size_t maxlen)
147 //Make sure we remove any hooks we did... this may not be necessary since
148 // SourceHook is capable of unloading plugins' hooks itself, but just to be safe.
150 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, m_ServerDll, &g_SamplePlugin, LevelInit, true);
151 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, m_ServerDll, &g_SamplePlugin, ServerActivate, true);
152 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, m_ServerDll, &g_SamplePlugin, GameFrame, true);
153 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, m_ServerDll, &g_SamplePlugin, LevelShutdown, false);
154 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientActive, m_ServerClients, &g_SamplePlugin, ClientActive, true);
155 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, m_ServerClients, &g_SamplePlugin, ClientDisconnect, true);
156 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, m_ServerClients, &g_SamplePlugin, ClientPutInServer, true);
157 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, m_ServerClients, &g_SamplePlugin, SetCommandClient, true);
158 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, m_ServerClients, &g_SamplePlugin, ClientSettingsChanged, true);
159 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientConnect, m_ServerClients, &g_SamplePlugin, ClientConnect, false);
160 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientCommand, m_ServerClients, &g_SamplePlugin, ClientCommand, false);
165 void SamplePlugin::AllPluginsLoaded()
167 //we don't really need this for anything other than interplugin communication
168 //and that's not used in this plugin.
169 //If we really wanted, we could override the factories so other plugins can request
170 // interfaces we make. In this callback, the plugin could be assured that either
171 // the interfaces it requires were either loaded in another plugin or not.