2
0
mirror of https://github.com/rehlds/metamod-r.git synced 2025-02-11 22:28:45 +03:00
metamod-r/metamod/src/conf_meta.cpp

181 lines
3.9 KiB
C++
Raw Normal View History

2016-07-26 07:22:47 +07:00
#include "precompiled.h"
2017-01-13 03:00:23 +03:00
MConfig::MConfig() : m_debuglevel(0), m_plugins_file(nullptr), m_exec_cfg(nullptr), m_list(nullptr), m_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).
void MConfig::init(option_t* global_options)
2016-07-26 07:22:47 +07:00
{
2017-01-13 03:00:23 +03:00
m_list = global_options;
for (auto optp = m_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
{
2017-01-13 03:00:23 +03:00
for (auto optp = m_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)) {
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
{
option_t* optp = find(key);
2016-07-26 07:22:47 +07:00
if (optp)
return set(optp, value);
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
{
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]))
{
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);
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))
{
*optval = TRUE;
2016-07-26 07:22:47 +07:00
}
2016-07-26 23:31:47 +07:00
else if (is_no(setstr))
{
*optval = FALSE;
2016-07-26 07:22:47 +07:00
}
2016-07-26 23:31:47 +07:00
else
{
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
}
2017-01-09 02:44:49 +03:00
META_DEBUG(3, "set config bool: %s = %s", setp->name, *optval ? "true" : "false");
2016-07-26 07:22:47 +07:00
break;
case CF_STR:
if (*optstr)
2016-07-26 23:31:47 +07:00
Q_free(*optstr);
*optstr = Q_strdup(setstr);
2017-01-09 02:44:49 +03:00
META_DEBUG(3, "set config string: %s = %s", setp->name, *optstr);
2016-07-26 07:22:47 +07:00
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);
2017-01-09 02:44:49 +03:00
META_DEBUG(3, "set config path: %s = %s", setp->name, *optstr);
2016-07-26 07:22:47 +07:00
break;
default:
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
{
FILE* fp;
char loadfile[PATH_MAX ];
2016-07-26 07:22:47 +07:00
char line[MAX_CONF_LEN];
char *optname, *optval;
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
}
2017-01-09 02:44:49 +03:00
META_DEBUG(2, "Loading from config file: %s", loadfile);
2017-01-17 00:30:02 +03:00
for (ln = 1; !feof(fp) && fgets(line, sizeof line, fp); ln++)
2016-07-26 23:31:47 +07:00
{
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
2017-01-13 03:00:23 +03:00
m_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
{
2017-01-13 03:00:23 +03:00
META_CONS("Config options from localinfo and %s:", m_filename);
2016-07-26 07:22:47 +07:00
2017-01-13 03:00:23 +03:00
for (auto optp = m_list; optp->name; optp++)
2016-07-26 23:31:47 +07:00
{
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:
printf(" %-20s\t%d\n", optp->name, *optval);
2016-07-26 07:22:47 +07:00
break;
case CF_BOOL:
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:
printf(" %-20s\t%s\n", optp->name, *optstr ? *optstr : "");
2016-07-26 07:22:47 +07:00
break;
case CF_NONE:
break;
}
}
}