core/systems/PluginInfoDatabase.cpp
author David Anderson <dvander@alliedmods.net>
Thu Jan 25 22:36:38 2007 +0000 (2007-01-25)
changeset 370 d3a484f98f6d
parent 293 83b5a2d33e3d
child 653 ef60870067ff
permissions -rw-r--r--
added license headers
     1 /**
     2  * ===============================================================
     3  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4  * ===============================================================
     5  *
     6  * This file is not open source and may not be copied without explicit
     7  * written permission of AlliedModders LLC.  This file may not be redistributed 
     8  * in whole or significant part.
     9  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    10  *
    11  * Version: $Id$
    12  */
    13 
    14 #include <stdarg.h>
    15 #include <string.h>
    16 #include "PluginInfoDatabase.h"
    17 #include "PluginSys.h"
    18 
    19 void PluginSettings::Init()
    20 {
    21 	name = -1;
    22 	pause_val = false;
    23 	type_val = PluginType_MapUpdated;
    24 	optarray = -1;
    25 	opts_num = 0;
    26 	opts_size = 0;
    27 }
    28 
    29 /**
    30  * :TODO: write the logger, make these errors log instead of being static
    31  * NOTE: once we do that, we will have to change some code to ignore sections
    32  */
    33 
    34 CPluginInfoDatabase::CPluginInfoDatabase()
    35 {
    36 	m_strtab = NULL;
    37 	m_infodb_count = 0;
    38 	m_infodb_size = 0;
    39 	m_infodb = -1;
    40 }
    41 
    42 CPluginInfoDatabase::~CPluginInfoDatabase()
    43 {
    44 	delete m_strtab;
    45 }
    46 
    47 void CPluginInfoDatabase::ReadSMC_ParseStart()
    48 {
    49 	/* Create or reset our string table */
    50 	if (m_strtab)
    51 	{
    52 		m_strtab->Reset();
    53 	} else {
    54 		m_strtab = new BaseStringTable(1024);
    55 	}
    56 
    57 	/* Set our internal states to the beginning */
    58 	in_plugins = false;
    59 	cur_plugin = -1;
    60 	in_options = false;
    61 	m_infodb_size = 0;
    62 	m_infodb_count = 0;
    63 	m_infodb = -1;
    64 }
    65 
    66 SMCParseResult CPluginInfoDatabase::MakeError(const char *fmt, ...)
    67 {
    68 	char buffer[512];
    69 	va_list ap;
    70 
    71 	va_start(ap, fmt);
    72 	vsnprintf(buffer, sizeof(buffer), fmt, ap);
    73 	va_end(ap);
    74 
    75 	m_errmsg = m_strtab->AddString(buffer);
    76 
    77 	return SMCParse_HaltFail;
    78 }
    79 
    80 unsigned int CPluginInfoDatabase::GetSettingsNum()
    81 {
    82 	return m_infodb_count;
    83 }
    84 
    85 PluginSettings *CPluginInfoDatabase::GetSettingsIfMatch(unsigned int index, const char *filename)
    86 {
    87 	BaseMemTable *memtab = m_strtab->GetMemTable();
    88 	int *table = (int *)memtab->GetAddress(m_infodb);
    89 
    90 	if (!table || index >= m_infodb_count)
    91 	{
    92 		return NULL;
    93 	}
    94 
    95 	PluginSettings *plugin = (PluginSettings *)memtab->GetAddress(table[index]);
    96 
    97 	const char *name = m_strtab->GetString(plugin->name);
    98 
    99 	if (!name)
   100 	{
   101 		return NULL;
   102 	}
   103 
   104 	if (!g_PluginSys.TestAliasMatch(name, filename))
   105 	{
   106 		return NULL;
   107 	}
   108 
   109 	return plugin;
   110 }
   111 
   112 void CPluginInfoDatabase::GetOptionsForPlugin(PluginSettings *settings, unsigned int opt_num, const char **key, const char **val)
   113 {
   114 	PluginOpts *table = (PluginOpts *)m_strtab->GetMemTable()->GetAddress(settings->optarray);
   115 	if (!table)
   116 	{
   117 		*key = NULL;
   118 		*val = NULL;
   119 		return;
   120 	}
   121 
   122 	if (opt_num >= settings->opts_num)
   123 	{
   124 		*key = NULL;
   125 		*val = NULL;
   126 		return;
   127 	}
   128 
   129 	*key = m_strtab->GetString(table[opt_num].key);
   130 	*val = m_strtab->GetString(table[opt_num].val);
   131 }
   132 
   133 SMCParseResult CPluginInfoDatabase::ReadSMC_KeyValue(const char *key, 
   134 													 const char *value, 
   135 													 bool key_quotes, 
   136 													 bool value_quotes)
   137 {
   138 	if (cur_plugin != -1)
   139 	{
   140 		PluginSettings *plugin = (PluginSettings *)m_strtab->GetMemTable()->GetAddress(cur_plugin);
   141 		if (!in_options)
   142 		{
   143 			if (strcmp(key, "pause") == 0)
   144 			{
   145 				if (strcasecmp(value, "yes") == 0)
   146 				{
   147 					plugin->pause_val = true;
   148 				} else {
   149 					plugin->pause_val = false;
   150 				}
   151 			} else if (strcmp(key, "lifetime") == 0) {
   152 				if (strcasecmp(value, "private") == 0)
   153 				{
   154 					plugin->type_val = PluginType_Private;
   155 				} else if (strcasecmp(value, "mapsync") == 0) {
   156 					plugin->type_val = PluginType_MapUpdated;
   157 				} else if (strcasecmp(value, "maponly") == 0) {
   158 					plugin->type_val = PluginType_MapOnly;
   159 				} else if (strcasecmp(value, "global") == 0) {
   160 					plugin->type_val = PluginType_Global;
   161 				} else {
   162 					return MakeError("Unknown value for key \"lifetime\": \"%s\"", value);
   163 				}
   164 			} else {
   165 				return MakeError("Unknown property key: \"%s\"", key);
   166 			}
   167 		} else {
   168 			/* Cache every option, valid or not */
   169 			int keyidx = m_strtab->AddString(key);
   170 			int validx = m_strtab->AddString(value);
   171 			PluginOpts *table;
   172 			BaseMemTable *memtab = m_strtab->GetMemTable();
   173 			if (plugin->opts_num + 1 > plugin->opts_size)
   174 			{
   175 				unsigned int oldsize = plugin->opts_size;
   176 				if (oldsize == 0)
   177 				{
   178 					//right now we don't have many
   179 					plugin->opts_size = 2;
   180 				} else {
   181 					plugin->opts_size *= 2;
   182 				}
   183 				int newidx = memtab->CreateMem(plugin->opts_size * sizeof(PluginOpts), (void **)&table);
   184 				/* in case it resized */
   185 				plugin = (PluginSettings *)memtab->GetAddress(cur_plugin);
   186 				if (plugin->optarray != -1)
   187 				{
   188 					void *oldtable = memtab->GetAddress(plugin->optarray);
   189 					memcpy(table, oldtable, oldsize * sizeof(PluginOpts));
   190 				}
   191 				plugin->optarray = newidx;
   192 			} else {
   193 				table = (PluginOpts *)memtab->GetAddress(plugin->optarray);
   194 			}
   195 			PluginOpts *opt = &table[plugin->opts_num++];
   196 			opt->key = keyidx;
   197 			opt->val = validx;
   198 		}
   199 	} else if (in_plugins) {
   200 		return MakeError("Unknown property key: \"%s\"", key);
   201 	} else {
   202 		/* Ignore anything we don't know about! */
   203 	}
   204 
   205 	return SMCParse_Continue;
   206 }
   207 
   208 SMCParseResult CPluginInfoDatabase::ReadSMC_LeavingSection()
   209 {
   210 	if (in_plugins)
   211 	{
   212 		if (cur_plugin != -1)
   213 		{
   214 			if (in_options)
   215 			{
   216 				in_options = false;
   217 			} else {
   218 				/* If the plugin is ending, add it to the table */
   219 				BaseMemTable *memtab = m_strtab->GetMemTable();
   220 				int *table;
   221 				if (m_infodb_count + 1 > m_infodb_size)
   222 				{
   223 					unsigned int oldsize = m_infodb_size;
   224 					if (!m_infodb_size)
   225 					{
   226 						m_infodb_size = 8;
   227 					} else {
   228 						m_infodb_size *= 2;
   229 					}
   230 					int newidx = memtab->CreateMem(m_infodb_size, (void **)&table);
   231 					if (m_infodb != -1)
   232 					{
   233 						void *oldtable = (int *)memtab->GetAddress(m_infodb);
   234 						memcpy(table, oldtable, oldsize * sizeof(int));
   235 					}
   236 					m_infodb = newidx;
   237 				} else {
   238 					table = (int *)memtab->GetAddress(m_infodb);
   239 				}
   240 				/* Assign to table and scrap the current plugin */
   241 				table[m_infodb_count++] = cur_plugin;
   242 				cur_plugin = -1;
   243 			}
   244 		} else {
   245 			in_plugins = false;
   246 		}
   247 	}
   248 
   249 	return SMCParse_Continue;
   250 }
   251 
   252 SMCParseResult CPluginInfoDatabase::ReadSMC_NewSection(const char *name, bool opt_quotes)
   253 {
   254 	if (!in_plugins)
   255 	{
   256 		/* If we're not in the main Plugins section, and we don't get it for the name, error out */
   257 		if (strcmp(name, "Plugins") != 0)
   258 		{
   259 			return MakeError("Unknown root section: \"%s\"", name);
   260 		} else {
   261 			/* Otherwise set our states */
   262 			in_plugins = true;
   263 			cur_plugin = -1;
   264 			in_options = false;
   265 		}
   266 	} else {
   267 		if (cur_plugin == -1)
   268 		{
   269 			/* If we get a plugin node and we don't have a current plugin, create a new one */
   270 			PluginSettings *plugin;
   271 			cur_plugin = m_strtab->GetMemTable()->CreateMem(sizeof(PluginSettings), (void **)&plugin);
   272 			plugin->Init();
   273 			plugin->name = m_strtab->AddString(name);
   274 			in_options = false;
   275 		} else {
   276 			if (!in_options && strcmp(name, "Options") == 0)
   277 			{
   278 				in_options = true;
   279 			} else {
   280 				return MakeError("Unknown plugin sub-section: \"%s\"", name);
   281 			}
   282 		}
   283 	}
   284 
   285 	return SMCParse_Continue;
   286 }