1
0
mirror of synced 2025-01-31 12:03:46 +01:00

sys: Load content store content when it's first opened (#795)

* refresh store on open

* show the user if the request failed

* do not refresh if a refresh is in progress

This makes ImHex crash

* InProgress

* fix formatting

* use a localized message

* set localized message in other langs
This commit is contained in:
iTrooz_ 2022-10-27 13:12:24 +02:00 committed by GitHub
parent e76d5854b8
commit 32ab1c1a06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 29 additions and 4 deletions

View File

@ -13,6 +13,13 @@
namespace hex::plugin::builtin {
enum class RequestStatus {
NotAttempted,
InProgress,
Failed,
Succeded,
};
struct StoreEntry {
std::string name;
std::string description;
@ -45,6 +52,7 @@ namespace hex::plugin::builtin {
std::future<Response<std::string>> m_apiRequest;
std::future<Response<void>> m_download;
std::fs::path m_downloadPath;
RequestStatus m_requestStatus = RequestStatus::NotAttempted;
std::vector<StoreEntry> m_patterns, m_includes, m_magics, m_constants, m_yara, m_encodings;

View File

@ -25,10 +25,10 @@ namespace hex::plugin::builtin {
using namespace std::literals::chrono_literals;
ViewStore::ViewStore() : View("hex.builtin.view.store.name") {
this->refresh();
ContentRegistry::Interface::addMenuItem("hex.builtin.menu.help", 3000, [&, this] {
if (ImGui::MenuItem("hex.builtin.view.store.name"_lang)) {
if (this->m_requestStatus == RequestStatus::NotAttempted)
this->refresh();
TaskManager::doLater([] { ImGui::OpenPopup(View::toWindowName("hex.builtin.view.store.name").c_str()); });
this->getWindowOpenState() = true;
}
@ -126,6 +126,11 @@ namespace hex::plugin::builtin {
}
void ViewStore::refresh() {
// do not refresh if a refresh is already in progress
if (this->m_requestStatus == RequestStatus::InProgress)
return;
this->m_requestStatus = RequestStatus::InProgress;
this->m_patterns.clear();
this->m_includes.clear();
this->m_magics.clear();
@ -138,7 +143,8 @@ namespace hex::plugin::builtin {
void ViewStore::parseResponse() {
auto response = this->m_apiRequest.get();
if (response.code == 200) {
this->m_requestStatus = response.code == 200 ? RequestStatus::Succeded : RequestStatus::Failed;
if (this->m_requestStatus == RequestStatus::Succeded) {
auto json = nlohmann::json::parse(response.body);
auto parseStoreEntries = [](auto storeJson, const std::string &name, fs::ImHexPath pathType, std::vector<StoreEntry> &results) {
@ -198,7 +204,10 @@ namespace hex::plugin::builtin {
this->parseResponse();
}
this->drawStore();
if (this->m_requestStatus == RequestStatus::Failed)
ImGui::TextFormattedColored(ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarRed), "hex.builtin.view.store.netfailed"_lang);
this->drawStore();
ImGui::EndPopup();
} else {

View File

@ -437,6 +437,7 @@ namespace hex::plugin::builtin {
{ "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.netfailed", "Net request to load store content failed!" },
{ "hex.builtin.view.store.download_error", "Datei konnte nicht heruntergeladen werden! Zielordner konnte nicht gefunden werden." },
{ "hex.builtin.view.diff.name", "Diffing" },

View File

@ -441,6 +441,7 @@ namespace hex::plugin::builtin {
{ "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.netfailed", "Net request to load store content failed!" },
{ "hex.builtin.view.store.download_error", "Failed to download file! Destination folder does not exist." },
{ "hex.builtin.view.diff.name", "Diffing" },

View File

@ -442,6 +442,7 @@ namespace hex::plugin::builtin {
{ "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.netfailed", "Net request to load store content failed!" },
{ "hex.builtin.view.store.download_error", "Impossibile scaricare file! La cartella di destinazione non esiste." },
{ "hex.builtin.view.diff.name", "Diffing" },

View File

@ -443,6 +443,7 @@ namespace hex::plugin::builtin {
{ "hex.builtin.view.store.tab.yara", "Yaraルール" },
{ "hex.builtin.view.store.tab.encodings", "エンコード" },
{ "hex.builtin.view.store.loading", "ストアコンテンツを読み込み中…" },
// { "hex.builtin.view.store.netfailed", "Net request to load store content failed!" },
{ "hex.builtin.view.store.download_error", "ファイルのダウンロードに失敗しました。ダウンロード先フォルダが存在しません。" },
{ "hex.builtin.view.diff.name", "比較" },

View File

@ -440,6 +440,7 @@ namespace hex::plugin::builtin {
{ "hex.builtin.view.store.tab.yara", "Yara 규칙" },
{ "hex.builtin.view.store.tab.encodings", "인코딩" },
{ "hex.builtin.view.store.loading", "스토어 콘텐츠 불러오는 중..." },
// { "hex.builtin.view.store.netfailed", "Net request to load store content failed!" },
{ "hex.builtin.view.store.download_error", "파일 다운로드에 실패했습니다! 저장 폴더가 존재하지 않습니다." },
{ "hex.builtin.view.diff.name", "파일 비교" },

View File

@ -440,6 +440,7 @@ namespace hex::plugin::builtin {
{ "hex.builtin.view.store.tab.yara", "Regras Yara" },
{ "hex.builtin.view.store.tab.encodings", "Codificações" },
{ "hex.builtin.view.store.loading", "Carregando conteúdo da loja..." },
// { "hex.builtin.view.store.netfailed", "Net request to load store content failed!" },
{ "hex.builtin.view.store.download_error", "Falha ao baixar o arquivo! A pasta de destino não existe." },
{ "hex.builtin.view.diff.name", "Diferenciando" },

View File

@ -442,6 +442,7 @@ namespace hex::plugin::builtin {
{ "hex.builtin.view.store.tab.yara", "Yara 规则" },
{ "hex.builtin.view.store.tab.encodings", "编码" },
{ "hex.builtin.view.store.loading", "正在加载在线内容..." },
// { "hex.builtin.view.store.netfailed", "Net request to load store content failed!" },
{ "hex.builtin.view.store.download_error", "下载文件失败!目标文件夹不存在。" },
{ "hex.builtin.view.diff.name", "差异" },

View File

@ -440,6 +440,7 @@ namespace hex::plugin::builtin {
{ "hex.builtin.view.store.tab.yara", "Yara 規則" },
{ "hex.builtin.view.store.tab.encodings", "編碼" },
{ "hex.builtin.view.store.loading", "正在載入商店內容..." },
// { "hex.builtin.view.store.netfailed", "Net request to load store content failed!" },
{ "hex.builtin.view.store.download_error", "無法下載檔案!目的地資料夾不存在。" },
{ "hex.builtin.view.diff.name", "Diffing" },