diff --git a/src/config/configmanager.cpp b/src/config/configmanager.cpp index 228b93426b7..e0eefc98298 100644 --- a/src/config/configmanager.cpp +++ b/src/config/configmanager.cpp @@ -373,6 +373,10 @@ bool ConfigManager::load() { } bool ConfigManager::reload() { + m_configString.clear(); + m_configInteger.clear(); + m_configBoolean.clear(); + m_configFloat.clear(); const bool result = load(); if (transformToSHA1(getString(SERVER_MOTD)) != g_game().getMotdHash()) { g_game().incrementMotdNum(); @@ -437,34 +441,77 @@ float ConfigManager::loadFloatConfig(lua_State* L, const ConfigKey_t &key, const } const std::string &ConfigManager::getString(const ConfigKey_t &key, const std::source_location &location /*= std::source_location::current()*/) const { - static const std::string dummyStr; - if (configs.contains(key) && std::holds_alternative(configs.at(key))) { - return std::get(configs.at(key)); + auto itCache = m_configString.find(key); + if (itCache != m_configString.end()) { + return itCache->second; } + + auto it = configs.find(key); + if (it != configs.end()) { + if (const auto* value = std::get_if(&it->second)) { + m_configString[key] = *value; + return *value; + } + } + + static const std::string staticEmptyString; g_logger().warn("[{}] accessing invalid or wrong type index: {}[{}]. Called line: {}:{}, in {}", __FUNCTION__, magic_enum::enum_name(key), fmt::underlying(key), location.line(), location.column(), location.function_name()); - return dummyStr; + return staticEmptyString; } int32_t ConfigManager::getNumber(const ConfigKey_t &key, const std::source_location &location /*= std::source_location::current()*/) const { - if (configs.contains(key) && std::holds_alternative(configs.at(key))) { - return std::get(configs.at(key)); + auto itCache = m_configInteger.find(key); + if (itCache != m_configInteger.end()) { + return itCache->second; } + + auto it = configs.find(key); + if (it != configs.end()) { + if (std::holds_alternative(it->second)) { + const auto value = std::get(it->second); + m_configInteger[key] = value; + return value; + } + } + g_logger().warn("[{}] accessing invalid or wrong type index: {}[{}]. Called line: {}:{}, in {}", __FUNCTION__, magic_enum::enum_name(key), fmt::underlying(key), location.line(), location.column(), location.function_name()); return 0; } bool ConfigManager::getBoolean(const ConfigKey_t &key, const std::source_location &location /*= std::source_location::current()*/) const { - if (configs.contains(key) && std::holds_alternative(configs.at(key))) { - return std::get(configs.at(key)); + auto itCache = m_configBoolean.find(key); + if (itCache != m_configBoolean.end()) { + return itCache->second; } + + auto it = configs.find(key); + if (it != configs.end()) { + if (std::holds_alternative(it->second)) { + const auto value = std::get(it->second); + m_configBoolean[key] = value; + return value; + } + } + g_logger().warn("[{}] accessing invalid or wrong type index: {}[{}]. Called line: {}:{}, in {}", __FUNCTION__, magic_enum::enum_name(key), fmt::underlying(key), location.line(), location.column(), location.function_name()); return false; } float ConfigManager::getFloat(const ConfigKey_t &key, const std::source_location &location /*= std::source_location::current()*/) const { - if (configs.contains(key) && std::holds_alternative(configs.at(key))) { - return std::get(configs.at(key)); + auto itCache = m_configFloat.find(key); + if (itCache != m_configFloat.end()) { + return itCache->second; } + + auto it = configs.find(key); + if (it != configs.end()) { + if (std::holds_alternative(it->second)) { + const auto value = std::get(it->second); + m_configFloat[key] = value; + return value; + } + } + g_logger().warn("[{}] accessing invalid or wrong type index: {}[{}]. Called line: {}:{}, in {}", __FUNCTION__, magic_enum::enum_name(key), fmt::underlying(key), location.line(), location.column(), location.function_name()); return 0.0f; } diff --git a/src/config/configmanager.hpp b/src/config/configmanager.hpp index ddc15e45ed3..51c23dfe0dc 100644 --- a/src/config/configmanager.hpp +++ b/src/config/configmanager.hpp @@ -42,7 +42,12 @@ class ConfigManager { [[nodiscard]] float getFloat(const ConfigKey_t &key, const std::source_location &location = std::source_location::current()) const; private: - phmap::flat_hash_map configs; + mutable std::unordered_map m_configString; + mutable std::unordered_map m_configBoolean; + mutable std::unordered_map m_configInteger; + mutable std::unordered_map m_configFloat; + + std::unordered_map configs; std::string loadStringConfig(lua_State* L, const ConfigKey_t &key, const char* identifier, const std::string &defaultValue); int32_t loadIntConfig(lua_State* L, const ConfigKey_t &key, const char* identifier, const int32_t &defaultValue); bool loadBoolConfig(lua_State* L, const ConfigKey_t &key, const char* identifier, const bool &defaultValue);