core/systems/PluginInfoDatabase.cpp
author David Anderson <dvander@alliedmods.net>
Mon Oct 22 16:48:54 2007 +0000 (2007-10-22)
changeset 1639 5c7fffd146ae
parent 1436 38994f8f28c3
child 1656 b0b86d57530c
permissions -rw-r--r--
fixed linux build
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 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 SMCParseResult 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 SMCParse_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 SMCParseResult CPluginInfoDatabase::ReadSMC_KeyValue(const char *key, 
   153 													 const char *value, 
   154 													 bool key_quotes, 
   155 													 bool value_quotes)
   156 {
   157 	if (cur_plugin != -1)
   158 	{
   159 		PluginSettings *plugin = (PluginSettings *)m_strtab->GetMemTable()->GetAddress(cur_plugin);
   160 		if (!in_options)
   161 		{
   162 			if (strcmp(key, "pause") == 0)
   163 			{
   164 				if (strcasecmp(value, "yes") == 0)
   165 				{
   166 					plugin->pause_val = true;
   167 				} else {
   168 					plugin->pause_val = false;
   169 				}
   170 			} else if (strcmp(key, "lifetime") == 0) {
   171 				if (strcasecmp(value, "private") == 0)
   172 				{
   173 					plugin->type_val = PluginType_Private;
   174 				} else if (strcasecmp(value, "mapsync") == 0) {
   175 					plugin->type_val = PluginType_MapUpdated;
   176 				} else if (strcasecmp(value, "maponly") == 0) {
   177 					plugin->type_val = PluginType_MapOnly;
   178 				} else if (strcasecmp(value, "global") == 0) {
   179 					plugin->type_val = PluginType_Global;
   180 				} else {
   181 					return MakeError("Unknown value for key \"lifetime\": \"%s\"", value);
   182 				}
   183 			} else if (strcmp(key, "blockload") == 0) {
   184 				plugin->blockload_val = true;
   185 			} else {
   186 				return MakeError("Unknown property key: \"%s\"", key);
   187 			}
   188 		} else {
   189 			/* Cache every option, valid or not */
   190 			int keyidx = m_strtab->AddString(key);
   191 			int validx = m_strtab->AddString(value);
   192 			PluginOpts *table;
   193 			BaseMemTable *memtab = m_strtab->GetMemTable();
   194 			plugin = (PluginSettings *)memtab->GetAddress(cur_plugin);
   195 			if (plugin->opts_num + 1 > plugin->opts_size)
   196 			{
   197 				unsigned int oldsize = plugin->opts_size;
   198 				if (oldsize == 0)
   199 				{
   200 					//right now we don't have many
   201 					plugin->opts_size = 2;
   202 				} else {
   203 					plugin->opts_size *= 2;
   204 				}
   205 				int newidx = memtab->CreateMem(plugin->opts_size * sizeof(PluginOpts), (void **)&table);
   206 				/* in case it resized */
   207 				plugin = (PluginSettings *)memtab->GetAddress(cur_plugin);
   208 				if (plugin->optarray != -1)
   209 				{
   210 					void *oldtable = memtab->GetAddress(plugin->optarray);
   211 					memcpy(table, oldtable, oldsize * sizeof(PluginOpts));
   212 				}
   213 				plugin->optarray = newidx;
   214 			} else {
   215 				table = (PluginOpts *)memtab->GetAddress(plugin->optarray);
   216 			}
   217 			PluginOpts *opt = &table[plugin->opts_num++];
   218 			opt->key = keyidx;
   219 			opt->val = validx;
   220 		}
   221 	} else if (in_plugins) {
   222 		return MakeError("Unknown property key: \"%s\"", key);
   223 	} else {
   224 		/* Ignore anything we don't know about! */
   225 	}
   226 
   227 	return SMCParse_Continue;
   228 }
   229 
   230 SMCParseResult CPluginInfoDatabase::ReadSMC_LeavingSection()
   231 {
   232 	if (in_plugins)
   233 	{
   234 		if (cur_plugin != -1)
   235 		{
   236 			if (in_options)
   237 			{
   238 				in_options = false;
   239 			} else {
   240 				/* If the plugin is ending, add it to the table */
   241 				BaseMemTable *memtab = m_strtab->GetMemTable();
   242 				int *table;
   243 				if (m_infodb_count + 1 > m_infodb_size)
   244 				{
   245 					unsigned int oldsize = m_infodb_size;
   246 					if (!m_infodb_size)
   247 					{
   248 						m_infodb_size = 8;
   249 					} else {
   250 						m_infodb_size *= 2;
   251 					}
   252 					int newidx = memtab->CreateMem(m_infodb_size, (void **)&table);
   253 					if (m_infodb != -1)
   254 					{
   255 						void *oldtable = (int *)memtab->GetAddress(m_infodb);
   256 						memcpy(table, oldtable, oldsize * sizeof(int));
   257 					}
   258 					m_infodb = newidx;
   259 				} else {
   260 					table = (int *)memtab->GetAddress(m_infodb);
   261 				}
   262 				/* Assign to table and scrap the current plugin */
   263 				table[m_infodb_count++] = cur_plugin;
   264 				cur_plugin = -1;
   265 			}
   266 		} else {
   267 			in_plugins = false;
   268 		}
   269 	}
   270 
   271 	return SMCParse_Continue;
   272 }
   273 
   274 SMCParseResult CPluginInfoDatabase::ReadSMC_NewSection(const char *name, bool opt_quotes)
   275 {
   276 	if (!in_plugins)
   277 	{
   278 		/* If we're not in the main Plugins section, and we don't get it for the name, error out */
   279 		if (strcmp(name, "Plugins") != 0)
   280 		{
   281 			return MakeError("Unknown root section: \"%s\"", name);
   282 		} else {
   283 			/* Otherwise set our states */
   284 			in_plugins = true;
   285 			cur_plugin = -1;
   286 			in_options = false;
   287 		}
   288 	} else {
   289 		if (cur_plugin == -1)
   290 		{
   291 			/* If we get a plugin node and we don't have a current plugin, create a new one */
   292 			PluginSettings *plugin;
   293 			int i_name = m_strtab->AddString(name);
   294 			cur_plugin = m_strtab->GetMemTable()->CreateMem(sizeof(PluginSettings), (void **)&plugin);
   295 			plugin->Init();
   296 			plugin->name = i_name;
   297 			in_options = false;
   298 		} else {
   299 			if (!in_options && strcmp(name, "Options") == 0)
   300 			{
   301 				in_options = true;
   302 			} else {
   303 				return MakeError("Unknown plugin sub-section: \"%s\"", name);
   304 			}
   305 		}
   306 	}
   307 
   308 	return SMCParse_Continue;
   309 }