From 6b22464771889db935cfd119ad66e456702e57a8 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 16 Dec 2021 10:07:31 +0100 Subject: [PATCH] fix: Loading a project crashes imhex --- .../content/views/view_data_processor.cpp | 6 +++++- .../hex/helpers/project_file_handler.hpp | 14 ++++++------- .../source/helpers/project_file_handler.cpp | 21 +++++++++++++------ 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/plugins/builtin/source/content/views/view_data_processor.cpp b/plugins/builtin/source/content/views/view_data_processor.cpp index 92a62636c..5e08d1ae8 100644 --- a/plugins/builtin/source/content/views/view_data_processor.cpp +++ b/plugins/builtin/source/content/views/view_data_processor.cpp @@ -31,7 +31,11 @@ namespace hex::plugin::builtin { }); EventManager::subscribe(this, [this] { - this->loadNodes(ProjectFile::getDataProcessorContent()); + try { + this->loadNodes(ProjectFile::getDataProcessorContent()); + } catch (nlohmann::json::exception &e) { + + } }); EventManager::subscribe(this, [this](const std::string &path){ diff --git a/plugins/libimhex/include/hex/helpers/project_file_handler.hpp b/plugins/libimhex/include/hex/helpers/project_file_handler.hpp index 9be4c7c84..d7cd44414 100644 --- a/plugins/libimhex/include/hex/helpers/project_file_handler.hpp +++ b/plugins/libimhex/include/hex/helpers/project_file_handler.hpp @@ -92,14 +92,14 @@ namespace hex { } private: - static inline std::string s_currProjectFilePath; - static inline bool s_hasUnsavedChanged = false; + static std::string s_currProjectFilePath; + static bool s_hasUnsavedChanged; - static inline std::string s_filePath; - static inline std::string s_pattern; - static inline Patches s_patches; - static inline std::list s_bookmarks; - static inline std::string s_dataProcessorContent; + static std::string s_filePath; + static std::string s_pattern; + static Patches s_patches; + static std::list s_bookmarks; + static std::string s_dataProcessorContent; }; } \ No newline at end of file diff --git a/plugins/libimhex/source/helpers/project_file_handler.cpp b/plugins/libimhex/source/helpers/project_file_handler.cpp index 7e1de098f..038d1ef4e 100644 --- a/plugins/libimhex/source/helpers/project_file_handler.cpp +++ b/plugins/libimhex/source/helpers/project_file_handler.cpp @@ -9,6 +9,15 @@ using json = nlohmann::json; namespace hex { + std::string ProjectFile::s_currProjectFilePath; + bool ProjectFile::s_hasUnsavedChanged = false; + + std::string ProjectFile::s_filePath; + std::string ProjectFile::s_pattern; + Patches ProjectFile::s_patches; + std::list ProjectFile::s_bookmarks; + std::string ProjectFile::s_dataProcessorContent; + void to_json(json& j, const ImHexApi::Bookmarks::Entry& b) { j = json{ { "address", b.region.address }, { "size", b.region.size }, { "name", b.name.data() }, { "comment", b.comment.data() }, { "locked", b.locked }, { "color", b.color } }; } @@ -16,12 +25,12 @@ namespace hex { void from_json(const json& j, ImHexApi::Bookmarks::Entry& b) { std::string name, comment; - j.at("address").get_to(b.region.address); - j.at("size").get_to(b.region.size); - j.at("name").get_to(name); - j.at("comment").get_to(comment); - j.at("locked").get_to(b.locked); - j.at("color").get_to(b.color); + if (j.contains("address")) j.at("address").get_to(b.region.address); + if (j.contains("size")) j.at("size").get_to(b.region.size); + if (j.contains("name")) j.at("name").get_to(name); + if (j.contains("comment")) j.at("comment").get_to(comment); + if (j.contains("locked")) j.at("locked").get_to(b.locked); + if (j.contains("color")) j.at("color").get_to(b.color); std::copy(name.begin(), name.end(), std::back_inserter(b.name)); b.name.push_back('\0');