sourcemm/sample_mm/SamplePlugin.cpp
author David Anderson <dvander@alliedmods.net>
Thu Oct 06 21:59:10 2005 +0000 (2005-10-06)
changeset 124 b04971393ee4
parent 68 5433659a62ae
child 140 800dc9ba92c5
permissions -rw-r--r--
Finalized versioning and PI
Added QueryRunning() output to meta info/list.
     1 /* ======== sample_mm ========
     2 * Copyright (C) 2004-2005 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 SamplePlugin g_SamplePlugin;
    16 
    17 PLUGIN_EXPOSE(SamplePlugin, g_SamplePlugin);
    18 
    19 //This has all of the necessary hook declarations.  Read it!
    20 #include "meta_hooks.h"
    21 
    22 bool SamplePlugin::LevelInit(const char *pMapName, const char *pMapEntities, const char *pOldLevel, const char *pLandmarkName, bool loadGame, bool background)
    23 {
    24 	META_LOG(g_PLAPI, "LevelInit() called: pMapName=%s", pMapName); 
    25 	RETURN_META_VALUE(MRES_IGNORED, true);
    26 }
    27 
    28 void SamplePlugin::ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
    29 {
    30 	META_LOG(g_PLAPI, "ServerActivate() called: edictCount=%d, clientMax=%d", edictCount, clientMax);
    31 	RETURN_META(MRES_IGNORED);
    32 }
    33 
    34 void SamplePlugin::GameFrame(bool simulating)
    35 {
    36 	//don't log this, it just pumps stuff to the screen ;]
    37 	//META_LOG(g_PLAPI, "GameFrame() called: simulating=%d", simulating);
    38 	RETURN_META(MRES_IGNORED);
    39 }
    40 
    41 void SamplePlugin::LevelShutdown( void )
    42 {
    43 	META_LOG(g_PLAPI, "LevelShutdown() called");
    44 	RETURN_META(MRES_IGNORED);
    45 }
    46 
    47 void SamplePlugin::ClientActive(edict_t *pEntity, bool bLoadGame)
    48 {
    49 	META_LOG(g_PLAPI, "ClientActive called: pEntity=%d", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0);
    50 	RETURN_META(MRES_IGNORED);
    51 }
    52 
    53 void SamplePlugin::ClientDisconnect(edict_t *pEntity)
    54 {
    55 	META_LOG(g_PLAPI, "ClientDisconnect called: pEntity=%d", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0);
    56 	RETURN_META(MRES_IGNORED);
    57 }
    58 
    59 void SamplePlugin::ClientPutInServer(edict_t *pEntity, char const *playername)
    60 {
    61 	META_LOG(g_PLAPI, "ClientPutInServer called: pEntity=%d, playername=%s", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, playername);
    62 	RETURN_META(MRES_IGNORED);
    63 }
    64 
    65 void SamplePlugin::SetCommandClient(int index)
    66 {
    67 	META_LOG(g_PLAPI, "SetCommandClient() called: index=%d", index);
    68 	RETURN_META(MRES_IGNORED);
    69 }
    70 
    71 void SamplePlugin::ClientSettingsChanged(edict_t *pEdict)
    72 {
    73 	META_LOG(g_PLAPI, "ClientSettingsChanged called: pEdict=%d", pEdict ? m_Engine->IndexOfEdict(pEdict) : 0);
    74 	RETURN_META(MRES_IGNORED);
    75 }
    76 
    77 bool SamplePlugin::ClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen)
    78 {
    79 	META_LOG(g_PLAPI, "ClientConnect called: pEntity=%d, pszName=%s, pszAddress=%s", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, pszName, pszAddress);
    80 	RETURN_META_VALUE(MRES_IGNORED, true);
    81 }
    82 
    83 void SamplePlugin::ClientCommand(edict_t *pEntity)
    84 {
    85 	META_LOG(g_PLAPI, "ClientCommand called: pEntity=%d (commandString=%s)", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, m_Engine->Cmd_Args() ? m_Engine->Cmd_Args() : "");
    86 	RETURN_META(MRES_IGNORED);
    87 }
    88 
    89 bool FireEvent_Handler(IGameEvent *event, bool bDontBroadcast)
    90 {
    91 	if (!event || !event->GetName())
    92 		RETURN_META_VALUE(MRES_IGNORED, false);
    93 
    94 	const char *name = event->GetName();
    95 
    96 	META_LOG(g_PLAPI, "FireGameEvent called: name=%s", name); 
    97 
    98 	RETURN_META_VALUE(MRES_IGNORED, true);
    99 }
   100 
   101 bool SamplePlugin::Load(PluginId id, ISmmAPI *ismm, factories *list, char *error, size_t maxlen, bool late)
   102 {
   103 	PLUGIN_SAVEVARS();
   104 
   105 	m_ServerDll = (IServerGameDLL *)((ismm->serverFactory())(INTERFACEVERSION_SERVERGAMEDLL, NULL));
   106 	m_Engine = (IVEngineServer *)((ismm->engineFactory())(INTERFACEVERSION_VENGINESERVER, NULL));
   107 	m_ServerClients = (IServerGameClients *)((ismm->serverFactory())(INTERFACEVERSION_SERVERGAMECLIENTS, NULL));
   108 	m_GameEventManager = (IGameEventManager2 *)((ismm->engineFactory())(INTERFACEVERSION_GAMEEVENTSMANAGER2, NULL));
   109 
   110 	if (!m_ServerDll)
   111 	{
   112 		snprintf(error, maxlen, "Could not find interface %s", INTERFACEVERSION_SERVERGAMEDLL);
   113 		return false;
   114 	}
   115 	if (!m_Engine)
   116 	{
   117 		snprintf(error, maxlen, "Could not find interface %s", INTERFACEVERSION_VENGINESERVER);
   118 		return false;
   119 	}
   120 	if (!m_ServerClients)
   121 	{
   122 		snprintf(error, maxlen, "Could not find interface %s", INTERFACEVERSION_SERVERGAMECLIENTS);
   123 		return false;
   124 	}
   125 	if (!m_GameEventManager)
   126 	{
   127 		snprintf(error, maxlen, "Could not find interface %s", INTERFACEVERSION_GAMEEVENTSMANAGER2);
   128 		return false;
   129 	}
   130 
   131 	META_LOG(g_PLAPI, "Starting plugin.\n");
   132 
   133 	//Init our cvars/concmds
   134 	ConCommandBaseMgr::OneTimeInit(&g_Accessor);
   135 
   136 	//We're hooking the following things as POST, in order to seem like Server Plugins.
   137 	//However, I don't actually know if Valve has done server plugins as POST or not.
   138 	//Change the last parameter to 'false' in order to change this to PRE.
   139 	//SH_ADD_HOOK_MEMFUNC means "SourceHook, Add Hook, Member Function".
   140 
   141 	//Hook LevelInit to our function
   142 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelInit, true);
   143 	//Hook ServerActivate to our function
   144 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, m_ServerDll, &g_SamplePlugin, &SamplePlugin::ServerActivate, true);
   145 	//Hook GameFrame to our function
   146 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, m_ServerDll, &g_SamplePlugin, &SamplePlugin::GameFrame, true);
   147 	//Hook LevelShutdown to our function -- this makes more sense as pre I guess
   148 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelShutdown, false);
   149 	//Hook ClientActivate to our function
   150 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientActive, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientActive, true);
   151 	//Hook ClientDisconnect to our function
   152 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientDisconnect, true);
   153 	//Hook ClientPutInServer to our function
   154 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientPutInServer, true);
   155 	//Hook SetCommandClient to our function
   156 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, m_ServerClients, &g_SamplePlugin, &SamplePlugin::SetCommandClient, true);
   157 	//Hook ClientSettingsChanged to our function
   158 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientSettingsChanged, true);
   159 
   160 	//The following functions are pre handled, because that's how they are in IServerPluginCallbacks
   161 
   162 	//Hook ClientConnect to our function
   163 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientConnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientConnect, false);
   164 	//Hook ClientCommand to our function
   165 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientCommand, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientCommand, false);
   166 
   167 	//This hook is a static hook, no member function
   168 	SH_ADD_HOOK_STATICFUNC(IGameEventManager2, FireEvent, m_GameEventManager, FireEvent_Handler, false); 
   169 
   170 	//Get the call class for IVServerEngine so we can safely call functions without
   171 	// invoking their hooks (when needed).
   172 	m_Engine_CC = SH_GET_CALLCLASS(m_Engine);
   173 
   174 	SH_CALL(m_Engine_CC, &IVEngineServer::LogPrint)("All hooks started!\n");
   175 
   176 	return true;
   177 }
   178 
   179 bool SamplePlugin::Unload(char *error, size_t maxlen)
   180 {
   181 	//IT IS CRUCIAL THAT YOU REMOVE CVARS.
   182 	//As of Metamod:Source 1.00-RC2, it will automatically remove them for you.
   183 	//But this is only if you've registered them correctly!
   184     
   185 	//Make sure we remove any hooks we did... this may not be necessary since
   186 	//SourceHook is capable of unloading plugins' hooks itself, but just to be safe.
   187 
   188 	SH_REMOVE_HOOK_STATICFUNC(IGameEventManager2, FireEvent, m_GameEventManager, FireEvent_Handler, false); 
   189 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelInit, true);
   190 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, m_ServerDll, &g_SamplePlugin, &SamplePlugin::ServerActivate, true);
   191 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, m_ServerDll, &g_SamplePlugin, &SamplePlugin::GameFrame, true);
   192 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelShutdown, false);
   193 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientActive, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientActive, true);
   194 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientDisconnect, true);
   195 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientPutInServer, true);
   196 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, m_ServerClients, &g_SamplePlugin, &SamplePlugin::SetCommandClient, true);
   197 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientSettingsChanged, true);
   198 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientConnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientConnect, false);
   199 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientCommand, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientCommand, false);
   200 
   201 	//this, sourcehook does not keep track of.  we must do this.
   202 	SH_RELEASE_CALLCLASS(m_Engine_CC);
   203 
   204 	return true;
   205 }
   206 
   207 void SamplePlugin::AllPluginsLoaded()
   208 {
   209 	//we don't really need this for anything other than interplugin communication
   210 	//and that's not used in this plugin.
   211 	//If we really wanted, we could override the factories so other plugins can request
   212 	// interfaces we make.  In this callback, the plugin could be assured that either
   213 	// the interfaces it requires were either loaded in another plugin or not.
   214 }