sourcemm/sample_mm/SamplePlugin.cpp
author David Anderson <dvander@alliedmods.net>
Tue Aug 15 20:10:12 2006 +0000 (2006-08-15)
changeset 251 3bd7e7008ac3
parent 249 eaf9071cedc8
child 294 0d87bc8606f8
permissions -rw-r--r--
fixed the VInterfaceMatch API
     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 bool SamplePlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
   129 {
   130 	PLUGIN_SAVEVARS();
   131 
   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);
   136 
   137 	META_LOG(g_PLAPI, "Starting plugin.\n");
   138 
   139 	ismm->AddListener(this, &g_Listener);
   140 
   141 	//Init our cvars/concmds
   142 	ConCommandBaseMgr::OneTimeInit(&g_Accessor);
   143 
   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".
   148 
   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);
   167 
   168 	//The following functions are pre handled, because that's how they are in IServerPluginCallbacks
   169 
   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);
   174 
   175 	//This hook is a static hook, no member function
   176 	SH_ADD_HOOK_STATICFUNC(IGameEventManager2, FireEvent, m_GameEventManager, FireEvent_Handler, false); 
   177 
   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);
   181 
   182 	SH_CALL(m_Engine_CC, &IVEngineServer::LogPrint)("All hooks started!\n");
   183 
   184 	g_SMAPI->AddListener(g_PLAPI, this);
   185 
   186 	return true;
   187 }
   188 
   189 bool SamplePlugin::Unload(char *error, size_t maxlen)
   190 {
   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!
   194     
   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.
   197 
   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);
   210 
   211 	//this, sourcehook does not keep track of.  we must do this.
   212 	SH_RELEASE_CALLCLASS(m_Engine_CC);
   213 
   214 	return true;
   215 }
   216 
   217 void SamplePlugin::AllPluginsLoaded()
   218 {
   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.
   224 }
   225 
   226 void *MyListener::OnMetamodQuery(const char *iface, int *ret)
   227 {
   228 	if (strcmp(iface, "SamplePlugin")==0)
   229 	{
   230 		if (ret)
   231 			*ret = IFACE_OK;
   232 		return static_cast<void *>(&g_SamplePlugin);
   233 	}
   234 
   235 	if (ret)
   236 		*ret = IFACE_FAILED;
   237 
   238 	return NULL;
   239 }
   240