sample_mm/sample_mm.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Nov 14 03:26:48 2008 -0600 (2008-11-14)
changeset 538 6ec9d491ec1e
parent 509 811679ebed9e
child 605 131878209276
permissions -rw-r--r--
Update makefile to allow building against Orange Box and Left 4 Dead
     1 /**
     2  * vim: set ts=4 :
     3  * ======================================================
     4  * Metamod:Source Sample Plugin
     5  * Written by AlliedModders LLC.
     6  * ======================================================
     7  *
     8  * This software is provided 'as-is', without any express or implied warranty.
     9  * In no event will the authors be held liable for any damages arising from 
    10  * the use of this software.
    11  *
    12  * This sample plugin is public domain.
    13  *
    14  * Version: $Id$
    15  */
    16 
    17 #include <stdio.h>
    18 #include "sample_mm.h"
    19 
    20 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, 0, bool, char const *, char const *, char const *, char const *, bool, bool);
    21 SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int, int);
    22 SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, 0, bool);
    23 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, 0);
    24 SH_DECL_HOOK2_void(IServerGameClients, ClientActive, SH_NOATTRIB, 0, edict_t *, bool);
    25 SH_DECL_HOOK1_void(IServerGameClients, ClientDisconnect, SH_NOATTRIB, 0, edict_t *);
    26 SH_DECL_HOOK2_void(IServerGameClients, ClientPutInServer, SH_NOATTRIB, 0, edict_t *, char const *);
    27 SH_DECL_HOOK1_void(IServerGameClients, SetCommandClient, SH_NOATTRIB, 0, int);
    28 SH_DECL_HOOK1_void(IServerGameClients, ClientSettingsChanged, SH_NOATTRIB, 0, edict_t *);
    29 SH_DECL_HOOK5(IServerGameClients, ClientConnect, SH_NOATTRIB, 0, bool, edict_t *, const char*, const char *, char *, int);
    30 SH_DECL_HOOK2(IGameEventManager2, FireEvent, SH_NOATTRIB, 0, bool, IGameEvent *, bool);
    31 
    32 #if defined ENGINE_ORANGEBOX
    33 SH_DECL_HOOK2_void(IServerGameClients, NetworkIDValidated, SH_NOATTRIB, 0, const char *, const char *);
    34 SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
    35 #elif defined ENGINE_ORIGINAL
    36 SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
    37 #endif
    38 
    39 StubPlugin g_StubPlugin;
    40 IServerGameDLL *server = NULL;
    41 IServerGameClients *gameclients = NULL;
    42 IVEngineServer *engine = NULL;
    43 IServerPluginHelpers *helpers = NULL;
    44 IGameEventManager2 *gameevents = NULL;
    45 IServerPluginCallbacks *vsp_callbacks = NULL;
    46 IPlayerInfoManager *playerinfomanager = NULL;
    47 ICvar *icvar = NULL;
    48 
    49 ConVar sample_cvar("sample_cvar", "42", 0);
    50 
    51 /** 
    52  * Something like this is needed to register cvars/CON_COMMANDs.
    53  */
    54 class BaseAccessor : public IConCommandBaseAccessor
    55 {
    56 public:
    57 	bool RegisterConCommandBase(ConCommandBase *pCommandBase)
    58 	{
    59 		/* Always call META_REGCVAR instead of going through the engine. */
    60 		return META_REGCVAR(pCommandBase);
    61 	}
    62 } s_BaseAccessor;
    63 
    64 PLUGIN_EXPOSE(StubPlugin, g_StubPlugin);
    65 bool StubPlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
    66 {
    67 	PLUGIN_SAVEVARS();
    68 
    69 	GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
    70 	GET_V_IFACE_CURRENT(GetEngineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
    71 	GET_V_IFACE_CURRENT(GetEngineFactory, helpers, IServerPluginHelpers, INTERFACEVERSION_ISERVERPLUGINHELPERS);
    72 	GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
    73 	GET_V_IFACE_ANY(GetServerFactory, server, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
    74 	GET_V_IFACE_ANY(GetServerFactory, gameclients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
    75 	GET_V_IFACE_ANY(GetServerFactory, playerinfomanager, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
    76 
    77 	META_LOG(g_PLAPI, "Starting plugin.");
    78 
    79 	/* Load the VSP listener.  This is usually needed for IServerPluginHelpers. */
    80 #if defined METAMOD_PLAPI_VERSION
    81 	if ((vsp_callbacks = ismm->GetVSPInfo(NULL)) == NULL)
    82 #endif
    83 	{
    84 		ismm->AddListener(this, this);
    85 		ismm->EnableVSPListener();
    86 	}
    87 
    88 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, server, this, &StubPlugin::Hook_LevelInit, true);
    89 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, server, this, &StubPlugin::Hook_ServerActivate, true);
    90 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, server, this, &StubPlugin::Hook_GameFrame, true);
    91 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, server, this, &StubPlugin::Hook_LevelShutdown, false);
    92 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientActive, gameclients, this, &StubPlugin::Hook_ClientActive, true);
    93 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, gameclients, this, &StubPlugin::Hook_ClientDisconnect, true);
    94 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, gameclients, this, &StubPlugin::Hook_ClientPutInServer, true);
    95 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, gameclients, this, &StubPlugin::Hook_SetCommandClient, true);
    96 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, gameclients, this, &StubPlugin::Hook_ClientSettingsChanged, false);
    97 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientConnect, gameclients, this, &StubPlugin::Hook_ClientConnect, false);
    98 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientCommand, gameclients, this, &StubPlugin::Hook_ClientCommand, false);
    99 
   100 #if defined ENGINE_ORIGINAL
   101 	m_EngineCC = SH_GET_CALLCLASS(engine);
   102 #endif
   103 
   104 	ENGINE_CALL(&IVEngineServer::LogPrint)("All hooks started!\n");
   105 
   106 #if defined ENGINE_ORANGEBOX
   107 	g_pCVar = icvar;
   108 	ConVar_Register(0, &s_BaseAccessor);
   109 #elif defined ENGINE_ORIGINAL
   110 	ConCommandBaseMgr::OneTimeInit(&s_BaseAccessor);
   111 #endif
   112 
   113 	return true;
   114 }
   115 
   116 bool StubPlugin::Unload(char *error, size_t maxlen)
   117 {
   118 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, server, this, &StubPlugin::Hook_LevelInit, true);
   119 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, server, this, &StubPlugin::Hook_ServerActivate, true);
   120 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, server, this, &StubPlugin::Hook_GameFrame, true);
   121 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, server, this, &StubPlugin::Hook_LevelShutdown, false);
   122 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientActive, gameclients, this, &StubPlugin::Hook_ClientActive, true);
   123 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, gameclients, this, &StubPlugin::Hook_ClientDisconnect, true);
   124 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, gameclients, this, &StubPlugin::Hook_ClientPutInServer, true);
   125 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, gameclients, this, &StubPlugin::Hook_SetCommandClient, true);
   126 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, gameclients, this, &StubPlugin::Hook_ClientSettingsChanged, false);
   127 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientConnect, gameclients, this, &StubPlugin::Hook_ClientConnect, false);
   128 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientCommand, gameclients, this, &StubPlugin::Hook_ClientCommand, false);
   129 
   130 #if defined ENGINE_ORIGINAL
   131 	SH_RELEASE_CALLCLASS(m_EngineCC);
   132 #endif
   133 
   134 	return true;
   135 }
   136 
   137 void StubPlugin::OnVSPListening(IServerPluginCallbacks *iface)
   138 {
   139 	vsp_callbacks = iface;
   140 }
   141 
   142 void StubPlugin::Hook_ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
   143 {
   144 	META_LOG(g_PLAPI, "ServerActivate() called: edictCount = %d, clientMax = %d", edictCount, clientMax);
   145 }
   146 
   147 void StubPlugin::AllPluginsLoaded()
   148 {
   149 	/* This is where we'd do stuff that relies on the mod or other plugins 
   150 	 * being initialized (for example, cvars added and events registered).
   151 	 */
   152 }
   153 
   154 void StubPlugin::Hook_ClientActive(edict_t *pEntity, bool bLoadGame)
   155 {
   156 	META_LOG(g_PLAPI, "Hook_ClientActive(%d, %d)", engine->IndexOfEdict(pEntity), bLoadGame);
   157 }
   158 
   159 #if defined ENGINE_ORANGEBOX
   160 void StubPlugin::Hook_ClientCommand(edict_t *pEntity, const CCommand &args)
   161 #elif defined ENGINE_ORIGINAL
   162 void StubPlugin::Hook_ClientCommand(edict_t *pEntity)
   163 #endif
   164 {
   165 #if defined ENGINE_ORIGINAL
   166 	CCommand args;
   167 #endif
   168 
   169 	if (!pEntity || pEntity->IsFree())
   170 	{
   171 		return;
   172 	}
   173 
   174 	const char *cmd = args.Arg(0);
   175 	if (strcmp(cmd, "menu") == 0)
   176 	{
   177 		KeyValues *kv = new KeyValues("menu");
   178 		kv->SetString("title", "You've got options, hit ESC");
   179 		kv->SetInt("level", 1);
   180 		kv->SetColor("color", Color(255, 0, 0, 255));
   181 		kv->SetInt("time", 20);
   182 		kv->SetString("msg", "Pick an option\nOr don't.");
   183 
   184 		for (int i = 1; i < 9; i++)
   185 		{
   186 			char num[10], msg[10], cmd[10];
   187 			MM_Format( num, sizeof(num), "%i", i );
   188 			MM_Format( msg, sizeof(msg), "Option %i", i );
   189 			MM_Format( cmd, sizeof(cmd), "option %i", i );
   190 
   191 			KeyValues *item1 = kv->FindKey(num, true);
   192 			item1->SetString("msg", msg);
   193 			item1->SetString("command", cmd);
   194 		}
   195 
   196 		helpers->CreateMessage(pEntity, DIALOG_MENU, kv, vsp_callbacks);
   197 		kv->deleteThis();
   198 		RETURN_META(MRES_SUPERCEDE);
   199 	}
   200 	else if (strcmp(cmd, "rich") == 0)
   201 	{
   202 		KeyValues *kv = new KeyValues("menu");
   203 		kv->SetString("title", "A rich message");
   204 		kv->SetInt("level", 1);
   205 		kv->SetInt("time", 20);
   206 		kv->SetString("msg", "This is a long long long text string.\n\nIt also has line breaks.");
   207 
   208 		helpers->CreateMessage(pEntity, DIALOG_TEXT, kv, vsp_callbacks);
   209 		kv->deleteThis();
   210 		RETURN_META(MRES_SUPERCEDE);
   211 	}
   212 	else if (strcmp(cmd, "msg") == 0)
   213 	{
   214 		KeyValues *kv = new KeyValues("menu");
   215 		kv->SetString("title", "Just a simple hello");
   216 		kv->SetInt("level", 1);
   217 		kv->SetInt("time", 20);
   218 
   219 		helpers->CreateMessage(pEntity, DIALOG_MSG, kv, vsp_callbacks);
   220 		kv->deleteThis();
   221 		RETURN_META(MRES_SUPERCEDE);
   222 	}
   223 	else if (strcmp(cmd, "entry") == 0)
   224 	{
   225 		KeyValues *kv = new KeyValues("entry");
   226 		kv->SetString("title", "Stuff");
   227 		kv->SetString("msg", "Enter something");
   228 		kv->SetString("command", "say"); // anything they enter into the dialog turns into a say command
   229 		kv->SetInt("level", 1);
   230 		kv->SetInt("time", 20);
   231 
   232 		helpers->CreateMessage(pEntity, DIALOG_ENTRY, kv, vsp_callbacks);
   233 		kv->deleteThis();
   234 		RETURN_META(MRES_SUPERCEDE);
   235 	}
   236 }
   237 
   238 void StubPlugin::Hook_ClientSettingsChanged(edict_t *pEdict)
   239 {
   240 	if (playerinfomanager)
   241 	{
   242 		IPlayerInfo *playerinfo = playerinfomanager->GetPlayerInfo(pEdict);
   243 
   244 		const char *name = engine->GetClientConVarValue(engine->IndexOfEdict(pEdict), "name");
   245 
   246 		if (playerinfo != NULL
   247 			&& name != NULL
   248 			&& strcmp(engine->GetPlayerNetworkIDString(pEdict), "BOT") != 0
   249 			&& playerinfo->GetName() != NULL
   250 			&& strcmp(name, playerinfo->GetName()) == 0)
   251 		{
   252 			char msg[128];
   253 			MM_Format(msg, sizeof(msg), "Your name changed to \"%s\" (from \"%s\")\n", name, playerinfo->GetName());
   254 			engine->ClientPrintf(pEdict, msg);
   255 		}
   256 	}
   257 }
   258 
   259 bool StubPlugin::Hook_ClientConnect(edict_t *pEntity,
   260 									const char *pszName,
   261 									const char *pszAddress,
   262 									char *reject,
   263 									int maxrejectlen)
   264 {
   265 	META_LOG(g_PLAPI, "Hook_ClientConnect(%d, \"%s\", \"%s\")", engine->IndexOfEdict(pEntity), pszName, pszAddress);
   266 
   267 	return true;
   268 }
   269 
   270 void StubPlugin::Hook_ClientPutInServer(edict_t *pEntity, char const *playername)
   271 {
   272 	KeyValues *kv = new KeyValues( "msg" );
   273 	kv->SetString( "title", "Hello" );
   274 	kv->SetString( "msg", "Hello there" );
   275 	kv->SetColor( "color", Color( 255, 0, 0, 255 ));
   276 	kv->SetInt( "level", 5);
   277 	kv->SetInt( "time", 10);
   278 	helpers->CreateMessage(pEntity, DIALOG_MSG, kv, vsp_callbacks);
   279 	kv->deleteThis();
   280 }
   281 
   282 void StubPlugin::Hook_ClientDisconnect(edict_t *pEntity)
   283 {
   284 	META_LOG(g_PLAPI, "Hook_ClientDisconnect(%d)", engine->IndexOfEdict(pEntity));
   285 }
   286 
   287 void StubPlugin::Hook_GameFrame(bool simulating)
   288 {
   289 	/**
   290 	 * simulating:
   291 	 * ***********
   292 	 * true  | game is ticking
   293 	 * false | game is not ticking
   294 	 */
   295 }
   296 
   297 bool StubPlugin::Hook_LevelInit(const char *pMapName,
   298 								char const *pMapEntities,
   299 								char const *pOldLevel,
   300 								char const *pLandmarkName,
   301 								bool loadGame,
   302 								bool background)
   303 {
   304 	META_LOG(g_PLAPI, "Hook_LevelInit(%s)", pMapName);
   305 
   306 	return true;
   307 }
   308 
   309 void StubPlugin::Hook_LevelShutdown()
   310 {
   311 	META_LOG(g_PLAPI, "Hook_LevelShutdown()");
   312 }
   313 
   314 void StubPlugin::Hook_SetCommandClient(int index)
   315 {
   316 	META_LOG(g_PLAPI, "Hook_SetCommandClient(%d)", index);
   317 }
   318 
   319 bool StubPlugin::Pause(char *error, size_t maxlen)
   320 {
   321 	return true;
   322 }
   323 
   324 bool StubPlugin::Unpause(char *error, size_t maxlen)
   325 {
   326 	return true;
   327 }
   328 
   329 const char *StubPlugin::GetLicense()
   330 {
   331 	return "Public Domain";
   332 }
   333 
   334 const char *StubPlugin::GetVersion()
   335 {
   336 	return "1.0.0.0";
   337 }
   338 
   339 const char *StubPlugin::GetDate()
   340 {
   341 	return __DATE__;
   342 }
   343 
   344 const char *StubPlugin::GetLogTag()
   345 {
   346 	return "SAMPLE";
   347 }
   348 
   349 const char *StubPlugin::GetAuthor()
   350 {
   351 	return "AlliedModders LLC";
   352 }
   353 
   354 const char *StubPlugin::GetDescription()
   355 {
   356 	return "Sample basic plugin";
   357 }
   358 
   359 const char *StubPlugin::GetName()
   360 {
   361 	return "Sample Plugin";
   362 }
   363 
   364 const char *StubPlugin::GetURL()
   365 {
   366 	return "http://www.sourcemm.net/";
   367 }