core/PluginInfoDatabase.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Tue Jan 22 23:18:27 2013 -0500 (2013-01-22)
changeset 3749 4dc3d723c490
parent 2811 af581de19119
permissions -rw-r--r--
Triggering build against new mac libprotobuf.
     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 <stdarg.h>
    33 #include <string.h>
    34 #include "PluginInfoDatabase.h"
    35 #include "PluginSys.h"
    36 
    37 void PluginSettings::Init()
    38 {
    39 	name = -1;
    40 	pause_val = false;
    41 	type_val = PluginType_MapUpdated;
    42 	optarray = -1;
    43 	opts_num = 0;
    44 	opts_size = 0;
    45 	blockload_val = false;
    46 }
    47 
    48 /**
    49  * :TODO: write the logger, make these errors log instead of being static
    50  * NOTE: once we do that, we will have to change some code to ignore sections
    51  */
    52 
    53 CPluginInfoDatabase::CPluginInfoDatabase()
    54 {
    55 	m_strtab = NULL;
    56 	m_infodb_count = 0;
    57 	m_infodb_size = 0;
    58 	m_infodb = -1;
    59 }
    60 
    61 CPluginInfoDatabase::~CPluginInfoDatabase()
    62 {
    63 	delete m_strtab;
    64 }
    65 
    66 void CPluginInfoDatabase::ReadSMC_ParseStart()
    67 {
    68 	/* Create or reset our string table */
    69 	if (m_strtab)
    70 	{
    71 		m_strtab->Reset();
    72 	} else {
    73 		m_strtab = new BaseStringTable(1024);
    74 	}
    75 
    76 	/* Set our internal states to the beginning */
    77 	in_plugins = false;
    78 	cur_plugin = -1;
    79 	in_options = false;
    80 	m_infodb_size = 0;
    81 	m_infodb_count = 0;
    82 	m_infodb = -1;
    83 }
    84 
    85 SMCResult CPluginInfoDatabase::MakeError(const char *fmt, ...)
    86 {
    87 	char buffer[512];
    88 	va_list ap;
    89 
    90 	va_start(ap, fmt);
    91 	vsnprintf(buffer, sizeof(buffer), fmt, ap);
    92 	va_end(ap);
    93 
    94 	m_errmsg = m_strtab->AddString(buffer);
    95 
    96 	return SMCResult_HaltFail;
    97 }
    98 
    99 unsigned int CPluginInfoDatabase::GetSettingsNum()
   100 {
   101 	return m_infodb_count;
   102 }
   103 
   104 PluginSettings *CPluginInfoDatabase::GetSettingsIfMatch(unsigned int index, const char *filename)
   105 {
   106 	BaseMemTable *memtab = m_strtab->GetMemTable();
   107 	int *table = (int *)memtab->GetAddress(m_infodb);
   108 
   109 	if (!table || index >= m_infodb_count)
   110 	{
   111 		return NULL;
   112 	}
   113 
   114 	PluginSettings *plugin = (PluginSettings *)memtab->GetAddress(table[index]);
   115 
   116 	const char *name = m_strtab->GetString(plugin->name);
   117 
   118 	if (!name)
   119 	{
   120 		return NULL;
   121 	}
   122 
   123 	if (!g_PluginSys.TestAliasMatch(name, filename))
   124 	{
   125 		return NULL;
   126 	}
   127 
   128 	return plugin;
   129 }
   130 
   131 void CPluginInfoDatabase::GetOptionsForPlugin(PluginSettings *settings, unsigned int opt_num, const char **key, const char **val)
   132 {
   133 	PluginOpts *table = (PluginOpts *)m_strtab->GetMemTable()->GetAddress(settings->optarray);
   134 	if (!table)
   135 	{
   136 		*key = NULL;
   137 		*val = NULL;
   138 		return;
   139 	}
   140 
   141 	if (opt_num >= settings->opts_num)
   142 	{
   143 		*key = NULL;
   144 		*val = NULL;
   145 		return;
   146 	}
   147 
   148 	*key = m_strtab->GetString(table[opt_num].key);
   149 	*val = m_strtab->GetString(table[opt_num].val);
   150 }
   151 
   152 SMCResult CPluginInfoDatabase::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
   153 {
   154 	if (cur_plugin != -1)
   155 	{
   156 		PluginSettings *plugin = (PluginSettings *)m_strtab->GetMemTable()->GetAddress(cur_plugin);
   157 		if (!in_options)
   158 		{
   159 			if (strcmp(key, "pause") == 0)
   160 			{
   161 				if (strcasecmp(value, "yes") == 0)
   162 				{
   163 					plugin->pause_val = true;
   164 				} 
   165 				else 
   166 				{
   167 					plugin->pause_val = false;
   168 				}
   169 			} 
   170 			else if (strcmp(key, "lifetime") == 0) 
   171 			{
   172 				if (strcasecmp(value, "private") == 0)
   173 				{
   174 					plugin->type_val = PluginType_Private;
   175 				} 
   176 				else if (strcasecmp(value, "mapsync") == 0) 
   177 				{
   178 					plugin->type_val = PluginType_MapUpdated;
   179 				}
   180 				else if (strcasecmp(value, "maponly") == 0) 
   181 				{
   182 					plugin->type_val = PluginType_MapOnly;
   183 				}
   184 				else if (strcasecmp(value, "global") == 0) 
   185 				{
   186 					plugin->type_val = PluginType_Global;
   187 				}
   188 				else 
   189 				{
   190 					return MakeError("Unknown value for key \"lifetime\": \"%s\"", value);
   191 				}
   192 			}
   193 			else if (strcmp(key, "blockload") == 0) 
   194 			{
   195 				plugin->blockload_val = true;
   196 			} 
   197 			else 
   198 			{
   199 				return MakeError("Unknown property key: \"%s\"", key);
   200 			}
   201 		}
   202 		else 
   203 		{
   204 			/* Cache every option, valid or not */
   205 			int keyidx = m_strtab->AddString(key);
   206 			int validx = m_strtab->AddString(value);
   207 			PluginOpts *table;
   208 			BaseMemTable *memtab = m_strtab->GetMemTable();
   209 			plugin = (PluginSettings *)memtab->GetAddress(cur_plugin);
   210 			if (plugin->opts_num + 1 > plugin->opts_size)
   211 			{
   212 				unsigned int oldsize = plugin->opts_size;
   213 				if (oldsize == 0)
   214 				{
   215 					//right now we don't have many
   216 					plugin->opts_size = 2;
   217 				} 
   218 				else
   219 				{
   220 					plugin->opts_size *= 2;
   221 				}
   222 				int newidx = memtab->CreateMem(plugin->opts_size * sizeof(PluginOpts), (void **)&table);
   223 				/* in case it resized */
   224 				plugin = (PluginSettings *)memtab->GetAddress(cur_plugin);
   225 				if (plugin->optarray != -1)
   226 				{
   227 					void *oldtable = memtab->GetAddress(plugin->optarray);
   228 					memcpy(table, oldtable, oldsize * sizeof(PluginOpts));
   229 				}
   230 				plugin->optarray = newidx;
   231 			}
   232 			else
   233 			{
   234 				table = (PluginOpts *)memtab->GetAddress(plugin->optarray);
   235 			}
   236 			PluginOpts *opt = &table[plugin->opts_num++];
   237 			opt->key = keyidx;
   238 			opt->val = validx;
   239 		}
   240 	}
   241 	else if (in_plugins)
   242 	{
   243 		return MakeError("Unknown property key: \"%s\"", key);
   244 	}
   245 	else 
   246 	{
   247 		/* Ignore anything we don't know about! */
   248 	}
   249 
   250 	return SMCResult_Continue;
   251 }
   252 
   253 SMCResult CPluginInfoDatabase::ReadSMC_LeavingSection(const SMCStates *states)
   254 {
   255 	if (in_plugins)
   256 	{
   257 		if (cur_plugin != -1)
   258 		{
   259 			if (in_options)
   260 			{
   261 				in_options = false;
   262 			}
   263 			else 
   264 			{
   265 				/* If the plugin is ending, add it to the table */
   266 				BaseMemTable *memtab = m_strtab->GetMemTable();
   267 				int *table;
   268 				if (m_infodb_count + 1 > m_infodb_size)
   269 				{
   270 					unsigned int oldsize = m_infodb_size;
   271 					if (!m_infodb_size)
   272 					{
   273 						m_infodb_size = 8;
   274 					}
   275 					else 
   276 					{
   277 						m_infodb_size *= 2;
   278 					}
   279 					int newidx = memtab->CreateMem(m_infodb_size * sizeof(int), (void **)&table);
   280 					if (m_infodb != -1)
   281 					{
   282 						void *oldtable = (int *)memtab->GetAddress(m_infodb);
   283 						memcpy(table, oldtable, oldsize * sizeof(int));
   284 					}
   285 					m_infodb = newidx;
   286 				}
   287 				else 
   288 				{
   289 					table = (int *)memtab->GetAddress(m_infodb);
   290 				}
   291 				/* Assign to table and scrap the current plugin */
   292 				table[m_infodb_count++] = cur_plugin;
   293 				cur_plugin = -1;
   294 			}
   295 		}
   296 		else 
   297 		{
   298 			in_plugins = false;
   299 		}
   300 	}
   301 
   302 	return SMCResult_Continue;
   303 }
   304 
   305 SMCResult CPluginInfoDatabase::ReadSMC_NewSection(const SMCStates *states, const char *name)
   306 {
   307 	if (!in_plugins)
   308 	{
   309 		/* If we're not in the main Plugins section, and we don't get it for the name, error out */
   310 		if (strcmp(name, "Plugins") != 0)
   311 		{
   312 			return MakeError("Unknown root section: \"%s\"", name);
   313 		}
   314 		else 
   315 		{
   316 			/* Otherwise set our states */
   317 			in_plugins = true;
   318 			cur_plugin = -1;
   319 			in_options = false;
   320 		}
   321 	}
   322 	else
   323 	{
   324 		if (cur_plugin == -1)
   325 		{
   326 			/* If we get a plugin node and we don't have a current plugin, create a new one */
   327 			PluginSettings *plugin;
   328 			int i_name = m_strtab->AddString(name);
   329 			cur_plugin = m_strtab->GetMemTable()->CreateMem(sizeof(PluginSettings), (void **)&plugin);
   330 			plugin->Init();
   331 			plugin->name = i_name;
   332 			in_options = false;
   333 		} 
   334 		else 
   335 		{
   336 			if (!in_options && strcmp(name, "Options") == 0)
   337 			{
   338 				in_options = true;
   339 			}
   340 			else
   341 			{
   342 				return MakeError("Unknown plugin sub-section: \"%s\"", name);
   343 			}
   344 		}
   345 	}
   346 
   347 	return SMCResult_Continue;
   348 }
   349