Trigger build for hl2sdk-l4d changes.
2 * vim: set ts=4 sw=4 tw=99 noet :
3 * ======================================================
4 * Metamod:Source Sample Plugin
5 * Written by AlliedModders LLC.
6 * ======================================================
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.
12 * This sample plugin is public domain.
16 #include "sample_mm.h"
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);
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 &);
34 SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
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;
46 CGlobalVars *gpGlobals = NULL;
48 ConVar sample_cvar("sample_cvar", "42", 0);
51 * Something like this is needed to register cvars/CON_COMMANDs.
53 class BaseAccessor : public IConCommandBaseAccessor
56 bool RegisterConCommandBase(ConCommandBase *pCommandBase)
58 /* Always call META_REGCVAR instead of going through the engine. */
59 return META_REGCVAR(pCommandBase);
63 PLUGIN_EXPOSE(SamplePlugin, g_SamplePlugin);
64 bool SamplePlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
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);
76 gpGlobals = ismm->GetCGlobals();
78 META_LOG(g_PLAPI, "Starting plugin.");
80 /* Load the VSP listener. This is usually needed for IServerPluginHelpers. */
81 if ((vsp_callbacks = ismm->GetVSPInfo(NULL)) == NULL)
83 ismm->AddListener(this, this);
84 ismm->EnableVSPListener();
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);
99 ENGINE_CALL(LogPrint)("All hooks started!\n");
101 #if SOURCE_ENGINE >= SE_ORANGEBOX
103 ConVar_Register(0, &s_BaseAccessor);
105 ConCommandBaseMgr::OneTimeInit(&s_BaseAccessor);
111 bool SamplePlugin::Unload(char *error, size_t maxlen)
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);
128 void SamplePlugin::OnVSPListening(IServerPluginCallbacks *iface)
130 vsp_callbacks = iface;
133 void SamplePlugin::Hook_ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
135 META_LOG(g_PLAPI, "ServerActivate() called: edictCount = %d, clientMax = %d", edictCount, clientMax);
138 void SamplePlugin::AllPluginsLoaded()
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).
145 void SamplePlugin::Hook_ClientActive(edict_t *pEntity, bool bLoadGame)
147 META_LOG(g_PLAPI, "Hook_ClientActive(%d, %d)", IndexOfEdict(pEntity), bLoadGame);
150 #if SOURCE_ENGINE >= SE_ORANGEBOX
151 void SamplePlugin::Hook_ClientCommand(edict_t *pEntity, const CCommand &args)
153 void SamplePlugin::Hook_ClientCommand(edict_t *pEntity)
156 #if SOURCE_ENGINE <= SE_DARKMESSIAH
160 if (!pEntity || pEntity->IsFree())
165 const char *cmd = args.Arg(0);
166 if (strcmp(cmd, "menu") == 0)
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.");
175 for (int i = 1; i < 9; i++)
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 );
182 KeyValues *item1 = kv->FindKey(num, true);
183 item1->SetString("msg", msg);
184 item1->SetString("command", cmd);
187 helpers->CreateMessage(pEntity, DIALOG_MENU, kv, vsp_callbacks);
189 RETURN_META(MRES_SUPERCEDE);
191 else if (strcmp(cmd, "rich") == 0)
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.");
199 helpers->CreateMessage(pEntity, DIALOG_TEXT, kv, vsp_callbacks);
201 RETURN_META(MRES_SUPERCEDE);
203 else if (strcmp(cmd, "msg") == 0)
205 KeyValues *kv = new KeyValues("menu");
206 kv->SetString("title", "Just a simple hello");
207 kv->SetInt("level", 1);
208 kv->SetInt("time", 20);
210 helpers->CreateMessage(pEntity, DIALOG_MSG, kv, vsp_callbacks);
212 RETURN_META(MRES_SUPERCEDE);
214 else if (strcmp(cmd, "entry") == 0)
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);
223 helpers->CreateMessage(pEntity, DIALOG_ENTRY, kv, vsp_callbacks);
225 RETURN_META(MRES_SUPERCEDE);
229 void SamplePlugin::Hook_ClientSettingsChanged(edict_t *pEdict)
231 if (playerinfomanager)
233 IPlayerInfo *playerinfo = playerinfomanager->GetPlayerInfo(pEdict);
235 const char *name = engine->GetClientConVarValue(IndexOfEdict(pEdict), "name");
237 if (playerinfo != NULL
239 && strcmp(engine->GetPlayerNetworkIDString(pEdict), "BOT") != 0
240 && playerinfo->GetName() != NULL
241 && strcmp(name, playerinfo->GetName()) == 0)
244 MM_Format(msg, sizeof(msg), "Your name changed to \"%s\" (from \"%s\")\n", name, playerinfo->GetName());
245 engine->ClientPrintf(pEdict, msg);
250 bool SamplePlugin::Hook_ClientConnect(edict_t *pEntity,
252 const char *pszAddress,
256 META_LOG(g_PLAPI, "Hook_ClientConnect(%d, \"%s\", \"%s\")", IndexOfEdict(pEntity), pszName, pszAddress);
261 void SamplePlugin::Hook_ClientPutInServer(edict_t *pEntity, char const *playername)
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);
273 void SamplePlugin::Hook_ClientDisconnect(edict_t *pEntity)
275 META_LOG(g_PLAPI, "Hook_ClientDisconnect(%d)", IndexOfEdict(pEntity));
278 void SamplePlugin::Hook_GameFrame(bool simulating)
283 * true | game is ticking
284 * false | game is not ticking
288 bool SamplePlugin::Hook_LevelInit(const char *pMapName,
289 char const *pMapEntities,
290 char const *pOldLevel,
291 char const *pLandmarkName,
295 META_LOG(g_PLAPI, "Hook_LevelInit(%s)", pMapName);
300 void SamplePlugin::Hook_LevelShutdown()
302 META_LOG(g_PLAPI, "Hook_LevelShutdown()");
305 void SamplePlugin::Hook_SetCommandClient(int index)
307 META_LOG(g_PLAPI, "Hook_SetCommandClient(%d)", index);
310 bool SamplePlugin::Pause(char *error, size_t maxlen)
315 bool SamplePlugin::Unpause(char *error, size_t maxlen)
320 const char *SamplePlugin::GetLicense()
322 return "Public Domain";
325 const char *SamplePlugin::GetVersion()
330 const char *SamplePlugin::GetDate()
335 const char *SamplePlugin::GetLogTag()
340 const char *SamplePlugin::GetAuthor()
342 return "AlliedModders LLC";
345 const char *SamplePlugin::GetDescription()
347 return "Sample basic plugin";
350 const char *SamplePlugin::GetName()
352 return "Sample Plugin";
355 const char *SamplePlugin::GetURL()
357 return "http://www.sourcemm.net/";