sample_mm/sample_mm.cpp
author Pavol Marko <pavolmarko@gmail.com>
Sun Oct 21 21:55:17 2007 +0000 (2007-10-21)
branchhookman_autogen
changeset 433 a2d986ddd522
parent 379 e8a3f2617a3e
child 497 e6abd02a6a95
permissions -rw-r--r--
Changed license in sourcehook_hookmangen_x86.h (agreed to by BAILOPAN and faluco)
Updated MSVC7 proj file
     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 ORANGEBOX_BUILD
    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 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 PLUGIN_EXPOSE(StubPlugin, g_StubPlugin);
    49 bool StubPlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
    50 {
    51 	PLUGIN_SAVEVARS();
    52 
    53 	GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
    54 	GET_V_IFACE_CURRENT(GetEngineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
    55 	GET_V_IFACE_CURRENT(GetEngineFactory, helpers, IServerPluginHelpers, INTERFACEVERSION_ISERVERPLUGINHELPERS);
    56 	GET_V_IFACE_ANY(GetServerFactory, server, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
    57 	GET_V_IFACE_ANY(GetServerFactory, gameclients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
    58 	GET_V_IFACE_ANY(GetServerFactory, playerinfomanager, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
    59 
    60 	META_LOG(g_PLAPI, "Starting plugin.");
    61 
    62 	/* Load the VSP listener.  Most people won't need this. */
    63 	if ((vsp_callbacks = ismm->GetVSPInfo(NULL)) == NULL)
    64 	{
    65 		ismm->AddListener(this, this);
    66 		ismm->EnableVSPListener();
    67 	}
    68 
    69 	m_hooks.push_back(SH_ADD_HOOK(IServerGameDLL, LevelInit, server, SH_MEMBER(this, &StubPlugin::Hook_LevelInit), true));
    70 	m_hooks.push_back(SH_ADD_HOOK(IServerGameDLL, ServerActivate, server, SH_MEMBER(this, &StubPlugin::Hook_ServerActivate), true));
    71 	m_hooks.push_back(SH_ADD_HOOK(IServerGameDLL, GameFrame, server, SH_MEMBER(this, &StubPlugin::Hook_GameFrame), true));
    72 	m_hooks.push_back(SH_ADD_HOOK(IServerGameDLL, LevelShutdown, server, SH_MEMBER(this, &StubPlugin::Hook_LevelShutdown), false));
    73 	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientActive, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientActive), true));
    74 	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientDisconnect, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientDisconnect), true));
    75 	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientPutInServer, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientPutInServer), true));
    76 	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, SetCommandClient, gameclients, SH_MEMBER(this, &StubPlugin::Hook_SetCommandClient), true));
    77 	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientSettingsChanged, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientSettingsChanged), false));
    78 	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientConnect, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientConnect), false));
    79 	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientCommand, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientCommand), false));
    80 
    81 	SH_CALL(engine, &IVEngineServer::LogPrint)("All hooks started!\n");
    82 
    83 	return true;
    84 }
    85 
    86 bool StubPlugin::Unload(char *error, size_t maxlen)
    87 {
    88 	for (size_t i = 0; i < m_hooks.size(); i++)
    89 	{
    90 		if (m_hooks[i] != 0)
    91 		{
    92 			SH_REMOVE_HOOK_ID(m_hooks[i]);
    93 		}
    94 	}
    95 	m_hooks.clear();
    96 
    97 	return true;
    98 }
    99 
   100 void StubPlugin::OnVSPListening(IServerPluginCallbacks *iface)
   101 {
   102 	vsp_callbacks = iface;
   103 }
   104 
   105 void StubPlugin::Hook_ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
   106 {
   107 	META_LOG(g_PLAPI, "ServerActivate() called: edictCount = %d, clientMax = %d", edictCount, clientMax);
   108 }
   109 
   110 void StubPlugin::AllPluginsLoaded()
   111 {
   112 	/* This is where we'd do stuff that relies on the mod or other plugins 
   113 	 * being initialized (for example, cvars added and events registered).
   114 	 */
   115 }
   116 
   117 void StubPlugin::Hook_ClientActive(edict_t *pEntity, bool bLoadGame)
   118 {
   119 	META_LOG(g_PLAPI, "Hook_ClientActive(%d, %d)", engine->IndexOfEdict(pEntity), bLoadGame);
   120 }
   121 
   122 #if defined ORANGEBOX_BUILD
   123 void StubPlugin::Hook_ClientCommand(edict_t *pEntity, const CCommand &args)
   124 #else
   125 void StubPlugin::Hook_ClientCommand(edict_t *pEntity)
   126 #endif
   127 {
   128 #if !defined ORANGEBOX_BUILD
   129 	CCommand args;
   130 #endif
   131 
   132 	if (!pEntity || !pEntity->IsFree())
   133 	{
   134 		return;
   135 	}
   136 
   137 	const char *cmd = args.Arg(0);
   138 	if (strcmp(cmd, "menu") == 0)
   139 	{
   140 		KeyValues *kv = new KeyValues("menu");
   141 		kv->SetString("title", "You've got options, hit ESC");
   142 		kv->SetInt("level", 1);
   143 		kv->SetColor("color", Color(255, 0, 0, 255));
   144 		kv->SetInt("time", 20);
   145 		kv->SetString("msg", "Pick an option\nOr don't.");
   146 
   147 		for (int i = 1; i < 9; i++)
   148 		{
   149 			char num[10], msg[10], cmd[10];
   150 			g_SMAPI->Format( num, sizeof(num), "%i", i );
   151 			g_SMAPI->Format( msg, sizeof(msg), "Option %i", i );
   152 			g_SMAPI->Format( cmd, sizeof(cmd), "option %i", i );
   153 
   154 			KeyValues *item1 = kv->FindKey(num, true);
   155 			item1->SetString("msg", msg);
   156 			item1->SetString("command", cmd);
   157 		}
   158 
   159 		helpers->CreateMessage(pEntity, DIALOG_MENU, kv, vsp_callbacks);
   160 		kv->deleteThis();
   161 		RETURN_META(MRES_SUPERCEDE);
   162 	}
   163 	else if (strcmp(cmd, "rich") == 0)
   164 	{
   165 		KeyValues *kv = new KeyValues("menu");
   166 		kv->SetString("title", "A rich message");
   167 		kv->SetInt("level", 1);
   168 		kv->SetInt("time", 20);
   169 		kv->SetString("msg", "This is a long long long text string.\n\nIt also has line breaks.");
   170 
   171 		helpers->CreateMessage(pEntity, DIALOG_TEXT, kv, vsp_callbacks);
   172 		kv->deleteThis();
   173 		RETURN_META(MRES_SUPERCEDE);
   174 	}
   175 	else if (strcmp(cmd, "msg") == 0)
   176 	{
   177 		KeyValues *kv = new KeyValues("menu");
   178 		kv->SetString("title", "Just a simple hello");
   179 		kv->SetInt("level", 1);
   180 		kv->SetInt("time", 20);
   181 
   182 		helpers->CreateMessage(pEntity, DIALOG_MSG, kv, vsp_callbacks);
   183 		kv->deleteThis();
   184 		RETURN_META(MRES_SUPERCEDE);
   185 	}
   186 	else if (strcmp(cmd, "entry") == 0)
   187 	{
   188 		KeyValues *kv = new KeyValues("entry");
   189 		kv->SetString("title", "Stuff");
   190 		kv->SetString("msg", "Enter something");
   191 		kv->SetString("command", "say"); // anything they enter into the dialog turns into a say command
   192 		kv->SetInt("level", 1);
   193 		kv->SetInt("time", 20);
   194 
   195 		helpers->CreateMessage(pEntity, DIALOG_ENTRY, kv, vsp_callbacks);
   196 		kv->deleteThis();
   197 		RETURN_META(MRES_SUPERCEDE);
   198 	}
   199 }
   200 
   201 void StubPlugin::Hook_ClientSettingsChanged(edict_t *pEdict)
   202 {
   203 	if (playerinfomanager)
   204 	{
   205 		IPlayerInfo *playerinfo = playerinfomanager->GetPlayerInfo(pEdict);
   206 
   207 		const char *name = engine->GetClientConVarValue(engine->IndexOfEdict(pEdict), "name");
   208 
   209 		if (playerinfo != NULL
   210 			&& name != NULL
   211 			&& playerinfo->GetName() != NULL
   212 			&& strcmp(name, playerinfo->GetName()) == 0)
   213 		{
   214 			char msg[128];
   215 			g_SMAPI->Format(msg, sizeof(msg), "Your name changed to \"%s\" (from \"%s\")\n", name, playerinfo->GetName());
   216 			engine->ClientPrintf(pEdict, msg);
   217 		}
   218 	}
   219 }
   220 
   221 bool StubPlugin::Hook_ClientConnect(edict_t *pEntity,
   222 									const char *pszName,
   223 									const char *pszAddress,
   224 									char *reject,
   225 									int maxrejectlen)
   226 {
   227 	META_LOG(g_PLAPI, "Hook_ClientConnect(%d, \"%s\", \"%s\")", engine->IndexOfEdict(pEntity), pszName, pszAddress);
   228 
   229 	return true;
   230 }
   231 
   232 void StubPlugin::Hook_ClientPutInServer(edict_t *pEntity, char const *playername)
   233 {
   234 	KeyValues *kv = new KeyValues( "msg" );
   235 	kv->SetString( "title", "Hello" );
   236 	kv->SetString( "msg", "Hello there" );
   237 	kv->SetColor( "color", Color( 255, 0, 0, 255 ));
   238 	kv->SetInt( "level", 5);
   239 	kv->SetInt( "time", 10);
   240 	helpers->CreateMessage(pEntity, DIALOG_MSG, kv, vsp_callbacks);
   241 	kv->deleteThis();
   242 }
   243 
   244 void StubPlugin::Hook_ClientDisconnect(edict_t *pEntity)
   245 {
   246 	META_LOG(g_PLAPI, "Hook_ClientDisconnect(%d)", engine->IndexOfEdict(pEntity));
   247 }
   248 
   249 void StubPlugin::Hook_GameFrame(bool simulating)
   250 {
   251 	/**
   252 	 * simulating:
   253 	 * ***********
   254 	 * true  | game is ticking
   255 	 * false | game is not ticking
   256 	 */
   257 }
   258 
   259 bool StubPlugin::Hook_LevelInit(const char *pMapName,
   260 								char const *pMapEntities,
   261 								char const *pOldLevel,
   262 								char const *pLandmarkName,
   263 								bool loadGame,
   264 								bool background)
   265 {
   266 	META_LOG(g_PLAPI, "Hook_LevelInit(%s)", pMapName);
   267 
   268 	return true;
   269 }
   270 
   271 void StubPlugin::Hook_LevelShutdown()
   272 {
   273 	META_LOG(g_PLAPI, "Hook_LevelShutdown()");
   274 }
   275 
   276 void StubPlugin::Hook_SetCommandClient(int index)
   277 {
   278 	META_LOG(g_PLAPI, "Hook_SetCommandClient(%d)", index);
   279 }
   280 
   281 bool StubPlugin::Pause(char *error, size_t maxlen)
   282 {
   283 	return true;
   284 }
   285 
   286 bool StubPlugin::Unpause(char *error, size_t maxlen)
   287 {
   288 	return true;
   289 }
   290 
   291 const char *StubPlugin::GetLicense()
   292 {
   293 	return "Public Domain";
   294 }
   295 
   296 const char *StubPlugin::GetVersion()
   297 {
   298 	return "1.0.0.0";
   299 }
   300 
   301 const char *StubPlugin::GetDate()
   302 {
   303 	return __DATE__;
   304 }
   305 
   306 const char *StubPlugin::GetLogTag()
   307 {
   308 	return "SAMPLE";
   309 }
   310 
   311 const char *StubPlugin::GetAuthor()
   312 {
   313 	return "AlliedModders LLC";
   314 }
   315 
   316 const char *StubPlugin::GetDescription()
   317 {
   318 	return "Sample basic plugin";
   319 }
   320 
   321 const char *StubPlugin::GetName()
   322 {
   323 	return "Sample Plugin";
   324 }
   325 
   326 const char *StubPlugin::GetURL()
   327 {
   328 	return "http://www.sourcemm.net/";
   329 }