Changed a few natives/stocks that should have used IsClientInGame() rather than IsClientConnected()
3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
15 #include "ConVarManager.h"
16 #include "HalfLife2.h"
17 #include "PluginSys.h"
18 #include "ForwardSys.h"
19 #include "HandleSys.h"
20 #include "sm_srvcmds.h"
21 #include "sm_stringutil.h"
22 #include <sh_vector.h>
24 ConVarManager g_ConVarManager;
26 SH_DECL_HOOK5_void(IServerGameDLL, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
27 SH_DECL_HOOK5_void(IServerPluginCallbacks, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
29 const ParamType CONVARCHANGE_PARAMS[] = {Param_Cell, Param_String, Param_String};
30 typedef List<const ConVar *> ConVarList;
32 ConVarManager::ConVarManager() : m_ConVarType(0), m_VSPIface(NULL), m_CanQueryConVars(false)
34 /* Create a convar lookup trie */
35 m_ConVarCache = sm_trie_create();
38 ConVarManager::~ConVarManager()
40 List<ConVarInfo *>::iterator iter;
42 /* Destroy our convar lookup trie */
43 sm_trie_destroy(m_ConVarCache);
45 /* Destroy all the ConVarInfo structures */
46 for (iter = m_ConVars.begin(); iter != m_ConVars.end(); iter++)
54 void ConVarManager::OnSourceModAllInitialized()
56 /* Only valid with ServerGameDLL006 or greater */
57 if (g_SMAPI->GetGameDLLVersion() >= 6)
59 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, OnQueryCvarValueFinished, gamedll, this, &ConVarManager::OnQueryCvarValueFinished, false);
60 m_CanQueryConVars = true;
65 /* Set up access rights for the 'ConVar' handle type */
66 sec.access[HandleAccess_Read] = 0;
67 sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
68 sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
70 /* Create the 'ConVar' handle type */
71 m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL);
73 /* Add the 'convars' option to the 'sm' console command */
74 g_RootMenu.AddRootConsoleCommand("convars", "View convars created by a plugin", this);
77 void ConVarManager::OnSourceModShutdown()
79 if (m_CanQueryConVars)
81 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, OnQueryCvarValueFinished, gamedll, this, &ConVarManager::OnQueryCvarValueFinished, false);
82 SH_REMOVE_HOOK_MEMFUNC(IServerPluginCallbacks, OnQueryCvarValueFinished, m_VSPIface, this, &ConVarManager::OnQueryCvarValueFinished, false);
85 IChangeableForward *fwd;
86 List<ConVarInfo *>::iterator i;
88 /* Iterate list of ConVarInfo structures */
89 for (i = m_ConVars.begin(); i != m_ConVars.end(); i++)
91 fwd = (*i)->pChangeForward;
93 /* Free any convar-change forwards that still exist */
96 g_Forwards.ReleaseForward(fwd);
100 /* Remove the 'convars' option from the 'sm' console command */
101 g_RootMenu.RemoveRootConsoleCommand("convars", this);
103 /* Remove the 'ConVar' handle type */
104 g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent);
107 void ConVarManager::OnSourceModVSPReceived(IServerPluginCallbacks *iface)
109 /* This will be called after OnSourceModAllInitialized(), so...
111 * If we haven't been able to hook the IServerGameDLL version at this point,
112 * then use hook IServerPluginCallbacks version from the engine.
115 /* The Ship currently only supports ServerPluginCallbacks001, but we need 002 */
116 if (g_IsOriginalEngine)
121 if (!m_CanQueryConVars)
125 SH_ADD_HOOK_MEMFUNC(IServerPluginCallbacks, OnQueryCvarValueFinished, iface, this, &ConVarManager::OnQueryCvarValueFinished, false);
126 m_CanQueryConVars = true;
130 void ConVarManager::OnPluginUnloaded(IPlugin *plugin)
132 ConVarList *pConVarList;
134 /* If plugin has a convar list, free its memory */
135 if (plugin->GetProperty("ConVarList", (void **)&pConVarList, true))
141 void ConVarManager::OnHandleDestroy(HandleType_t type, void *object)
144 ConVar *pConVar = static_cast<ConVar *>(object);
146 /* Find convar in lookup trie */
147 sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), reinterpret_cast<void **>(&info));
149 /* If convar was created by SourceMod plugin... */
152 /* Delete string allocations */
153 delete [] pConVar->GetName();
154 delete [] pConVar->GetDefault();
155 delete [] pConVar->GetHelpText();
157 /* Then unlink it from SourceMM */
158 g_SMAPI->UnregisterConCmdBase(g_PLAPI, pConVar);
162 void ConVarManager::OnRootConsoleCommand(const char *command, unsigned int argcount)
166 /* Get plugin index that was passed */
167 int id = atoi(g_RootMenu.GetArgument(2));
169 /* Get plugin object */
170 CPlugin *plugin = g_PluginSys.GetPluginByOrder(id);
174 g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
178 /* Get plugin name */
179 const sm_plugininfo_t *plinfo = plugin->GetPublicInfo();
180 const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : plugin->GetFilename();
182 ConVarList *pConVarList;
183 ConVarList::iterator iter;
185 /* If no convar list... */
186 if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
188 g_RootMenu.ConsolePrint("[SM] No convars found for: %s", plname);
192 g_RootMenu.ConsolePrint("[SM] Listing %d convars for: %s", pConVarList->size(), plname);
193 g_RootMenu.ConsolePrint(" %-32.31s %s", "[Name]", "[Value]");
195 /* Iterate convar list and display each one */
196 for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
198 const ConVar *pConVar = (*iter);
199 g_RootMenu.ConsolePrint(" %-32.31s %s", pConVar->GetName(), pConVar->GetString());
205 /* Display usage of subcommand */
206 g_RootMenu.ConsolePrint("[SM] Usage: sm convars <plugin #>");
209 Handle_t ConVarManager::CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal, const char *description, int flags, bool hasMin, float min, bool hasMax, float max)
211 ConVar *pConVar = NULL;
212 ConVarInfo *pInfo = NULL;
215 /* Find out if the convar exists already */
216 pConVar = icvar->FindVar(name);
218 /* If the convar already exists... */
221 /* Add convar to plugin's list */
222 AddConVarToPluginList(pContext, pConVar);
224 /* First find out if we already have a handle to it */
225 if (sm_trie_retrieve(m_ConVarCache, name, (void **)&pInfo))
227 return pInfo->handle;
229 /* If we don't, then create a new handle from the convar */
230 hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
232 /* Create and initialize ConVarInfo structure */
233 pInfo = new ConVarInfo();
234 pInfo->handle = hndl;
235 pInfo->sourceMod = false;
236 pInfo->pChangeForward = NULL;
237 pInfo->origCallback = pConVar->GetCallback();
239 /* Insert struct into caches */
240 m_ConVars.push_back(pInfo);
241 sm_trie_insert(m_ConVarCache, name, pInfo);
247 /* To prevent creating a convar that has the same name as a console command... ugh */
248 ConCommandBase *pBase = icvar->GetCommands();
252 if (pBase->IsCommand() && strcmp(pBase->GetName(), name) == 0)
257 pBase = const_cast<ConCommandBase *>(pBase->GetNext());
260 /* Since an existing convar (or concmd with the same name) was not found , now we can finally create it */
261 pConVar = new ConVar(sm_strdup(name), sm_strdup(defaultVal), flags, sm_strdup(description), hasMin, min, hasMax, max);
263 /* Add convar to plugin's list */
264 AddConVarToPluginList(pContext, pConVar);
266 /* Create a handle from the new convar */
267 hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
269 /* Create and initialize ConVarInfo structure */
270 pInfo = new ConVarInfo();
271 pInfo->handle = hndl;
272 pInfo->sourceMod = true;
273 pInfo->pChangeForward = NULL;
274 pInfo->origCallback = NULL;
276 /* Insert struct into caches */
277 m_ConVars.push_back(pInfo);
278 sm_trie_insert(m_ConVarCache, name, pInfo);
283 Handle_t ConVarManager::FindConVar(const char *name)
285 ConVar *pConVar = NULL;
289 /* Search for convar */
290 pConVar = icvar->FindVar(name);
292 /* If it doesn't exist, then return an invalid handle */
298 /* At this point, the convar exists. So, find out if we already have a handle */
299 if (sm_trie_retrieve(m_ConVarCache, name, (void **)&pInfo))
301 return pInfo->handle;
304 /* If we don't have a handle, then create a new one */
305 hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
307 /* Create and initialize ConVarInfo structure */
308 pInfo = new ConVarInfo();
309 pInfo->handle = hndl;
310 pInfo->sourceMod = false;
311 pInfo->pChangeForward = NULL;
312 pInfo->origCallback = pConVar->GetCallback();
314 /* Insert struct into our caches */
315 m_ConVars.push_back(pInfo);
316 sm_trie_insert(m_ConVarCache, name, pInfo);
321 void ConVarManager::HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
324 IChangeableForward *pForward;
326 /* Find the convar in the lookup trie */
327 if (sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), (void **)&pInfo))
329 /* Get the forward */
330 pForward = pInfo->pChangeForward;
332 /* If forward does not exist, create it */
335 pForward = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, CONVARCHANGE_PARAMS);
336 pInfo->pChangeForward = pForward;
338 /* Install our own callback */
339 pConVar->InstallChangeCallback(OnConVarChanged);
342 /* Add function to forward's list */
343 pForward->AddFunction(pFunction);
347 void ConVarManager::UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
350 IChangeableForward *pForward;
351 IPluginContext *pContext = pFunction->GetParentContext();
353 /* Find the convar in the lookup trie */
354 if (sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), (void **)&pInfo))
356 /* Get the forward */
357 pForward = pInfo->pChangeForward;
359 /* If the forward doesn't exist, we can't unhook anything */
362 pContext->ThrowNativeError("Convar \"%s\" has no active hook", pConVar->GetName());
366 /* Remove the function from the forward's list */
367 if (!pForward->RemoveFunction(pFunction))
369 pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", pConVar->GetName());
373 /* If the forward now has 0 functions in it... */
374 if (pForward->GetFunctionCount() == 0)
376 /* Free this forward */
377 g_Forwards.ReleaseForward(pForward);
378 pInfo->pChangeForward = NULL;
380 /* Put back the original convar callback */
381 pConVar->InstallChangeCallback(pInfo->origCallback);
386 QueryCvarCookie_t ConVarManager::QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback, Handle_t hndl)
388 QueryCvarCookie_t cookie;
390 /* Call StartQueryCvarValue() in either the IVEngineServer or IServerPluginHelpers depending on situation */
393 cookie = engine->StartQueryCvarValue(pPlayer, name);
395 cookie = serverpluginhelpers->StartQueryCvarValue(pPlayer, name);
398 ConVarQuery query = {cookie, pCallback, hndl};
399 m_ConVarQueries.push_back(query);
404 void ConVarManager::AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar)
406 ConVarList *pConVarList;
407 ConVarList::iterator iter;
408 bool inserted = false;
409 const char *orig = pConVar->GetName();
411 IPlugin *plugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
413 /* Check plugin for an existing convar list */
414 if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
416 pConVarList = new ConVarList();
417 plugin->SetProperty("ConVarList", pConVarList);
418 } else if (pConVarList->find(pConVar) != pConVarList->end()) {
419 /* If convar is already in list, then don't add it */
423 /* Insert convar into list which is sorted alphabetically */
424 for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
426 if (strcmp(orig, (*iter)->GetName()) < 0)
428 pConVarList->insert(iter, pConVar);
436 pConVarList->push_back(pConVar);
440 void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue)
442 /* If the values are the same, exit early in order to not trigger callbacks */
443 if (strcmp(pConVar->GetString(), oldValue) == 0)
448 Trie *pCache = g_ConVarManager.GetConVarCache();
451 /* Find the convar in the lookup trie */
452 sm_trie_retrieve(pCache, pConVar->GetName(), (void **)&pInfo);
454 FnChangeCallback origCallback = pInfo->origCallback;
455 IChangeableForward *pForward = pInfo->pChangeForward;
457 /* If there was a change callback installed previously, call it */
460 origCallback(pConVar, oldValue);
463 /* Now call forwards in plugins that have hooked this */
464 pForward->PushCell(pInfo->handle);
465 pForward->PushString(oldValue);
466 pForward->PushString(pConVar->GetString());
467 pForward->Execute(NULL);
470 void ConVarManager::OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result, const char *cvarName, const char *cvarValue)
472 IPluginFunction *pCallback = NULL;
474 List<ConVarQuery>::iterator iter;
476 for (iter = m_ConVarQueries.begin(); iter != m_ConVarQueries.end(); iter++)
478 ConVarQuery &query = (*iter);
480 if (query.cookie == cookie)
482 pCallback = query.pCallback;
493 pCallback->PushCell(cookie);
494 pCallback->PushCell(engine->IndexOfEdict(pPlayer));
495 pCallback->PushCell(result);
496 pCallback->PushString(cvarName);
498 if (result == eQueryCvarValueStatus_ValueIntact)
500 pCallback->PushString(cvarValue);
502 pCallback->PushString("\0");
505 pCallback->PushCell(value);
506 pCallback->Execute(&ret);
508 m_ConVarQueries.erase(iter);
513 static int s_YamagramState = 0;
515 void _YamagramPrinterTwoPointOhOh(int yamagram)
520 g_RootMenu.ConsolePrint("Answer the following questions correctly and Gaben may not eat you after all.");
521 g_RootMenu.ConsolePrint("You will be given one hint in the form of my patented yamagrams.");
522 g_RootMenu.ConsolePrint("Type sm_nana to see the last question.");
523 g_RootMenu.ConsolePrint("Type sm_nana <answer> to attempt an answer of the question.");
524 g_RootMenu.ConsolePrint("-------------------------------");
525 _YamagramPrinterTwoPointOhOh(1);
528 g_RootMenu.ConsolePrint("Question Ichi (1)");
529 g_RootMenu.ConsolePrint("One can turn into a cow by doing what action?");
530 g_RootMenu.ConsolePrint("Hint: AGE SANS GRIT");
533 g_RootMenu.ConsolePrint("Question Ni (2)");
534 g_RootMenu.ConsolePrint("What kind of hat should you wear when using the Internet?");
535 g_RootMenu.ConsolePrint("Hint: BRR MOOSE");
538 g_RootMenu.ConsolePrint("Question San (3)");
539 g_RootMenu.ConsolePrint("Who is the lead developer of SourceMod?");
540 g_RootMenu.ConsolePrint("Hint: VEAL BANDANA DID RIP SOON");
543 g_RootMenu.ConsolePrint("Question Yon (4)");
544 g_RootMenu.ConsolePrint("A terrible translation of 'SVN Revision' to Japanese romaji might be ...");
545 g_RootMenu.ConsolePrint("Hint: I TAKE IN AN AIR OK");
548 g_RootMenu.ConsolePrint("Question Go (5)");
549 g_RootMenu.ConsolePrint("What is a fundamental concept in the game of Go?");
550 g_RootMenu.ConsolePrint("Hint: AD LADEN THIEF");
553 g_RootMenu.ConsolePrint("Question Roku (6)");
554 g_RootMenu.ConsolePrint("Why am I asking all these strange questions?");
555 g_RootMenu.ConsolePrint("Hint: CHUBBY TITAN EATS EWE WAGE DATA");
558 g_RootMenu.ConsolePrint("Question Nana (7)");
559 g_RootMenu.ConsolePrint("What is my name?");
560 g_RootMenu.ConsolePrint("Hint: AD MODE LAG US");
566 s_YamagramState = yamagram;
569 void _IntExt_CallYamagrams()
571 bool correct = false;
572 const char *arg = engine->Cmd_Args();
574 if (!arg || arg[0] == '\0')
576 _YamagramPrinterTwoPointOhOh(s_YamagramState);
580 switch (s_YamagramState)
583 correct = !strcasecmp(arg, "eating grass");
586 correct = !strcasecmp(arg, "sombrero");
589 correct = !strcasecmp(arg, "david bailopan anderson");
592 correct = !strcasecmp(arg, "kaitei no kairan");
595 correct = !strcasecmp(arg, "life and death");
598 correct = !strcasecmp(arg, "because gabe wanted it that way");
601 g_RootMenu.ConsolePrint("Congratulations, you have answered 6 of my questions.");
602 g_RootMenu.ConsolePrint("However, I have one final question for you. It wouldn't be nana without it.");
603 g_RootMenu.ConsolePrint("-------------------------------");
604 _YamagramPrinterTwoPointOhOh(7);
609 correct = !strcasecmp(arg, "damaged soul");
612 g_RootMenu.ConsolePrint("You don't know how lucky you are to still be alive!");
613 g_RootMenu.ConsolePrint("Congratulations. You have answered all 7 questions correctly.");
614 g_RootMenu.ConsolePrint("The SourceMod Dev Team will be at your door with anti-Gaben grenades");
615 g_RootMenu.ConsolePrint("within seconds. You will also be provided with a rocket launcher,");
616 g_RootMenu.ConsolePrint("just in case Alfred decides to strike you with a blitzkrieg in retaliation.");
626 if (s_YamagramState > 0)
630 g_RootMenu.ConsolePrint("Correct! You are one step closer to avoiding the deadly jaws of Gaben.");
631 g_RootMenu.ConsolePrint("-------------------------------");
634 g_RootMenu.ConsolePrint("Wrong! You better be more careful. Gaben may be at your door at any minute.");
639 _YamagramPrinterTwoPointOhOh(s_YamagramState);
642 void _IntExt_EnableYamagrams()
644 static ConCommand *pCmd = NULL;
647 pCmd = new ConCommand("sm_nana", _IntExt_CallYamagrams, "Try these yamagrams!", FCVAR_GAMEDLL);
648 g_RootMenu.ConsolePrint("[SM] Warning: Gaben has been alerted of your actions. You may be eaten.");
650 g_RootMenu.ConsolePrint("[SM] Gaben has already been alerted of your actions...");
654 void _IntExt_OnHostnameChanged(ConVar *pConVar, char const *oldValue)
656 if (strcmp(oldValue, "Good morning, DS-san.") == 0
657 && strcmp(pConVar->GetString(), "Good night, talking desk lamp.") == 0)
659 _IntExt_EnableYamagrams();