Config: Handle unknown system strings better
Currently, a simple typo in the system name will trigger an assert message that complains about a "programming error". This is not user friendly and misleading. So this changes GetSystemFromName to return an std::optional, which allows for callers to check whether the system exists and handle failures better.
This commit is contained in:
parent
653977cec7
commit
05c8d229af
|
@ -101,15 +101,14 @@ const std::string& GetSystemName(System system)
|
||||||
return system_to_name.at(system);
|
return system_to_name.at(system);
|
||||||
}
|
}
|
||||||
|
|
||||||
System GetSystemFromName(const std::string& name)
|
std::optional<System> GetSystemFromName(const std::string& name)
|
||||||
{
|
{
|
||||||
const auto system = std::find_if(system_to_name.begin(), system_to_name.end(),
|
const auto system = std::find_if(system_to_name.begin(), system_to_name.end(),
|
||||||
[&name](const auto& entry) { return entry.second == name; });
|
[&name](const auto& entry) { return entry.second == name; });
|
||||||
if (system != system_to_name.end())
|
if (system != system_to_name.end())
|
||||||
return system->first;
|
return system->first;
|
||||||
|
|
||||||
_assert_msg_(COMMON, false, "Programming error! Couldn't convert '%s' to system!", name.c_str());
|
return {};
|
||||||
return System::Main;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& GetLayerName(LayerType layer)
|
const std::string& GetLayerName(LayerType layer)
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "Common/Config/ConfigInfo.h"
|
#include "Common/Config/ConfigInfo.h"
|
||||||
|
@ -38,7 +39,7 @@ void Shutdown();
|
||||||
void ClearCurrentRunLayer();
|
void ClearCurrentRunLayer();
|
||||||
|
|
||||||
const std::string& GetSystemName(System system);
|
const std::string& GetSystemName(System system);
|
||||||
System GetSystemFromName(const std::string& system);
|
std::optional<System> GetSystemFromName(const std::string& system);
|
||||||
const std::string& GetLayerName(LayerType layer);
|
const std::string& GetLayerName(LayerType layer);
|
||||||
LayerType GetActiveLayerForConfig(const ConfigLocation&);
|
LayerType GetActiveLayerForConfig(const ConfigLocation&);
|
||||||
|
|
||||||
|
|
|
@ -157,8 +157,9 @@ static ConfigLocation MapINIToRealLocation(const std::string& section, const std
|
||||||
std::getline(buffer, config_section, '.');
|
std::getline(buffer, config_section, '.');
|
||||||
fail |= buffer.fail();
|
fail |= buffer.fail();
|
||||||
|
|
||||||
if (!fail)
|
const std::optional<Config::System> system = Config::GetSystemFromName(system_str);
|
||||||
return {Config::GetSystemFromName(system_str), config_section, key};
|
if (!fail && system)
|
||||||
|
return {*system, config_section, key};
|
||||||
|
|
||||||
WARN_LOG(CORE, "Unknown game INI option in section %s: %s", section.c_str(), key.c_str());
|
WARN_LOG(CORE, "Unknown game INI option in section %s: %s", section.c_str(), key.c_str());
|
||||||
return {Config::System::Main, "", ""};
|
return {Config::System::Main, "", ""};
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
// Refer to the license.txt file included.
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <optional>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
@ -40,8 +41,12 @@ public:
|
||||||
std::getline(buffer, section, '.');
|
std::getline(buffer, section, '.');
|
||||||
std::getline(buffer, key, '=');
|
std::getline(buffer, key, '=');
|
||||||
std::getline(buffer, value, '=');
|
std::getline(buffer, value, '=');
|
||||||
Config::System system = Config::GetSystemFromName(system_str);
|
const std::optional<Config::System> system = Config::GetSystemFromName(system_str);
|
||||||
m_values.emplace_back(std::make_tuple(Config::ConfigLocation{system, section, key}, value));
|
if (system)
|
||||||
|
{
|
||||||
|
m_values.emplace_back(
|
||||||
|
std::make_tuple(Config::ConfigLocation{*system, section, key}, value));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue