1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/concmd_cleaner.cpp Sun Mar 30 07:00:22 2008 +0000
1.3 @@ -0,0 +1,175 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourceMod
1.8 + * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 + * =============================================================================
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify it under
1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
1.13 + * Free Software Foundation.
1.14 + *
1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 + * details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License along with
1.21 + * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 + *
1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 + * by the Valve Corporation. You must obey the GNU General Public License in
1.27 + * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 + * this exception to all derivative works. AlliedModders LLC defines further
1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 + * or <http://www.sourcemod.net/license.php>.
1.31 + *
1.32 + * Version: $Id$
1.33 + */
1.34 +
1.35 +#include "sm_globals.h"
1.36 +#include <sh_list.h>
1.37 +#include <convar.h>
1.38 +#include "concmd_cleaner.h"
1.39 +#include "sm_stringutil.h"
1.40 +#include "sourcemm_api.h"
1.41 +
1.42 +#if defined ORANGEBOX_BUILD
1.43 +SH_DECL_HOOK1_void(ICvar, UnregisterConCommand, SH_NOATTRIB, 0, ConCommandBase *);
1.44 +#endif
1.45 +
1.46 +using namespace SourceHook;
1.47 +
1.48 +struct ConCommandInfo
1.49 +{
1.50 + ConCommandBase *pBase;
1.51 + IConCommandTracker *cls;
1.52 + char name[64];
1.53 +};
1.54 +
1.55 +List<ConCommandInfo *> tracked_bases;
1.56 +
1.57 +ConCommandBase *FindConCommandBase(const char *name);
1.58 +
1.59 +class ConCommandCleaner : public SMGlobalClass
1.60 +{
1.61 +public:
1.62 +#if defined ORANGEBOX_BUILD
1.63 + void OnSourceModAllInitialized()
1.64 + {
1.65 + SH_ADD_HOOK_MEMFUNC(ICvar, UnregisterConCommand, icvar, this, &ConCommandCleaner::UnlinkConCommandBase, false);
1.66 + }
1.67 +
1.68 + void OnSourceModShutdown()
1.69 + {
1.70 + SH_REMOVE_HOOK_MEMFUNC(ICvar, UnregisterConCommand, icvar, this, &ConCommandCleaner::UnlinkConCommandBase, false);
1.71 + }
1.72 +#endif
1.73 +
1.74 + void UnlinkConCommandBase(ConCommandBase *pBase)
1.75 + {
1.76 + ConCommandInfo *pInfo;
1.77 + List<ConCommandInfo *>::iterator iter = tracked_bases.begin();
1.78 +
1.79 + if (pBase)
1.80 + {
1.81 + while (iter != tracked_bases.end())
1.82 + {
1.83 + if ((*iter)->pBase == pBase)
1.84 + {
1.85 + pInfo = (*iter);
1.86 + iter = tracked_bases.erase(iter);
1.87 + pInfo->cls->OnUnlinkConCommandBase(pBase, pBase->GetName(), true);
1.88 + delete pInfo;
1.89 + }
1.90 + else
1.91 + {
1.92 + iter++;
1.93 + }
1.94 + }
1.95 + }
1.96 + else
1.97 + {
1.98 + while (iter != tracked_bases.end())
1.99 + {
1.100 + /* This is just god-awful! */
1.101 + if (FindConCommandBase((*iter)->name) != (*iter)->pBase)
1.102 + {
1.103 + pInfo = (*iter);
1.104 + iter = tracked_bases.erase(iter);
1.105 + pInfo->cls->OnUnlinkConCommandBase(pBase, pInfo->name, false);
1.106 + delete pInfo;
1.107 + }
1.108 + else
1.109 + {
1.110 + iter++;
1.111 + }
1.112 + }
1.113 + }
1.114 + }
1.115 +
1.116 + void AddTarget(ConCommandBase *pBase, IConCommandTracker *cls)
1.117 + {
1.118 + ConCommandInfo *info = new ConCommandInfo;
1.119 +
1.120 + info->pBase = pBase;
1.121 + info->cls = cls;
1.122 + strncopy(info->name, pBase->GetName(), sizeof(info->name));
1.123 +
1.124 + tracked_bases.push_back(info);
1.125 + }
1.126 +
1.127 + void RemoveTarget(ConCommandBase *pBase, IConCommandTracker *cls)
1.128 + {
1.129 + List<ConCommandInfo *>::iterator iter;
1.130 + ConCommandInfo *pInfo;
1.131 +
1.132 + iter = tracked_bases.begin();
1.133 + while (iter != tracked_bases.end())
1.134 + {
1.135 + pInfo = (*iter);
1.136 + if (pInfo->pBase == pBase && pInfo->cls == cls)
1.137 + {
1.138 + delete pInfo;
1.139 + iter = tracked_bases.erase(iter);
1.140 + }
1.141 + else
1.142 + {
1.143 + iter++;
1.144 + }
1.145 + }
1.146 + }
1.147 +} s_ConCmdTracker;
1.148 +
1.149 +ConCommandBase *FindConCommandBase(const char *name)
1.150 +{
1.151 + const ConCommandBase *pBase = icvar->GetCommands();
1.152 +
1.153 + while (pBase != NULL)
1.154 + {
1.155 + if (strcmp(pBase->GetName(), name) == 0)
1.156 + {
1.157 + return const_cast<ConCommandBase *>(pBase);
1.158 + }
1.159 + pBase = pBase->GetNext();
1.160 + }
1.161 +
1.162 + return NULL;
1.163 +}
1.164 +
1.165 +void TrackConCommandBase(ConCommandBase *pBase, IConCommandTracker *me)
1.166 +{
1.167 + s_ConCmdTracker.AddTarget(pBase, me);
1.168 +}
1.169 +
1.170 +void UntrackConCommandBase(ConCommandBase *pBase, IConCommandTracker *me)
1.171 +{
1.172 + s_ConCmdTracker.RemoveTarget(pBase, me);
1.173 +}
1.174 +
1.175 +void Global_OnUnlinkConCommandBase(ConCommandBase *pBase)
1.176 +{
1.177 + s_ConCmdTracker.UnlinkConCommandBase(pBase);
1.178 +}