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