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