Sample plugin can now do anything server plugins can (except NetworkIDValidated callback)
authorDavid Anderson <dvander@alliedmods.net>
Mon Apr 18 23:33:44 2005 +0000 (2005-04-18)
changeset 223fe8e457c5a2
parent 21 2c90a194f7c4
child 23 86f3894c86e9
Sample plugin can now do anything server plugins can (except NetworkIDValidated callback)
sourcemm/sample_mm/SamplePlugin.cpp
sourcemm/sample_mm/SamplePlugin.h
sourcemm/sample_mm/meta_hooks.h
sourcemm/sample_mm/sample_mm.vcproj
     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 -}
     2.1 --- a/sourcemm/sample_mm/SamplePlugin.h	Mon Apr 18 19:50:33 2005 +0000
     2.2 +++ b/sourcemm/sample_mm/SamplePlugin.h	Mon Apr 18 23:33:44 2005 +0000
     2.3 @@ -52,6 +52,53 @@
     2.4  	{
     2.5  		return "SAMPLE";
     2.6  	}
     2.7 +public:
     2.8 +	//These functions are from IServerPluginCallbacks
     2.9 +	//Note, the parameters might be a little different to match the actual calls!
    2.10 +
    2.11 +	//Called on LevelInit.  Server plugins only have pMapName
    2.12 +	bool LevelInit(const char *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background);
    2.13 +
    2.14 +	//Called on ServerActivate.  Same definition as server plugins
    2.15 +	void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax);
    2.16 +
    2.17 +	//Called on a game tick.  Same definition as server plugins
    2.18 +	void GameFrame(bool simulating);
    2.19 +
    2.20 +	//Called on level shutdown.  Same definition as server plugins 
    2.21 +	void LevelShutdown(void);
    2.22 +
    2.23 +	//Client is activate (whatever that means).  We get an extra parameter...
    2.24 +	// "If bLoadGame is true, don't spawn the player because its state is already setup."
    2.25 +	void ClientActive(edict_t *pEntity, bool bLoadGame);
    2.26 +
    2.27 +	//Client disconnects - same as server plugins
    2.28 +	void ClientDisconnect(edict_t *pEntity);
    2.29 +
    2.30 +	//Client is put in server - same as server plugins
    2.31 +	void ClientPutInServer(edict_t *pEntity, char const *playername);
    2.32 +
    2.33 +	//Sets the client index - same as server plugins
    2.34 +	void SetCommandClient(int index);
    2.35 +
    2.36 +	//Called on client settings changed (duh) - same as server plugins
    2.37 +	void ClientSettingsChanged(edict_t *pEdict);
    2.38 +
    2.39 +	//Called on client connect.  Unlike server plugins, we return whether the 
    2.40 +	// connection is allowed rather than set it through a pointer in the first parameter.
    2.41 +	// You can still supercede the GameDLL through RETURN_META_VALUE(MRES_SUPERCEDE, true/false)
    2.42 +	bool ClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen);
    2.43 +
    2.44 +	//Called when a client uses a command.  Unlike server plugins, it's void.
    2.45 +	// You can still supercede the gamedll through RETURN_META(MRES_SUPERCEDE).
    2.46 +	void ClientCommand(edict_t *pEntity);
    2.47 +
    2.48 +private:
    2.49 +	IVEngineServer *m_Engine;
    2.50 +	IServerGameDLL *m_ServerDll;
    2.51 +	IServerGameClients *m_ServerClients;
    2.52 +	IServerGameDLL *m_ServerDll_CC;
    2.53 +	IServerGameClients *m_ServerClients_CC;
    2.54  };
    2.55  
    2.56  extern SamplePlugin g_SamplePlugin;
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/sourcemm/sample_mm/meta_hooks.h	Mon Apr 18 23:33:44 2005 +0000
     3.3 @@ -0,0 +1,20 @@
     3.4 +#ifndef _INCLUDE_META_HOOKS_H
     3.5 +#define _INCLUDE_META_HOOKS_H
     3.6 +
     3.7 +//Declare the hooks we will be using.  Hooking will not compile without these.
     3.8 +//The macro naming scheme is SH_DECL_HOOKn[_void].
     3.9 +//If you have 5 parameters, it would be HOOK5.  If the function is void, add _void.
    3.10 +//It stands for "SourceHook, Declare Hook".
    3.11 +SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, 0, bool, char const *, char const *, char const *, char const *, bool, bool);
    3.12 +SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int, int);
    3.13 +SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, 0, bool);
    3.14 +SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, 0);
    3.15 +SH_DECL_HOOK2_void(IServerGameClients, ClientActive, SH_NOATTRIB, 0, edict_t *, bool);
    3.16 +SH_DECL_HOOK1_void(IServerGameClients, ClientDisconnect, SH_NOATTRIB, 0, edict_t *);
    3.17 +SH_DECL_HOOK2_void(IServerGameClients, ClientPutInServer, SH_NOATTRIB, 0, edict_t *, char const *);
    3.18 +SH_DECL_HOOK1_void(IServerGameClients, SetCommandClient, SH_NOATTRIB, 0, int);
    3.19 +SH_DECL_HOOK1_void(IServerGameClients, ClientSettingsChanged, SH_NOATTRIB, 0, edict_t *);
    3.20 +SH_DECL_HOOK5(IServerGameClients, ClientConnect, SH_NOATTRIB, 0, bool, edict_t *, const char*, const char *, char *, int);
    3.21 +SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
    3.22 +
    3.23 +#endif //_INCLUDE_META_HOOKS_H
     4.1 --- a/sourcemm/sample_mm/sample_mm.vcproj	Mon Apr 18 19:50:33 2005 +0000
     4.2 +++ b/sourcemm/sample_mm/sample_mm.vcproj	Mon Apr 18 23:33:44 2005 +0000
     4.3 @@ -125,6 +125,9 @@
     4.4  			Filter="h;hpp;hxx;hm;inl;inc;xsd"
     4.5  			UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}">
     4.6  			<File
     4.7 +				RelativePath=".\meta_hooks.h">
     4.8 +			</File>
     4.9 +			<File
    4.10  				RelativePath=".\SamplePlugin.h">
    4.11  			</File>
    4.12  		</Filter>