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