ds@1952: /**
ds@1952: * vim: set ts=4 :
ds@1952: * =============================================================================
ds@1952: * SourceMod
ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
ds@1952: * =============================================================================
ds@1952: *
ds@1952: * This program is free software; you can redistribute it and/or modify it under
ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952: * Free Software Foundation.
ds@1952: *
ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1952: * details.
ds@1952: *
ds@1952: * You should have received a copy of the GNU General Public License along with
ds@1952: * this program. If not, see .
ds@1952: *
ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1952: * this exception to all derivative works. AlliedModders LLC defines further
ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952: * or .
ds@1952: *
ds@1952: * Version: $Id$
ds@1952: */
ds@1952:
ds@1952: #include "sm_globals.h"
ds@1952: #include
ds@1952: #include
ds@1952: #include "concmd_cleaner.h"
ds@1952: #include "sm_stringutil.h"
ds@1952: #include "sourcemm_api.h"
ds@1952:
ds@1952: #if defined ORANGEBOX_BUILD
ds@1952: SH_DECL_HOOK1_void(ICvar, UnregisterConCommand, SH_NOATTRIB, 0, ConCommandBase *);
ds@1952: #endif
ds@1952:
ds@1952: using namespace SourceHook;
ds@1952:
ds@1952: struct ConCommandInfo
ds@1952: {
ds@1952: ConCommandBase *pBase;
ds@1952: IConCommandTracker *cls;
ds@1952: char name[64];
ds@1952: };
ds@1952:
ds@1952: List tracked_bases;
ds@1952:
ds@1952: ConCommandBase *FindConCommandBase(const char *name);
ds@1952:
ds@1952: class ConCommandCleaner : public SMGlobalClass
ds@1952: {
ds@1952: public:
ds@1952: #if defined ORANGEBOX_BUILD
ds@1952: void OnSourceModAllInitialized()
ds@1952: {
ds@1952: SH_ADD_HOOK_MEMFUNC(ICvar, UnregisterConCommand, icvar, this, &ConCommandCleaner::UnlinkConCommandBase, false);
ds@1952: }
ds@1952:
ds@1952: void OnSourceModShutdown()
ds@1952: {
ds@1952: SH_REMOVE_HOOK_MEMFUNC(ICvar, UnregisterConCommand, icvar, this, &ConCommandCleaner::UnlinkConCommandBase, false);
ds@1952: }
ds@1952: #endif
ds@1952:
ds@1952: void UnlinkConCommandBase(ConCommandBase *pBase)
ds@1952: {
ds@1952: ConCommandInfo *pInfo;
ds@1952: List::iterator iter = tracked_bases.begin();
ds@1952:
ds@1952: if (pBase)
ds@1952: {
ds@1952: while (iter != tracked_bases.end())
ds@1952: {
ds@1952: if ((*iter)->pBase == pBase)
ds@1952: {
ds@1952: pInfo = (*iter);
ds@1952: iter = tracked_bases.erase(iter);
ds@1952: pInfo->cls->OnUnlinkConCommandBase(pBase, pBase->GetName(), true);
ds@1952: delete pInfo;
ds@1952: }
ds@1952: else
ds@1952: {
ds@1952: iter++;
ds@1952: }
ds@1952: }
ds@1952: }
ds@1952: else
ds@1952: {
ds@1952: while (iter != tracked_bases.end())
ds@1952: {
ds@1952: /* This is just god-awful! */
ds@1952: if (FindConCommandBase((*iter)->name) != (*iter)->pBase)
ds@1952: {
ds@1952: pInfo = (*iter);
ds@1952: iter = tracked_bases.erase(iter);
ds@1952: pInfo->cls->OnUnlinkConCommandBase(pBase, pInfo->name, false);
ds@1952: delete pInfo;
ds@1952: }
ds@1952: else
ds@1952: {
ds@1952: iter++;
ds@1952: }
ds@1952: }
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: void AddTarget(ConCommandBase *pBase, IConCommandTracker *cls)
ds@1952: {
ds@1952: ConCommandInfo *info = new ConCommandInfo;
ds@1952:
ds@1952: info->pBase = pBase;
ds@1952: info->cls = cls;
ds@1952: strncopy(info->name, pBase->GetName(), sizeof(info->name));
ds@1952:
ds@1952: tracked_bases.push_back(info);
ds@1952: }
ds@1952:
ds@1952: void RemoveTarget(ConCommandBase *pBase, IConCommandTracker *cls)
ds@1952: {
ds@1952: List::iterator iter;
ds@1952: ConCommandInfo *pInfo;
ds@1952:
ds@1952: iter = tracked_bases.begin();
ds@1952: while (iter != tracked_bases.end())
ds@1952: {
ds@1952: pInfo = (*iter);
ds@1952: if (pInfo->pBase == pBase && pInfo->cls == cls)
ds@1952: {
ds@1952: delete pInfo;
ds@1952: iter = tracked_bases.erase(iter);
ds@1952: }
ds@1952: else
ds@1952: {
ds@1952: iter++;
ds@1952: }
ds@1952: }
ds@1952: }
ds@1952: } s_ConCmdTracker;
ds@1952:
ds@1952: ConCommandBase *FindConCommandBase(const char *name)
ds@1952: {
ds@1952: const ConCommandBase *pBase = icvar->GetCommands();
ds@1952:
ds@1952: while (pBase != NULL)
ds@1952: {
ds@1952: if (strcmp(pBase->GetName(), name) == 0)
ds@1952: {
ds@1952: return const_cast(pBase);
ds@1952: }
ds@1952: pBase = pBase->GetNext();
ds@1952: }
ds@1952:
ds@1952: return NULL;
ds@1952: }
ds@1952:
ds@1952: void TrackConCommandBase(ConCommandBase *pBase, IConCommandTracker *me)
ds@1952: {
ds@1952: s_ConCmdTracker.AddTarget(pBase, me);
ds@1952: }
ds@1952:
ds@1952: void UntrackConCommandBase(ConCommandBase *pBase, IConCommandTracker *me)
ds@1952: {
ds@1952: s_ConCmdTracker.RemoveTarget(pBase, me);
ds@1952: }
ds@1952:
ds@1952: void Global_OnUnlinkConCommandBase(ConCommandBase *pBase)
ds@1952: {
ds@1952: s_ConCmdTracker.UnlinkConCommandBase(pBase);
ds@1952: }