2 * ===============================================================
3 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ===============================================================
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
16 #include "PluginInfoDatabase.h"
17 #include "PluginSys.h"
19 void PluginSettings::Init()
23 type_val = PluginType_MapUpdated;
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
34 CPluginInfoDatabase::CPluginInfoDatabase()
42 CPluginInfoDatabase::~CPluginInfoDatabase()
47 void CPluginInfoDatabase::ReadSMC_ParseStart()
49 /* Create or reset our string table */
54 m_strtab = new BaseStringTable(1024);
57 /* Set our internal states to the beginning */
66 SMCParseResult CPluginInfoDatabase::MakeError(const char *fmt, ...)
72 vsnprintf(buffer, sizeof(buffer), fmt, ap);
75 m_errmsg = m_strtab->AddString(buffer);
77 return SMCParse_HaltFail;
80 unsigned int CPluginInfoDatabase::GetSettingsNum()
82 return m_infodb_count;
85 PluginSettings *CPluginInfoDatabase::GetSettingsIfMatch(unsigned int index, const char *filename)
87 BaseMemTable *memtab = m_strtab->GetMemTable();
88 int *table = (int *)memtab->GetAddress(m_infodb);
90 if (!table || index >= m_infodb_count)
95 PluginSettings *plugin = (PluginSettings *)memtab->GetAddress(table[index]);
97 const char *name = m_strtab->GetString(plugin->name);
104 if (!g_PluginSys.TestAliasMatch(name, filename))
112 void CPluginInfoDatabase::GetOptionsForPlugin(PluginSettings *settings, unsigned int opt_num, const char **key, const char **val)
114 PluginOpts *table = (PluginOpts *)m_strtab->GetMemTable()->GetAddress(settings->optarray);
122 if (opt_num >= settings->opts_num)
129 *key = m_strtab->GetString(table[opt_num].key);
130 *val = m_strtab->GetString(table[opt_num].val);
133 SMCParseResult CPluginInfoDatabase::ReadSMC_KeyValue(const char *key,
138 if (cur_plugin != -1)
140 PluginSettings *plugin = (PluginSettings *)m_strtab->GetMemTable()->GetAddress(cur_plugin);
143 if (strcmp(key, "pause") == 0)
145 if (strcasecmp(value, "yes") == 0)
147 plugin->pause_val = true;
149 plugin->pause_val = false;
151 } else if (strcmp(key, "lifetime") == 0) {
152 if (strcasecmp(value, "private") == 0)
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;
162 return MakeError("Unknown value for key \"lifetime\": \"%s\"", value);
165 return MakeError("Unknown property key: \"%s\"", key);
168 /* Cache every option, valid or not */
169 int keyidx = m_strtab->AddString(key);
170 int validx = m_strtab->AddString(value);
172 BaseMemTable *memtab = m_strtab->GetMemTable();
173 if (plugin->opts_num + 1 > plugin->opts_size)
175 unsigned int oldsize = plugin->opts_size;
178 //right now we don't have many
179 plugin->opts_size = 2;
181 plugin->opts_size *= 2;
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)
188 void *oldtable = memtab->GetAddress(plugin->optarray);
189 memcpy(table, oldtable, oldsize * sizeof(PluginOpts));
191 plugin->optarray = newidx;
193 table = (PluginOpts *)memtab->GetAddress(plugin->optarray);
195 PluginOpts *opt = &table[plugin->opts_num++];
199 } else if (in_plugins) {
200 return MakeError("Unknown property key: \"%s\"", key);
202 /* Ignore anything we don't know about! */
205 return SMCParse_Continue;
208 SMCParseResult CPluginInfoDatabase::ReadSMC_LeavingSection()
212 if (cur_plugin != -1)
218 /* If the plugin is ending, add it to the table */
219 BaseMemTable *memtab = m_strtab->GetMemTable();
221 if (m_infodb_count + 1 > m_infodb_size)
223 unsigned int oldsize = m_infodb_size;
230 int newidx = memtab->CreateMem(m_infodb_size, (void **)&table);
233 void *oldtable = (int *)memtab->GetAddress(m_infodb);
234 memcpy(table, oldtable, oldsize * sizeof(int));
238 table = (int *)memtab->GetAddress(m_infodb);
240 /* Assign to table and scrap the current plugin */
241 table[m_infodb_count++] = cur_plugin;
249 return SMCParse_Continue;
252 SMCParseResult CPluginInfoDatabase::ReadSMC_NewSection(const char *name, bool opt_quotes)
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)
259 return MakeError("Unknown root section: \"%s\"", name);
261 /* Otherwise set our states */
267 if (cur_plugin == -1)
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);
273 plugin->name = m_strtab->AddString(name);
276 if (!in_options && strcmp(name, "Options") == 0)
280 return MakeError("Unknown plugin sub-section: \"%s\"", name);
285 return SMCParse_Continue;