summaryrefslogtreecommitdiff
path: root/settings.c
diff options
context:
space:
mode:
authorDarkvater <darkvater@openttd.org>2006-04-22 13:17:13 +0000
committerDarkvater <darkvater@openttd.org>2006-04-22 13:17:13 +0000
commite3280971b12c15cbe077f59cf28f8a6e4977d51d (patch)
treea4d01abf72144eb6b67a3c809d3072f51de5a63e /settings.c
parent28213ea9dcb911c0cc30471e68661de6be6593b8 (diff)
downloadopenttd-e3280971b12c15cbe077f59cf28f8a6e4977d51d.tar.xz
(svn r4527) - Feature [NewGRF]: Add support for specifying parameters to GRF files. Usage: "mygrf = 1". You can pass up to 128 parameters, each one seperated by a comma or a space-character. Big thanks to peter1138 for the inspiration and examples.
Diffstat (limited to 'settings.c')
-rw-r--r--settings.c73
1 files changed, 56 insertions, 17 deletions
diff --git a/settings.c b/settings.c
index b6f239e92..1372eec94 100644
--- a/settings.c
+++ b/settings.c
@@ -34,6 +34,7 @@
#include "console.h"
#include "saveload.h"
#include "npf.h"
+#include "newgrf.h"
/** The patch values that are used for new games and/or modified in config file */
Patches _patches_newgame;
@@ -43,6 +44,10 @@ typedef struct IniItem IniItem;
typedef struct IniGroup IniGroup;
typedef struct SettingsMemoryPool SettingsMemoryPool;
+typedef const char *SettingListCallbackProc(const IniItem *item, uint index);
+typedef void SettingDescProc(IniFile *ini, const SettingDesc *desc, const char *grpname, void *object);
+typedef void SettingDescProcList(IniFile *ini, const char *grpname, char **list, uint len, SettingListCallbackProc proc);
+
static void pool_init(SettingsMemoryPool **pool);
static void *pool_alloc(SettingsMemoryPool **pool, uint size);
static void *pool_strdup(SettingsMemoryPool **pool, const char *mem, uint size);
@@ -817,20 +822,25 @@ static void ini_save_settings(IniFile *ini, const SettingDesc *sd, const char *g
* file that will be parsed
* @param list pointer to an string(pointer) array that will store the parsed
* entries of the given section
- * @param len the maximum number of items available for the above list */
-static void ini_load_setting_list(IniFile *ini, const char *grpname, char **list, uint len)
+ * @param len the maximum number of items available for the above list
+ * @param proc callback function that can override how the values are stored
+ * inside the list */
+static void ini_load_setting_list(IniFile *ini, const char *grpname, char **list, uint len, SettingListCallbackProc proc)
{
IniGroup *group = ini_getgroup(ini, grpname, -1);
IniItem *item;
- uint i;
+ const char *entry;
+ uint i, j;
if (group == NULL) return;
- item = group->item;
- for (i = 0; i != len; i++) {
- if (item == NULL) break;
- list[i] = strdup(item->name);
- item = item->next;
+ for (i = j = 0, item = group->item; item != NULL; item = item->next) {
+ entry = (proc != NULL) ? proc(item, i++) : item->name;
+
+ if (entry == NULL || list == NULL) continue;
+
+ if (j == len) break;
+ list[j++] = strdup(entry);
}
}
@@ -843,26 +853,30 @@ static void ini_load_setting_list(IniFile *ini, const char *grpname, char **list
* source to be saved into the relevant ini section
* @param len the maximum number of items available for the above list
* @param proc callback function that can will provide the source data if defined */
-static void ini_save_setting_list(IniFile *ini, const char *grpname, char **list, uint len)
+static void ini_save_setting_list(IniFile *ini, const char *grpname, char **list, uint len, SettingListCallbackProc proc)
{
IniGroup *group = ini_getgroup(ini, grpname, -1);
IniItem *item = NULL;
+ const char *entry;
uint i;
bool first = true;
+ if (proc == NULL && list == NULL) return;
if (group == NULL) return;
group->item = NULL;
for (i = 0; i != len; i++) {
- if (list[i] == NULL || list[i][0] == '\0') continue;
+ entry = (proc != NULL) ? proc(NULL, i) : list[i];
+
+ if (entry == NULL || *entry == '\0') continue;
if (first) { // add first item to the head of the group
- item = ini_item_alloc(group, list[i], strlen(list[i]));
+ item = ini_item_alloc(group, entry, strlen(entry));
item->value = item->name;
group->item = item;
first = false;
} else { // all other items are attached to the previous one
- item->next = ini_item_alloc(group, list[i], strlen(list[i]));
+ item->next = ini_item_alloc(group, entry, strlen(entry));
item = item->next;
item->value = item->name;
}
@@ -1385,8 +1399,33 @@ static const SettingDesc _currency_settings[] = {
#undef NO
#undef CR
-typedef void SettingDescProc(IniFile *ini, const SettingDesc *desc, const char *grpname, void *object);
-typedef void SettingDescProcList(IniFile *ini, const char *grpname, char **list, uint len);
+const char *GRFProcessParams(const IniItem *item, uint index)
+{
+ GRFConfig *c;
+
+ /* Saving newgrf stuff to configuration, not done since it is kept the same */
+ if (item == NULL) return NULL;
+
+ /* Loading newgrf stuff from configuration file */
+ c = calloc(1, sizeof(*c));
+ c->filename = strdup(item->name);
+ c->num_params = parse_intlist(item->value, (int*)c->param, lengthof(c->param));
+ if (c->num_params == (byte)-1) {
+ ShowInfoF("ini: error in array '%s'", item->name);
+ c->num_params = 0;
+ }
+
+ if (_first_grfconfig == NULL) {
+ _first_grfconfig = c;
+ } else {
+ GRFConfig *c2;
+ /* Attach the label to the end of the list */
+ for (c2 = _first_grfconfig; c2->next != NULL; c2 = c2->next);
+ c2->next = c;
+ }
+
+ return c->filename;
+}
/* Common handler for saving/loading variables to the configuration file */
static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescProcList *proc_list)
@@ -1403,8 +1442,8 @@ static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescP
#ifdef ENABLE_NETWORK
proc(ini, (const SettingDesc*)_network_settings, "network", NULL);
- proc_list(ini, "servers", _network_host_list, lengthof(_network_host_list));
- proc_list(ini, "bans", _network_ban_list, lengthof(_network_ban_list));
+ proc_list(ini, "servers", _network_host_list, lengthof(_network_host_list), NULL);
+ proc_list(ini, "bans", _network_ban_list, lengthof(_network_ban_list), NULL);
#endif /* ENABLE_NETWORK */
}
@@ -1413,7 +1452,7 @@ void LoadFromConfig(void)
{
IniFile *ini = ini_load(_config_file);
HandleSettingDescs(ini, ini_load_settings, ini_load_setting_list);
- ini_load_setting_list(ini, "newgrf", _newgrf_files, lengthof(_newgrf_files));
+ ini_load_setting_list(ini, "newgrf", NULL, 0, GRFProcessParams);
ini_free(ini);
}