sourcemm/sample_mm/SamplePlugin.cpp
author David Anderson <dvander@alliedmods.net>
Sun May 01 18:50:58 2005 +0000 (2005-05-01)
changeset 54 7253f31cb91b
parent 44 935c3ec3baf5
child 68 5433659a62ae
permissions -rw-r--r--
Fixed binary output in Makefile
Fixed GCC compatibility for sampleplugin
     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 
    14 SamplePlugin g_SamplePlugin;
    15 
    16 PLUGIN_EXPOSE(SamplePlugin, g_SamplePlugin);
    17 
    18 //This has all of the necessary hook declarations.  Read it!
    19 #include "meta_hooks.h"
    20 
    21 bool SamplePlugin::LevelInit(const char *pMapName, const char *pMapEntities, const char *pOldLevel, const char *pLandmarkName, bool loadGame, bool background)
    22 {
    23 	META_LOG(g_PLAPI, "LevelInit() called: pMapName=%s", pMapName); 
    24 	RETURN_META_VALUE(MRES_IGNORED, true);
    25 }
    26 
    27 void SamplePlugin::ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
    28 {
    29 	META_LOG(g_PLAPI, "ServerActivate() called: edictCount=%d, clientMax=%d", edictCount, clientMax);
    30 	RETURN_META(MRES_IGNORED);
    31 }
    32 
    33 void SamplePlugin::GameFrame(bool simulating)
    34 {
    35 	META_LOG(g_PLAPI, "GameFrame() called: simulating=%d", simulating);
    36 	RETURN_META(MRES_IGNORED);
    37 }
    38 
    39 void SamplePlugin::LevelShutdown( void )
    40 {
    41 	META_LOG(g_PLAPI, "LevelShutdown() called");
    42 	RETURN_META(MRES_IGNORED);
    43 }
    44 
    45 void SamplePlugin::ClientActive(edict_t *pEntity, bool bLoadGame)
    46 {
    47 	META_LOG(g_PLAPI, "ClientActive called: pEntity=%d", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0);
    48 	RETURN_META(MRES_IGNORED);
    49 }
    50 
    51 void SamplePlugin::ClientDisconnect(edict_t *pEntity)
    52 {
    53 	META_LOG(g_PLAPI, "ClientDisconnect called: pEntity=%d", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0);
    54 	RETURN_META(MRES_IGNORED);
    55 }
    56 
    57 void SamplePlugin::ClientPutInServer(edict_t *pEntity, char const *playername)
    58 {
    59 	META_LOG(g_PLAPI, "ClientPutInServer called: pEntity=%d, playername=%s", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, playername);
    60 	RETURN_META(MRES_IGNORED);
    61 }
    62 
    63 void SamplePlugin::SetCommandClient(int index)
    64 {
    65 	META_LOG(g_PLAPI, "SetCommandClient() called: index=%d", index);
    66 	RETURN_META(MRES_IGNORED);
    67 }
    68 
    69 void SamplePlugin::ClientSettingsChanged(edict_t *pEdict)
    70 {
    71 	META_LOG(g_PLAPI, "ClientSettingsChanged called: pEdict=%d", pEdict ? m_Engine->IndexOfEdict(pEdict) : 0);
    72 	RETURN_META(MRES_IGNORED);
    73 }
    74 
    75 bool SamplePlugin::ClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen)
    76 {
    77 	META_LOG(g_PLAPI, "ClientConnect called: pEntity=%d, pszName=%s, pszAddress=%s", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, pszName, pszAddress);
    78 	RETURN_META_VALUE(MRES_IGNORED, true);
    79 }
    80 
    81 void SamplePlugin::ClientCommand(edict_t *pEntity)
    82 {
    83 	META_LOG(g_PLAPI, "ClientCommand called: pEntity=%d (commandString=%s)", pEntity ? m_Engine->IndexOfEdict(pEntity) : 0, m_Engine->Cmd_Args() ? m_Engine->Cmd_Args() : "");
    84 	RETURN_META(MRES_IGNORED);
    85 }
    86 
    87 bool SamplePlugin::Load(PluginId id, ISmmAPI *ismm, factories *list, char *error, size_t maxlen)
    88 {
    89 	PLUGIN_SAVEVARS();
    90 
    91 	m_ServerDll = (IServerGameDLL *)((ismm->serverFactory())(INTERFACEVERSION_SERVERGAMEDLL, NULL));
    92 	m_Engine = (IVEngineServer *)((ismm->engineFactory())(INTERFACEVERSION_VENGINESERVER, NULL));
    93 	m_ServerClients = (IServerGameClients *)((ismm->serverFactory())(INTERFACEVERSION_SERVERGAMECLIENTS, NULL));
    94 
    95 	if (!m_ServerDll)
    96 	{
    97 		snprintf(error, maxlen, "Could not find interface %s", INTERFACEVERSION_SERVERGAMEDLL);
    98 		return false;
    99 	}
   100 	if (!m_Engine)
   101 	{
   102 		snprintf(error, maxlen, "Could not find interface %s", INTERFACEVERSION_VENGINESERVER);
   103 		return false;
   104 	}
   105 	if (!m_ServerClients)
   106 	{
   107 		snprintf(error, maxlen, "Could not find interface %s", INTERFACEVERSION_SERVERGAMECLIENTS);
   108 		return false;
   109 	}
   110 
   111 	META_LOG(g_PLAPI, "Starting plugin.\n");
   112 
   113 	//We're hooking the following things as POST, in order to seem like Server Plugins.
   114 	//However, I don't actually know if Valve has done server plugins as POST or not.
   115 	//Change the last parameter to 'false' in order to change this to PRE.
   116 	//SH_ADD_HOOK_MEMFUNC means "SourceHook, Add Hook, Member Function".
   117 
   118 	//Hook LevelInit to our function
   119 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelInit, true);
   120 	//Hook ServerActivate to our function
   121 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, m_ServerDll, &g_SamplePlugin, &SamplePlugin::ServerActivate, true);
   122 	//Hook GameFrame to our function
   123 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, m_ServerDll, &g_SamplePlugin, &SamplePlugin::GameFrame, true);
   124 	//Hook LevelShutdown to our function -- this makes more sense as pre I guess
   125 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelShutdown, false);
   126 	//Hook ClientActivate to our function
   127 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientActive, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientActive, true);
   128 	//Hook ClientDisconnect to our function
   129 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientDisconnect, true);
   130 	//Hook ClientPutInServer to our function
   131 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientPutInServer, true);
   132 	//Hook SetCommandClient to our function
   133 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, m_ServerClients, &g_SamplePlugin, &SamplePlugin::SetCommandClient, true);
   134 	//Hook ClientSettingsChanged to our function
   135 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientSettingsChanged, true);
   136 
   137 	//The following functions are pre handled, because that's how they are in IServerPluginCallbacks
   138 
   139 	//Hook ClientConnect to our function
   140 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientConnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientConnect, false);
   141 	//Hook ClientCommand to our function
   142 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientCommand, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientCommand, false);
   143 
   144 	//Get the call class for IVServerEngine so we can safely call functions without
   145 	// invoking their hooks (when needed).
   146 	m_Engine_CC = SH_GET_CALLCLASS(m_Engine);
   147 
   148 	SH_CALL(m_Engine_CC, &IVEngineServer::LogPrint)("All hooks started!\n");
   149 
   150 	return true;
   151 }
   152 
   153 bool SamplePlugin::Unload(char *error, size_t maxlen)
   154 {
   155 	//Make sure we remove any hooks we did... this may not be necessary since
   156 	// SourceHook is capable of unloading plugins' hooks itself, but just to be safe.
   157 
   158 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelInit, true);
   159 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, m_ServerDll, &g_SamplePlugin, &SamplePlugin::ServerActivate, true);
   160 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, m_ServerDll, &g_SamplePlugin, &SamplePlugin::GameFrame, true);
   161 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, m_ServerDll, &g_SamplePlugin, &SamplePlugin::LevelShutdown, false);
   162 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientActive, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientActive, true);
   163 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientDisconnect, true);
   164 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientPutInServer, true);
   165 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, m_ServerClients, &g_SamplePlugin, &SamplePlugin::SetCommandClient, true);
   166 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientSettingsChanged, true);
   167 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientConnect, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientConnect, false);
   168 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientCommand, m_ServerClients, &g_SamplePlugin, &SamplePlugin::ClientCommand, false);
   169 
   170 	SH_RELEASE_CALLCLASS(m_Engine_CC);
   171 
   172 	return true;
   173 }
   174 
   175 void SamplePlugin::AllPluginsLoaded()
   176 {
   177 	//we don't really need this for anything other than interplugin communication
   178 	//and that's not used in this plugin.
   179 	//If we really wanted, we could override the factories so other plugins can request
   180 	// interfaces we make.  In this callback, the plugin could be assured that either
   181 	// the interfaces it requires were either loaded in another plugin or not.
   182 }