1
0
mirror of synced 2024-11-13 18:50:53 +01:00

sys: Finish up the online store

This commit is contained in:
WerWolv 2021-09-08 17:22:54 +02:00
parent 4b41ae6980
commit 4cc637c0b2
4 changed files with 65 additions and 23 deletions

View File

@ -297,6 +297,19 @@ namespace hex::plugin::builtin {
{ "hex.view.constants.name", "Konstanten" }, { "hex.view.constants.name", "Konstanten" },
{ "hex.view.store.name", "Content Store" },
{ "hex.view.store.desc", "Downloade extra Content von ImHex' online Datenbank" },
{ "hex.view.store.reload", "Neu laden" },
{ "hex.view.store.name", "Name" },
{ "hex.view.store.description", "Beschreibung" },
{ "hex.view.store.download", "Download" },
{ "hex.view.store.update", "Update" },
{ "hex.view.store.remove", "Entfernen" },
{ "hex.view.store.tab.patterns", "Patterns" },
{ "hex.view.store.tab.libraries", "Libraries" },
{ "hex.view.store.tab.magics", "Magic Files" },
{ "hex.view.store.loading", "Store inhalt wird geladen..." },
/* Builtin plugin features */ /* Builtin plugin features */
{ "hex.builtin.command.calc.desc", "Rechner" }, { "hex.builtin.command.calc.desc", "Rechner" },

View File

@ -297,6 +297,20 @@ namespace hex::plugin::builtin {
{ "hex.view.constants.name", "Constants" }, { "hex.view.constants.name", "Constants" },
{ "hex.view.store.name", "Content Store" },
{ "hex.view.store.desc", "Download new content from ImHex' online database" },
{ "hex.view.store.reload", "Reload" },
{ "hex.view.store.name", "Name" },
{ "hex.view.store.description", "Description" },
{ "hex.view.store.download", "Download" },
{ "hex.view.store.update", "Update" },
{ "hex.view.store.remove", "Remove" },
{ "hex.view.store.tab.patterns", "Patterns" },
{ "hex.view.store.tab.libraries", "Libraries" },
{ "hex.view.store.tab.magics", "Magic Files" },
{ "hex.view.store.loading", "Loading store content..." },
/* Builtin plugin features */ /* Builtin plugin features */
{ "hex.builtin.command.calc.desc", "Calculator" }, { "hex.builtin.command.calc.desc", "Calculator" },

View File

@ -296,6 +296,20 @@ namespace hex::plugin::builtin {
// { "hex.view.constants.name", "Constants" }, // { "hex.view.constants.name", "Constants" },
//{ "hex.view.store.name", "Content Store" },
// { "hex.view.store.desc", "Download new content from ImHex' online database" },
// { "hex.view.store.reload", "Reload" },
// { "hex.view.store.name", "Name" },
// { "hex.view.store.description", "Description" },
// { "hex.view.store.download", "Download" },
// { "hex.view.store.update", "Update" },
// { "hex.view.store.remove", "Remove" },
// { "hex.view.store.tab.patterns", "Patterns" },
// { "hex.view.store.tab.libraries", "Libraries" },
// { "hex.view.store.tab.magics", "Magic Files" },
// { "hex.view.store.loading", "Loading store content..." },
/* Builtin plugin features */ /* Builtin plugin features */
{ "hex.builtin.command.calc.desc", "Calcolatrice" }, { "hex.builtin.command.calc.desc", "Calcolatrice" },

View File

@ -7,9 +7,11 @@
#include <hex/helpers/utils.hpp> #include <hex/helpers/utils.hpp>
#include <hex/helpers/crypto.hpp> #include <hex/helpers/crypto.hpp>
#include <hex/helpers/logger.hpp> #include <hex/helpers/logger.hpp>
#include <hex/helpers/magic.hpp>
#include <fstream> #include <fstream>
#include <filesystem> #include <filesystem>
#include <functional>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
namespace hex { namespace hex {
@ -26,24 +28,24 @@ namespace hex {
ViewStore::~ViewStore() { } ViewStore::~ViewStore() { }
void ViewStore::drawStore() { void ViewStore::drawStore() {
ImGui::Header("ImHex content store", true); ImGui::Header("hex.view.store.desc"_lang, true);
if (ImGui::Button("Reload")) { if (ImGui::Button("hex.view.store.reload"_lang)) {
this->refresh(); this->refresh();
} }
auto drawTab = [this](auto title, ImHexPath pathType, auto &content) { auto drawTab = [this](auto title, ImHexPath pathType, auto &content, std::function<void()> downloadDoneCallback) {
if (ImGui::BeginTabItem(title)) { if (ImGui::BeginTabItem(title)) {
if (ImGui::BeginTable("##pattern_language", 3, ImGuiTableFlags_ScrollY | ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingStretchProp)) { if (ImGui::BeginTable("##pattern_language", 3, ImGuiTableFlags_ScrollY | ImGuiTableFlags_Borders | ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_RowBg)) {
ImGui::TableSetupScrollFreeze(0, 1); ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_None, 1.0); ImGui::TableSetupColumn("hex.view.store.name"_lang, ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Description", ImGuiTableColumnFlags_None, 3.0); ImGui::TableSetupColumn("hex.view.store.description"_lang, ImGuiTableColumnFlags_None);
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_None, 1.0); ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableHeadersRow(); ImGui::TableHeadersRow();
for (auto &entry : content) { for (auto &entry : content) {
ImGui::TableNextRow(); ImGui::TableNextRow(ImGuiTableRowFlags_None, ImGui::GetTextLineHeight() + 4.0F * ImGui::GetStyle().FramePadding.y);
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::TextUnformatted(entry.name.c_str()); ImGui::TextUnformatted(entry.name.c_str());
ImGui::TableNextColumn(); ImGui::TableNextColumn();
@ -53,7 +55,7 @@ namespace hex {
ImGui::BeginDisabled(this->m_download.valid() && this->m_download.wait_for(0s) != std::future_status::ready); ImGui::BeginDisabled(this->m_download.valid() && this->m_download.wait_for(0s) != std::future_status::ready);
{ {
if (entry.downloading) { if (entry.downloading) {
ImGui::TextSpinner("Downloading..."); ImGui::TextSpinner("");
if (this->m_download.valid() && this->m_download.wait_for(0s) == std::future_status::ready) { if (this->m_download.valid() && this->m_download.wait_for(0s) == std::future_status::ready) {
entry.downloading = false; entry.downloading = false;
@ -62,6 +64,7 @@ namespace hex {
if (response.code == 200) { if (response.code == 200) {
entry.installed = true; entry.installed = true;
entry.hasUpdate = false; entry.hasUpdate = false;
downloadDoneCallback();
} else } else
log::error("Download failed!"); log::error("Download failed!");
@ -70,17 +73,17 @@ namespace hex {
} }
} else if (entry.hasUpdate) { } else if (entry.hasUpdate) {
if (ImGui::Button("Update")) { if (ImGui::Button("hex.view.store.update"_lang)) {
this->download(pathType, entry.fileName, entry.link, true); this->download(pathType, entry.fileName, entry.link, true);
entry.downloading = true; entry.downloading = true;
} }
} else if (!entry.installed) { } else if (!entry.installed) {
if (ImGui::Button("Download")) { if (ImGui::Button("hex.view.store.download"_lang)) {
this->download(pathType, entry.fileName, entry.link, false); this->download(pathType, entry.fileName, entry.link, false);
entry.downloading = true; entry.downloading = true;
} }
} else { } else {
if (ImGui::Button("Remove")) { if (ImGui::Button("hex.view.store.remove"_lang)) {
this->remove(pathType, entry.fileName); this->remove(pathType, entry.fileName);
entry.installed = false; entry.installed = false;
} }
@ -96,9 +99,11 @@ namespace hex {
}; };
if (ImGui::BeginTabBar("storeTabs")) { if (ImGui::BeginTabBar("storeTabs")) {
drawTab("Patterns", ImHexPath::Patterns, this->m_patterns); drawTab("hex.view.store.tab.patterns"_lang, ImHexPath::Patterns, this->m_patterns, []{});
drawTab("Libraries", ImHexPath::PatternsInclude, this->m_includes); drawTab("hex.view.store.tab.libraries"_lang, ImHexPath::PatternsInclude, this->m_includes, []{});
drawTab("Magic", ImHexPath::Magic, this->m_magics); drawTab("hex.view.store.tab.magics"_lang, ImHexPath::Magic, this->m_magics, []{
magic::compile();
});
ImGui::EndTabBar(); ImGui::EndTabBar();
} }
@ -114,9 +119,7 @@ namespace hex {
void ViewStore::parseResponse() { void ViewStore::parseResponse() {
auto response = this->m_apiRequest.get(); auto response = this->m_apiRequest.get();
if (response.code != 200) if (response.code == 200) {
ImGui::TextUnformatted("Invalid response from store API");
else {
auto json = nlohmann::json::parse(response.body); auto json = nlohmann::json::parse(response.body);
auto parseStoreEntries = [](auto storeJson, const std::string &name, ImHexPath pathType, std::vector<StoreEntry> &results) { auto parseStoreEntries = [](auto storeJson, const std::string &name, ImHexPath pathType, std::vector<StoreEntry> &results) {
@ -157,13 +160,11 @@ namespace hex {
} }
}; };
parseStoreEntries(json, "pattern_language", ImHexPath::Patterns, this->m_patterns); parseStoreEntries(json, "patterns", ImHexPath::Patterns, this->m_patterns);
parseStoreEntries(json, "includes", ImHexPath::PatternsInclude, this->m_includes); parseStoreEntries(json, "includes", ImHexPath::PatternsInclude, this->m_includes);
parseStoreEntries(json, "magic", ImHexPath::Magic, this->m_magics); parseStoreEntries(json, "magic", ImHexPath::Magic, this->m_magics);
this->m_apiRequest = { };
} }
this->m_apiRequest = { };
} }
@ -172,7 +173,7 @@ namespace hex {
if (ImGui::BeginPopupModal(View::toWindowName("hex.view.store.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_AlwaysAutoResize)) { if (ImGui::BeginPopupModal(View::toWindowName("hex.view.store.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_AlwaysAutoResize)) {
if (this->m_apiRequest.valid()) { if (this->m_apiRequest.valid()) {
if (this->m_apiRequest.wait_for(0s) != std::future_status::ready) if (this->m_apiRequest.wait_for(0s) != std::future_status::ready)
ImGui::TextSpinner("Loading store content..."); ImGui::TextSpinner("hex.view.store.loading"_lang);
else else
this->parseResponse(); this->parseResponse();
} }