Remove dev part of bootstrap, for release builds.
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.
18 #include "sample_mm.h"
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);
32 #if SOURCE_ENGINE >= SE_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 &);
36 SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
39 SamplePlugin g_SamplePlugin;
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;
48 CGlobalVars *gpGlobals = NULL;
50 ConVar sample_cvar("sample_cvar", "42", 0);
53 * Something like this is needed to register cvars/CON_COMMANDs.
55 class BaseAccessor : public IConCommandBaseAccessor
58 bool RegisterConCommandBase(ConCommandBase *pCommandBase)
60 /* Always call META_REGCVAR instead of going through the engine. */
61 return META_REGCVAR(pCommandBase);
65 PLUGIN_EXPOSE(SamplePlugin, g_SamplePlugin);
66 bool SamplePlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
70 GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
71 GET_V_IFACE_CURRENT(GetEngineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
72 GET_V_IFACE_CURRENT(GetEngineFactory, helpers, IServerPluginHelpers, INTERFACEVERSION_ISERVERPLUGINHELPERS);
73 GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
74 GET_V_IFACE_ANY(GetServerFactory, server, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
75 GET_V_IFACE_ANY(GetServerFactory, gameclients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
76 GET_V_IFACE_ANY(GetServerFactory, playerinfomanager, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
78 gpGlobals = ismm->GetCGlobals();
80 META_LOG(g_PLAPI, "Starting plugin.");
82 /* Load the VSP listener. This is usually needed for IServerPluginHelpers. */
83 #if defined METAMOD_PLAPI_VERSION
84 if ((vsp_callbacks = ismm->GetVSPInfo(NULL)) == NULL)
87 ismm->AddListener(this, this);
88 ismm->EnableVSPListener();
91 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, server, this, &SamplePlugin::Hook_LevelInit, true);
92 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, server, this, &SamplePlugin::Hook_ServerActivate, true);
93 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, server, this, &SamplePlugin::Hook_GameFrame, true);
94 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, server, this, &SamplePlugin::Hook_LevelShutdown, false);
95 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientActive, gameclients, this, &SamplePlugin::Hook_ClientActive, true);
96 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, gameclients, this, &SamplePlugin::Hook_ClientDisconnect, true);
97 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, gameclients, this, &SamplePlugin::Hook_ClientPutInServer, true);
98 SH_ADD_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, gameclients, this, &SamplePlugin::Hook_SetCommandClient, true);
99 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, gameclients, this, &SamplePlugin::Hook_ClientSettingsChanged, false);
100 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientConnect, gameclients, this, &SamplePlugin::Hook_ClientConnect, false);
101 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientCommand, gameclients, this, &SamplePlugin::Hook_ClientCommand, false);
103 #if SOURCE_ENGINE == SE_EPISODEONE
104 m_EngineCC = SH_GET_CALLCLASS(engine);
107 ENGINE_CALL(&IVEngineServer::LogPrint)("All hooks started!\n");
109 #if SOURCE_ENGINE >= SE_ORANGEBOX
111 ConVar_Register(0, &s_BaseAccessor);
113 ConCommandBaseMgr::OneTimeInit(&s_BaseAccessor);
119 bool SamplePlugin::Unload(char *error, size_t maxlen)
121 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, server, this, &SamplePlugin::Hook_LevelInit, true);
122 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, server, this, &SamplePlugin::Hook_ServerActivate, true);
123 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, server, this, &SamplePlugin::Hook_GameFrame, true);
124 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, server, this, &SamplePlugin::Hook_LevelShutdown, false);
125 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientActive, gameclients, this, &SamplePlugin::Hook_ClientActive, true);
126 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, gameclients, this, &SamplePlugin::Hook_ClientDisconnect, true);
127 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, gameclients, this, &SamplePlugin::Hook_ClientPutInServer, true);
128 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, gameclients, this, &SamplePlugin::Hook_SetCommandClient, true);
129 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, gameclients, this, &SamplePlugin::Hook_ClientSettingsChanged, false);
130 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientConnect, gameclients, this, &SamplePlugin::Hook_ClientConnect, false);
131 SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientCommand, gameclients, this, &SamplePlugin::Hook_ClientCommand, false);
133 #if SOURCE_ENGINE == SE_EPISODEONE
134 SH_RELEASE_CALLCLASS(m_EngineCC);
140 void SamplePlugin::OnVSPListening(IServerPluginCallbacks *iface)
142 vsp_callbacks = iface;
145 void SamplePlugin::Hook_ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
147 META_LOG(g_PLAPI, "ServerActivate() called: edictCount = %d, clientMax = %d", edictCount, clientMax);
150 void SamplePlugin::AllPluginsLoaded()
152 /* This is where we'd do stuff that relies on the mod or other plugins
153 * being initialized (for example, cvars added and events registered).
157 void SamplePlugin::Hook_ClientActive(edict_t *pEntity, bool bLoadGame)
159 META_LOG(g_PLAPI, "Hook_ClientActive(%d, %d)", IndexOfEdict(pEntity), bLoadGame);
162 #if SOURCE_ENGINE >= SE_ORANGEBOX
163 void SamplePlugin::Hook_ClientCommand(edict_t *pEntity, const CCommand &args)
165 void SamplePlugin::Hook_ClientCommand(edict_t *pEntity)
168 #if SOURCE_ENGINE == SE_EPISODEONE
172 if (!pEntity || pEntity->IsFree())
177 const char *cmd = args.Arg(0);
178 if (strcmp(cmd, "menu") == 0)
180 KeyValues *kv = new KeyValues("menu");
181 kv->SetString("title", "You've got options, hit ESC");
182 kv->SetInt("level", 1);
183 kv->SetColor("color", Color(255, 0, 0, 255));
184 kv->SetInt("time", 20);
185 kv->SetString("msg", "Pick an option\nOr don't.");
187 for (int i = 1; i < 9; i++)
189 char num[10], msg[10], cmd[10];
190 MM_Format( num, sizeof(num), "%i", i );
191 MM_Format( msg, sizeof(msg), "Option %i", i );
192 MM_Format( cmd, sizeof(cmd), "option %i", i );
194 KeyValues *item1 = kv->FindKey(num, true);
195 item1->SetString("msg", msg);
196 item1->SetString("command", cmd);
199 helpers->CreateMessage(pEntity, DIALOG_MENU, kv, vsp_callbacks);
201 RETURN_META(MRES_SUPERCEDE);
203 else if (strcmp(cmd, "rich") == 0)
205 KeyValues *kv = new KeyValues("menu");
206 kv->SetString("title", "A rich message");
207 kv->SetInt("level", 1);
208 kv->SetInt("time", 20);
209 kv->SetString("msg", "This is a long long long text string.\n\nIt also has line breaks.");
211 helpers->CreateMessage(pEntity, DIALOG_TEXT, kv, vsp_callbacks);
213 RETURN_META(MRES_SUPERCEDE);
215 else if (strcmp(cmd, "msg") == 0)
217 KeyValues *kv = new KeyValues("menu");
218 kv->SetString("title", "Just a simple hello");
219 kv->SetInt("level", 1);
220 kv->SetInt("time", 20);
222 helpers->CreateMessage(pEntity, DIALOG_MSG, kv, vsp_callbacks);
224 RETURN_META(MRES_SUPERCEDE);
226 else if (strcmp(cmd, "entry") == 0)
228 KeyValues *kv = new KeyValues("entry");
229 kv->SetString("title", "Stuff");
230 kv->SetString("msg", "Enter something");
231 kv->SetString("command", "say"); // anything they enter into the dialog turns into a say command
232 kv->SetInt("level", 1);
233 kv->SetInt("time", 20);
235 helpers->CreateMessage(pEntity, DIALOG_ENTRY, kv, vsp_callbacks);
237 RETURN_META(MRES_SUPERCEDE);
241 void SamplePlugin::Hook_ClientSettingsChanged(edict_t *pEdict)
243 if (playerinfomanager)
245 IPlayerInfo *playerinfo = playerinfomanager->GetPlayerInfo(pEdict);
247 const char *name = engine->GetClientConVarValue(IndexOfEdict(pEdict), "name");
249 if (playerinfo != NULL
251 && strcmp(engine->GetPlayerNetworkIDString(pEdict), "BOT") != 0
252 && playerinfo->GetName() != NULL
253 && strcmp(name, playerinfo->GetName()) == 0)
256 MM_Format(msg, sizeof(msg), "Your name changed to \"%s\" (from \"%s\")\n", name, playerinfo->GetName());
257 engine->ClientPrintf(pEdict, msg);
262 bool SamplePlugin::Hook_ClientConnect(edict_t *pEntity,
264 const char *pszAddress,
268 META_LOG(g_PLAPI, "Hook_ClientConnect(%d, \"%s\", \"%s\")", IndexOfEdict(pEntity), pszName, pszAddress);
273 void SamplePlugin::Hook_ClientPutInServer(edict_t *pEntity, char const *playername)
275 KeyValues *kv = new KeyValues( "msg" );
276 kv->SetString( "title", "Hello" );
277 kv->SetString( "msg", "Hello there" );
278 kv->SetColor( "color", Color( 255, 0, 0, 255 ));
279 kv->SetInt( "level", 5);
280 kv->SetInt( "time", 10);
281 helpers->CreateMessage(pEntity, DIALOG_MSG, kv, vsp_callbacks);
285 void SamplePlugin::Hook_ClientDisconnect(edict_t *pEntity)
287 META_LOG(g_PLAPI, "Hook_ClientDisconnect(%d)", IndexOfEdict(pEntity));
290 void SamplePlugin::Hook_GameFrame(bool simulating)
295 * true | game is ticking
296 * false | game is not ticking
300 bool SamplePlugin::Hook_LevelInit(const char *pMapName,
301 char const *pMapEntities,
302 char const *pOldLevel,
303 char const *pLandmarkName,
307 META_LOG(g_PLAPI, "Hook_LevelInit(%s)", pMapName);
312 void SamplePlugin::Hook_LevelShutdown()
314 META_LOG(g_PLAPI, "Hook_LevelShutdown()");
317 void SamplePlugin::Hook_SetCommandClient(int index)
319 META_LOG(g_PLAPI, "Hook_SetCommandClient(%d)", index);
322 bool SamplePlugin::Pause(char *error, size_t maxlen)
327 bool SamplePlugin::Unpause(char *error, size_t maxlen)
332 const char *SamplePlugin::GetLicense()
334 return "Public Domain";
337 const char *SamplePlugin::GetVersion()
342 const char *SamplePlugin::GetDate()
347 const char *SamplePlugin::GetLogTag()
352 const char *SamplePlugin::GetAuthor()
354 return "AlliedModders LLC";
357 const char *SamplePlugin::GetDescription()
359 return "Sample basic plugin";
362 const char *SamplePlugin::GetName()
364 return "Sample Plugin";
367 const char *SamplePlugin::GetURL()
369 return "http://www.sourcemm.net/";