sourcemm/sample_mm/SamplePlugin.cpp
author David Anderson <dvander@alliedmods.net>
Mon Aug 14 05:37:58 2006 +0000 (2006-08-14)
changeset 241 b5ed7cd47d67
parent 240 11c567392c11
child 249 eaf9071cedc8
permissions -rw-r--r--
Updated this to be a bit more modern.
     1 /* ======== sample_mm ========
     2  * Copyright (C) 2004-2006 Metamod:Source Development Team
     3  * No warranties of any kind
     4  *
     5  * License: zlib/libpng
     6  *
     7  * Author(s): David "BAILOPAN" Anderson
     8  * ============================
     9  */
    10 
    11 #include <oslink.h>
    12 #include "SamplePlugin.h"
    13 #include "cvars.h"
    14 
    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);
    31 
    32 SamplePlugin g_SamplePlugin;
    33 MyListener g_Listener;
    34 
    35 PLUGIN_EXPOSE(SamplePlugin, g_SamplePlugin);
    36 
    37 bool SamplePlugin::LevelInit(const char *pMapName, const char *pMapEntities, const char *pOldLevel, const char *pLandmarkName, bool loadGame, bool background)
    38 {
    39 	META_LOG(g_PLAPI, "LevelInit() called: pMapName=%s", pMapName); 
    40 	RETURN_META_VALUE(MRES_IGNORED, true);
    41 }
    42 
    43 void SamplePlugin::OnLevelShutdown()
    44 {
    45 	META_LOG(g_PLAPI, "OnLevelShutdown() called from listener");
    46 }
    47 
    48 void SamplePlugin::ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
    49 {
    50 	META_LOG(g_PLAPI, "ServerActivate() called: edictCount=%d, clientMax=%d", edictCount, clientMax);
    51 	RETURN_META(MRES_IGNORED);
    52 }
    53 
    54 void SamplePlugin::GameFrame(bool simulating)
    55 {
    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);
    59 }
    60 
    61 void SamplePlugin::LevelShutdown( void )
    62 {
    63 	META_LOG(g_PLAPI, "LevelShutdown() called");
    64 	RETURN_META(MRES_IGNORED);
    65 }
    66 
    67 void SamplePlugin::ClientActive(edict_t *pEntity, bool bLoadGame)
    68 {
    69 	META_LOG(g_PLAPI, "ClientActive called: pEntity=%d", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0);
    70 	RETURN_META(MRES_IGNORED);
    71 }
    72 
    73 void SamplePlugin::ClientDisconnect(edict_t *pEntity)
    74 {
    75 	META_LOG(g_PLAPI, "ClientDisconnect called: pEntity=%d", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0);
    76 	RETURN_META(MRES_IGNORED);
    77 }
    78 
    79 void SamplePlugin::ClientPutInServer(edict_t *pEntity, char const *playername)
    80 {
    81 	META_LOG(g_PLAPI, "ClientPutInServer called: pEntity=%d, playername=%s", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, playername);
    82 	RETURN_META(MRES_IGNORED);
    83 }
    84 
    85 void SamplePlugin::SetCommandClient(int index)
    86 {
    87 	META_LOG(g_PLAPI, "SetCommandClient() called: index=%d", index);
    88 	RETURN_META(MRES_IGNORED);
    89 }
    90 
    91 void SamplePlugin::ClientSettingsChanged(edict_t *pEdict)
    92 {
    93 	META_LOG(g_PLAPI, "ClientSettingsChanged called: pEdict=%d", pEdict ? m_Engine->IndexOfEdict(pEdict) : 0);
    94 	RETURN_META(MRES_IGNORED);
    95 }
    96 
    97 bool SamplePlugin::ClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen)
    98 {
    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);
   101 }
   102 
   103 void SamplePlugin::ClientCommand(edict_t *pEntity)
   104 {
   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);
   107 }
   108 
   109 bool FireEvent_Handler(IGameEvent *event, bool bDontBroadcast)
   110 {
   111 	if (!event || !event->GetName())
   112 		RETURN_META_VALUE(MRES_IGNORED, false);
   113 
   114 	/**
   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.
   119 	 */
   120 
   121 	const char *name = event->GetName();
   122 
   123 	META_LOG(g_PLAPI, "FireGameEvent called: name=%s", name); 
   124 
   125 	RETURN_META_VALUE(MRES_IGNORED, true);
   126 }
   127 
   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); \
   130 	if (!v_var) \
   131 	{ \
   132 		if (error && maxlen) \
   133 		{ \
   134 			snprintf(error, maxlen, "Could not find interface: %s", v_name); \
   135 		} \
   136 		return false; \
   137 	}
   138 	
   139 
   140 bool SamplePlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
   141 {
   142 	PLUGIN_SAVEVARS();
   143 
   144 	int num = 0;
   145 
   146 	GET_V_IFACE(serverFactory, m_ServerDll, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
   147 	GET_V_IFACE(engineFactory, m_Engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
   148 	GET_V_IFACE(serverFactory, m_ServerClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
   149 	GET_V_IFACE(engineFactory, m_GameEventManager, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
   150 
   151 	META_LOG(g_PLAPI, "Starting plugin.\n");
   152 
   153 	ismm->AddListener(this, &g_Listener);
   154 
   155 	//Init our cvars/concmds
   156 	ConCommandBaseMgr::OneTimeInit(&g_Accessor);
   157 
   158 	//We're hooking the following things as POST, in order to seem like Server Plugins.
   159 	//However, I don't actually know if Valve has done server plugins as POST or not.
   160 	//Change the last parameter to 'false' in order to change this to PRE.
   161 	//SH_ADD_HOOK_MEMFUNC means "SourceHook, Add Hook, Member Function".
   162 
   163 	//Hook LevelInit to our function
   164 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelInit, true);
   165 	//Hook ServerActivate to our function
   166 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, m_ServerDll, &g_SamplePlugin, &SamplePlugin::ServerActivate, true);
   167 	//Hook GameFrame to our function
   168 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, m_ServerDll, &g_SamplePlugin, &SamplePlugin::GameFrame, true);
   169 	//Hook LevelShutdown to our function -- this makes more sense as pre I guess
   170 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelShutdown, false);
   171 	//Hook ClientActivate to our function
   172 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientActive, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientActive, true);
   173 	//Hook ClientDisconnect to our function
   174 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientDisconnect, true);
   175 	//Hook ClientPutInServer to our function
   176 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientPutInServer, true);
   177 	//Hook SetCommandClient to our function
   178 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, m_ServerClients, &g_SamplePlugin, &SamplePlugin::SetCommandClient, true);
   179 	//Hook ClientSettingsChanged to our function
   180 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientSettingsChanged, true);
   181 
   182 	//The following functions are pre handled, because that's how they are in IServerPluginCallbacks
   183 
   184 	//Hook ClientConnect to our function
   185 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientConnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientConnect, false);
   186 	//Hook ClientCommand to our function
   187 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientCommand, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientCommand, false);
   188 
   189 	//This hook is a static hook, no member function
   190 	SH_ADD_HOOK_STATICFUNC(IGameEventManager2, FireEvent, m_GameEventManager, FireEvent_Handler, false); 
   191 
   192 	//Get the call class for IVServerEngine so we can safely call functions without
   193 	// invoking their hooks (when needed).
   194 	m_Engine_CC = SH_GET_CALLCLASS(m_Engine);
   195 
   196 	SH_CALL(m_Engine_CC, &IVEngineServer::LogPrint)("All hooks started!\n");
   197 
   198 	g_SMAPI->AddListener(g_PLAPI, this);
   199 
   200 	return true;
   201 }
   202 
   203 bool SamplePlugin::Unload(char *error, size_t maxlen)
   204 {
   205 	//IT IS CRUCIAL THAT YOU REMOVE CVARS.
   206 	//As of Metamod:Source 1.00-RC2, it will automatically remove them for you.
   207 	//But this is only if you've registered them correctly!
   208     
   209 	//Make sure we remove any hooks we did... this may not be necessary since
   210 	//SourceHook is capable of unloading plugins' hooks itself, but just to be safe.
   211 
   212 	SH_REMOVE_HOOK_STATICFUNC(IGameEventManager2, FireEvent, m_GameEventManager, FireEvent_Handler, false); 
   213 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelInit, true);
   214 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, m_ServerDll, &g_SamplePlugin, &SamplePlugin::ServerActivate, true);
   215 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, m_ServerDll, &g_SamplePlugin, &SamplePlugin::GameFrame, true);
   216 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelShutdown, false);
   217 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientActive, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientActive, true);
   218 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientDisconnect, true);
   219 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientPutInServer, true);
   220 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, m_ServerClients, &g_SamplePlugin, &SamplePlugin::SetCommandClient, true);
   221 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientSettingsChanged, true);
   222 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientConnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientConnect, false);
   223 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientCommand, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientCommand, false);
   224 
   225 	//this, sourcehook does not keep track of.  we must do this.
   226 	SH_RELEASE_CALLCLASS(m_Engine_CC);
   227 
   228 	return true;
   229 }
   230 
   231 void SamplePlugin::AllPluginsLoaded()
   232 {
   233 	//we don't really need this for anything other than interplugin communication
   234 	//and that's not used in this plugin.
   235 	//If we really wanted, we could override the factories so other plugins can request
   236 	// interfaces we make.  In this callback, the plugin could be assured that either
   237 	// the interfaces it requires were either loaded in another plugin or not.
   238 }
   239 
   240 void *MyListener::OnMetamodQuery(const char *iface, int *ret)
   241 {
   242 	if (strcmp(iface, "SamplePlugin")==0)
   243 	{
   244 		if (ret)
   245 			*ret = IFACE_OK;
   246 		return static_cast<void *>(&g_SamplePlugin);
   247 	}
   248 
   249 	if (ret)
   250 		*ret = IFACE_FAILED;
   251 
   252 	return NULL;
   253 }
   254