diff --git a/src/openrct2/config/IniReader.cpp b/src/openrct2/config/IniReader.cpp index e3e0bbe588..ce9c67d900 100644 --- a/src/openrct2/config/IniReader.cpp +++ b/src/openrct2/config/IniReader.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -7,9 +8,54 @@ #include "../core/String.hpp" #include "../core/StringBuilder.hpp" +template +struct ConfigEnumEntry +{ + std::string Key; + T Value; + + ConfigEnumEntry(const std::string &key, T value) + : Key(key), + Value(value) + { + } +}; + +template class ConfigEnum { +private: + std::vector> _entries; +public: + ConfigEnum(std::initializer_list> entries) + { + _entries = entries; + } + + std::string GetName(T value) + { + for (const auto &entry : _entries) const + { + if (entry.Value == value) + { + return entry.Key; + } + } + return std::string(); + } + + T GetValue(const std::string &key, T defaultValue) const + { + for (const auto &entry : _entries) + { + if (String::Equals(entry.Key, key, true)) + { + return entry.Value; + } + } + return defaultValue; + } }; struct Span @@ -91,7 +137,7 @@ public: return String::Equals(value, "true", true); } - sint32 GetSint32(const std::string &name, sint32 defaultValue, const ConfigEnum * configEnum = nullptr) + sint32 GetSint32(const std::string &name, sint32 defaultValue) { auto it = _values.find(name); if (it == _values.end()) @@ -103,6 +149,18 @@ public: return std::stoi(value); } + template + T GetEnum(const std::string &name, T defaultValue, const ConfigEnum &configEnum) + { + auto it = _values.find(name); + if (it == _values.end()) + { + return defaultValue; + } + + return configEnum.GetValue(it->second, defaultValue); + } + std::string GetString(const std::string &name, const std::string &defaultValue) { auto it = _values.find(name); @@ -290,6 +348,13 @@ extern "C" { #include "../config.h" + auto Enum_MeasurementFormat = ConfigEnum( + { + ConfigEnumEntry("IMPERIAL", MEASUREMENT_FORMAT_IMPERIAL), + ConfigEnumEntry("METRIC", MEASUREMENT_FORMAT_METRIC), + ConfigEnumEntry("SI", MEASUREMENT_FORMAT_SI), + }); + bool config_open(const utf8 * path) { try @@ -300,6 +365,7 @@ extern "C" gConfigGeneral.always_show_gridlines = iniReader.GetBoolean("always_show_gridlines", false); gConfigGeneral.window_width = iniReader.GetSint32("window_width", -1); gConfigGeneral.window_height = iniReader.GetSint32("window_height", -1); + gConfigGeneral.measurement_format = iniReader.GetEnum("measurement_format", MEASUREMENT_FORMAT_METRIC, Enum_MeasurementFormat); } return true; }