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