impr: Harden settings system to not crash ImHex when having outdated configs

Fixes #1514
This commit is contained in:
WerWolv
2024-02-03 12:16:36 +01:00
parent 2ea0bbe5ca
commit a03e8dd879
22 changed files with 106 additions and 93 deletions

View File

@@ -277,6 +277,8 @@ namespace hex {
nlohmann::json &getSettingsData();
Widgets::Widget* add(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedSubCategory, const UnlocalizedString &unlocalizedName, std::unique_ptr<Widgets::Widget> &&widget);
void printSettingReadError(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedName, const nlohmann::json::exception &e);
}
template<std::derived_from<Widgets::Widget> T>
@@ -291,8 +293,29 @@ namespace hex {
void setCategoryDescription(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedDescription);
[[nodiscard]] nlohmann::json read(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedName, const nlohmann::json &defaultValue);
void write(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedName, const nlohmann::json &value);
template<typename T>
[[nodiscard]] T read(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedName, const std::common_type_t<T> &defaultValue) {
auto setting = impl::getSetting(unlocalizedCategory, unlocalizedName, defaultValue);
try {
if (setting.is_number() && std::same_as<T, bool>)
setting = setting.template get<int>() != 0;
if (setting.is_null())
setting = defaultValue;
return setting.template get<T>();
} catch (const nlohmann::json::exception &e) {
impl::printSettingReadError(unlocalizedCategory, unlocalizedName, e);
return defaultValue;
}
}
template<typename T>
void write(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedName, const std::common_type_t<T> &value) {
impl::getSetting(unlocalizedCategory, unlocalizedName, value) = value;
}
}
/* Command Palette Command Registry. Allows adding of new commands to the command palette */

View File

@@ -155,6 +155,11 @@ namespace hex {
return entry->widget.get();
}
void printSettingReadError(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedName, const nlohmann::json::exception& e) {
hex::log::error("Failed to read setting {}/{}: {}", unlocalizedCategory.get(), unlocalizedName.get(), e.what());
}
}
void setCategoryDescription(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedDescription) {
@@ -163,21 +168,6 @@ namespace hex {
category->unlocalizedDescription = unlocalizedDescription;
}
nlohmann::json read(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedName, const nlohmann::json &defaultValue) {
auto setting = impl::getSetting(unlocalizedCategory, unlocalizedName, defaultValue);
if (setting.is_number() && defaultValue.is_boolean())
setting = setting.get<int>() != 0;
if (setting.is_null())
setting = defaultValue;
return setting;
}
void write(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedName, const nlohmann::json &value) {
impl::getSetting(unlocalizedCategory, unlocalizedName, value) = value;
}
namespace Widgets {
bool Checkbox::draw(const std::string &name) {