sample_mm/sample_mm.cpp
changeset 379 e8a3f2617a3e
child 497 e6abd02a6a95
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sample_mm/sample_mm.cpp	Sun Oct 07 03:12:19 2007 +0000
     1.3 @@ -0,0 +1,329 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * ======================================================
     1.7 + * Metamod:Source Sample Plugin
     1.8 + * Written by AlliedModders LLC.
     1.9 + * ======================================================
    1.10 + *
    1.11 + * This software is provided 'as-is', without any express or implied warranty.
    1.12 + * In no event will the authors be held liable for any damages arising from 
    1.13 + * the use of this software.
    1.14 + *
    1.15 + * This sample plugin is public domain.
    1.16 + *
    1.17 + * Version: $Id$
    1.18 + */
    1.19 +
    1.20 +#include <stdio.h>
    1.21 +#include "sample_mm.h"
    1.22 +
    1.23 +SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, 0, bool, char const *, char const *, char const *, char const *, bool, bool);
    1.24 +SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int, int);
    1.25 +SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, 0, bool);
    1.26 +SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, 0);
    1.27 +SH_DECL_HOOK2_void(IServerGameClients, ClientActive, SH_NOATTRIB, 0, edict_t *, bool);
    1.28 +SH_DECL_HOOK1_void(IServerGameClients, ClientDisconnect, SH_NOATTRIB, 0, edict_t *);
    1.29 +SH_DECL_HOOK2_void(IServerGameClients, ClientPutInServer, SH_NOATTRIB, 0, edict_t *, char const *);
    1.30 +SH_DECL_HOOK1_void(IServerGameClients, SetCommandClient, SH_NOATTRIB, 0, int);
    1.31 +SH_DECL_HOOK1_void(IServerGameClients, ClientSettingsChanged, SH_NOATTRIB, 0, edict_t *);
    1.32 +SH_DECL_HOOK5(IServerGameClients, ClientConnect, SH_NOATTRIB, 0, bool, edict_t *, const char*, const char *, char *, int);
    1.33 +SH_DECL_HOOK2(IGameEventManager2, FireEvent, SH_NOATTRIB, 0, bool, IGameEvent *, bool);
    1.34 +
    1.35 +#if defined ORANGEBOX_BUILD
    1.36 +SH_DECL_HOOK2_void(IServerGameClients, NetworkIDValidated, SH_NOATTRIB, 0, const char *, const char *);
    1.37 +SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
    1.38 +#else
    1.39 +SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
    1.40 +#endif
    1.41 +
    1.42 +StubPlugin g_StubPlugin;
    1.43 +IServerGameDLL *server = NULL;
    1.44 +IServerGameClients *gameclients = NULL;
    1.45 +IVEngineServer *engine = NULL;
    1.46 +IServerPluginHelpers *helpers = NULL;
    1.47 +IGameEventManager2 *gameevents = NULL;
    1.48 +IServerPluginCallbacks *vsp_callbacks = NULL;
    1.49 +IPlayerInfoManager *playerinfomanager = NULL;
    1.50 +
    1.51 +PLUGIN_EXPOSE(StubPlugin, g_StubPlugin);
    1.52 +bool StubPlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
    1.53 +{
    1.54 +	PLUGIN_SAVEVARS();
    1.55 +
    1.56 +	GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
    1.57 +	GET_V_IFACE_CURRENT(GetEngineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
    1.58 +	GET_V_IFACE_CURRENT(GetEngineFactory, helpers, IServerPluginHelpers, INTERFACEVERSION_ISERVERPLUGINHELPERS);
    1.59 +	GET_V_IFACE_ANY(GetServerFactory, server, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
    1.60 +	GET_V_IFACE_ANY(GetServerFactory, gameclients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
    1.61 +	GET_V_IFACE_ANY(GetServerFactory, playerinfomanager, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
    1.62 +
    1.63 +	META_LOG(g_PLAPI, "Starting plugin.");
    1.64 +
    1.65 +	/* Load the VSP listener.  Most people won't need this. */
    1.66 +	if ((vsp_callbacks = ismm->GetVSPInfo(NULL)) == NULL)
    1.67 +	{
    1.68 +		ismm->AddListener(this, this);
    1.69 +		ismm->EnableVSPListener();
    1.70 +	}
    1.71 +
    1.72 +	m_hooks.push_back(SH_ADD_HOOK(IServerGameDLL, LevelInit, server, SH_MEMBER(this, &StubPlugin::Hook_LevelInit), true));
    1.73 +	m_hooks.push_back(SH_ADD_HOOK(IServerGameDLL, ServerActivate, server, SH_MEMBER(this, &StubPlugin::Hook_ServerActivate), true));
    1.74 +	m_hooks.push_back(SH_ADD_HOOK(IServerGameDLL, GameFrame, server, SH_MEMBER(this, &StubPlugin::Hook_GameFrame), true));
    1.75 +	m_hooks.push_back(SH_ADD_HOOK(IServerGameDLL, LevelShutdown, server, SH_MEMBER(this, &StubPlugin::Hook_LevelShutdown), false));
    1.76 +	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientActive, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientActive), true));
    1.77 +	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientDisconnect, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientDisconnect), true));
    1.78 +	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientPutInServer, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientPutInServer), true));
    1.79 +	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, SetCommandClient, gameclients, SH_MEMBER(this, &StubPlugin::Hook_SetCommandClient), true));
    1.80 +	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientSettingsChanged, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientSettingsChanged), false));
    1.81 +	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientConnect, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientConnect), false));
    1.82 +	m_hooks.push_back(SH_ADD_HOOK(IServerGameClients, ClientCommand, gameclients, SH_MEMBER(this, &StubPlugin::Hook_ClientCommand), false));
    1.83 +
    1.84 +	SH_CALL(engine, &IVEngineServer::LogPrint)("All hooks started!\n");
    1.85 +
    1.86 +	return true;
    1.87 +}
    1.88 +
    1.89 +bool StubPlugin::Unload(char *error, size_t maxlen)
    1.90 +{
    1.91 +	for (size_t i = 0; i < m_hooks.size(); i++)
    1.92 +	{
    1.93 +		if (m_hooks[i] != 0)
    1.94 +		{
    1.95 +			SH_REMOVE_HOOK_ID(m_hooks[i]);
    1.96 +		}
    1.97 +	}
    1.98 +	m_hooks.clear();
    1.99 +
   1.100 +	return true;
   1.101 +}
   1.102 +
   1.103 +void StubPlugin::OnVSPListening(IServerPluginCallbacks *iface)
   1.104 +{
   1.105 +	vsp_callbacks = iface;
   1.106 +}
   1.107 +
   1.108 +void StubPlugin::Hook_ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
   1.109 +{
   1.110 +	META_LOG(g_PLAPI, "ServerActivate() called: edictCount = %d, clientMax = %d", edictCount, clientMax);
   1.111 +}
   1.112 +
   1.113 +void StubPlugin::AllPluginsLoaded()
   1.114 +{
   1.115 +	/* This is where we'd do stuff that relies on the mod or other plugins 
   1.116 +	 * being initialized (for example, cvars added and events registered).
   1.117 +	 */
   1.118 +}
   1.119 +
   1.120 +void StubPlugin::Hook_ClientActive(edict_t *pEntity, bool bLoadGame)
   1.121 +{
   1.122 +	META_LOG(g_PLAPI, "Hook_ClientActive(%d, %d)", engine->IndexOfEdict(pEntity), bLoadGame);
   1.123 +}
   1.124 +
   1.125 +#if defined ORANGEBOX_BUILD
   1.126 +void StubPlugin::Hook_ClientCommand(edict_t *pEntity, const CCommand &args)
   1.127 +#else
   1.128 +void StubPlugin::Hook_ClientCommand(edict_t *pEntity)
   1.129 +#endif
   1.130 +{
   1.131 +#if !defined ORANGEBOX_BUILD
   1.132 +	CCommand args;
   1.133 +#endif
   1.134 +
   1.135 +	if (!pEntity || !pEntity->IsFree())
   1.136 +	{
   1.137 +		return;
   1.138 +	}
   1.139 +
   1.140 +	const char *cmd = args.Arg(0);
   1.141 +	if (strcmp(cmd, "menu") == 0)
   1.142 +	{
   1.143 +		KeyValues *kv = new KeyValues("menu");
   1.144 +		kv->SetString("title", "You've got options, hit ESC");
   1.145 +		kv->SetInt("level", 1);
   1.146 +		kv->SetColor("color", Color(255, 0, 0, 255));
   1.147 +		kv->SetInt("time", 20);
   1.148 +		kv->SetString("msg", "Pick an option\nOr don't.");
   1.149 +
   1.150 +		for (int i = 1; i < 9; i++)
   1.151 +		{
   1.152 +			char num[10], msg[10], cmd[10];
   1.153 +			g_SMAPI->Format( num, sizeof(num), "%i", i );
   1.154 +			g_SMAPI->Format( msg, sizeof(msg), "Option %i", i );
   1.155 +			g_SMAPI->Format( cmd, sizeof(cmd), "option %i", i );
   1.156 +
   1.157 +			KeyValues *item1 = kv->FindKey(num, true);
   1.158 +			item1->SetString("msg", msg);
   1.159 +			item1->SetString("command", cmd);
   1.160 +		}
   1.161 +
   1.162 +		helpers->CreateMessage(pEntity, DIALOG_MENU, kv, vsp_callbacks);
   1.163 +		kv->deleteThis();
   1.164 +		RETURN_META(MRES_SUPERCEDE);
   1.165 +	}
   1.166 +	else if (strcmp(cmd, "rich") == 0)
   1.167 +	{
   1.168 +		KeyValues *kv = new KeyValues("menu");
   1.169 +		kv->SetString("title", "A rich message");
   1.170 +		kv->SetInt("level", 1);
   1.171 +		kv->SetInt("time", 20);
   1.172 +		kv->SetString("msg", "This is a long long long text string.\n\nIt also has line breaks.");
   1.173 +
   1.174 +		helpers->CreateMessage(pEntity, DIALOG_TEXT, kv, vsp_callbacks);
   1.175 +		kv->deleteThis();
   1.176 +		RETURN_META(MRES_SUPERCEDE);
   1.177 +	}
   1.178 +	else if (strcmp(cmd, "msg") == 0)
   1.179 +	{
   1.180 +		KeyValues *kv = new KeyValues("menu");
   1.181 +		kv->SetString("title", "Just a simple hello");
   1.182 +		kv->SetInt("level", 1);
   1.183 +		kv->SetInt("time", 20);
   1.184 +
   1.185 +		helpers->CreateMessage(pEntity, DIALOG_MSG, kv, vsp_callbacks);
   1.186 +		kv->deleteThis();
   1.187 +		RETURN_META(MRES_SUPERCEDE);
   1.188 +	}
   1.189 +	else if (strcmp(cmd, "entry") == 0)
   1.190 +	{
   1.191 +		KeyValues *kv = new KeyValues("entry");
   1.192 +		kv->SetString("title", "Stuff");
   1.193 +		kv->SetString("msg", "Enter something");
   1.194 +		kv->SetString("command", "say"); // anything they enter into the dialog turns into a say command
   1.195 +		kv->SetInt("level", 1);
   1.196 +		kv->SetInt("time", 20);
   1.197 +
   1.198 +		helpers->CreateMessage(pEntity, DIALOG_ENTRY, kv, vsp_callbacks);
   1.199 +		kv->deleteThis();
   1.200 +		RETURN_META(MRES_SUPERCEDE);
   1.201 +	}
   1.202 +}
   1.203 +
   1.204 +void StubPlugin::Hook_ClientSettingsChanged(edict_t *pEdict)
   1.205 +{
   1.206 +	if (playerinfomanager)
   1.207 +	{
   1.208 +		IPlayerInfo *playerinfo = playerinfomanager->GetPlayerInfo(pEdict);
   1.209 +
   1.210 +		const char *name = engine->GetClientConVarValue(engine->IndexOfEdict(pEdict), "name");
   1.211 +
   1.212 +		if (playerinfo != NULL
   1.213 +			&& name != NULL
   1.214 +			&& playerinfo->GetName() != NULL
   1.215 +			&& strcmp(name, playerinfo->GetName()) == 0)
   1.216 +		{
   1.217 +			char msg[128];
   1.218 +			g_SMAPI->Format(msg, sizeof(msg), "Your name changed to \"%s\" (from \"%s\")\n", name, playerinfo->GetName());
   1.219 +			engine->ClientPrintf(pEdict, msg);
   1.220 +		}
   1.221 +	}
   1.222 +}
   1.223 +
   1.224 +bool StubPlugin::Hook_ClientConnect(edict_t *pEntity,
   1.225 +									const char *pszName,
   1.226 +									const char *pszAddress,
   1.227 +									char *reject,
   1.228 +									int maxrejectlen)
   1.229 +{
   1.230 +	META_LOG(g_PLAPI, "Hook_ClientConnect(%d, \"%s\", \"%s\")", engine->IndexOfEdict(pEntity), pszName, pszAddress);
   1.231 +
   1.232 +	return true;
   1.233 +}
   1.234 +
   1.235 +void StubPlugin::Hook_ClientPutInServer(edict_t *pEntity, char const *playername)
   1.236 +{
   1.237 +	KeyValues *kv = new KeyValues( "msg" );
   1.238 +	kv->SetString( "title", "Hello" );
   1.239 +	kv->SetString( "msg", "Hello there" );
   1.240 +	kv->SetColor( "color", Color( 255, 0, 0, 255 ));
   1.241 +	kv->SetInt( "level", 5);
   1.242 +	kv->SetInt( "time", 10);
   1.243 +	helpers->CreateMessage(pEntity, DIALOG_MSG, kv, vsp_callbacks);
   1.244 +	kv->deleteThis();
   1.245 +}
   1.246 +
   1.247 +void StubPlugin::Hook_ClientDisconnect(edict_t *pEntity)
   1.248 +{
   1.249 +	META_LOG(g_PLAPI, "Hook_ClientDisconnect(%d)", engine->IndexOfEdict(pEntity));
   1.250 +}
   1.251 +
   1.252 +void StubPlugin::Hook_GameFrame(bool simulating)
   1.253 +{
   1.254 +	/**
   1.255 +	 * simulating:
   1.256 +	 * ***********
   1.257 +	 * true  | game is ticking
   1.258 +	 * false | game is not ticking
   1.259 +	 */
   1.260 +}
   1.261 +
   1.262 +bool StubPlugin::Hook_LevelInit(const char *pMapName,
   1.263 +								char const *pMapEntities,
   1.264 +								char const *pOldLevel,
   1.265 +								char const *pLandmarkName,
   1.266 +								bool loadGame,
   1.267 +								bool background)
   1.268 +{
   1.269 +	META_LOG(g_PLAPI, "Hook_LevelInit(%s)", pMapName);
   1.270 +
   1.271 +	return true;
   1.272 +}
   1.273 +
   1.274 +void StubPlugin::Hook_LevelShutdown()
   1.275 +{
   1.276 +	META_LOG(g_PLAPI, "Hook_LevelShutdown()");
   1.277 +}
   1.278 +
   1.279 +void StubPlugin::Hook_SetCommandClient(int index)
   1.280 +{
   1.281 +	META_LOG(g_PLAPI, "Hook_SetCommandClient(%d)", index);
   1.282 +}
   1.283 +
   1.284 +bool StubPlugin::Pause(char *error, size_t maxlen)
   1.285 +{
   1.286 +	return true;
   1.287 +}
   1.288 +
   1.289 +bool StubPlugin::Unpause(char *error, size_t maxlen)
   1.290 +{
   1.291 +	return true;
   1.292 +}
   1.293 +
   1.294 +const char *StubPlugin::GetLicense()
   1.295 +{
   1.296 +	return "Public Domain";
   1.297 +}
   1.298 +
   1.299 +const char *StubPlugin::GetVersion()
   1.300 +{
   1.301 +	return "1.0.0.0";
   1.302 +}
   1.303 +
   1.304 +const char *StubPlugin::GetDate()
   1.305 +{
   1.306 +	return __DATE__;
   1.307 +}
   1.308 +
   1.309 +const char *StubPlugin::GetLogTag()
   1.310 +{
   1.311 +	return "SAMPLE";
   1.312 +}
   1.313 +
   1.314 +const char *StubPlugin::GetAuthor()
   1.315 +{
   1.316 +	return "AlliedModders LLC";
   1.317 +}
   1.318 +
   1.319 +const char *StubPlugin::GetDescription()
   1.320 +{
   1.321 +	return "Sample basic plugin";
   1.322 +}
   1.323 +
   1.324 +const char *StubPlugin::GetName()
   1.325 +{
   1.326 +	return "Sample Plugin";
   1.327 +}
   1.328 +
   1.329 +const char *StubPlugin::GetURL()
   1.330 +{
   1.331 +	return "http://www.sourcemm.net/";
   1.332 +}