core/concmd_cleaner.cpp
author Asher Baker <asherkin@gmail.com>
Thu Apr 26 16:58:18 2012 +0100 (2012-04-26)
changeset 3488 bee90c8ef6c3
parent 2806 686c283b08fc
child 3819 d17aed3d46bf
permissions -rw-r--r--
Fixed client serials not being unique on Windows (bug 5285, r=psychonic).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "sm_globals.h"
    33 #include <sh_list.h>
    34 #include <convar.h>
    35 #include "concmd_cleaner.h"
    36 #include "sm_stringutil.h"
    37 #include "sourcemm_api.h"
    38 #include "compat_wrappers.h"
    39 
    40 #if SOURCE_ENGINE >= SE_ORANGEBOX
    41 SH_DECL_HOOK1_void(ICvar, UnregisterConCommand, SH_NOATTRIB, 0, ConCommandBase *);
    42 SH_DECL_HOOK1_void(ICvar, RegisterConCommand, SH_NOATTRIB, 0, ConCommandBase *);
    43 #else
    44 SH_DECL_HOOK1_void(ICvar, RegisterConCommandBase, SH_NOATTRIB, 0, ConCommandBase *);
    45 #endif
    46 
    47 using namespace SourceHook;
    48 
    49 struct ConCommandInfo
    50 {
    51 	ConCommandBase *pBase;
    52 	IConCommandTracker *cls;
    53 	char name[64];
    54 };
    55 
    56 List<ConCommandInfo *> tracked_bases;
    57 IConCommandLinkListener *IConCommandLinkListener::head = NULL;
    58 
    59 ConCommandBase *FindConCommandBase(const char *name);
    60 
    61 class ConCommandCleaner : public SMGlobalClass
    62 {
    63 public:
    64 	void OnSourceModAllInitialized()
    65 	{
    66 #if SOURCE_ENGINE >= SE_ORANGEBOX
    67 		SH_ADD_HOOK_MEMFUNC(ICvar, UnregisterConCommand, icvar, this, &ConCommandCleaner::UnlinkConCommandBase, false);
    68 		SH_ADD_HOOK_MEMFUNC(ICvar, RegisterConCommand, icvar, this, &ConCommandCleaner::LinkConCommandBase, false);
    69 #else
    70 		SH_ADD_HOOK_MEMFUNC(ICvar, RegisterConCommandBase, icvar, this, &ConCommandCleaner::LinkConCommandBase, false);
    71 #endif
    72 	}
    73 
    74 	void OnSourceModShutdown()
    75 	{
    76 #if SOURCE_ENGINE >= SE_ORANGEBOX
    77 		SH_REMOVE_HOOK_MEMFUNC(ICvar, UnregisterConCommand, icvar, this, &ConCommandCleaner::UnlinkConCommandBase, false);
    78 		SH_REMOVE_HOOK_MEMFUNC(ICvar, RegisterConCommand, icvar, this, &ConCommandCleaner::LinkConCommandBase, false);
    79 #else
    80 		SH_REMOVE_HOOK_MEMFUNC(ICvar, RegisterConCommandBase, icvar, this, &ConCommandCleaner::LinkConCommandBase, false);
    81 #endif
    82 	}
    83 
    84 	void LinkConCommandBase(ConCommandBase *pBase)
    85 	{
    86 		IConCommandLinkListener *listener = IConCommandLinkListener::head;
    87 		while (listener)
    88 		{
    89 			listener->OnLinkConCommand(pBase);
    90 			listener = listener->next;
    91 		}
    92 	}
    93 
    94 	void UnlinkConCommandBase(ConCommandBase *pBase)
    95 	{
    96 		ConCommandInfo *pInfo;
    97 		List<ConCommandInfo *>::iterator iter = tracked_bases.begin();
    98 
    99 		IConCommandLinkListener *listener = IConCommandLinkListener::head;
   100 		while (listener)
   101 		{
   102 			listener->OnUnlinkConCommand(pBase);
   103 			listener = listener->next;
   104 		}
   105 
   106 		if (pBase)
   107 		{
   108 			while (iter != tracked_bases.end())
   109 			{
   110 				if ((*iter)->pBase == pBase)
   111 				{
   112 					pInfo = (*iter);
   113 					iter = tracked_bases.erase(iter);
   114 					pInfo->cls->OnUnlinkConCommandBase(pBase, pBase->GetName(), true);
   115 					delete pInfo;
   116 				}
   117 				else
   118 				{
   119 					iter++;
   120 				}
   121 			}
   122 		}
   123 		else
   124 		{
   125 			while (iter != tracked_bases.end())
   126 			{
   127 				/* This is just god-awful! */
   128 				if (FindCommandBase((*iter)->name) != (*iter)->pBase)
   129 				{
   130 					pInfo = (*iter);
   131 					iter = tracked_bases.erase(iter);
   132 					pInfo->cls->OnUnlinkConCommandBase(pBase, pInfo->name, false);
   133 					delete pInfo;
   134 				}
   135 				else
   136 				{
   137 					iter++;
   138 				}
   139 			}
   140 		}
   141 	}
   142 
   143 	void AddTarget(ConCommandBase *pBase, IConCommandTracker *cls)
   144 	{
   145 		ConCommandInfo *info = new ConCommandInfo;
   146 
   147 		info->pBase = pBase;
   148 		info->cls = cls;
   149 		strncopy(info->name, pBase->GetName(), sizeof(info->name));
   150 
   151 		tracked_bases.push_back(info);
   152 	}
   153 
   154 	void RemoveTarget(ConCommandBase *pBase, IConCommandTracker *cls)
   155 	{
   156 		List<ConCommandInfo *>::iterator iter;
   157 		ConCommandInfo *pInfo;
   158 
   159 		iter = tracked_bases.begin();
   160 		while (iter != tracked_bases.end())
   161 		{
   162 			pInfo = (*iter);
   163 			if (pInfo->pBase == pBase && pInfo->cls == cls)
   164 			{
   165 				delete pInfo;
   166 				iter = tracked_bases.erase(iter);
   167 			}
   168 			else
   169 			{
   170 				iter++;
   171 			}
   172 		}
   173 	}
   174 } s_ConCmdTracker;
   175 
   176 void TrackConCommandBase(ConCommandBase *pBase, IConCommandTracker *me)
   177 {
   178 	s_ConCmdTracker.AddTarget(pBase, me);
   179 }
   180 
   181 void UntrackConCommandBase(ConCommandBase *pBase, IConCommandTracker *me)
   182 {
   183 	s_ConCmdTracker.RemoveTarget(pBase, me);
   184 }
   185 
   186 void Global_OnUnlinkConCommandBase(ConCommandBase *pBase)
   187 {
   188 	s_ConCmdTracker.UnlinkConCommandBase(pBase);
   189 }