Added SourceMod to the sv_tags list and added a tags API for plugins (bug 3688, r=dvander)
authorMatt Woodrow <pred@alliedmods.net>
Thu Mar 05 11:59:49 2009 +1300 (2009-03-05)
changeset 2648dbcc58361ada
parent 2646 06420bac0e89
child 2649 ac44973873ce
Added SourceMod to the sv_tags list and added a tags API for plugins (bug 3688, r=dvander)
core/Makefile
core/TagsSystem.cpp
core/TagsSystem.h
core/msvc9/sourcemod_mm.vcproj
core/smn_console.cpp
plugins/include/console.inc
     1.1 --- a/core/Makefile	Tue Mar 03 09:41:55 2009 -0500
     1.2 +++ b/core/Makefile	Thu Mar 05 11:59:49 2009 +1300
     1.3 @@ -19,7 +19,7 @@
     1.4  	sourcemm_api.cpp sourcemod.cpp MenuStyle_Base.cpp MenuStyle_Valve.cpp MenuManager.cpp \
     1.5  	MenuStyle_Radio.cpp ChatTriggers.cpp ADTFactory.cpp MenuVoting.cpp sm_crc32.cpp \
     1.6  	frame_hooks.cpp concmd_cleaner.cpp Profiler.cpp PhraseCollection.cpp NextMap.cpp \
     1.7 -	NativeOwner.cpp 
     1.8 +	NativeOwner.cpp TagsSystem.cpp
     1.9  OBJECTS += smn_admin.cpp smn_bitbuffer.cpp smn_console.cpp smn_core.cpp \
    1.10  	smn_datapacks.cpp smn_entities.cpp smn_events.cpp smn_fakenatives.cpp \
    1.11  	smn_filesystem.cpp smn_float.cpp smn_functions.cpp smn_gameconfigs.cpp smn_halflife.cpp \
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/core/TagsSystem.cpp	Thu Mar 05 11:59:49 2009 +1300
     2.3 @@ -0,0 +1,206 @@
     2.4 +/**
     2.5 +* vim: set ts=4 :
     2.6 +* =============================================================================
     2.7 +* SourceMod
     2.8 +* Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     2.9 +* =============================================================================
    2.10 +*
    2.11 +* This program is free software; you can redistribute it and/or modify it under
    2.12 +* the terms of the GNU General Public License, version 3.0, as published by the
    2.13 +* Free Software Foundation.
    2.14 +* 
    2.15 +* This program is distributed in the hope that it will be useful, but WITHOUT
    2.16 +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    2.17 +* FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    2.18 +* details.
    2.19 +*
    2.20 +* You should have received a copy of the GNU General Public License along with
    2.21 +* this program.  If not, see <http://www.gnu.org/licenses/>.
    2.22 +*
    2.23 +* As a special exception, AlliedModders LLC gives you permission to link the
    2.24 +* code of this program (as well as its derivative works) to "Half-Life 2," the
    2.25 +* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    2.26 +* by the Valve Corporation.  You must obey the GNU General Public License in
    2.27 +* all respects for all other code used.  Additionally, AlliedModders LLC grants
    2.28 +* this exception to all derivative works.  AlliedModders LLC defines further
    2.29 +* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    2.30 +* or <http://www.sourcemod.net/license.php>.
    2.31 +*
    2.32 +* Version: $Id$
    2.33 +*/
    2.34 +
    2.35 +#include "TagsSystem.h"
    2.36 +#include "sourcemod.h"
    2.37 +#include "sourcemm_api.h"
    2.38 +#include "sm_stringutil.h"
    2.39 +#include "PluginSys.h"
    2.40 +#include "compat_wrappers.h"
    2.41 +
    2.42 +TagHandler g_Tags;
    2.43 +
    2.44 +TagHandler::TagHandler()
    2.45 +{
    2.46 +	m_SvTags = NULL;
    2.47 +}
    2.48 +
    2.49 +TagHandler::~TagHandler()
    2.50 +{
    2.51 +	SourceHook::List<char *>::iterator iter = m_TagList.begin();
    2.52 +
    2.53 +	while (iter != m_TagList.end())
    2.54 +	{
    2.55 +		g_pMemAlloc->Free(*iter);
    2.56 +		iter = m_TagList.erase(iter);
    2.57 +	}
    2.58 +}
    2.59 +
    2.60 +void TagHandler::OnSourceModAllInitialized_Post()
    2.61 +{
    2.62 +	g_PluginSys.AddPluginsListener(this);
    2.63 +
    2.64 +	m_SvTags = icvar->FindVar("sv_tags");
    2.65 +	AddTag("sourcemod");
    2.66 +}
    2.67 +
    2.68 +void TagHandler::OnSourceModLevelActivated()
    2.69 +{
    2.70 +	SourceHook::List<char *>::iterator iter = m_TagList.begin();
    2.71 +
    2.72 +	while (iter != m_TagList.end())
    2.73 +	{
    2.74 +		ApplyTag(*iter);
    2.75 +		iter++;
    2.76 +	}
    2.77 +}
    2.78 +
    2.79 +void TagHandler::OnSourceModShutdown()
    2.80 +{
    2.81 +	RemoveTag("sourcemod");
    2.82 +}
    2.83 +
    2.84 +void TagHandler::OnPluginDestroyed(IPlugin *plugin)
    2.85 +{
    2.86 +	SourceHook::List<char *> *pList = NULL;
    2.87 +
    2.88 +	if (!plugin->GetProperty("ServerTags", (void **)&pList, false) || !pList)
    2.89 +	{
    2.90 +		return;
    2.91 +	}
    2.92 +
    2.93 +	SourceHook::List<char *>::iterator iter = pList->begin();
    2.94 +
    2.95 +	while (iter != pList->end())
    2.96 +	{
    2.97 +		RemoveTag(*iter);
    2.98 +		g_pMemAlloc->Free(*iter);
    2.99 +		iter = pList->erase(iter);
   2.100 +	}
   2.101 +
   2.102 +	delete pList;
   2.103 +}
   2.104 +
   2.105 +bool TagHandler::AddTag(const char *addTag)
   2.106 +{
   2.107 +	SourceHook::List<char *>::iterator iter = m_TagList.begin();
   2.108 +
   2.109 +	while (iter != m_TagList.end())
   2.110 +	{
   2.111 +		if (strcmp(*iter, addTag) == 0)
   2.112 +		{
   2.113 +			return false;
   2.114 +		}
   2.115 +	}
   2.116 +
   2.117 +	m_TagList.push_back(strdup(addTag));
   2.118 +	ApplyTag(addTag);
   2.119 +
   2.120 +	return true;
   2.121 +}
   2.122 +
   2.123 +bool TagHandler::RemoveTag(const char *removeTag)
   2.124 +{
   2.125 +	SourceHook::List<char *>::iterator iter = m_TagList.begin();
   2.126 +
   2.127 +	while (iter != m_TagList.end())
   2.128 +	{
   2.129 +		if (strcmp(*iter, removeTag) == 0)
   2.130 +		{
   2.131 +			g_pMemAlloc->Free(*iter);
   2.132 +			m_TagList.erase(iter);
   2.133 +			StripTag(removeTag);
   2.134 +
   2.135 +			return true;
   2.136 +		}
   2.137 +	}
   2.138 +
   2.139 +	return false;
   2.140 +}
   2.141 +
   2.142 +void TagHandler::ApplyTag(const char *addTag)
   2.143 +{
   2.144 +	if (m_SvTags == NULL)
   2.145 +	{
   2.146 +		return;
   2.147 +	}
   2.148 +
   2.149 +	const char *curTags = m_SvTags->GetString();
   2.150 +
   2.151 +	if (strstr(curTags, addTag) != NULL)
   2.152 +	{
   2.153 +		/* Already tagged */
   2.154 +		return;
   2.155 +	}
   2.156 +
   2.157 +	if (curTags[0] == '\0')
   2.158 +	{
   2.159 +		m_SvTags->SetValue(addTag);
   2.160 +		return;
   2.161 +	}
   2.162 +
   2.163 +	/* New tags buffer (+2 for , and null char) */
   2.164 +	size_t newLen = strlen(curTags) + strlen(addTag) + 2;
   2.165 +	char *newTags = (char *)alloca(newLen);
   2.166 +
   2.167 +	g_SourceMod.Format(newTags, newLen, "%s,%s", curTags, addTag);
   2.168 +
   2.169 +	m_SvTags->SetValue(newTags);
   2.170 +}
   2.171 +
   2.172 +void TagHandler::StripTag(const char *removeTag)
   2.173 +{
   2.174 +	if (m_SvTags == NULL)
   2.175 +	{
   2.176 +		return;
   2.177 +	}
   2.178 +
   2.179 +	const char *curTags = m_SvTags->GetString();
   2.180 +
   2.181 +	if (strcmp(curTags, removeTag) == 0)
   2.182 +	{
   2.183 +		m_SvTags->SetValue("");
   2.184 +		return;
   2.185 +	}
   2.186 +
   2.187 +	char *newTags = strdup(curTags);
   2.188 +	size_t searchLen = strlen(removeTag) + 2;
   2.189 +	char *search = (char *)alloca(searchLen);
   2.190 +
   2.191 +	if (strncmp(curTags, removeTag, strlen(removeTag)) == 0)
   2.192 +	{
   2.193 +		strcpy(search, removeTag);
   2.194 +		search[searchLen-2] = ',';
   2.195 +		search[searchLen-1] = '\0';
   2.196 +	}
   2.197 +	else
   2.198 +	{
   2.199 +		strcpy(search+1, removeTag);
   2.200 +		search[0] = ',';
   2.201 +		search[searchLen-1] = '\0';
   2.202 +	}
   2.203 +
   2.204 +	UTIL_ReplaceAll(newTags, strlen(newTags) + 1, search, "" , true);
   2.205 +
   2.206 +	m_SvTags->SetValue(newTags);
   2.207 +
   2.208 +	g_pMemAlloc->Free(newTags);
   2.209 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/core/TagsSystem.h	Thu Mar 05 11:59:49 2009 +1300
     3.3 @@ -0,0 +1,71 @@
     3.4 +/**
     3.5 +* vim: set ts=4 :
     3.6 +* =============================================================================
     3.7 +* SourceMod
     3.8 +* Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     3.9 +* =============================================================================
    3.10 +*
    3.11 +* This program is free software; you can redistribute it and/or modify it under
    3.12 +* the terms of the GNU General Public License, version 3.0, as published by the
    3.13 +* Free Software Foundation.
    3.14 +* 
    3.15 +* This program is distributed in the hope that it will be useful, but WITHOUT
    3.16 +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    3.17 +* FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    3.18 +* details.
    3.19 +*
    3.20 +* You should have received a copy of the GNU General Public License along with
    3.21 +* this program.  If not, see <http://www.gnu.org/licenses/>.
    3.22 +*
    3.23 +* As a special exception, AlliedModders LLC gives you permission to link the
    3.24 +* code of this program (as well as its derivative works) to "Half-Life 2," the
    3.25 +* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    3.26 +* by the Valve Corporation.  You must obey the GNU General Public License in
    3.27 +* all respects for all other code used.  Additionally, AlliedModders LLC grants
    3.28 +* this exception to all derivative works.  AlliedModders LLC defines further
    3.29 +* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    3.30 +* or <http://www.sourcemod.net/license.php>.
    3.31 +*
    3.32 +* Version: $Id$
    3.33 +*/
    3.34 +
    3.35 +#ifndef _INCLUDE_SOURCEMOD_TAGSSYSTEM_H_
    3.36 +#define _INCLUDE_SOURCEMOD_TAGSSYSTEM_H_
    3.37 +
    3.38 +#include "sm_globals.h"
    3.39 +#include "sh_list.h"
    3.40 +#include "convar.h"
    3.41 +#include "IPluginSys.h"
    3.42 +
    3.43 +class TagHandler : 
    3.44 +	public SMGlobalClass,
    3.45 +	public IPluginsListener
    3.46 +{
    3.47 +public:
    3.48 +	TagHandler();
    3.49 +	~TagHandler();
    3.50 +
    3.51 +public: // SMGlobalClass
    3.52 +	void OnSourceModAllInitialized_Post();
    3.53 +	void OnSourceModLevelActivated();
    3.54 +	void OnSourceModShutdown();
    3.55 +
    3.56 +public: // IPluginsListener
    3.57 +	void OnPluginDestroyed(IPlugin *plugin);
    3.58 +
    3.59 +public:
    3.60 +	bool AddTag(const char *addTag);
    3.61 +	bool RemoveTag(const char *removeTag);
    3.62 +
    3.63 +private:
    3.64 +	void ApplyTag(const char *addTag);
    3.65 +	void StripTag(const char *removeTag);
    3.66 +
    3.67 +private:
    3.68 +	SourceHook::List<char *>m_TagList;
    3.69 +	ConVar *m_SvTags;
    3.70 +};
    3.71 +
    3.72 +extern TagHandler g_Tags;
    3.73 +
    3.74 +#endif // _INCLUDE_SOURCEMOD_TAGSSYSTEM_H_
     4.1 --- a/core/msvc9/sourcemod_mm.vcproj	Tue Mar 03 09:41:55 2009 -0500
     4.2 +++ b/core/msvc9/sourcemod_mm.vcproj	Thu Mar 05 11:59:49 2009 +1300
     4.3 @@ -1513,6 +1513,10 @@
     4.4  				>
     4.5  			</File>
     4.6  			<File
     4.7 +				RelativePath="..\TagsSystem.cpp"
     4.8 +				>
     4.9 +			</File>
    4.10 +			<File
    4.11  				RelativePath="..\TextParsers.cpp"
    4.12  				>
    4.13  			</File>
    4.14 @@ -1751,6 +1755,10 @@
    4.15  				>
    4.16  			</File>
    4.17  			<File
    4.18 +				RelativePath="..\TagsSystem.h"
    4.19 +				>
    4.20 +			</File>
    4.21 +			<File
    4.22  				RelativePath="..\TextParsers.h"
    4.23  				>
    4.24  			</File>
     5.1 --- a/core/smn_console.cpp	Tue Mar 03 09:41:55 2009 -0500
     5.2 +++ b/core/smn_console.cpp	Thu Mar 05 11:59:49 2009 +1300
     5.3 @@ -43,6 +43,7 @@
     5.4  #include <inetchannel.h>
     5.5  #include <bitbuf.h>
     5.6  #include <sm_trie_tpl.h>
     5.7 +#include "TagsSystem.h"
     5.8  
     5.9  #if SOURCE_ENGINE == SE_LEFT4DEAD
    5.10  #define NET_SETCONVAR	6
    5.11 @@ -1304,6 +1305,60 @@
    5.12  	return 1;
    5.13  }
    5.14  
    5.15 +static cell_t AddServerTag(IPluginContext *pContext, const cell_t *params)
    5.16 +{
    5.17 +	char *value;
    5.18 +	pContext->LocalToString(params[1], &value);
    5.19 +
    5.20 +	if (!g_Tags.AddTag(value))
    5.21 +	{
    5.22 +		return 0;
    5.23 +	}
    5.24 +
    5.25 +	IPlugin *pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
    5.26 +	SourceHook::List<char *> *pList = NULL;
    5.27 +
    5.28 +	if (!pPlugin->GetProperty("ServerTags", (void **)&pList, false) || !pList)
    5.29 +	{
    5.30 +		pList = new SourceHook::List<char *>;
    5.31 +		pPlugin->SetProperty("ServerTags", pList);
    5.32 +	}
    5.33 +
    5.34 +	pList->push_back(strdup(value));
    5.35 +
    5.36 +	return 1;
    5.37 +}
    5.38 +
    5.39 +static cell_t RemoveServerTag(IPluginContext *pContext, const cell_t *params)
    5.40 +{
    5.41 +	char *value;
    5.42 +	pContext->LocalToString(params[1], &value);
    5.43 +
    5.44 +	IPlugin *pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
    5.45 +	SourceHook::List<char *> *pList = NULL;
    5.46 +
    5.47 +	if (!pPlugin->GetProperty("ServerTags", (void **)&pList, false) || !pList)
    5.48 +	{
    5.49 +		pList = new SourceHook::List<char *>;
    5.50 +		pPlugin->SetProperty("ServerTags", pList);
    5.51 +	}
    5.52 +
    5.53 +	SourceHook::List<char *>::iterator iter = pList->begin();
    5.54 +
    5.55 +	while (iter != pList->end())
    5.56 +	{
    5.57 +		if (strcmp(*iter, value) == 0)
    5.58 +		{
    5.59 +			g_pMemAlloc->Free(*iter);
    5.60 +			pList->erase(iter);
    5.61 +
    5.62 +			return g_Tags.RemoveTag(value);
    5.63 +		}
    5.64 +	}
    5.65 +
    5.66 +	return 0;
    5.67 +}
    5.68 +
    5.69  REGISTER_NATIVES(consoleNatives)
    5.70  {
    5.71  	{"CreateConVar",		sm_CreateConVar},
    5.72 @@ -1351,5 +1406,7 @@
    5.73  	{"FindFirstConCommand",	FindFirstConCommand},
    5.74  	{"FindNextConCommand",	FindNextConCommand},
    5.75  	{"SendConVarValue",		SendConVarValue},
    5.76 +	{"AddServerTag",		AddServerTag},
    5.77 +	{"RemoveServerTag",		RemoveServerTag},
    5.78  	{NULL,					NULL}
    5.79  };
     6.1 --- a/plugins/include/console.inc	Tue Mar 03 09:41:55 2009 -0500
     6.2 +++ b/plugins/include/console.inc	Thu Mar 05 11:59:49 2009 +1300
     6.3 @@ -815,3 +815,23 @@
     6.4   * @error 				Invalid client index, client not in game, or client is fake
     6.5   */
     6.6  native bool:SendConVarValue(client, Handle:convar, const String:value[]);
     6.7 +
     6.8 +/**
     6.9 + * Appends a string to Valve's sv_tags convar and makes sure it remains after mapchanges.
    6.10 + *
    6.11 + * Note: Tags are automatically removed on plugin unload
    6.12 + *
    6.13 + * @param tag			Tag string to append.
    6.14 + * @noreturn
    6.15 + */
    6.16 +native AddServerTag(const String:tag[]);
    6.17 +
    6.18 +/**
    6.19 + * Removes a string from valve's sv_tags convar.
    6.20 + *
    6.21 + * Note: You can only remove tags created by you.
    6.22 + *
    6.23 + * @param tag			Tag string to remove.
    6.24 + * @noreturn
    6.25 + */
    6.26 +native RemoveServerTag(const String:tag[]);