ds@580: /**
ds@653: * vim: set ts=4 :
ds@1330: * =============================================================================
dvander@1231: * SourceMod
dvander@1231: * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
ds@1330: * =============================================================================
ds@653: *
ds@1330: * This program is free software; you can redistribute it and/or modify it under
ds@1330: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330: * Free Software Foundation.
dvander@1231: *
ds@1330: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1330: * details.
dvander@1231: *
ds@1330: * You should have received a copy of the GNU General Public License along with
ds@1330: * this program. If not, see .
dvander@1231: *
ds@1330: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1330: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1330: * this exception to all derivative works. AlliedModders LLC defines further
ds@1330: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330: * or .
ds@653: *
ds@653: * Version: $Id$
ds@653: */
ds@580:
ds@580: #include "ConVarManager.h"
ds@696: #include "HalfLife2.h"
ds@580: #include "PluginSys.h"
ds@580: #include "ForwardSys.h"
ds@580: #include "HandleSys.h"
ds@580: #include "sm_srvcmds.h"
ds@580: #include "sm_stringutil.h"
ds@580: #include
dvander@1582: #include
ds@580:
ds@580: ConVarManager g_ConVarManager;
ds@580:
ds@696: SH_DECL_HOOK5_void(IServerGameDLL, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
ds@696: SH_DECL_HOOK5_void(IServerPluginCallbacks, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
ds@696:
ds@582: const ParamType CONVARCHANGE_PARAMS[] = {Param_Cell, Param_String, Param_String};
ds@582: typedef List ConVarList;
dvander@1582: KTrie convar_cache;
ds@582:
dvander@1581: ConVarManager::ConVarManager() : m_ConVarType(0), m_bIsDLLQueryHooked(false), m_bIsVSPQueryHooked(false)
ds@580: {
ds@580: }
ds@580:
ds@580: ConVarManager::~ConVarManager()
ds@580: {
ds@580: }
ds@580:
ds@580: void ConVarManager::OnSourceModAllInitialized()
ds@580: {
dvander@1581: /**
dvander@1581: * Episode 2 has this function by default, but the older versions do not.
dvander@1581: */
dvander@1581: #if !defined ORANGEBOX_BUILD
ds@696: if (g_SMAPI->GetGameDLLVersion() >= 6)
dvander@1581: #endif
ds@696: {
ds@696: SH_ADD_HOOK_MEMFUNC(IServerGameDLL, OnQueryCvarValueFinished, gamedll, this, &ConVarManager::OnQueryCvarValueFinished, false);
dvander@1581: m_bIsDLLQueryHooked = true;
ds@696: }
ds@696:
ds@580: HandleAccess sec;
ds@580:
ds@582: /* Set up access rights for the 'ConVar' handle type */
ds@580: sec.access[HandleAccess_Read] = 0;
ds@580: sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
ds@580: sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
ds@580:
ds@582: /* Create the 'ConVar' handle type */
ds@580: m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL);
ds@580:
ds@582: /* Add the 'convars' option to the 'sm' console command */
ds@990: g_RootMenu.AddRootConsoleCommand("cvars", "View convars created by a plugin", this);
ds@580: }
ds@580:
ds@580: void ConVarManager::OnSourceModShutdown()
ds@580: {
dvander@1582: List::iterator iter = m_ConVars.begin();
dvander@1582: HandleSecurity sec(NULL, g_pCoreIdent);
dvander@1582:
dvander@1582: /* Iterate list of ConVarInfo structures, remove every one of them */
dvander@1582: while (iter != m_ConVars.end())
dvander@1582: {
dvander@1582: ConVarInfo *pInfo = (*iter);
dvander@1582:
dvander@1582: iter = m_ConVars.erase(iter);
dvander@1582:
dvander@1582: g_HandleSys.FreeHandle(pInfo->handle, &sec);
dvander@1582: if (pInfo->pChangeForward != NULL)
dvander@1582: {
dvander@1582: g_Forwards.ReleaseForward(pInfo->pChangeForward);
dvander@1582: }
dvander@1582: if (pInfo->sourceMod)
dvander@1582: {
dvander@1582: /* If we created it, we won't be tracking it, therefore it is
dvander@1582: * safe to remove everything in one go.
dvander@1582: */
dvander@1582: META_UNREGCVAR(pInfo->pVar);
dvander@1582: delete [] pInfo->pVar->GetName();
dvander@1582: delete [] pInfo->pVar->GetHelpText();
dvander@1582: delete [] pInfo->pVar->GetDefault();
dvander@1582: delete pInfo->pVar;
dvander@1582: }
dvander@1582: else
dvander@1582: {
dvander@1582: /* If we didn't create it, we might be tracking it. Also,
dvander@1582: * it could be unreadable.
dvander@1582: */
dvander@1582: UntrackConCommandBase(pInfo->pVar, this);
dvander@1582: }
dvander@1582:
dvander@1582: /* It's not safe to read the name here, so we simply delete the
dvander@1582: * the info struct and clear the lookup cache at the end.
dvander@1582: */
dvander@1582: delete pInfo;
dvander@1582: }
dvander@1582: convar_cache.clear();
dvander@1582:
dvander@1582: /* Unhook things */
dvander@1581: if (m_bIsDLLQueryHooked)
ds@696: {
ds@696: SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, OnQueryCvarValueFinished, gamedll, this, &ConVarManager::OnQueryCvarValueFinished, false);
dvander@1581: m_bIsDLLQueryHooked = false;
dvander@1581: }
dvander@1581: else if (m_bIsVSPQueryHooked)
dvander@1581: {
dvander@1581: SH_REMOVE_HOOK_MEMFUNC(IServerPluginCallbacks, OnQueryCvarValueFinished, vsp_interface, this, &ConVarManager::OnQueryCvarValueFinished, false);
dvander@1581: m_bIsVSPQueryHooked = false;
ds@696: }
ds@696:
ds@582: /* Remove the 'convars' option from the 'sm' console command */
ds@990: g_RootMenu.RemoveRootConsoleCommand("cvars", this);
ds@580:
ds@582: /* Remove the 'ConVar' handle type */
ds@580: g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent);
ds@580: }
ds@580:
dvander@1581: /**
dvander@1581: * Orange Box will never use this.
dvander@1581: */
dvander@1581: void ConVarManager::OnSourceModVSPReceived()
ds@696: {
dvander@1581: /**
dvander@1581: * Don't bother if the DLL is already hooked.
ds@696: */
dvander@1581: if (m_bIsDLLQueryHooked)
ds@696: {
ds@696: return;
ds@696: }
ds@696:
dvander@1581: /* For later MM:S versions, use the updated API, since it's cleaner. */
dvander@1581: #if defined METAMOD_PLAPI_VERSION
dvander@1581: int engine = g_SMAPI->GetSourceEngineBuild();
dvander@1581: if (engine == SOURCE_ENGINE_ORIGINAL || vsp_version < 2)
ds@696: {
dvander@1581: return;
dvander@1581: }
dvander@1581: #else
dvander@1581: if (g_HL2.IsOriginalEngine() || vsp_version < 2)
dvander@1581: {
dvander@1581: return;
dvander@1581: }
dvander@1581: #endif
ds@696:
dvander@1581: SH_ADD_HOOK_MEMFUNC(IServerPluginCallbacks, OnQueryCvarValueFinished, vsp_interface, this, &ConVarManager::OnQueryCvarValueFinished, false);
dvander@1581: m_bIsVSPQueryHooked = true;
ds@696: }
ds@696:
dvander@1582: bool convar_cache_lookup(const char *name, ConVarInfo **pVar)
ds@1145: {
dvander@1582: ConVarInfo **pLookup = convar_cache.retrieve(name);
dvander@1582: if (pLookup != NULL)
ds@1145: {
dvander@1582: *pVar = *pLookup;
dvander@1582: return true;
dvander@1582: }
dvander@1582: else
dvander@1582: {
dvander@1582: return false;
ds@1145: }
ds@1145: }
ds@1145:
dvander@1582: void ConVarManager::OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name, bool is_read_safe)
dvander@1582: {
dvander@1582: /* Only check convars that have not been created by SourceMod's core */
dvander@1582: ConVarInfo *pInfo;
dvander@1582: if (!convar_cache_lookup(name, &pInfo))
dvander@1582: {
dvander@1582: return;
dvander@1582: }
ds@1145:
ds@1145: HandleSecurity sec(NULL, g_pCoreIdent);
ds@1145:
dvander@1582: /* Remove it from our cache */
dvander@1582: m_ConVars.remove(pInfo);
dvander@1582: convar_cache.remove(name);
dvander@1582:
dvander@1582: /* Now make sure no plugins are referring to this pointer */
dvander@1582: IPluginIterator *pl_iter = g_PluginSys.GetPluginIterator();
dvander@1582: while (pl_iter->MorePlugins())
ds@1145: {
dvander@1582: IPlugin *pl = pl_iter->GetPlugin();
ds@1145:
dvander@1582: ConVarList *pConVarList;
dvander@1582: if (pl->GetProperty("ConVarList", (void **)&pConVarList, true)
dvander@1582: && pConVarList != NULL)
ds@1145: {
dvander@1582: pConVarList->remove(pInfo->pVar);
dvander@1582: }
ds@1145:
dvander@1582: pl_iter->NextPlugin();
ds@1145: }
dvander@1582:
dvander@1582: /* Free resources */
dvander@1582: g_HandleSys.FreeHandle(pInfo->handle, &sec);
dvander@1582: delete pInfo;
ds@1145: }
ds@1145:
ds@582: void ConVarManager::OnPluginUnloaded(IPlugin *plugin)
ds@580: {
ds@582: ConVarList *pConVarList;
ds@580:
ds@582: /* If plugin has a convar list, free its memory */
ds@582: if (plugin->GetProperty("ConVarList", (void **)&pConVarList, true))
ds@580: {
ds@582: delete pConVarList;
ds@580: }
ds@580: }
ds@580:
ds@580: void ConVarManager::OnHandleDestroy(HandleType_t type, void *object)
ds@580: {
ds@580: }
ds@580:
faluco@1541: void ConVarManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
ds@580: {
faluco@1541: int argcount = command.ArgC();
ds@580: if (argcount >= 3)
ds@580: {
ds@582: /* Get plugin index that was passed */
faluco@1541: int id = atoi(command.Arg(2));
ds@580:
ds@582: /* Get plugin object */
ds@582: CPlugin *plugin = g_PluginSys.GetPluginByOrder(id);
ds@582:
ds@582: if (!plugin)
ds@580: {
ds@582: g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
ds@580: return;
ds@580: }
ds@580:
ds@582: /* Get plugin name */
ds@582: const sm_plugininfo_t *plinfo = plugin->GetPublicInfo();
ds@582: const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : plugin->GetFilename();
ds@580:
ds@582: ConVarList *pConVarList;
ds@582: ConVarList::iterator iter;
ds@580:
ds@582: /* If no convar list... */
ds@582: if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
ds@580: {
ds@582: g_RootMenu.ConsolePrint("[SM] No convars found for: %s", plname);
ds@580: return;
ds@580: }
ds@580:
ds@582: g_RootMenu.ConsolePrint("[SM] Listing %d convars for: %s", pConVarList->size(), plname);
ds@582: g_RootMenu.ConsolePrint(" %-32.31s %s", "[Name]", "[Value]");
ds@580:
ds@582: /* Iterate convar list and display each one */
ds@582: for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
ds@580: {
ds@582: const ConVar *pConVar = (*iter);
ds@582: g_RootMenu.ConsolePrint(" %-32.31s %s", pConVar->GetName(), pConVar->GetString());
ds@580: }
ds@580:
ds@580: return;
ds@580: }
ds@580:
ds@582: /* Display usage of subcommand */
ds@580: g_RootMenu.ConsolePrint("[SM] Usage: sm convars ");
ds@580: }
ds@580:
ds@582: 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)
ds@580: {
ds@580: ConVar *pConVar = NULL;
ds@582: ConVarInfo *pInfo = NULL;
ds@580: Handle_t hndl = 0;
ds@580:
ds@582: /* Find out if the convar exists already */
ds@580: pConVar = icvar->FindVar(name);
ds@580:
ds@582: /* If the convar already exists... */
ds@582: if (pConVar)
ds@580: {
ds@759: /* Add convar to plugin's list */
ds@759: AddConVarToPluginList(pContext, pConVar);
ds@759:
ds@582: /* First find out if we already have a handle to it */
dvander@1582: if (convar_cache_lookup(name, &pInfo))
ds@580: {
ds@582: return pInfo->handle;
dvander@1581: }
dvander@1581: else
dvander@1581: {
ds@582: /* Create and initialize ConVarInfo structure */
ds@582: pInfo = new ConVarInfo();
ds@582: pInfo->sourceMod = false;
ds@696: pInfo->pChangeForward = NULL;
ds@582: pInfo->origCallback = pConVar->GetCallback();
dvander@1582: pInfo->pVar = pConVar;
dvander@1582:
dvander@1582: /* If we don't, then create a new handle from the convar */
dvander@1582: hndl = g_HandleSys.CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
dvander@1582: if (hndl == BAD_HANDLE)
dvander@1582: {
dvander@1582: delete pInfo;
dvander@1582: return BAD_HANDLE;
dvander@1582: }
dvander@1582:
dvander@1582: pInfo->handle = hndl;
ds@580:
ds@582: /* Insert struct into caches */
ds@582: m_ConVars.push_back(pInfo);
dvander@1582: convar_cache.insert(name, pInfo);
dvander@1582: TrackConCommandBase(pConVar, this);
ds@580:
ds@580: return hndl;
ds@580: }
ds@580: }
ds@580:
ds@696: /* To prevent creating a convar that has the same name as a console command... ugh */
ds@580: ConCommandBase *pBase = icvar->GetCommands();
ds@580:
ds@580: while (pBase)
ds@580: {
ds@582: if (pBase->IsCommand() && strcmp(pBase->GetName(), name) == 0)
ds@580: {
ds@582: return BAD_HANDLE;
ds@580: }
ds@580:
ds@580: pBase = const_cast(pBase->GetNext());
ds@580: }
ds@580:
ds@582: /* Create and initialize ConVarInfo structure */
ds@582: pInfo = new ConVarInfo();
ds@582: pInfo->handle = hndl;
ds@582: pInfo->sourceMod = true;
ds@696: pInfo->pChangeForward = NULL;
ds@582: pInfo->origCallback = NULL;
dvander@1582:
dvander@1582: /* Create a handle from the new convar */
dvander@1582: hndl = g_HandleSys.CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
dvander@1582: if (hndl == BAD_HANDLE)
dvander@1582: {
dvander@1582: delete pInfo;
dvander@1582: return BAD_HANDLE;
dvander@1582: }
dvander@1582:
dvander@1582: pInfo->handle = hndl;
dvander@1582:
dvander@1582: /* Since an existing convar (or concmd with the same name) was not found , now we can finally create it */
dvander@1582: pConVar = new ConVar(sm_strdup(name), sm_strdup(defaultVal), flags, sm_strdup(description), hasMin, min, hasMax, max);
dvander@1582: pInfo->pVar = pConVar;
dvander@1582:
dvander@1582: /* Add convar to plugin's list */
dvander@1582: AddConVarToPluginList(pContext, pConVar);
ds@580:
ds@582: /* Insert struct into caches */
ds@582: m_ConVars.push_back(pInfo);
dvander@1582: convar_cache.insert(name, pInfo);
ds@580:
ds@580: return hndl;
ds@580: }
ds@580:
ds@580: Handle_t ConVarManager::FindConVar(const char *name)
ds@580: {
ds@580: ConVar *pConVar = NULL;
ds@582: ConVarInfo *pInfo;
ds@582: Handle_t hndl;
ds@580:
ds@582: /* Search for convar */
ds@580: pConVar = icvar->FindVar(name);
ds@580:
ds@582: /* If it doesn't exist, then return an invalid handle */
ds@582: if (!pConVar)
ds@580: {
ds@580: return BAD_HANDLE;
ds@580: }
ds@580:
ds@582: /* At this point, the convar exists. So, find out if we already have a handle */
dvander@1582: if (convar_cache_lookup(name, &pInfo))
ds@580: {
ds@582: return pInfo->handle;
ds@580: }
ds@580:
ds@729: /* Create and initialize ConVarInfo structure */
ds@582: pInfo = new ConVarInfo();
ds@582: pInfo->sourceMod = false;
ds@696: pInfo->pChangeForward = NULL;
ds@582: pInfo->origCallback = pConVar->GetCallback();
dvander@1582: pInfo->pVar = pConVar;
dvander@1582:
dvander@1582: /* If we don't have a handle, then create a new one */
dvander@1582: hndl = g_HandleSys.CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
dvander@1582: if (hndl == BAD_HANDLE)
dvander@1582: {
dvander@1582: delete pInfo;
dvander@1582: return BAD_HANDLE;
dvander@1582: }
dvander@1582:
dvander@1582: pInfo->handle = hndl;
ds@580:
ds@582: /* Insert struct into our caches */
ds@582: m_ConVars.push_back(pInfo);
dvander@1582: convar_cache.insert(name, pInfo);
dvander@1582: TrackConCommandBase(pConVar, this);
ds@580:
ds@580: return hndl;
ds@580: }
ds@580:
ds@582: void ConVarManager::HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
ds@580: {
ds@582: ConVarInfo *pInfo;
ds@582: IChangeableForward *pForward;
ds@580:
ds@582: /* Find the convar in the lookup trie */
dvander@1582: if (convar_cache_lookup(pConVar->GetName(), &pInfo))
ds@580: {
ds@582: /* Get the forward */
ds@696: pForward = pInfo->pChangeForward;
ds@580:
ds@582: /* If forward does not exist, create it */
ds@582: if (!pForward)
ds@582: {
ds@582: pForward = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, CONVARCHANGE_PARAMS);
ds@696: pInfo->pChangeForward = pForward;
ds@580:
ds@582: /* Install our own callback */
ds@580: pConVar->InstallChangeCallback(OnConVarChanged);
ds@580: }
ds@582:
ds@582: /* Add function to forward's list */
ds@582: pForward->AddFunction(pFunction);
ds@580: }
ds@580: }
ds@580:
ds@582: void ConVarManager::UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
ds@580: {
ds@582: ConVarInfo *pInfo;
ds@582: IChangeableForward *pForward;
ds@582: IPluginContext *pContext = pFunction->GetParentContext();
ds@580:
ds@582: /* Find the convar in the lookup trie */
dvander@1582: if (convar_cache_lookup(pConVar->GetName(), &pInfo))
ds@580: {
ds@582: /* Get the forward */
ds@696: pForward = pInfo->pChangeForward;
ds@580:
ds@582: /* If the forward doesn't exist, we can't unhook anything */
ds@582: if (!pForward)
ds@580: {
ds@580: pContext->ThrowNativeError("Convar \"%s\" has no active hook", pConVar->GetName());
ds@580: return;
ds@580: }
ds@580:
ds@582: /* Remove the function from the forward's list */
ds@582: if (!pForward->RemoveFunction(pFunction))
ds@580: {
ds@580: pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", pConVar->GetName());
ds@580: return;
ds@580: }
ds@580:
ds@582: /* If the forward now has 0 functions in it... */
ds@582: if (pForward->GetFunctionCount() == 0)
ds@580: {
ds@582: /* Free this forward */
ds@582: g_Forwards.ReleaseForward(pForward);
ds@696: pInfo->pChangeForward = NULL;
ds@582:
ds@582: /* Put back the original convar callback */
ds@582: pConVar->InstallChangeCallback(pInfo->origCallback);
ds@582: }
ds@582: }
ds@582: }
ds@582:
ds@696: QueryCvarCookie_t ConVarManager::QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback, Handle_t hndl)
ds@696: {
ds@696: QueryCvarCookie_t cookie;
ds@696:
ds@696: /* Call StartQueryCvarValue() in either the IVEngineServer or IServerPluginHelpers depending on situation */
dvander@1581: if (m_bIsDLLQueryHooked)
ds@696: {
ds@696: cookie = engine->StartQueryCvarValue(pPlayer, name);
dvander@1581: }
dvander@1581: else if (m_bIsVSPQueryHooked)
dvander@1581: {
ds@696: cookie = serverpluginhelpers->StartQueryCvarValue(pPlayer, name);
ds@696: }
dvander@1581: else
dvander@1581: {
dvander@1581: return InvalidQueryCvarCookie;
dvander@1581: }
ds@696:
ds@696: ConVarQuery query = {cookie, pCallback, hndl};
ds@696: m_ConVarQueries.push_back(query);
ds@696:
ds@696: return cookie;
ds@696: }
ds@696:
ds@582: void ConVarManager::AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar)
ds@582: {
ds@582: ConVarList *pConVarList;
ds@582: ConVarList::iterator iter;
ds@582: bool inserted = false;
ds@582: const char *orig = pConVar->GetName();
ds@582:
ds@582: IPlugin *plugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@580:
ds@582: /* Check plugin for an existing convar list */
ds@582: if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
ds@582: {
ds@582: pConVarList = new ConVarList();
ds@582: plugin->SetProperty("ConVarList", pConVarList);
dvander@1581: }
dvander@1581: else if (pConVarList->find(pConVar) != pConVarList->end())
dvander@1581: {
ds@867: /* If convar is already in list, then don't add it */
ds@867: return;
ds@582: }
ds@582:
ds@582: /* Insert convar into list which is sorted alphabetically */
ds@582: for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
ds@582: {
ds@582: if (strcmp(orig, (*iter)->GetName()) < 0)
ds@582: {
ds@582: pConVarList->insert(iter, pConVar);
ds@582: inserted = true;
ds@582: break;
ds@580: }
ds@580: }
ds@582:
ds@582: if (!inserted)
ds@582: {
ds@582: pConVarList->push_back(pConVar);
ds@582: }
ds@580: }
ds@580:
faluco@1541: #if defined ORANGEBOX_BUILD
dvander@1544: void ConVarManager::OnConVarChanged(IConVar *pIConVar, const char *oldValue, float flOldValue)
faluco@1541: #else
ds@580: void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue)
faluco@1541: #endif
ds@580: {
ds@582: /* If the values are the same, exit early in order to not trigger callbacks */
dvander@1544: #if defined ORANGEBOX_BUILD
dvander@1544: ConVar *pConVar = (ConVar *)pIConVar;
dvander@1544: #endif
dvander@1544: if (strcmp(pConVar->GetString(), oldValue) == 0)
ds@580: {
ds@580: return;
ds@580: }
ds@580:
ds@582: ConVarInfo *pInfo;
ds@580:
ds@582: /* Find the convar in the lookup trie */
dvander@1582: if (!convar_cache_lookup(pConVar->GetName(), &pInfo))
dvander@1582: {
dvander@1582: return;
dvander@1582: }
ds@580:
faluco@1541: FnChangeCallback_t origCallback = pInfo->origCallback;
ds@696: IChangeableForward *pForward = pInfo->pChangeForward;
ds@580:
ds@582: /* If there was a change callback installed previously, call it */
ds@580: if (origCallback)
ds@580: {
faluco@1541: #if defined ORANGEBOX_BUILD
faluco@1541: origCallback(pConVar, oldValue, flOldValue);
faluco@1541: #else
ds@580: origCallback(pConVar, oldValue);
faluco@1541: #endif
ds@580: }
ds@580:
ds@582: /* Now call forwards in plugins that have hooked this */
ds@582: pForward->PushCell(pInfo->handle);
ds@582: pForward->PushString(oldValue);
faluco@1541: pForward->PushString(reinterpret_cast(pConVar)->GetString());
ds@582: pForward->Execute(NULL);
ds@580: }
ds@627:
dvander@1581: bool ConVarManager::IsQueryingSupported()
dvander@1581: {
dvander@1581: return (m_bIsDLLQueryHooked || m_bIsVSPQueryHooked);
dvander@1581: }
dvander@1581:
ds@696: void ConVarManager::OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result, const char *cvarName, const char *cvarValue)
ds@696: {
ds@696: IPluginFunction *pCallback = NULL;
ds@696: cell_t value = 0;
ds@696: List::iterator iter;
ds@696:
ds@696: for (iter = m_ConVarQueries.begin(); iter != m_ConVarQueries.end(); iter++)
ds@696: {
ds@696: ConVarQuery &query = (*iter);
ds@696: if (query.cookie == cookie)
ds@696: {
ds@696: pCallback = query.pCallback;
ds@696: value = query.value;
ds@696: break;
ds@696: }
ds@696: }
ds@696:
ds@696: if (pCallback)
ds@696: {
ds@696: cell_t ret;
ds@696:
ds@696: pCallback->PushCell(cookie);
ds@696: pCallback->PushCell(engine->IndexOfEdict(pPlayer));
ds@696: pCallback->PushCell(result);
ds@696: pCallback->PushString(cvarName);
ds@696:
ds@696: if (result == eQueryCvarValueStatus_ValueIntact)
ds@696: {
ds@696: pCallback->PushString(cvarValue);
dvander@1581: }
dvander@1581: else
dvander@1581: {
ds@696: pCallback->PushString("\0");
ds@696: }
ds@696:
ds@696: pCallback->PushCell(value);
ds@696: pCallback->Execute(&ret);
ds@696:
ds@696: m_ConVarQueries.erase(iter);
ds@696: }
ds@696: }
ds@696:
dvander@1582: HandleError ConVarManager::ReadConVarHandle(Handle_t hndl, ConVar **pVar)
dvander@1582: {
dvander@1582: ConVarInfo *pInfo;
dvander@1582: HandleError error;
dvander@1582:
dvander@1582: if ((error = g_HandleSys.ReadHandle(hndl, m_ConVarType, NULL, (void **)&pInfo)) != HandleError_None)
dvander@1582: {
dvander@1582: return error;
dvander@1582: }
dvander@1582:
dvander@1582: if (pVar)
dvander@1582: {
dvander@1582: *pVar = pInfo->pVar;
dvander@1582: }
dvander@1582:
dvander@1582: return error;
dvander@1582: }
ds@696:
ds@627: static int s_YamagramState = 0;
ds@627:
ds@627: void _YamagramPrinterTwoPointOhOh(int yamagram)
ds@627: {
ds@627: switch (yamagram)
ds@627: {
ds@627: case 0:
ds@627: g_RootMenu.ConsolePrint("Answer the following questions correctly and Gaben may not eat you after all.");
ds@627: g_RootMenu.ConsolePrint("You will be given one hint in the form of my patented yamagrams.");
ds@627: g_RootMenu.ConsolePrint("Type sm_nana to see the last question.");
ds@627: g_RootMenu.ConsolePrint("Type sm_nana to attempt an answer of the question.");
ds@627: g_RootMenu.ConsolePrint("-------------------------------");
ds@627: _YamagramPrinterTwoPointOhOh(1);
ds@627: return;
ds@627: case 1:
ds@627: g_RootMenu.ConsolePrint("Question Ichi (1)");
ds@627: g_RootMenu.ConsolePrint("One can turn into a cow by doing what action?");
ds@627: g_RootMenu.ConsolePrint("Hint: AGE SANS GRIT");
ds@627: break;
ds@627: case 2:
ds@627: g_RootMenu.ConsolePrint("Question Ni (2)");
ds@627: g_RootMenu.ConsolePrint("What kind of hat should you wear when using the Internet?");
ds@627: g_RootMenu.ConsolePrint("Hint: BRR MOOSE");
ds@627: break;
ds@627: case 3:
ds@627: g_RootMenu.ConsolePrint("Question San (3)");
ds@627: g_RootMenu.ConsolePrint("Who is the lead developer of SourceMod?");
ds@627: g_RootMenu.ConsolePrint("Hint: VEAL BANDANA DID RIP SOON");
ds@627: break;
ds@627: case 4:
ds@627: g_RootMenu.ConsolePrint("Question Yon (4)");
ds@627: g_RootMenu.ConsolePrint("A terrible translation of 'SVN Revision' to Japanese romaji might be ...");
ds@627: g_RootMenu.ConsolePrint("Hint: I TAKE IN AN AIR OK");
ds@627: break;
ds@627: case 5:
ds@627: g_RootMenu.ConsolePrint("Question Go (5)");
ds@627: g_RootMenu.ConsolePrint("What is a fundamental concept in the game of Go?");
ds@628: g_RootMenu.ConsolePrint("Hint: AD LADEN THIEF");
ds@627: break;
ds@627: case 6:
ds@627: g_RootMenu.ConsolePrint("Question Roku (6)");
ds@627: g_RootMenu.ConsolePrint("Why am I asking all these strange questions?");
ds@627: g_RootMenu.ConsolePrint("Hint: CHUBBY TITAN EATS EWE WAGE DATA");
ds@627: break;
ds@627: case 7:
ds@627: g_RootMenu.ConsolePrint("Question Nana (7)");
ds@627: g_RootMenu.ConsolePrint("What is my name?");
ds@627: g_RootMenu.ConsolePrint("Hint: AD MODE LAG US");
ds@627: break;
ds@627: default:
ds@627: break;
ds@627: }
ds@627:
ds@627: s_YamagramState = yamagram;
ds@627: }
ds@627:
faluco@1541: #if defined ORANGEBOX_BUILD
faluco@1541: void _IntExt_CallYamagrams(const CCommand &cmd)
faluco@1541: {
faluco@1541: #else
ds@627: void _IntExt_CallYamagrams()
ds@627: {
faluco@1541: CCommand cmd;
faluco@1541: #endif
ds@627: bool correct = false;
faluco@1541: const char *arg = cmd.ArgS();
ds@627:
ds@627: if (!arg || arg[0] == '\0')
ds@627: {
ds@627: _YamagramPrinterTwoPointOhOh(s_YamagramState);
ds@627: return;
ds@627: }
ds@627:
ds@627: switch (s_YamagramState)
ds@627: {
ds@627: case 1:
ds@627: correct = !strcasecmp(arg, "eating grass");
ds@627: break;
ds@627: case 2:
ds@627: correct = !strcasecmp(arg, "sombrero");
ds@627: break;
ds@627: case 3:
ds@627: correct = !strcasecmp(arg, "david bailopan anderson");
ds@627: break;
ds@627: case 4:
ds@627: correct = !strcasecmp(arg, "kaitei no kairan");
ds@627: break;
ds@627: case 5:
ds@628: correct = !strcasecmp(arg, "life and death");
ds@627: break;
ds@627: case 6:
ds@627: correct = !strcasecmp(arg, "because gabe wanted it that way");
ds@627: if (correct)
ds@627: {
ds@627: g_RootMenu.ConsolePrint("Congratulations, you have answered 6 of my questions.");
ds@627: g_RootMenu.ConsolePrint("However, I have one final question for you. It wouldn't be nana without it.");
ds@627: g_RootMenu.ConsolePrint("-------------------------------");
ds@627: _YamagramPrinterTwoPointOhOh(7);
ds@627: return;
ds@627: }
ds@627: break;
ds@627: case 7:
ds@627: correct = !strcasecmp(arg, "damaged soul");
ds@627: if (correct)
ds@627: {
ds@627: g_RootMenu.ConsolePrint("You don't know how lucky you are to still be alive!");
ds@627: g_RootMenu.ConsolePrint("Congratulations. You have answered all 7 questions correctly.");
ds@627: g_RootMenu.ConsolePrint("The SourceMod Dev Team will be at your door with anti-Gaben grenades");
ds@627: g_RootMenu.ConsolePrint("within seconds. You will also be provided with a rocket launcher,");
ds@627: g_RootMenu.ConsolePrint("just in case Alfred decides to strike you with a blitzkrieg in retaliation.");
ds@627:
ds@627: s_YamagramState = 0;
ds@627: return;
ds@627: }
ds@627: break;
ds@627: default:
ds@627: break;
ds@627: }
ds@627:
ds@627: if (s_YamagramState > 0)
ds@627: {
ds@627: if (correct)
ds@627: {
ds@627: g_RootMenu.ConsolePrint("Correct! You are one step closer to avoiding the deadly jaws of Gaben.");
ds@627: g_RootMenu.ConsolePrint("-------------------------------");
ds@627: s_YamagramState++;
ds@627: } else {
ds@627: g_RootMenu.ConsolePrint("Wrong! You better be more careful. Gaben may be at your door at any minute.");
ds@627: return;
ds@627: }
ds@627: }
ds@627:
ds@627: _YamagramPrinterTwoPointOhOh(s_YamagramState);
ds@627: }
ds@627:
ds@627: void _IntExt_EnableYamagrams()
ds@627: {
ds@627: static ConCommand *pCmd = NULL;
ds@627: if (!pCmd)
ds@627: {
ds@627: pCmd = new ConCommand("sm_nana", _IntExt_CallYamagrams, "Try these yamagrams!", FCVAR_GAMEDLL);
ds@627: g_RootMenu.ConsolePrint("[SM] Warning: Gaben has been alerted of your actions. You may be eaten.");
ds@627: } else {
ds@627: g_RootMenu.ConsolePrint("[SM] Gaben has already been alerted of your actions...");
ds@627: }
ds@627: }
ds@627:
faluco@1541: #if defined ORANGEBOX_BUILD
faluco@1541: void _IntExt_OnHostnameChanged(IConVar *pConVar, const char *oldValue, float flOldValue)
faluco@1541: #else
ds@627: void _IntExt_OnHostnameChanged(ConVar *pConVar, char const *oldValue)
faluco@1541: #endif
ds@627: {
ds@627: if (strcmp(oldValue, "Good morning, DS-san.") == 0
faluco@1541: && strcmp(reinterpret_cast(pConVar)->GetString(), "Good night, talking desk lamp.") == 0)
ds@627: {
ds@627: _IntExt_EnableYamagrams();
ds@627: }
ds@627: }