sourcemm/sample_mm/SamplePlugin.cpp
changeset 22 3fe8e457c5a2
parent 20 7d2ff2c0999b
child 24 9f04f9fb2485
     1.1 --- a/sourcemm/sample_mm/SamplePlugin.cpp	Mon Apr 18 19:50:33 2005 +0000
     1.2 +++ b/sourcemm/sample_mm/SamplePlugin.cpp	Mon Apr 18 23:33:44 2005 +0000
     1.3 @@ -5,46 +5,168 @@
     1.4  
     1.5  PLUGIN_EXPOSE(SamplePlugin, g_SamplePlugin);
     1.6  
     1.7 -SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, 0, bool, char const *, char const *, char const *, char const *, bool, bool);
     1.8 +//This has all of the necessary hook declarations.  Read it!
     1.9 +#include "meta_hooks.h"
    1.10 +
    1.11 +bool SamplePlugin::LevelInit(const char *pMapName, const char *pMapEntities, const char *pOldLevel, const char *pLandmarkName, bool loadGame, bool background)
    1.12 +{
    1.13 +	META_LOG(g_PLAPI, "LevelInit() called: pMapName=%s", pMapName); 
    1.14 +	RETURN_META_VALUE(MRES_IGNORED, true);
    1.15 +}
    1.16 +
    1.17 +void SamplePlugin::ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
    1.18 +{
    1.19 +	META_LOG(g_PLAPI, "ServerActivate() called: edictCount=%d, clientMax=%d", edictCount, clientMax);
    1.20 +	RETURN_META(MRES_IGNORED);
    1.21 +}
    1.22 +
    1.23 +void SamplePlugin::GameFrame(bool simulating)
    1.24 +{
    1.25 +	META_LOG(g_PLAPI, "GameFrame() called: simulating=%d", simulating);
    1.26 +	RETURN_META(MRES_IGNORED);
    1.27 +}
    1.28 +
    1.29 +void SamplePlugin::LevelShutdown( void )
    1.30 +{
    1.31 +	META_LOG(g_PLAPI, "LevelShutdown() called");
    1.32 +	RETURN_META(MRES_IGNORED);
    1.33 +}
    1.34 +
    1.35 +void SamplePlugin::ClientActive(edict_t *pEntity, bool bLoadGame)
    1.36 +{
    1.37 +	META_LOG(g_PLAPI, "ClientActive called: pEntity=%d", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0);
    1.38 +	RETURN_META(MRES_IGNORED);
    1.39 +}
    1.40  
    1.41 -bool LevelInit_handler( char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background );
    1.42 +void SamplePlugin::ClientDisconnect(edict_t *pEntity)
    1.43 +{
    1.44 +	META_LOG(g_PLAPI, "ClientDisconnect called: pEntity=%d", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0);
    1.45 +	RETURN_META(MRES_IGNORED);
    1.46 +}
    1.47 +
    1.48 +void SamplePlugin::ClientPutInServer(edict_t *pEntity, char const *playername)
    1.49 +{
    1.50 +	META_LOG(g_PLAPI, "ClientActivate called: pEntity=%d, playername=%s", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, playername);
    1.51 +	RETURN_META(MRES_IGNORED);
    1.52 +}
    1.53 +
    1.54 +void SamplePlugin::SetCommandClient(int index)
    1.55 +{
    1.56 +	META_LOG(g_PLAPI, "SetCommandClient() called: index=%d", index);
    1.57 +	RETURN_META(MRES_IGNORED);
    1.58 +}
    1.59 +
    1.60 +void SamplePlugin::ClientSettingsChanged(edict_t *pEdict)
    1.61 +{
    1.62 +	META_LOG(g_PLAPI, "ClientSettingsChanged called: pEdict=%d", pEdict ? m_Engine->IndexOfEdict(pEdict) : 0);
    1.63 +	RETURN_META(MRES_IGNORED);
    1.64 +}
    1.65 +
    1.66 +bool SamplePlugin::ClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen)
    1.67 +{
    1.68 +	META_LOG(g_PLAPI, "ClientConnect called: pEntity=%d, pszName=%s, pszAddress=%s", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, pszName, pszAddress);
    1.69 +	RETURN_META_VALUE(MRES_IGNORED, true);
    1.70 +}
    1.71 +
    1.72 +void SamplePlugin::ClientCommand(edict_t *pEntity)
    1.73 +{
    1.74 +	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.75 +	RETURN_META(MRES_IGNORED);
    1.76 +}
    1.77  
    1.78  bool SamplePlugin::Load(PluginId id, ISmmAPI *ismm, factories *list, char *error, size_t maxlen)
    1.79  {
    1.80  	PLUGIN_SAVEVARS();
    1.81  
    1.82 -	IServerGameDLL *isgd = (IServerGameDLL *)((ismm->serverFactory())(INTERFACEVERSION_SERVERGAMEDLL, NULL));
    1.83 +	m_ServerDll = (IServerGameDLL *)((ismm->serverFactory())(INTERFACEVERSION_SERVERGAMEDLL, NULL));
    1.84 +	m_Engine = (IVEngineServer *)((ismm->engineFactory())(INTERFACEVERSION_VENGINESERVER, NULL));
    1.85 +	m_ServerClients = (IServerGameClients *)((ismm->serverFactory())(INTERFACEVERSION_SERVERGAMECLIENTS, NULL));
    1.86  
    1.87 -	if (!isgd)
    1.88 +	if (!m_ServerDll)
    1.89  	{
    1.90  		snprintf(error, maxlen, "Could not find interface %s", INTERFACEVERSION_SERVERGAMEDLL);
    1.91  		return false;
    1.92  	}
    1.93 +	if (!m_Engine)
    1.94 +	{
    1.95 +		snprintf(error, maxlen, "Could not find interface %s", INTERFACEVERSION_VENGINESERVER);
    1.96 +		return false;
    1.97 +	}
    1.98 +	if (!m_ServerClients)
    1.99 +	{
   1.100 +		snprintf(error, maxlen, "Could not find interface %s", INTERFACEVERSION_SERVERGAMECLIENTS);
   1.101 +		return false;
   1.102 +	}
   1.103  
   1.104 -	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelInit, isgd, LevelInit_handler, false);
   1.105 +	//We're hooking the following things as POST, in order to seem like Server Plugins.
   1.106 +	//However, I don't actually know if Valve has done server plugins as POST or not.
   1.107 +	//Change the last parameter to 'false' in order to change this to PRE.
   1.108 +	//SH_ADD_HOOK_MEMFUNC means "SourceHook, Add Hook, Member Function".
   1.109 +
   1.110 +	//Hook LevelInit to our function
   1.111 +	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, m_ServerDll, &g_SamplePlugin, LevelInit, true);
   1.112 +	//Hook ServerActivate to our function
   1.113 +	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, m_ServerDll, &g_SamplePlugin, ServerActivate, true);
   1.114 +	//Hook GameFrame to our function
   1.115 +	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, m_ServerDll, &g_SamplePlugin, GameFrame, true);
   1.116 +	//Hook LevelShutdown to our function -- this makes more sense as pre I guess
   1.117 +	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, m_ServerDll, &g_SamplePlugin, LevelShutdown, false);
   1.118 +	//Hook ClientActivate to our function
   1.119 +	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientActive, m_ServerClients, &g_SamplePlugin, ClientActive, true);
   1.120 +	//Hook ClientDisconnect to our function
   1.121 +	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, m_ServerClients, &g_SamplePlugin, ClientDisconnect, true);
   1.122 +	//Hook ClientPutInServer to our function
   1.123 +	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, m_ServerClients, &g_SamplePlugin, ClientPutInServer, true);
   1.124 +	//Hook SetCommandClient to our function
   1.125 +	SH_ADD_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, m_ServerClients, &g_SamplePlugin, SetCommandClient, true);
   1.126 +	//Hook ClientSettingsChanged to our function
   1.127 +	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, m_ServerClients, &g_SamplePlugin, ClientSettingsChanged, true);
   1.128 +
   1.129 +	//The following functions are pre handled, because that's how they are in IServerPluginCallbacks
   1.130 +
   1.131 +	//Hook ClientConnect to our function
   1.132 +	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientConnect, m_ServerClients, &g_SamplePlugin, ClientConnect, false);
   1.133 +	//Hook ClientCommand to our function
   1.134 +	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientCommand, m_ServerClients, &g_SamplePlugin, ClientCommand, false);
   1.135 +
   1.136 +	//Now, importantly, SourceHook has modified the layout of the above classes.
   1.137 +	//If we want to call them in their original state (i.e., without invoking hooks)
   1.138 +	// we must request a "Call Class" from SourceHook.
   1.139 +	m_ServerClients_CC = SH_GET_CALLCLASS(IServerGameClients, m_ServerClients);
   1.140 +	m_ServerDll = SH_GET_CALLCLASS(IServerGameDLL, m_ServerDll);
   1.141  
   1.142  	return true;
   1.143  }
   1.144  
   1.145  bool SamplePlugin::Unload(char *error, size_t maxlen)
   1.146  {
   1.147 +	//Make sure we remove any hooks we did... this may not be necessary since
   1.148 +	// SourceHook is capable of unloading plugins' hooks itself, but just to be safe.
   1.149 +
   1.150 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, m_ServerDll, &g_SamplePlugin, LevelInit, true);
   1.151 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, m_ServerDll, &g_SamplePlugin, ServerActivate, true);
   1.152 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, m_ServerDll, &g_SamplePlugin, GameFrame, true);
   1.153 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, m_ServerDll, &g_SamplePlugin, LevelShutdown, false);
   1.154 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientActive, m_ServerClients, &g_SamplePlugin, ClientActive, true);
   1.155 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, m_ServerClients, &g_SamplePlugin, ClientDisconnect, true);
   1.156 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, m_ServerClients, &g_SamplePlugin, ClientPutInServer, true);
   1.157 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, m_ServerClients, &g_SamplePlugin, SetCommandClient, true);
   1.158 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, m_ServerClients, &g_SamplePlugin, ClientSettingsChanged, true);
   1.159 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientConnect, m_ServerClients, &g_SamplePlugin, ClientConnect, false);
   1.160 +	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientCommand, m_ServerClients, &g_SamplePlugin, ClientCommand, false);
   1.161 +
   1.162 +	//Lastly, release the call classes we requested
   1.163 +	SH_RELEASE_CALLCLASS(m_ServerDll_CC);
   1.164 +	SH_RELEASE_CALLCLASS(m_ServerClients_CC);
   1.165 +
   1.166  	return true;
   1.167  }
   1.168  
   1.169  void SamplePlugin::AllPluginsLoaded()
   1.170  {
   1.171 -	//we don't really care
   1.172 +	//we don't really need this for anything other than interplugin communication
   1.173 +	//and that's not used in this plugin.
   1.174 +	//If we really wanted, we could override the factories so other plugins can request
   1.175 +	// interfaces we make.  In this callback, the plugin could be assured that either
   1.176 +	// the interfaces it requires were either loaded in another plugin or not.
   1.177  }
   1.178 -
   1.179 -bool LevelInit_handler( char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background )
   1.180 -{
   1.181 -	FILE *fp = fopen("c:\\dump.txt", "at");
   1.182 -	if (!fp)
   1.183 -		RETURN_META_VALUE(MRES_IGNORED, false);
   1.184 -
   1.185 -	fprintf(fp, "Map name: %s (old level: %s)\n", pMapName?pMapName:"", pOldLevel?pOldLevel:"");
   1.186 -
   1.187 -	fclose(fp);
   1.188 -
   1.189 -	RETURN_META_VALUE(MRES_IGNORED, false);
   1.190 -}