2022-06-25 17:30:08 -04:00
|
|
|
#include <mods/server/server_properties.h>
|
2020-10-10 19:02:13 -04:00
|
|
|
|
|
|
|
static bool is_true(std::string const& val) {
|
|
|
|
return (val == "true" || val == "yes" || val == "1");
|
|
|
|
}
|
|
|
|
|
2024-09-20 21:30:47 -04:00
|
|
|
void ServerProperties::load(std::istream &stream) {
|
2020-10-10 19:02:13 -04:00
|
|
|
std::string line;
|
|
|
|
while (std::getline(stream, line)) {
|
|
|
|
if (line.length() > 0) {
|
|
|
|
if (line[0] == '#') {
|
|
|
|
continue;
|
|
|
|
}
|
2024-09-20 21:30:47 -04:00
|
|
|
const size_t i = line.find('=');
|
2020-10-10 19:02:13 -04:00
|
|
|
if (i == std::string::npos) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-09-20 21:30:47 -04:00
|
|
|
properties.insert(std::pair(line.substr(0, i), line.substr(i + 1)));
|
2020-10-10 19:02:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-20 21:30:47 -04:00
|
|
|
std::string ServerProperties::get_string(const std::string &name, const std::string &def) const {
|
|
|
|
return properties.contains(name) ? properties.at(name) : def;
|
2020-10-10 19:02:13 -04:00
|
|
|
}
|
|
|
|
|
2024-09-20 21:30:47 -04:00
|
|
|
int ServerProperties::get_int(const std::string &name, const std::string &def) const {
|
|
|
|
return properties.contains(name) ? std::stoi(properties.at(name)) : std::stoi(def);
|
2020-10-10 19:02:13 -04:00
|
|
|
}
|
|
|
|
|
2024-09-20 21:30:47 -04:00
|
|
|
bool ServerProperties::get_bool(const std::string &name, const std::string &def) const {
|
|
|
|
if (properties.contains(name)) {
|
|
|
|
const std::string &val = properties.at(name);
|
2020-10-10 19:02:13 -04:00
|
|
|
return is_true(val);
|
|
|
|
}
|
|
|
|
return is_true(def);
|
|
|
|
}
|