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