From 9cf7fc4a2e1311f4fa1325f7fe1ca64a598611d2 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 23 Jan 2022 21:52:24 +0100 Subject: [PATCH] store: Added encoding files to store, fixed crash when folder doesn't exist --- lib/libimhex/include/hex/helpers/paths.hpp | 1 + lib/libimhex/source/helpers/paths.cpp | 12 ++++ main/source/init/splash_window.cpp | 2 +- main/source/init/tasks.cpp | 1 + .../include/content/views/view_store.hpp | 6 +- .../source/content/views/view_store.cpp | 68 ++++++++++--------- plugins/builtin/source/lang/de_DE.cpp | 3 + plugins/builtin/source/lang/en_US.cpp | 3 + plugins/builtin/source/lang/it_IT.cpp | 3 + plugins/builtin/source/lang/zh_CN.cpp | 5 +- 10 files changed, 67 insertions(+), 37 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/paths.hpp b/lib/libimhex/include/hex/helpers/paths.hpp index 3d67db337..7b0b6a136 100644 --- a/lib/libimhex/include/hex/helpers/paths.hpp +++ b/lib/libimhex/include/hex/helpers/paths.hpp @@ -18,6 +18,7 @@ namespace hex { Config, Resources, Constants, + Encodings, Logs }; diff --git a/lib/libimhex/source/helpers/paths.cpp b/lib/libimhex/source/helpers/paths.cpp index b21e68e3b..2346446ef 100644 --- a/lib/libimhex/source/helpers/paths.cpp +++ b/lib/libimhex/source/helpers/paths.cpp @@ -98,6 +98,11 @@ namespace hex { return (path / "constants").string(); }); break; + case ImHexPath::Encodings: + std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path){ + return (path / "encodings").string(); + }); + break; case ImHexPath::Logs: std::transform(paths.begin(), paths.end(), std::back_inserter(result), [](auto &path){ return (path / "logs").string(); @@ -142,6 +147,9 @@ namespace hex { case ImHexPath::Constants: result.push_back((applicationSupportDir / "constants").string()); break; + case ImHexPath::Encodings: + result.push_back((applicationSupportDir / "encodings").string()); + break; case ImHexPath::Logs: result.push_back((applicationSupportDir / "logs").string()); break; @@ -199,6 +207,10 @@ namespace hex { std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result), [](auto p) { return (p / "constants").string(); }); break; + case ImHexPath::Encodings: + std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result), + [](auto p) { return (p / "encodings").string(); }); + break; case ImHexPath::Logs: std::transform(dataDirs.begin(), dataDirs.end(), std::back_inserter(result), [](auto p) { return (p / "logs").string(); }); diff --git a/main/source/init/splash_window.cpp b/main/source/init/splash_window.cpp index 901cf515a..8f4a78f1f 100644 --- a/main/source/init/splash_window.cpp +++ b/main/source/init/splash_window.cpp @@ -50,7 +50,7 @@ namespace hex::init { try { status = task() && status; } catch (std::exception &e) { - log::error("Init task {} threw an exception: {}", name, e.what()); + log::error("Init task '{}' threw an exception: {}", name, e.what()); status = false; } diff --git a/main/source/init/tasks.cpp b/main/source/init/tasks.cpp index 722080ba5..848bbec1e 100644 --- a/main/source/init/tasks.cpp +++ b/main/source/init/tasks.cpp @@ -66,6 +66,7 @@ namespace hex::init { ImHexPath::Config, ImHexPath::Constants, ImHexPath::Yara, + ImHexPath::Encodings, ImHexPath::Python, ImHexPath::Logs }; diff --git a/plugins/builtin/include/content/views/view_store.hpp b/plugins/builtin/include/content/views/view_store.hpp index 0f082f68c..72aedab94 100644 --- a/plugins/builtin/include/content/views/view_store.hpp +++ b/plugins/builtin/include/content/views/view_store.hpp @@ -43,15 +43,15 @@ namespace hex::plugin::builtin { std::future> m_download; fs::path m_downloadPath; - std::vector m_patterns, m_includes, m_magics, m_constants, m_yara; + std::vector m_patterns, m_includes, m_magics, m_constants, m_yara, m_encodings; void drawStore(); void refresh(); void parseResponse(); - void download(ImHexPath pathType, const std::string &fileName, const std::string &url, bool update); - void remove(ImHexPath pathType, const std::string &fileName); + bool download(ImHexPath pathType, const std::string &fileName, const std::string &url, bool update); + bool remove(ImHexPath pathType, const std::string &fileName); }; } \ No newline at end of file diff --git a/plugins/builtin/source/content/views/view_store.cpp b/plugins/builtin/source/content/views/view_store.cpp index 50dea4caf..e298b0346 100644 --- a/plugins/builtin/source/content/views/view_store.cpp +++ b/plugins/builtin/source/content/views/view_store.cpp @@ -42,7 +42,7 @@ namespace hex::plugin::builtin { this->refresh(); } - auto drawTab = [this](auto title, ImHexPath pathType, auto &content, std::function downloadDoneCallback) { + auto drawTab = [this](auto title, ImHexPath pathType, auto &content, const std::function &downloadDoneCallback) { if (ImGui::BeginTabItem(title)) { if (ImGui::BeginTable("##pattern_language", 3, ImGuiTableFlags_ScrollY | ImGuiTableFlags_Borders | ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_RowBg)) { ImGui::TableSetupScrollFreeze(0, 1); @@ -116,18 +116,15 @@ namespace hex::plugin::builtin { } else if (entry.hasUpdate) { if (ImGui::Button("hex.builtin.view.store.update"_lang)) { - this->download(pathType, entry.fileName, entry.link, true); - entry.downloading = true; + entry.downloading = this->download(pathType, entry.fileName, entry.link, true); } } else if (!entry.installed) { if (ImGui::Button("hex.builtin.view.store.download"_lang)) { - this->download(pathType, entry.fileName, entry.link, false); - entry.downloading = true; + entry.downloading = this->download(pathType, entry.fileName, entry.link, false); } } else { if (ImGui::Button("hex.builtin.view.store.remove"_lang)) { - this->remove(pathType, entry.fileName); - entry.installed = false; + entry.installed = !this->remove(pathType, entry.fileName); } } } @@ -143,17 +140,12 @@ namespace hex::plugin::builtin { }; if (ImGui::BeginTabBar("storeTabs")) { - auto extractTar = []{ - - }; - - drawTab("hex.builtin.view.store.tab.patterns"_lang, ImHexPath::Patterns, this->m_patterns, [](auto){}); - drawTab("hex.builtin.view.store.tab.libraries"_lang, ImHexPath::PatternsInclude, this->m_includes, [](auto){}); - drawTab("hex.builtin.view.store.tab.magics"_lang, ImHexPath::Magic, this->m_magics, [](auto){ - magic::compile(); - }); - drawTab("hex.builtin.view.store.tab.constants"_lang, ImHexPath::Constants, this->m_constants, [](auto){}); - drawTab("hex.builtin.view.store.tab.yara"_lang, ImHexPath::Yara, this->m_yara, [](auto){}); + drawTab("hex.builtin.view.store.tab.patterns"_lang, ImHexPath::Patterns, this->m_patterns, [](auto) {}); + drawTab("hex.builtin.view.store.tab.libraries"_lang, ImHexPath::PatternsInclude, this->m_includes, [](auto) {}); + drawTab("hex.builtin.view.store.tab.magics"_lang, ImHexPath::Magic, this->m_magics, [](auto) { magic::compile(); }); + drawTab("hex.builtin.view.store.tab.constants"_lang, ImHexPath::Constants, this->m_constants, [](auto) {}); + drawTab("hex.builtin.view.store.tab.encodings"_lang, ImHexPath::Encodings, this->m_encodings, [](auto) {}); + drawTab("hex.builtin.view.store.tab.yara"_lang, ImHexPath::Yara, this->m_yara, [](auto) {}); ImGui::EndTabBar(); } @@ -165,6 +157,7 @@ namespace hex::plugin::builtin { this->m_magics.clear(); this->m_constants.clear(); this->m_yara.clear(); + this->m_encodings.clear(); this->m_apiRequest = this->m_net.getString(ImHexApiURL + "/store"s); } @@ -217,6 +210,7 @@ namespace hex::plugin::builtin { parseStoreEntries(json, "magic", ImHexPath::Magic, this->m_magics); parseStoreEntries(json, "constants", ImHexPath::Constants, this->m_constants); parseStoreEntries(json, "yara", ImHexPath::Yara, this->m_yara); + parseStoreEntries(json, "encodings", ImHexPath::Encodings, this->m_encodings); } this->m_apiRequest = { }; @@ -241,26 +235,36 @@ namespace hex::plugin::builtin { } } - void ViewStore::download(ImHexPath pathType, const std::string &fileName, const std::string &url, bool update) { - if (!update) { - this->m_downloadPath = hex::getPath(pathType).back() / fs::path(fileName); - this->m_download = this->m_net.downloadFile(url, this->m_downloadPath); - } else { - for (const auto &path : hex::getPath(pathType)) { - auto fullPath = path / fs::path(fileName); - if (fs::exists(fullPath)) { - this->m_downloadPath = fullPath; - this->m_download = this->m_net.downloadFile(url, fullPath); - } + bool ViewStore::download(ImHexPath pathType, const std::string &fileName, const std::string &url, bool update) { + bool downloading = false; + for (const auto &path : hex::getPath(pathType)) { + auto fullPath = path / fs::path(fileName); + if (!update || fs::exists(fullPath)) { + downloading = true; + this->m_downloadPath = fullPath; + this->m_download = this->m_net.downloadFile(url, fullPath); + break; } } + + if (!downloading) { + View::showErrorPopup("hex.builtin.view.store.download_error"_lang); + return false; + } + + return true; } - void ViewStore::remove(ImHexPath pathType, const std::string &fileName) { + bool ViewStore::remove(ImHexPath pathType, const std::string &fileName) { + bool removed = false; for (const auto &path : hex::getPath(pathType)) { - fs::remove(path / fs::path(fileName)); - fs::remove(path / fs::path(fileName).stem()); + bool removedFile = fs::remove(path / fs::path(fileName)); + bool removedFolder = fs::remove(path / fs::path(fileName).stem()); + + removed = removedFile || removedFolder; } + + return removed; } } \ No newline at end of file diff --git a/plugins/builtin/source/lang/de_DE.cpp b/plugins/builtin/source/lang/de_DE.cpp index 5418a28cb..b4cb43d1f 100644 --- a/plugins/builtin/source/lang/de_DE.cpp +++ b/plugins/builtin/source/lang/de_DE.cpp @@ -348,8 +348,11 @@ namespace hex::plugin::builtin { { "hex.builtin.view.store.tab.libraries", "Libraries" }, { "hex.builtin.view.store.tab.magics", "Magic Files" }, { "hex.builtin.view.store.tab.constants", "Konstanten" }, + { "hex.builtin.view.store.tab.encodings", "Encodings" }, { "hex.builtin.view.store.tab.yara", "Yara Rules" }, { "hex.builtin.view.store.loading", "Store inhalt wird geladen..." }, + { "hex.builtin.view.store.download_error", "Datei konnte nicht heruntergeladen werden! Zielordner konnte nicht gefunden werden." }, + { "hex.builtin.view.diff.name", "Diffing" }, { "hex.builtin.view.provider_settings.name", "Provider Einstellungen" }, diff --git a/plugins/builtin/source/lang/en_US.cpp b/plugins/builtin/source/lang/en_US.cpp index 3606e7e7c..f64b49439 100644 --- a/plugins/builtin/source/lang/en_US.cpp +++ b/plugins/builtin/source/lang/en_US.cpp @@ -352,7 +352,10 @@ namespace hex::plugin::builtin { { "hex.builtin.view.store.tab.magics", "Magic Files" }, { "hex.builtin.view.store.tab.constants", "Constants" }, { "hex.builtin.view.store.tab.yara", "Yara Rules" }, + { "hex.builtin.view.store.tab.encodings", "Encodings" }, { "hex.builtin.view.store.loading", "Loading store content..." }, + { "hex.builtin.view.store.download_error", "Failed to download file! Destination folder does not exist." }, + { "hex.builtin.view.diff.name", "Diffing" }, { "hex.builtin.view.provider_settings.name", "Provider Settings" }, diff --git a/plugins/builtin/source/lang/it_IT.cpp b/plugins/builtin/source/lang/it_IT.cpp index 690060c95..58c17ee30 100644 --- a/plugins/builtin/source/lang/it_IT.cpp +++ b/plugins/builtin/source/lang/it_IT.cpp @@ -345,8 +345,11 @@ namespace hex::plugin::builtin { { "hex.builtin.view.store.tab.libraries", "Librerie" }, { "hex.builtin.view.store.tab.magics", "File Magici" }, { "hex.builtin.view.store.tab.constants", "Costanti" }, + //{ "hex.builtin.view.store.tab.encodings", "Encodings" }, { "hex.builtin.view.store.tab.yara", "Regole di Yara" }, { "hex.builtin.view.store.loading", "Caricamento del content store..." }, + //{ "hex.builtin.view.store.download_error", "Failed to download file! Destination folder does not exist." }, + //{ "hex.builtin.view.diff.name", "Diffing" }, //{ "hex.builtin.view.provider_settings.name", "Provider Settings" }, diff --git a/plugins/builtin/source/lang/zh_CN.cpp b/plugins/builtin/source/lang/zh_CN.cpp index 63b14f177..8170decb6 100644 --- a/plugins/builtin/source/lang/zh_CN.cpp +++ b/plugins/builtin/source/lang/zh_CN.cpp @@ -346,7 +346,10 @@ namespace hex::plugin::builtin { { "hex.builtin.view.store.tab.magics", "魔术数据库" }, { "hex.builtin.view.store.tab.constants", "常量" }, { "hex.builtin.view.store.tab.yara", "Yara规则" }, - { "hex.builtin.view.store.loading", "正在加载仓库内容..." }, + //{ "hex.builtin.view.store.tab.encodings", "Encodings" }, + { "hex.builtin.view.store.loading", "正在加载仓库内容..." }, + //{ "hex.builtin.view.store.download_error", "Failed to download file! Destination folder does not exist." }, + { "hex.builtin.view.diff.name", "差异" }, //{ "hex.builtin.view.provider_settings.name", "Provider Settings" },