2016-05-20 03:09:22 +02:00
|
|
|
#include "GameConfig.hpp"
|
2017-02-20 20:57:36 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
#include <rw/defines.hpp>
|
2017-11-02 03:36:13 +01:00
|
|
|
#include <rw/filesystem.hpp>
|
2016-05-20 03:09:22 +02:00
|
|
|
|
2016-12-28 18:35:20 +01:00
|
|
|
#include <boost/property_tree/ini_parser.hpp>
|
2017-02-20 20:57:36 +01:00
|
|
|
#include <boost/property_tree/ptree.hpp>
|
2016-12-28 18:35:20 +01:00
|
|
|
namespace pt = boost::property_tree;
|
2016-05-20 03:09:22 +02:00
|
|
|
|
|
|
|
const std::string kConfigDirectoryName("OpenRW");
|
|
|
|
|
2017-11-02 03:36:13 +01:00
|
|
|
GameConfig::GameConfig()
|
|
|
|
: m_configPath()
|
2017-02-18 07:33:53 +01:00
|
|
|
, m_parseResult()
|
2016-09-09 22:13:20 +02:00
|
|
|
, m_inputInvertY(false) {
|
2017-11-02 03:36:13 +01:00
|
|
|
}
|
2016-05-20 03:09:22 +02:00
|
|
|
|
2017-11-02 03:36:13 +01:00
|
|
|
void GameConfig::loadFile(const rwfs::path &path) {
|
|
|
|
m_configPath = path;
|
2017-01-15 03:16:46 +01:00
|
|
|
std::string dummy;
|
2017-02-20 20:57:36 +01:00
|
|
|
m_parseResult =
|
2017-11-02 03:36:13 +01:00
|
|
|
parseConfig(ParseType::FILE, path.string(), ParseType::CONFIG, dummy);
|
2016-05-20 03:09:22 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 03:36:13 +01:00
|
|
|
rwfs::path GameConfig::getConfigPath() const {
|
|
|
|
return m_configPath;
|
2016-05-20 03:09:22 +02:00
|
|
|
}
|
|
|
|
|
2017-01-15 03:16:46 +01:00
|
|
|
bool GameConfig::isValid() const {
|
2017-02-18 07:33:53 +01:00
|
|
|
return m_parseResult.isValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
const GameConfig::ParseResult &GameConfig::getParseResult() const {
|
|
|
|
return m_parseResult;
|
2016-05-20 03:09:22 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 03:36:13 +01:00
|
|
|
rwfs::path GameConfig::getDefaultConfigPath() {
|
2016-09-09 22:13:20 +02:00
|
|
|
#if defined(RW_LINUX) || defined(RW_FREEBSD) || defined(RW_NETBSD) || \
|
|
|
|
defined(RW_OPENBSD)
|
2017-02-20 20:57:36 +01:00
|
|
|
char *config_home = getenv("XDG_CONFIG_HOME");
|
2016-09-09 22:13:20 +02:00
|
|
|
if (config_home != nullptr) {
|
2017-11-02 03:36:13 +01:00
|
|
|
return rwfs::path(config_home) / kConfigDirectoryName;
|
2016-09-09 22:13:20 +02:00
|
|
|
}
|
2017-02-20 20:57:36 +01:00
|
|
|
char *home = getenv("HOME");
|
2016-09-09 22:13:20 +02:00
|
|
|
if (home != nullptr) {
|
2017-11-02 03:36:13 +01:00
|
|
|
return rwfs::path(home) / ".config/" / kConfigDirectoryName;
|
2016-09-09 22:13:20 +02:00
|
|
|
}
|
2016-05-21 11:15:42 +02:00
|
|
|
|
2016-05-25 17:32:17 +02:00
|
|
|
#elif defined(RW_OSX)
|
2017-02-20 20:57:36 +01:00
|
|
|
char *home = getenv("HOME");
|
2016-09-09 22:13:20 +02:00
|
|
|
if (home)
|
2017-11-02 03:36:13 +01:00
|
|
|
return rwfs::path(home) / "Library/Preferences/" /
|
2016-09-09 22:13:20 +02:00
|
|
|
kConfigDirectoryName;
|
2016-05-21 11:15:42 +02:00
|
|
|
|
2016-05-20 03:09:22 +02:00
|
|
|
#else
|
2017-11-02 03:36:13 +01:00
|
|
|
return rwfs::path();
|
2016-05-20 03:09:22 +02:00
|
|
|
#endif
|
2016-05-21 11:15:42 +02:00
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
// Well now we're stuck.
|
|
|
|
RW_ERROR("No default config path found.");
|
2017-11-02 03:36:13 +01:00
|
|
|
return rwfs::path();
|
2016-05-20 03:09:22 +02:00
|
|
|
}
|
|
|
|
|
2017-01-15 03:16:46 +01:00
|
|
|
std::string stripComments(const std::string &str) {
|
2016-12-28 18:35:20 +01:00
|
|
|
auto s = std::string(str, 0, str.find_first_of(";#"));
|
2017-02-20 20:57:36 +01:00
|
|
|
return s.erase(s.find_last_not_of(" \n\r\t") + 1);
|
2016-12-28 18:35:20 +01:00
|
|
|
}
|
|
|
|
|
2017-11-02 03:36:13 +01:00
|
|
|
struct PathTranslator {
|
|
|
|
typedef std::string internal_type;
|
|
|
|
typedef rwfs::path external_type;
|
|
|
|
boost::optional<external_type> get_value(const internal_type &str) {
|
|
|
|
return rwfs::path(str);
|
|
|
|
}
|
|
|
|
boost::optional<internal_type> put_value(const external_type &path) {
|
|
|
|
return path.string();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-28 18:35:20 +01:00
|
|
|
struct StringTranslator {
|
|
|
|
typedef std::string internal_type;
|
|
|
|
typedef std::string external_type;
|
|
|
|
boost::optional<external_type> get_value(const internal_type &str) {
|
2017-02-17 19:19:02 +01:00
|
|
|
return stripComments(str);
|
2016-09-09 22:13:20 +02:00
|
|
|
}
|
2016-12-28 18:35:20 +01:00
|
|
|
boost::optional<internal_type> put_value(const external_type &str) {
|
2017-02-17 19:19:02 +01:00
|
|
|
return str;
|
2016-12-28 18:35:20 +01:00
|
|
|
}
|
|
|
|
};
|
2016-05-20 03:09:22 +02:00
|
|
|
|
2016-12-28 18:35:20 +01:00
|
|
|
struct BoolTranslator {
|
|
|
|
typedef std::string internal_type;
|
2017-02-20 20:57:36 +01:00
|
|
|
typedef bool external_type;
|
2016-12-28 18:35:20 +01:00
|
|
|
boost::optional<external_type> get_value(const internal_type &str) {
|
2017-02-17 19:19:02 +01:00
|
|
|
boost::optional<external_type> res;
|
|
|
|
try {
|
|
|
|
res = std::stoi(stripComments(str)) != 0;
|
|
|
|
} catch (std::invalid_argument &e) {
|
|
|
|
}
|
|
|
|
return res;
|
2016-12-28 18:35:20 +01:00
|
|
|
}
|
|
|
|
boost::optional<internal_type> put_value(const external_type &b) {
|
2017-02-17 19:19:02 +01:00
|
|
|
return internal_type(b ? "1" : "0");
|
2016-12-28 18:35:20 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-15 03:16:46 +01:00
|
|
|
struct IntTranslator {
|
|
|
|
typedef std::string internal_type;
|
2017-02-20 20:57:36 +01:00
|
|
|
typedef int external_type;
|
2017-01-15 03:16:46 +01:00
|
|
|
boost::optional<external_type> get_value(const internal_type &str) {
|
2017-02-17 19:19:02 +01:00
|
|
|
boost::optional<external_type> res;
|
|
|
|
try {
|
|
|
|
res = std::stoi(stripComments(str));
|
|
|
|
} catch (std::invalid_argument &e) {
|
|
|
|
}
|
|
|
|
return res;
|
2017-01-15 03:16:46 +01:00
|
|
|
}
|
|
|
|
boost::optional<internal_type> put_value(const external_type &i) {
|
2017-02-18 02:35:28 +01:00
|
|
|
return std::to_string(i);
|
2017-01-15 03:16:46 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-18 07:33:53 +01:00
|
|
|
GameConfig::ParseResult GameConfig::saveConfig() {
|
2017-11-02 03:36:13 +01:00
|
|
|
auto configPath = getConfigPath().string();
|
|
|
|
return parseConfig(ParseType::CONFIG, "", ParseType::FILE, configPath);
|
2017-02-17 01:58:49 +01:00
|
|
|
}
|
|
|
|
|
2017-01-15 03:16:46 +01:00
|
|
|
std::string GameConfig::getDefaultINIString() {
|
|
|
|
std::string result;
|
|
|
|
parseConfig(ParseType::DEFAULT, "", ParseType::STRING, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-02-20 20:57:36 +01:00
|
|
|
GameConfig::ParseResult GameConfig::parseConfig(GameConfig::ParseType srcType,
|
|
|
|
const std::string &source,
|
|
|
|
ParseType destType,
|
|
|
|
std::string &destination) {
|
|
|
|
// srcTree: holds all key/value pairs
|
2017-01-15 03:16:46 +01:00
|
|
|
pt::ptree srcTree;
|
2017-02-18 16:46:40 +01:00
|
|
|
ParseResult parseResult(srcType, source, destType, destination);
|
2017-01-15 03:16:46 +01:00
|
|
|
|
2017-02-17 14:00:02 +01:00
|
|
|
try {
|
|
|
|
if (srcType == ParseType::STRING) {
|
2017-02-18 23:06:56 +01:00
|
|
|
std::istringstream iss(source);
|
|
|
|
pt::read_ini(iss, srcTree);
|
2017-02-17 14:00:02 +01:00
|
|
|
} else if (srcType == ParseType::FILE) {
|
2017-02-18 07:33:53 +01:00
|
|
|
pt::read_ini(source, srcTree);
|
2017-01-15 03:16:46 +01:00
|
|
|
}
|
2017-02-17 14:00:02 +01:00
|
|
|
} catch (pt::ini_parser_error &e) {
|
2017-02-17 19:19:02 +01:00
|
|
|
// Catches illegal input files (nonsensical input, duplicate keys)
|
2017-02-18 16:46:40 +01:00
|
|
|
parseResult.failInputFile(e.line(), e.message());
|
2017-02-17 14:00:02 +01:00
|
|
|
RW_MESSAGE(e.what());
|
2017-02-18 07:33:53 +01:00
|
|
|
return parseResult;
|
2017-01-15 03:16:46 +01:00
|
|
|
}
|
2016-05-20 03:09:22 +02:00
|
|
|
|
2017-02-17 19:19:02 +01:00
|
|
|
if (destType == ParseType::DEFAULT) {
|
2017-02-18 07:33:53 +01:00
|
|
|
parseResult.failArgument();
|
2017-02-17 19:19:02 +01:00
|
|
|
RW_ERROR("Target cannot be DEFAULT.");
|
2017-02-18 07:33:53 +01:00
|
|
|
return parseResult;
|
2017-02-17 19:19:02 +01:00
|
|
|
}
|
2017-02-18 07:33:53 +01:00
|
|
|
|
2017-02-20 20:57:36 +01:00
|
|
|
// knownKeys: holds all known keys
|
|
|
|
std::vector<std::string> knownKeys;
|
|
|
|
|
2016-12-28 18:35:20 +01:00
|
|
|
auto read_config = [&](const std::string &key, auto &target,
|
2017-02-20 20:57:36 +01:00
|
|
|
const auto &defaultValue, auto &translator,
|
|
|
|
bool optional = true) {
|
2017-01-15 03:16:46 +01:00
|
|
|
typedef typename std::remove_reference<decltype(target)>::type config_t;
|
|
|
|
|
|
|
|
config_t sourceValue;
|
2017-02-20 20:57:36 +01:00
|
|
|
knownKeys.push_back(key);
|
2017-01-15 03:16:46 +01:00
|
|
|
|
|
|
|
switch (srcType) {
|
|
|
|
case ParseType::DEFAULT:
|
|
|
|
sourceValue = defaultValue;
|
|
|
|
break;
|
|
|
|
case ParseType::CONFIG:
|
|
|
|
sourceValue = target;
|
|
|
|
break;
|
|
|
|
case ParseType::FILE:
|
|
|
|
case ParseType::STRING:
|
|
|
|
try {
|
|
|
|
sourceValue = srcTree.get<config_t>(key, translator);
|
|
|
|
} catch (pt::ptree_bad_path &e) {
|
2017-02-17 19:19:02 +01:00
|
|
|
// Catches missing key-value pairs: fail when required
|
|
|
|
if (!optional) {
|
2017-02-18 07:33:53 +01:00
|
|
|
parseResult.failRequiredMissing(key);
|
|
|
|
RW_MESSAGE(e.what());
|
|
|
|
return;
|
2017-01-15 03:16:46 +01:00
|
|
|
}
|
2017-02-17 19:19:02 +01:00
|
|
|
sourceValue = defaultValue;
|
|
|
|
} catch (pt::ptree_bad_data &e) {
|
|
|
|
// Catches illegal value data: always fail
|
2017-02-18 07:33:53 +01:00
|
|
|
parseResult.failInvalidData(key);
|
2017-02-17 19:19:02 +01:00
|
|
|
RW_MESSAGE(e.what());
|
|
|
|
return;
|
2017-01-15 03:16:46 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
srcTree.put(key, sourceValue, translator);
|
|
|
|
|
|
|
|
switch (destType) {
|
|
|
|
case ParseType::DEFAULT:
|
2017-02-17 19:19:02 +01:00
|
|
|
// Target cannot be DEFAULT (case already handled)
|
2017-02-18 07:33:53 +01:00
|
|
|
parseResult.failArgument();
|
2017-01-15 03:16:46 +01:00
|
|
|
break;
|
|
|
|
case ParseType::CONFIG:
|
2017-02-18 02:35:28 +01:00
|
|
|
// Don't care if success == false
|
2017-01-15 03:16:46 +01:00
|
|
|
target = sourceValue;
|
|
|
|
break;
|
|
|
|
case ParseType::FILE:
|
|
|
|
case ParseType::STRING:
|
|
|
|
break;
|
2016-12-28 18:35:20 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto deft = StringTranslator();
|
|
|
|
auto boolt = BoolTranslator();
|
2017-11-02 03:36:13 +01:00
|
|
|
auto patht = PathTranslator();
|
2016-12-28 18:35:20 +01:00
|
|
|
|
2017-02-17 01:17:50 +01:00
|
|
|
// Add new configuration parameters here.
|
|
|
|
// Additionally, add them to the unit test.
|
2017-01-15 03:16:46 +01:00
|
|
|
|
|
|
|
// @todo Don't allow path separators and relative directories
|
2017-02-20 20:57:36 +01:00
|
|
|
read_config("game.path", this->m_gamePath, "/opt/games/Grand Theft Auto 3",
|
2017-11-02 03:36:13 +01:00
|
|
|
patht, false);
|
2016-12-28 18:35:20 +01:00
|
|
|
read_config("game.language", this->m_gameLanguage, "american", deft);
|
|
|
|
|
|
|
|
read_config("input.invert_y", this->m_inputInvertY, false, boolt);
|
|
|
|
|
2017-02-20 20:57:36 +01:00
|
|
|
// Build the unknown key/value map from the correct source
|
|
|
|
switch (srcType) {
|
|
|
|
case ParseType::FILE:
|
|
|
|
case ParseType::STRING:
|
|
|
|
for (const auto §ion : srcTree) {
|
|
|
|
for (const auto &subKey : section.second) {
|
|
|
|
std::string key = section.first + "." + subKey.first;
|
|
|
|
if (std::find(knownKeys.begin(), knownKeys.end(), key) ==
|
|
|
|
knownKeys.end()) {
|
|
|
|
RW_MESSAGE("Unknown configuration key: " << key);
|
|
|
|
parseResult.addUnknownData(key, subKey.second.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ParseType::CONFIG:
|
|
|
|
parseResult.setUnknownData(m_parseResult.getUnknownData());
|
|
|
|
break;
|
|
|
|
case ParseType::DEFAULT:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the unknown key/value map to the correct destination
|
|
|
|
switch (destType) {
|
|
|
|
case ParseType::CONFIG:
|
|
|
|
m_parseResult.setUnknownData(parseResult.getUnknownData());
|
|
|
|
break;
|
|
|
|
case ParseType::STRING:
|
|
|
|
case ParseType::FILE:
|
|
|
|
for (const auto &keyvalue : parseResult.getUnknownData()) {
|
|
|
|
srcTree.put(keyvalue.first, keyvalue.second);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-02-17 19:19:02 +01:00
|
|
|
|
2017-04-22 03:08:15 +02:00
|
|
|
if (!parseResult.isValid()) return parseResult;
|
|
|
|
|
2017-02-17 19:19:02 +01:00
|
|
|
try {
|
|
|
|
if (destType == ParseType::STRING) {
|
|
|
|
std::ostringstream ostream;
|
2017-01-15 03:16:46 +01:00
|
|
|
pt::write_ini(ostream, srcTree);
|
2017-02-17 19:19:02 +01:00
|
|
|
destination = ostream.str();
|
|
|
|
} else if (destType == ParseType::FILE) {
|
|
|
|
pt::write_ini(destination, srcTree);
|
2017-01-15 03:16:46 +01:00
|
|
|
}
|
2017-02-17 19:19:02 +01:00
|
|
|
} catch (pt::ini_parser_error &e) {
|
2017-02-18 16:46:40 +01:00
|
|
|
parseResult.failOutputFile(e.line(), e.message());
|
2017-02-17 19:19:02 +01:00
|
|
|
RW_MESSAGE(e.what());
|
2016-12-28 18:35:20 +01:00
|
|
|
}
|
|
|
|
|
2017-11-02 03:36:13 +01:00
|
|
|
if (parseResult.type() == ParseResult::ErrorType::UNINITIALIZED) {
|
|
|
|
parseResult.markGood();
|
|
|
|
}
|
|
|
|
|
2017-02-18 07:33:53 +01:00
|
|
|
return parseResult;
|
2016-05-20 03:09:22 +02:00
|
|
|
}
|
2017-02-18 07:33:53 +01:00
|
|
|
|
2017-02-20 20:57:36 +01:00
|
|
|
std::string GameConfig::extractFilenameParseTypeData(ParseType type,
|
|
|
|
const std::string &data) {
|
2017-02-18 16:46:40 +01:00
|
|
|
switch (type) {
|
|
|
|
case ParseType::CONFIG:
|
|
|
|
return "<configuration>";
|
|
|
|
case ParseType::FILE:
|
|
|
|
return data;
|
|
|
|
case ParseType::STRING:
|
|
|
|
return "<string>";
|
|
|
|
case ParseType::DEFAULT:
|
|
|
|
default:
|
|
|
|
return "<default>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 20:57:36 +01:00
|
|
|
GameConfig::ParseResult::ParseResult(GameConfig::ParseType srcType,
|
|
|
|
const std::string &source,
|
|
|
|
GameConfig::ParseType destType,
|
|
|
|
const std::string &destination)
|
2017-02-18 16:46:40 +01:00
|
|
|
: m_result(ErrorType::GOOD)
|
|
|
|
, m_inputfilename(GameConfig::extractFilenameParseTypeData(srcType, source))
|
2017-02-20 20:57:36 +01:00
|
|
|
, m_outputfilename(
|
|
|
|
GameConfig::extractFilenameParseTypeData(destType, destination))
|
2017-02-18 16:46:40 +01:00
|
|
|
, m_line(0)
|
|
|
|
, m_message()
|
|
|
|
, m_keys_requiredMissing()
|
2017-02-20 20:57:36 +01:00
|
|
|
, m_keys_invalidData()
|
|
|
|
, m_unknownData() {
|
2017-02-18 16:46:40 +01:00
|
|
|
}
|
|
|
|
|
2017-02-18 07:33:53 +01:00
|
|
|
GameConfig::ParseResult::ParseResult()
|
2017-11-02 03:36:13 +01:00
|
|
|
: m_result(ErrorType::UNINITIALIZED)
|
2017-02-18 16:46:40 +01:00
|
|
|
, m_inputfilename()
|
|
|
|
, m_outputfilename()
|
2017-02-18 07:33:53 +01:00
|
|
|
, m_line(0)
|
|
|
|
, m_message()
|
|
|
|
, m_keys_requiredMissing()
|
|
|
|
, m_keys_invalidData() {
|
|
|
|
}
|
|
|
|
|
|
|
|
GameConfig::ParseResult::ErrorType GameConfig::ParseResult::type() const {
|
|
|
|
return this->m_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GameConfig::ParseResult::isValid() const {
|
|
|
|
return this->type() == ErrorType::GOOD;
|
|
|
|
}
|
|
|
|
|
2017-02-18 16:46:40 +01:00
|
|
|
void GameConfig::ParseResult::failInputFile(size_t line,
|
2017-02-20 20:57:36 +01:00
|
|
|
const std::string &message) {
|
2017-02-18 07:33:53 +01:00
|
|
|
this->m_result = ParseResult::ErrorType::INVALIDINPUTFILE;
|
|
|
|
this->m_line = line;
|
|
|
|
this->m_message = message;
|
|
|
|
}
|
|
|
|
|
2017-11-02 03:36:13 +01:00
|
|
|
void GameConfig::ParseResult::markGood() {
|
|
|
|
this-> m_result = ParseResult::ErrorType::GOOD;
|
|
|
|
}
|
|
|
|
|
2017-02-18 07:33:53 +01:00
|
|
|
void GameConfig::ParseResult::failArgument() {
|
|
|
|
this->m_result = ParseResult::ErrorType::INVALIDARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameConfig::ParseResult::failRequiredMissing(const std::string &key) {
|
|
|
|
this->m_result = ParseResult::ErrorType::INVALIDCONTENT;
|
|
|
|
this->m_keys_requiredMissing.push_back(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameConfig::ParseResult::failInvalidData(const std::string &key) {
|
|
|
|
this->m_result = ParseResult::ErrorType::INVALIDCONTENT;
|
|
|
|
this->m_keys_invalidData.push_back(key);
|
|
|
|
}
|
|
|
|
|
2017-02-18 16:46:40 +01:00
|
|
|
void GameConfig::ParseResult::failOutputFile(size_t line,
|
2017-02-20 20:57:36 +01:00
|
|
|
const std::string &message) {
|
2017-02-18 07:33:53 +01:00
|
|
|
this->m_result = ParseResult::ErrorType::INVALIDOUTPUTFILE;
|
|
|
|
this->m_line = line;
|
|
|
|
this->m_message = message;
|
|
|
|
}
|
|
|
|
|
2017-02-20 20:57:36 +01:00
|
|
|
const std::vector<std::string>
|
|
|
|
&GameConfig::ParseResult::getKeysRequiredMissing() const {
|
2017-02-18 07:33:53 +01:00
|
|
|
return this->m_keys_requiredMissing;
|
|
|
|
}
|
|
|
|
|
2017-02-20 20:57:36 +01:00
|
|
|
const std::vector<std::string> &GameConfig::ParseResult::getKeysInvalidData()
|
|
|
|
const {
|
2017-02-18 07:33:53 +01:00
|
|
|
return this->m_keys_invalidData;
|
|
|
|
}
|
|
|
|
|
2017-02-18 16:46:40 +01:00
|
|
|
std::string GameConfig::ParseResult::what() const {
|
2017-04-22 03:08:15 +02:00
|
|
|
std::ostringstream oss;
|
2017-02-18 16:46:40 +01:00
|
|
|
switch (this->m_result) {
|
2017-11-02 03:36:13 +01:00
|
|
|
case ErrorType::UNINITIALIZED:
|
|
|
|
oss << "Parsing was skipped or did not finish.";
|
|
|
|
break;
|
2017-02-18 16:46:40 +01:00
|
|
|
case ErrorType::GOOD:
|
2017-04-22 03:08:15 +02:00
|
|
|
oss << "Parsing completed without errors.";
|
|
|
|
break;
|
2017-02-18 16:46:40 +01:00
|
|
|
case ErrorType::INVALIDARGUMENT:
|
2017-04-22 03:08:15 +02:00
|
|
|
oss << "Invalid argument: destination cannot be the default "
|
|
|
|
"config.";
|
|
|
|
break;
|
|
|
|
case ErrorType::INVALIDINPUTFILE:
|
2017-02-20 20:57:36 +01:00
|
|
|
oss << "Error while reading \"" << this->m_inputfilename
|
|
|
|
<< "\":" << this->m_line << ":\n"
|
2017-04-22 03:08:15 +02:00
|
|
|
<< this->m_message << ".";
|
|
|
|
break;
|
|
|
|
case ErrorType::INVALIDOUTPUTFILE:
|
2017-02-20 20:57:36 +01:00
|
|
|
oss << "Error while writing \"" << this->m_inputfilename
|
|
|
|
<< "\":" << this->m_line << ":\n"
|
2017-04-22 03:08:15 +02:00
|
|
|
<< this->m_message << ".";
|
|
|
|
break;
|
|
|
|
case ErrorType::INVALIDCONTENT:
|
2017-02-20 20:57:36 +01:00
|
|
|
oss << "Error while parsing \"" << this->m_inputfilename << "\".";
|
2017-11-08 17:41:55 +01:00
|
|
|
if (!this->m_keys_requiredMissing.empty()) {
|
2017-02-18 16:46:40 +01:00
|
|
|
oss << "\nRequired keys that are missing:";
|
|
|
|
for (auto &key : this->m_keys_requiredMissing) {
|
|
|
|
oss << "\n - " << key;
|
|
|
|
}
|
|
|
|
}
|
2017-11-08 17:41:55 +01:00
|
|
|
if (!this->m_keys_invalidData.empty()) {
|
2017-02-18 16:46:40 +01:00
|
|
|
oss << "\nKeys that contain invalid data:";
|
|
|
|
for (auto &key : this->m_keys_invalidData) {
|
|
|
|
oss << "\n - " << key;
|
|
|
|
}
|
|
|
|
}
|
2017-04-22 03:08:15 +02:00
|
|
|
break;
|
2017-02-18 16:46:40 +01:00
|
|
|
default:
|
2017-04-22 03:08:15 +02:00
|
|
|
oss << "Unknown error.";
|
|
|
|
break;
|
|
|
|
}
|
2017-11-08 17:41:55 +01:00
|
|
|
if (!this->m_unknownData.empty()) {
|
2017-04-22 03:08:15 +02:00
|
|
|
oss << "\nUnknown configuration keys:";
|
|
|
|
for (const auto &keyvalue : m_unknownData) {
|
|
|
|
oss << "\n - " << keyvalue.first;
|
|
|
|
}
|
2017-02-18 16:46:40 +01:00
|
|
|
}
|
2017-04-22 03:08:15 +02:00
|
|
|
return oss.str();
|
2017-02-18 16:46:40 +01:00
|
|
|
}
|
2017-02-20 20:57:36 +01:00
|
|
|
|
|
|
|
void GameConfig::ParseResult::addUnknownData(const std::string &key,
|
|
|
|
const std::string &value) {
|
|
|
|
this->m_unknownData[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::map<std::string, std::string>
|
|
|
|
&GameConfig::ParseResult::getUnknownData() const {
|
|
|
|
return this->m_unknownData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameConfig::ParseResult::setUnknownData(
|
|
|
|
const std::map<std::string, std::string> &unknownData) {
|
|
|
|
this->m_unknownData = unknownData;
|
|
|
|
}
|