1
0
mirror of synced 2025-01-18 00:56:49 +01:00

api: Fix various crashes on bad settings data. (#186)

getSetting now returns a straight nlohmann::json instead of an optional.
If the data isn't present, it will return a json null.

All accesses to the settings will first check that the data has the
expected type.
This commit is contained in:
Robin Lambertz 2021-03-06 12:40:29 +01:00 committed by GitHub
parent 32cfaac1e2
commit 4f98149fa7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 11 additions and 11 deletions

View File

@ -5,7 +5,7 @@ namespace hex::plugin::builtin {
void registerSettings() {
ContentRegistry::Settings::add("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", 0, [](auto name, nlohmann::json &setting) {
static int selection = setting;
static int selection = setting.is_number() ? static_cast<int>(setting) : 0;
const char* themes[] = {
"hex.builtin.setting.interface.color.dark"_lang,

View File

@ -51,7 +51,7 @@ namespace hex {
static std::vector<std::string> read(std::string_view unlocalizedCategory, std::string_view unlocalizedName, const std::vector<std::string>& defaultValue = { });
static std::map<std::string, std::vector<Entry>>& getEntries();
static std::optional<nlohmann::json> getSetting(std::string_view unlocalizedCategory, std::string_view unlocalizedName);
static nlohmann::json getSetting(std::string_view unlocalizedCategory, std::string_view unlocalizedName);
static nlohmann::json& getSettingsData();
};

View File

@ -119,7 +119,7 @@ namespace hex {
return SharedData::settingsEntries;
}
std::optional<nlohmann::json> ContentRegistry::Settings::getSetting(std::string_view unlocalizedCategory, std::string_view unlocalizedName) {
nlohmann::json ContentRegistry::Settings::getSetting(std::string_view unlocalizedCategory, std::string_view unlocalizedName) {
auto &settings = getSettingsData();
if (!settings.contains(unlocalizedCategory)) return { };

View File

@ -20,8 +20,8 @@ namespace hex {
View::subscribeEvent(Events::SettingsChanged, [](auto) {
auto theme = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color");
if (theme.has_value()) {
switch (static_cast<int>(theme.value())) {
if (theme.is_number()) {
switch (static_cast<int>(theme)) {
default:
case 0: /* Dark theme */
imnodes::StyleColorsDark();

View File

@ -182,8 +182,8 @@ namespace hex {
View::subscribeEvent(Events::SettingsChanged, [this](auto) {
auto theme = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color");
if (theme.has_value()) {
switch (static_cast<int>(theme.value())) {
if (theme.is_number()) {
switch (static_cast<int>(theme)) {
default:
case 0: /* Dark theme */
this->m_textEditor.SetPalette(TextEditor::GetDarkPalette());

View File

@ -61,8 +61,8 @@ namespace hex {
{
auto theme = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color");
if (theme.has_value()) {
switch (static_cast<int>(theme.value())) {
if (theme.is_number()) {
switch (static_cast<int>(theme)) {
default:
case 0: /* Dark theme */
ImGui::StyleColorsDark();
@ -87,8 +87,8 @@ namespace hex {
{
auto language = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.language");
if (language.has_value())
LangEntry::loadLanguage(static_cast<std::string>(language.value()));
if (language.is_string())
LangEntry::loadLanguage(static_cast<std::string>(language));
}
return { };