2016-07-26 07:22:47 +07:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2016-07-30 02:03:01 +03:00
|
|
|
MConfig::MConfig() : debuglevel(0), plugins_file(nullptr), exec_cfg(nullptr), list(nullptr), filename(nullptr)
|
2016-07-26 07:22:47 +07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize default values from the stored options struct. Has to happen
|
|
|
|
// _after_ constructor, so that all the fields are allocated (d'oh).
|
2016-07-26 15:18:32 +03:00
|
|
|
void MConfig::init(option_t* global_options)
|
2016-07-26 07:22:47 +07:00
|
|
|
{
|
2016-07-26 15:18:32 +03:00
|
|
|
list = global_options;
|
|
|
|
for (auto optp = list; optp->name; optp++)
|
2016-07-26 07:22:47 +07:00
|
|
|
set(optp, optp->init);
|
|
|
|
}
|
|
|
|
|
2016-07-26 23:31:47 +07:00
|
|
|
option_t *MConfig::find(const char* lookup) const
|
2016-07-26 07:22:47 +07:00
|
|
|
{
|
2016-07-26 15:18:32 +03:00
|
|
|
for (auto optp = list; optp->name; optp++)
|
2016-07-26 23:31:47 +07:00
|
|
|
{
|
2016-07-30 02:03:01 +03:00
|
|
|
if (!strcmp(optp->name, lookup)) {
|
2016-07-26 15:18:32 +03:00
|
|
|
return optp;
|
|
|
|
}
|
2016-07-26 23:31:47 +07:00
|
|
|
}
|
|
|
|
|
2017-01-07 21:03:16 +03:00
|
|
|
return NULL;
|
2016-07-26 07:22:47 +07:00
|
|
|
}
|
|
|
|
|
2016-07-30 02:03:01 +03:00
|
|
|
bool MConfig::set(const char* key, const char* value) const
|
2016-07-26 07:22:47 +07:00
|
|
|
{
|
2016-07-26 15:18:32 +03:00
|
|
|
option_t* optp = find(key);
|
2016-07-26 07:22:47 +07:00
|
|
|
if (optp)
|
|
|
|
return set(optp, value);
|
2016-07-26 15:18:32 +03:00
|
|
|
|
2017-01-07 21:03:16 +03:00
|
|
|
return false;
|
2016-07-26 07:22:47 +07:00
|
|
|
}
|
|
|
|
|
2016-07-30 02:03:01 +03:00
|
|
|
bool MConfig::set(option_t* setp, const char* setstr)
|
2016-07-26 07:22:47 +07:00
|
|
|
{
|
2016-07-26 15:18:32 +03:00
|
|
|
char pathbuf[PATH_MAX ];
|
|
|
|
int* optval = (int *) setp->dest;
|
|
|
|
char** optstr = (char **) setp->dest;
|
|
|
|
// cvar_t *optcvar = (cvar_t *) setp->dest;
|
|
|
|
// SETOPT_FN optcmd = (SETOPT_FN) setp->dest;
|
2016-07-26 07:22:47 +07:00
|
|
|
|
|
|
|
if (!setstr)
|
2016-07-30 02:03:01 +03:00
|
|
|
return true;
|
2016-07-26 07:22:47 +07:00
|
|
|
|
2016-07-26 23:31:47 +07:00
|
|
|
switch (setp->type)
|
|
|
|
{
|
2016-07-26 07:22:47 +07:00
|
|
|
case CF_INT:
|
2016-07-26 23:31:47 +07:00
|
|
|
if (!isdigit(setstr[0]))
|
|
|
|
{
|
2016-07-26 15:18:32 +03:00
|
|
|
META_ERROR("option '%s' invalid format '%s'", setp->name, setstr);
|
2017-01-07 21:03:16 +03:00
|
|
|
return false;
|
2016-07-26 07:22:47 +07:00
|
|
|
}
|
2016-07-26 23:31:47 +07:00
|
|
|
*optval = Q_atoi(setstr);
|
2017-01-09 01:30:23 +03:00
|
|
|
META_DEBUG(3, "set config int: %s = %d", setp->name, *optval);
|
2016-07-26 07:22:47 +07:00
|
|
|
break;
|
|
|
|
case CF_BOOL:
|
2016-07-26 23:31:47 +07:00
|
|
|
if (is_yes(setstr))
|
|
|
|
{
|
2016-07-26 15:18:32 +03:00
|
|
|
*optval = TRUE;
|
2016-07-26 07:22:47 +07:00
|
|
|
}
|
2016-07-26 23:31:47 +07:00
|
|
|
else if (is_no(setstr))
|
|
|
|
{
|
2016-07-26 15:18:32 +03:00
|
|
|
*optval = FALSE;
|
2016-07-26 07:22:47 +07:00
|
|
|
}
|
2016-07-26 23:31:47 +07:00
|
|
|
else
|
|
|
|
{
|
2016-07-26 15:18:32 +03:00
|
|
|
META_ERROR("option '%s' invalid format '%s'", setp->name,
|
|
|
|
setstr);
|
2017-01-07 21:03:16 +03:00
|
|
|
return false;
|
2016-07-26 07:22:47 +07:00
|
|
|
}
|
|
|
|
META_DEBUG(3, ("set config bool: %s = %s", setp->name, *optval ? "true" : "false"));
|
|
|
|
break;
|
|
|
|
case CF_STR:
|
|
|
|
if (*optstr)
|
2016-07-26 23:31:47 +07:00
|
|
|
Q_free(*optstr);
|
|
|
|
*optstr = Q_strdup(setstr);
|
2016-07-26 07:22:47 +07:00
|
|
|
META_DEBUG(3, ("set config string: %s = %s", setp->name, *optstr));
|
|
|
|
break;
|
|
|
|
case CF_PATH:
|
|
|
|
if (*optstr)
|
2016-07-26 23:31:47 +07:00
|
|
|
Q_free(*optstr);
|
2016-07-26 07:22:47 +07:00
|
|
|
full_gamedir_path(setstr, pathbuf);
|
2016-07-26 23:31:47 +07:00
|
|
|
*optstr = Q_strdup(pathbuf);
|
2016-07-26 07:22:47 +07:00
|
|
|
META_DEBUG(3, ("set config path: %s = %s", setp->name, *optstr));
|
|
|
|
break;
|
|
|
|
default:
|
2016-07-26 15:18:32 +03:00
|
|
|
META_ERROR("unrecognized config type '%d'", setp->type);
|
2017-01-07 21:03:16 +03:00
|
|
|
return false;
|
2016-07-26 07:22:47 +07:00
|
|
|
}
|
2016-07-26 23:31:47 +07:00
|
|
|
|
2016-07-30 02:03:01 +03:00
|
|
|
return true;
|
2016-07-26 07:22:47 +07:00
|
|
|
}
|
|
|
|
|
2016-07-30 02:03:01 +03:00
|
|
|
bool MConfig::load(const char* fn)
|
2016-07-26 07:22:47 +07:00
|
|
|
{
|
2016-07-26 15:18:32 +03:00
|
|
|
FILE* fp;
|
|
|
|
char loadfile[PATH_MAX ];
|
2016-07-26 07:22:47 +07:00
|
|
|
char line[MAX_CONF_LEN];
|
|
|
|
char *optname, *optval;
|
2016-07-26 15:18:32 +03:00
|
|
|
option_t* optp;
|
2016-07-26 07:22:47 +07:00
|
|
|
int ln;
|
|
|
|
|
|
|
|
// Make full pathname (from gamedir if relative, collapse "..",
|
|
|
|
// backslashes, etc).
|
|
|
|
full_gamedir_path(fn, loadfile);
|
|
|
|
|
|
|
|
fp = fopen(loadfile, "r");
|
2016-07-26 23:31:47 +07:00
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
META_ERROR("unable to open config file '%s': %s", loadfile, strerror(errno));
|
2017-01-07 21:03:16 +03:00
|
|
|
return false;
|
2016-07-26 07:22:47 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
META_DEBUG(2, ("Loading from config file: %s", loadfile));
|
2016-07-26 23:31:47 +07:00
|
|
|
for (ln = 1; !feof(fp) && fgets(line, sizeof(line), fp); ln++)
|
|
|
|
{
|
|
|
|
if (line[0] == '#' || line[0] == ';' || !Q_strncmp(line, "//", 2))
|
2016-07-26 07:22:47 +07:00
|
|
|
continue;
|
2016-07-26 23:31:47 +07:00
|
|
|
|
|
|
|
if (!(optname = strtok(line, " \t\r\n")))
|
|
|
|
{
|
|
|
|
META_ERROR("'%s' line %d: bad config format: missing option", loadfile, ln);
|
2016-07-26 07:22:47 +07:00
|
|
|
continue;
|
|
|
|
}
|
2016-07-26 23:31:47 +07:00
|
|
|
|
|
|
|
if (!(optval = strtok(NULL, "\r\n")))
|
|
|
|
{
|
|
|
|
META_ERROR("'%s' line %d: bad config format: missing value", loadfile, ln);
|
2016-07-26 07:22:47 +07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-26 23:31:47 +07:00
|
|
|
if (!(optp = find(optname)))
|
|
|
|
{
|
|
|
|
META_ERROR("'%s' line %d: unknown option name '%s'", loadfile, ln, optname);
|
2016-07-26 07:22:47 +07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-26 23:31:47 +07:00
|
|
|
if (!set(optp, optval))
|
|
|
|
{
|
|
|
|
META_ERROR("'%s' line %d: unable to set option '%s' value '%s'", loadfile, ln, optname, optval);
|
2016-07-26 07:22:47 +07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2016-07-26 23:31:47 +07:00
|
|
|
|
|
|
|
filename = Q_strdup(loadfile);
|
2016-07-26 07:22:47 +07:00
|
|
|
fclose(fp);
|
2016-07-30 02:03:01 +03:00
|
|
|
return true;
|
2016-07-26 07:22:47 +07:00
|
|
|
}
|
|
|
|
|
2016-07-26 23:31:47 +07:00
|
|
|
void MConfig::show() const
|
2016-07-26 07:22:47 +07:00
|
|
|
{
|
2016-07-26 15:18:32 +03:00
|
|
|
META_CONS("Config options from localinfo and %s:", filename);
|
2016-07-26 07:22:47 +07:00
|
|
|
|
2016-07-26 23:31:47 +07:00
|
|
|
for (auto optp = list; optp->name; optp++)
|
|
|
|
{
|
|
|
|
int *optval = (int *)optp->dest;
|
|
|
|
char **optstr = (char **)optp->dest;
|
|
|
|
|
2016-07-26 07:22:47 +07:00
|
|
|
// cvar_t *optcvar = (cvar_t *) optp->dest;
|
|
|
|
// SETOPT_FN optcmd = (SETOPT_FN) optp->dest;
|
2016-07-26 23:31:47 +07:00
|
|
|
switch (optp->type)
|
|
|
|
{
|
2016-07-26 07:22:47 +07:00
|
|
|
case CF_INT:
|
2016-07-26 15:18:32 +03:00
|
|
|
printf(" %-20s\t%d\n", optp->name, *optval);
|
2016-07-26 07:22:47 +07:00
|
|
|
break;
|
|
|
|
case CF_BOOL:
|
2016-07-26 15:18:32 +03:00
|
|
|
printf(" %-20s\t%s\n", optp->name, *optval ? "true" : "false");
|
2016-07-26 07:22:47 +07:00
|
|
|
break;
|
|
|
|
case CF_STR:
|
|
|
|
case CF_PATH:
|
2016-07-26 15:18:32 +03:00
|
|
|
printf(" %-20s\t%s\n", optp->name, *optstr ? *optstr : "");
|
2016-07-26 07:22:47 +07:00
|
|
|
break;
|
|
|
|
case CF_NONE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|