2021-12-07 22:47:41 +01:00
|
|
|
#include "content/views/view_store.hpp"
|
2021-08-29 11:10:48 +02:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/api/content_registry.hpp>
|
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
#include <imgui.h>
|
|
|
|
#define IMGUI_DEFINE_MATH_OPERATORS
|
|
|
|
#include <imgui_internal.h>
|
|
|
|
|
|
|
|
#include <hex/helpers/utils.hpp>
|
|
|
|
#include <hex/helpers/crypto.hpp>
|
|
|
|
#include <hex/helpers/logger.hpp>
|
2021-09-08 17:22:54 +02:00
|
|
|
#include <hex/helpers/magic.hpp>
|
2022-03-04 11:36:37 +01:00
|
|
|
#include <hex/helpers/fs.hpp>
|
2022-06-16 15:42:08 +02:00
|
|
|
#include <hex/helpers/tar.hpp>
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
#include <filesystem>
|
2021-09-08 17:22:54 +02:00
|
|
|
#include <functional>
|
2021-09-03 02:34:40 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
2021-08-29 11:10:48 +02:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
#include <wolv/io/file.hpp>
|
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
namespace hex::plugin::builtin {
|
2021-08-29 11:10:48 +02:00
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
using namespace std::literals::string_literals;
|
|
|
|
using namespace std::literals::chrono_literals;
|
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
ViewStore::ViewStore() : View("hex.builtin.view.store.name") {
|
2023-03-20 13:11:43 +01:00
|
|
|
ContentRegistry::Interface::addMenuItemSeparator({ "hex.builtin.menu.help" }, 2000);
|
|
|
|
|
|
|
|
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.help", "hex.builtin.view.store.name" }, 2050, Shortcut::None, [&, this] {
|
|
|
|
if (this->m_requestStatus == RequestStatus::NotAttempted)
|
|
|
|
this->refresh();
|
|
|
|
TaskManager::doLater([] { ImGui::OpenPopup(View::toWindowName("hex.builtin.view.store.name").c_str()); });
|
|
|
|
this->getWindowOpenState() = true;
|
2022-01-23 02:28:38 +01:00
|
|
|
});
|
2023-03-23 11:23:07 +01:00
|
|
|
|
|
|
|
this->m_httpRequest.setTimeout(30'0000);
|
2021-09-03 02:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ViewStore::drawStore() {
|
2021-12-07 22:47:41 +01:00
|
|
|
ImGui::Header("hex.builtin.view.store.desc"_lang, true);
|
2021-09-03 02:34:40 +02:00
|
|
|
|
2023-03-23 13:32:47 +01:00
|
|
|
bool reloading = false;
|
|
|
|
if (this->m_apiRequest.valid()) {
|
|
|
|
if (this->m_apiRequest.wait_for(0s) != std::future_status::ready)
|
|
|
|
reloading = true;
|
|
|
|
else
|
|
|
|
this->parseResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::BeginDisabled(reloading);
|
2021-12-07 22:47:41 +01:00
|
|
|
if (ImGui::Button("hex.builtin.view.store.reload"_lang)) {
|
2021-09-03 02:34:40 +02:00
|
|
|
this->refresh();
|
|
|
|
}
|
2023-03-23 13:32:47 +01:00
|
|
|
ImGui::EndDisabled();
|
|
|
|
|
|
|
|
if (reloading) {
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::TextSpinner("hex.builtin.view.store.loading"_lang);
|
|
|
|
}
|
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
|
2023-02-16 18:55:21 +01:00
|
|
|
auto drawTab = [this](auto title, fs::ImHexPath pathType, auto &content, const std::function<void()> &downloadDoneCallback = []{}) {
|
2021-09-03 02:34:40 +02:00
|
|
|
if (ImGui::BeginTabItem(title)) {
|
2021-09-08 17:22:54 +02:00
|
|
|
if (ImGui::BeginTable("##pattern_language", 3, ImGuiTableFlags_ScrollY | ImGuiTableFlags_Borders | ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_RowBg)) {
|
2021-09-03 02:34:40 +02:00
|
|
|
ImGui::TableSetupScrollFreeze(0, 1);
|
2021-12-07 22:47:41 +01:00
|
|
|
ImGui::TableSetupColumn("hex.builtin.view.store.row.name"_lang, ImGuiTableColumnFlags_WidthFixed);
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.view.store.row.description"_lang, ImGuiTableColumnFlags_None);
|
2021-09-08 17:22:54 +02:00
|
|
|
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed);
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
ImGui::TableHeadersRow();
|
|
|
|
|
2021-09-09 01:57:11 +02:00
|
|
|
u32 id = 1;
|
2021-09-03 02:34:40 +02:00
|
|
|
for (auto &entry : content) {
|
2023-03-23 13:32:47 +01:00
|
|
|
ImGui::TableNextRow();
|
2021-09-03 02:34:40 +02:00
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextUnformatted(entry.name.c_str());
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextUnformatted(entry.description.c_str());
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
2023-03-23 13:32:47 +01:00
|
|
|
const auto buttonSize = ImVec2(100_scaled, ImGui::GetTextLineHeightWithSpacing());
|
|
|
|
|
2021-09-09 01:57:11 +02:00
|
|
|
ImGui::PushID(id);
|
2021-09-03 02:34:40 +02:00
|
|
|
ImGui::BeginDisabled(this->m_download.valid() && this->m_download.wait_for(0s) != std::future_status::ready);
|
|
|
|
{
|
|
|
|
if (entry.downloading) {
|
2023-03-23 13:32:47 +01:00
|
|
|
ImGui::ProgressBar(this->m_httpRequest.getProgress(), buttonSize, "");
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
if (this->m_download.valid() && this->m_download.wait_for(0s) == std::future_status::ready) {
|
|
|
|
entry.downloading = false;
|
|
|
|
|
|
|
|
auto response = this->m_download.get();
|
2023-03-23 11:23:07 +01:00
|
|
|
if (response.isSuccess()) {
|
2022-01-24 20:53:17 +01:00
|
|
|
entry.installed = true;
|
|
|
|
entry.hasUpdate = false;
|
|
|
|
|
|
|
|
if (entry.isFolder) {
|
2022-06-16 15:42:08 +02:00
|
|
|
Tar tar(this->m_downloadPath, Tar::Mode::Read);
|
|
|
|
tar.extractAll(this->m_downloadPath.parent_path() / this->m_downloadPath.stem());
|
2022-01-24 20:53:17 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 18:55:21 +01:00
|
|
|
downloadDoneCallback();
|
2021-09-03 02:34:40 +02:00
|
|
|
} else
|
2023-03-23 11:23:07 +01:00
|
|
|
log::error("Download failed! HTTP Code {}", response.getStatusCode());
|
2021-08-29 11:10:48 +02:00
|
|
|
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
this->m_download = {};
|
2021-09-03 02:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2023-03-23 13:32:47 +01:00
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
|
|
|
|
|
|
|
|
if (entry.hasUpdate) {
|
|
|
|
if (ImGui::Button("hex.builtin.view.store.update"_lang, buttonSize)) {
|
|
|
|
entry.downloading = this->download(pathType, entry.fileName, entry.link, true);
|
|
|
|
}
|
|
|
|
} else if (!entry.installed) {
|
|
|
|
if (ImGui::Button("hex.builtin.view.store.download"_lang, buttonSize)) {
|
|
|
|
entry.downloading = this->download(pathType, entry.fileName, entry.link, false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ImGui::Button("hex.builtin.view.store.remove"_lang, buttonSize)) {
|
|
|
|
entry.installed = !this->remove(pathType, entry.fileName);
|
|
|
|
}
|
2021-09-03 02:34:40 +02:00
|
|
|
}
|
2023-03-23 13:32:47 +01:00
|
|
|
ImGui::PopStyleVar();
|
2021-09-03 02:34:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::EndDisabled();
|
2021-09-09 01:57:11 +02:00
|
|
|
ImGui::PopID();
|
|
|
|
id++;
|
2021-09-03 02:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTable();
|
2021-08-29 11:10:48 +02:00
|
|
|
}
|
2021-09-03 02:34:40 +02:00
|
|
|
ImGui::EndTabItem();
|
2021-08-29 11:10:48 +02:00
|
|
|
}
|
2021-09-03 02:34:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (ImGui::BeginTabBar("storeTabs")) {
|
2023-02-16 18:55:21 +01:00
|
|
|
drawTab("hex.builtin.view.store.tab.patterns"_lang, fs::ImHexPath::Patterns, this->m_patterns);
|
|
|
|
drawTab("hex.builtin.view.store.tab.libraries"_lang, fs::ImHexPath::PatternsInclude, this->m_includes);
|
|
|
|
drawTab("hex.builtin.view.store.tab.magics"_lang, fs::ImHexPath::Magic, this->m_magics, magic::compile);
|
|
|
|
drawTab("hex.builtin.view.store.tab.nodes"_lang, fs::ImHexPath::Nodes, this->m_nodes);
|
2023-03-23 13:32:47 +01:00
|
|
|
drawTab("hex.builtin.view.store.tab.encodings"_lang, fs::ImHexPath::Encodings, this->m_encodings);
|
|
|
|
drawTab("hex.builtin.view.store.tab.constants"_lang, fs::ImHexPath::Constants, this->m_constants);
|
2023-02-16 18:55:21 +01:00
|
|
|
drawTab("hex.builtin.view.store.tab.themes"_lang, fs::ImHexPath::Themes, this->m_themes);
|
2023-03-23 13:32:47 +01:00
|
|
|
drawTab("hex.builtin.view.store.tab.yara"_lang, fs::ImHexPath::Yara, this->m_yara);
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
ImGui::EndTabBar();
|
2021-08-29 11:10:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
void ViewStore::refresh() {
|
2022-10-27 13:12:24 +02:00
|
|
|
// do not refresh if a refresh is already in progress
|
|
|
|
if (this->m_requestStatus == RequestStatus::InProgress)
|
|
|
|
return;
|
|
|
|
this->m_requestStatus = RequestStatus::InProgress;
|
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
this->m_patterns.clear();
|
|
|
|
this->m_includes.clear();
|
|
|
|
this->m_magics.clear();
|
2021-09-10 21:16:08 +02:00
|
|
|
this->m_constants.clear();
|
2021-09-23 22:56:49 +02:00
|
|
|
this->m_yara.clear();
|
2022-01-23 21:52:24 +01:00
|
|
|
this->m_encodings.clear();
|
2023-03-17 17:55:39 +01:00
|
|
|
this->m_nodes.clear();
|
|
|
|
this->m_themes.clear();
|
2021-09-03 02:34:40 +02:00
|
|
|
|
2023-03-23 11:23:07 +01:00
|
|
|
this->m_httpRequest.setUrl(ImHexApiURL + "/store"s);
|
|
|
|
this->m_apiRequest = this->m_httpRequest.execute();
|
2021-09-03 02:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ViewStore::parseResponse() {
|
|
|
|
auto response = this->m_apiRequest.get();
|
2023-03-23 11:23:07 +01:00
|
|
|
this->m_requestStatus = response.isSuccess() ? RequestStatus::Succeeded : RequestStatus::Failed;
|
2023-02-16 18:55:21 +01:00
|
|
|
if (this->m_requestStatus == RequestStatus::Succeeded) {
|
2023-03-23 11:23:07 +01:00
|
|
|
auto json = nlohmann::json::parse(response.getData());
|
2021-09-03 02:34:40 +02:00
|
|
|
|
2022-03-04 11:36:37 +01:00
|
|
|
auto parseStoreEntries = [](auto storeJson, const std::string &name, fs::ImHexPath pathType, std::vector<StoreEntry> &results) {
|
2021-09-03 02:34:40 +02:00
|
|
|
// Check if the response handles the type of files
|
|
|
|
if (storeJson.contains(name)) {
|
|
|
|
|
|
|
|
for (auto &entry : storeJson[name]) {
|
|
|
|
|
|
|
|
// Check if entry is valid
|
2021-09-23 22:56:49 +02:00
|
|
|
if (entry.contains("name") && entry.contains("desc") && entry.contains("file") && entry.contains("url") && entry.contains("hash") && entry.contains("folder")) {
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
// Parse entry
|
2022-01-24 20:53:17 +01:00
|
|
|
StoreEntry storeEntry = { entry["name"], entry["desc"], entry["file"], entry["url"], entry["hash"], entry["folder"], false, false, false };
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
// Check if file is installed already or has an update available
|
2022-03-04 11:36:37 +01:00
|
|
|
for (const auto &folder : fs::getDefaultPaths(pathType)) {
|
2021-09-03 02:34:40 +02:00
|
|
|
|
2022-03-04 11:36:37 +01:00
|
|
|
auto path = folder / std::fs::path(storeEntry.fileName);
|
2021-09-03 02:34:40 +02:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
if (wolv::io::fs::exists(path) && fs::isPathWritable(folder)) {
|
2021-09-03 02:34:40 +02:00
|
|
|
storeEntry.installed = true;
|
2021-08-29 11:10:48 +02:00
|
|
|
|
2023-03-23 11:23:07 +01:00
|
|
|
wolv::io::File file(path, wolv::io::File::Mode::Read);
|
|
|
|
auto bytes = file.readVector();
|
2021-09-03 02:34:40 +02:00
|
|
|
|
2023-03-23 11:23:07 +01:00
|
|
|
auto fileHash = crypt::sha256(bytes);
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
// Compare installed file hash with hash of repo file
|
|
|
|
if (std::vector(fileHash.begin(), fileHash.end()) != crypt::decode16(storeEntry.hash))
|
|
|
|
storeEntry.hasUpdate = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
results.push_back(storeEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-23 13:32:47 +01:00
|
|
|
|
|
|
|
std::sort(results.begin(), results.end(), [](const auto &lhs, const auto &rhs) {
|
|
|
|
return lhs.name < rhs.name;
|
|
|
|
});
|
2021-09-03 02:34:40 +02:00
|
|
|
};
|
|
|
|
|
2022-03-04 11:36:37 +01:00
|
|
|
parseStoreEntries(json, "patterns", fs::ImHexPath::Patterns, this->m_patterns);
|
|
|
|
parseStoreEntries(json, "includes", fs::ImHexPath::PatternsInclude, this->m_includes);
|
|
|
|
parseStoreEntries(json, "magic", fs::ImHexPath::Magic, this->m_magics);
|
|
|
|
parseStoreEntries(json, "constants", fs::ImHexPath::Constants, this->m_constants);
|
|
|
|
parseStoreEntries(json, "yara", fs::ImHexPath::Yara, this->m_yara);
|
|
|
|
parseStoreEntries(json, "encodings", fs::ImHexPath::Encodings, this->m_encodings);
|
2023-02-16 18:55:21 +01:00
|
|
|
parseStoreEntries(json, "nodes", fs::ImHexPath::Nodes, this->m_nodes);
|
|
|
|
parseStoreEntries(json, "themes", fs::ImHexPath::Themes, this->m_themes);
|
2021-09-03 02:34:40 +02:00
|
|
|
}
|
2022-01-24 20:53:17 +01:00
|
|
|
this->m_apiRequest = {};
|
2021-09-03 02:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ViewStore::drawContent() {
|
2022-02-21 18:57:53 +01:00
|
|
|
if (ImGui::BeginPopupModal(View::toWindowName("hex.builtin.view.store.name").c_str(), &this->getWindowOpenState())) {
|
2022-10-27 13:12:24 +02:00
|
|
|
if (this->m_requestStatus == RequestStatus::Failed)
|
|
|
|
ImGui::TextFormattedColored(ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarRed), "hex.builtin.view.store.netfailed"_lang);
|
|
|
|
|
|
|
|
this->drawStore();
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
ImGui::EndPopup();
|
|
|
|
} else {
|
|
|
|
this->getWindowOpenState() = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-04 11:36:37 +01:00
|
|
|
bool ViewStore::download(fs::ImHexPath pathType, const std::string &fileName, const std::string &url, bool update) {
|
2022-01-23 21:52:24 +01:00
|
|
|
bool downloading = false;
|
2022-03-04 11:36:37 +01:00
|
|
|
for (const auto &path : fs::getDefaultPaths(pathType)) {
|
|
|
|
if (!fs::isPathWritable(path))
|
2022-02-28 23:10:04 +01:00
|
|
|
continue;
|
|
|
|
|
2022-03-04 11:36:37 +01:00
|
|
|
auto fullPath = path / std::fs::path(fileName);
|
2022-02-28 23:10:04 +01:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
if (!update || wolv::io::fs::exists(fullPath)) {
|
2022-02-01 22:09:44 +01:00
|
|
|
downloading = true;
|
2022-01-23 21:52:24 +01:00
|
|
|
this->m_downloadPath = fullPath;
|
2023-03-23 11:23:07 +01:00
|
|
|
|
|
|
|
this->m_httpRequest.setUrl(url);
|
|
|
|
this->m_download = this->m_httpRequest.downloadFile(fullPath);
|
2022-01-23 21:52:24 +01:00
|
|
|
break;
|
2021-09-03 02:34:40 +02:00
|
|
|
}
|
|
|
|
}
|
2022-01-23 21:52:24 +01:00
|
|
|
|
|
|
|
if (!downloading) {
|
|
|
|
View::showErrorPopup("hex.builtin.view.store.download_error"_lang);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2021-09-03 02:34:40 +02:00
|
|
|
}
|
2022-01-24 20:53:17 +01:00
|
|
|
|
2022-03-04 11:36:37 +01:00
|
|
|
bool ViewStore::remove(fs::ImHexPath pathType, const std::string &fileName) {
|
2022-06-16 15:42:08 +02:00
|
|
|
bool removed = true;
|
2022-03-04 11:36:37 +01:00
|
|
|
for (const auto &path : fs::getDefaultPaths(pathType)) {
|
2022-06-16 15:42:08 +02:00
|
|
|
const auto filePath = path / fileName;
|
|
|
|
const auto folderPath = (path / std::fs::path(fileName).stem());
|
2022-01-23 21:52:24 +01:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
wolv::io::fs::remove(filePath);
|
|
|
|
wolv::io::fs::removeAll(folderPath);
|
2022-06-16 15:42:08 +02:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
removed = removed && !wolv::io::fs::exists(filePath) && !wolv::io::fs::exists(folderPath);
|
2021-10-17 22:23:01 +02:00
|
|
|
}
|
2022-01-23 21:52:24 +01:00
|
|
|
|
|
|
|
return removed;
|
2021-08-29 11:10:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|