From 1d4cbbe418c13b4d53582afc7937a6abdf776a02 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 20 Sep 2022 15:33:36 +0200 Subject: [PATCH] ux: Handle project loading errors better --- lib/libimhex/include/hex/api/project_file_manager.hpp | 2 ++ lib/libimhex/source/api/project_file_manager.cpp | 10 +++++++++- plugins/builtin/source/content/events.cpp | 4 +++- plugins/builtin/source/content/main_menu_items.cpp | 8 ++++++-- plugins/builtin/source/content/providers.cpp | 4 +++- .../builtin/source/content/views/view_bookmarks.cpp | 1 + .../source/content/views/view_data_processor.cpp | 1 + plugins/builtin/source/content/views/view_patches.cpp | 1 + .../source/content/views/view_pattern_editor.cpp | 1 + plugins/builtin/source/lang/de_DE.cpp | 2 ++ plugins/builtin/source/lang/en_US.cpp | 2 ++ plugins/builtin/source/lang/it_IT.cpp | 2 ++ plugins/builtin/source/lang/ja_JP.cpp | 2 ++ plugins/builtin/source/lang/ko_KR.cpp | 2 ++ plugins/builtin/source/lang/pt_BR.cpp | 2 ++ plugins/builtin/source/lang/zh_CN.cpp | 2 ++ plugins/builtin/source/lang/zh_TW.cpp | 2 ++ 17 files changed, 43 insertions(+), 5 deletions(-) diff --git a/lib/libimhex/include/hex/api/project_file_manager.hpp b/lib/libimhex/include/hex/api/project_file_manager.hpp index 3f3ae36d8..ebb21f921 100644 --- a/lib/libimhex/include/hex/api/project_file_manager.hpp +++ b/lib/libimhex/include/hex/api/project_file_manager.hpp @@ -19,12 +19,14 @@ namespace hex { struct Handler { using Function = std::function; std::fs::path basePath; + bool required; Function load, store; }; struct ProviderHandler { using Function = std::function; std::fs::path basePath; + bool required; Function load, store; }; diff --git a/lib/libimhex/source/api/project_file_manager.cpp b/lib/libimhex/source/api/project_file_manager.cpp index 8f512c340..7b43f5c97 100644 --- a/lib/libimhex/source/api/project_file_manager.cpp +++ b/lib/libimhex/source/api/project_file_manager.cpp @@ -45,6 +45,10 @@ namespace hex { log::info("{}", e.what()); result = false; } + + if (!result && handler.required) { + return false; + } } for (const auto &provider : ImHexApi::Provider::getProviders()) { @@ -57,12 +61,16 @@ namespace hex { log::info("{}", e.what()); result = false; } + + if (!result && handler.required) { + return false; + } } } ProjectFile::s_currProjectPath = filePath; - return result; + return true; } bool ProjectFile::store(std::optional filePath) { diff --git a/plugins/builtin/source/content/events.cpp b/plugins/builtin/source/content/events.cpp index b3a492078..d5086b140 100644 --- a/plugins/builtin/source/content/events.cpp +++ b/plugins/builtin/source/content/events.cpp @@ -78,7 +78,9 @@ namespace hex::plugin::builtin { } else if (name == "Open Project") { fs::openFileBrowser(fs::DialogMode::Open, { {"Project File", "hexproj"} }, [](const auto &path) { - ProjectFile::load(path); + if (!ProjectFile::load(path)) { + View::showErrorPopup("hex.builtin.popup.error.project.load"_lang); + } }); } }); diff --git a/plugins/builtin/source/content/main_menu_items.cpp b/plugins/builtin/source/content/main_menu_items.cpp index dbe38abbd..fe75ee668 100644 --- a/plugins/builtin/source/content/main_menu_items.cpp +++ b/plugins/builtin/source/content/main_menu_items.cpp @@ -64,7 +64,9 @@ namespace hex::plugin::builtin { fs::openFileBrowser(fs::DialogMode::Open, { {"Project File", "hexproj"} }, [](const auto &path) { - ProjectFile::load(path); + if (!ProjectFile::load(path)) { + View::showErrorPopup("hex.builtin.popup.error.project.load"_lang); + } }); } @@ -75,7 +77,9 @@ namespace hex::plugin::builtin { path.replace_extension(".hexproj"); } - ProjectFile::store(path); + if (!ProjectFile::load(path)) { + View::showErrorPopup("hex.builtin.popup.error.project.load"_lang); + } }); } }); diff --git a/plugins/builtin/source/content/providers.cpp b/plugins/builtin/source/content/providers.cpp index 56243d150..aa7a21892 100644 --- a/plugins/builtin/source/content/providers.cpp +++ b/plugins/builtin/source/content/providers.cpp @@ -24,6 +24,7 @@ namespace hex::plugin::builtin { ProjectFile::registerHandler({ .basePath = "providers", + .required = true, .load = [](const std::fs::path &basePath, Tar &tar) { auto json = nlohmann::json::parse(tar.readString(basePath / "providers.json")); auto providerIds = json["providers"].get>(); @@ -33,13 +34,14 @@ namespace hex::plugin::builtin { auto providerSettings = nlohmann::json::parse(tar.readString(basePath / hex::format("{}.json", id))); auto provider = ImHexApi::Provider::createProvider(providerSettings["type"].get(), true); + ON_SCOPE_EXIT { if (!success) ImHexApi::Provider::remove(provider, true); }; if (provider == nullptr) { success = false; continue; } provider->loadSettings(providerSettings["settings"]); - if (!provider->open()) + if (!provider->open() || !provider->isAvailable() || !provider->isReadable()) success = false; else EventManager::post(provider); diff --git a/plugins/builtin/source/content/views/view_bookmarks.cpp b/plugins/builtin/source/content/views/view_bookmarks.cpp index 78c004288..5a9c8c162 100644 --- a/plugins/builtin/source/content/views/view_bookmarks.cpp +++ b/plugins/builtin/source/content/views/view_bookmarks.cpp @@ -103,6 +103,7 @@ namespace hex::plugin::builtin { ProjectFile::registerPerProviderHandler({ .basePath = "bookmarks.json", + .required = false, .load = [](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) -> bool { auto fileContent = tar.read(basePath); if (fileContent.empty()) diff --git a/plugins/builtin/source/content/views/view_data_processor.cpp b/plugins/builtin/source/content/views/view_data_processor.cpp index 5d6f3085d..82a90e960 100644 --- a/plugins/builtin/source/content/views/view_data_processor.cpp +++ b/plugins/builtin/source/content/views/view_data_processor.cpp @@ -34,6 +34,7 @@ namespace hex::plugin::builtin { ProjectFile::registerPerProviderHandler({ .basePath = "data_processor.json", + .required = false, .load = [this](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) { auto save = tar.readString(basePath); diff --git a/plugins/builtin/source/content/views/view_patches.cpp b/plugins/builtin/source/content/views/view_patches.cpp index 2af2b5d0c..d45500f34 100644 --- a/plugins/builtin/source/content/views/view_patches.cpp +++ b/plugins/builtin/source/content/views/view_patches.cpp @@ -15,6 +15,7 @@ namespace hex::plugin::builtin { ProjectFile::registerPerProviderHandler({ .basePath = "patches.json", + .required = false, .load = [](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) { auto json = nlohmann::json::parse(tar.read(basePath)); provider->getPatches() = json["patches"].get>(); diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index b96e7dbfc..9dc5afeb6 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -302,6 +302,7 @@ namespace hex::plugin::builtin { ProjectFile::registerPerProviderHandler({ .basePath = "pattern_source_code.hexpat", + .required = false, .load = [this](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) { std::string sourceCode = tar.readString(basePath); diff --git a/plugins/builtin/source/lang/de_DE.cpp b/plugins/builtin/source/lang/de_DE.cpp index 2127dba5c..d9a78f93f 100644 --- a/plugins/builtin/source/lang/de_DE.cpp +++ b/plugins/builtin/source/lang/de_DE.cpp @@ -115,6 +115,8 @@ namespace hex::plugin::builtin { { "hex.builtin.popup.close_provider.desc", "Es wurden ungespeicherte Änderungen an diesem Provider vorgenommen.\nBist du sicher, dass du ihn schliessen willst?" }, { "hex.builtin.popup.error.read_only", "Schreibzugriff konnte nicht erlangt werden. Datei wurde im Lesemodus geöffnet." }, { "hex.builtin.popup.error.open", "Öffnen der Datei fehlgeschlagen!" }, + { "hex.builtin.popup.error.project.load", "Laden des Projektes fehlgeschlagen!" }, + { "hex.builtin.popup.error.project.save", "Speichern des Projektes fehlgeschlagen!!" }, { "hex.builtin.popup.error.create", "Erstellen der neuen Datei fehlgeschlagen!" }, { "hex.builtin.popup.error.task_exception", "Fehler in Task '{}':\n\n{}" }, diff --git a/plugins/builtin/source/lang/en_US.cpp b/plugins/builtin/source/lang/en_US.cpp index 01266add6..c8807f9e6 100644 --- a/plugins/builtin/source/lang/en_US.cpp +++ b/plugins/builtin/source/lang/en_US.cpp @@ -117,6 +117,8 @@ namespace hex::plugin::builtin { { "hex.builtin.popup.error.read_only", "Couldn't get write access. File opened in read-only mode." }, { "hex.builtin.popup.error.open", "Failed to open file!" }, { "hex.builtin.popup.error.create", "Failed to create new file!" }, + { "hex.builtin.popup.error.project.load", "Failed to load project!" }, + { "hex.builtin.popup.error.project.save", "Failed to save project!" }, { "hex.builtin.popup.error.task_exception", "Exception thrown in Task '{}':\n\n{}" }, { "hex.builtin.menu.file", "File" }, diff --git a/plugins/builtin/source/lang/it_IT.cpp b/plugins/builtin/source/lang/it_IT.cpp index 7911582b7..cc0d3a0f9 100644 --- a/plugins/builtin/source/lang/it_IT.cpp +++ b/plugins/builtin/source/lang/it_IT.cpp @@ -116,6 +116,8 @@ namespace hex::plugin::builtin { { "hex.builtin.popup.error.read_only", "Impossibile scrivere sul File. File aperto solo in modalità lettura" }, { "hex.builtin.popup.error.open", "Impossibile aprire il File!" }, { "hex.builtin.popup.error.create", "Impossibile creare il nuovo File!" }, + //{ "hex.builtin.popup.error.project.load", "Failed to load project!" }, + //{ "hex.builtin.popup.error.project.save", "Failed to save project!" }, //{ "hex.builtin.popup.error.task_exception", "Exception thrown in Task '{}':\n\n{}" }, { "hex.builtin.menu.file", "File" }, diff --git a/plugins/builtin/source/lang/ja_JP.cpp b/plugins/builtin/source/lang/ja_JP.cpp index 1cf700a98..0b0f7ec8c 100644 --- a/plugins/builtin/source/lang/ja_JP.cpp +++ b/plugins/builtin/source/lang/ja_JP.cpp @@ -116,6 +116,8 @@ namespace hex::plugin::builtin { { "hex.builtin.popup.error.read_only", "書き込み権限を取得できませんでした。ファイルが読み取り専用で開かれました。" }, { "hex.builtin.popup.error.open", "ファイルを開けませんでした。" }, { "hex.builtin.popup.error.create", "新しいファイルを作成できませんでした。" }, + //{ "hex.builtin.popup.error.project.load", "Failed to load project!" }, + //{ "hex.builtin.popup.error.project.save", "Failed to save project!" }, //{ "hex.builtin.popup.error.task_exception", "Exception thrown in Task '{}':\n\n{}" }, { "hex.builtin.menu.file", "ファイル" }, diff --git a/plugins/builtin/source/lang/ko_KR.cpp b/plugins/builtin/source/lang/ko_KR.cpp index 3a10165b9..6872fda47 100644 --- a/plugins/builtin/source/lang/ko_KR.cpp +++ b/plugins/builtin/source/lang/ko_KR.cpp @@ -116,6 +116,8 @@ namespace hex::plugin::builtin { { "hex.builtin.popup.error.read_only", "쓰기 권한을 가져올 수 없습니다. 파일이 읽기 전용 모드로 열렸습니다." }, { "hex.builtin.popup.error.open", "파일을 여는데 실패했습니다!" }, { "hex.builtin.popup.error.create", "새 파일을 만드는데 실패했습니다!" }, + //{ "hex.builtin.popup.error.project.load", "Failed to load project!" }, + //{ "hex.builtin.popup.error.project.save", "Failed to save project!" }, //{ "hex.builtin.popup.error.task_exception", "Exception thrown in Task '{}':\n\n{}" }, { "hex.builtin.menu.file", "파일" }, diff --git a/plugins/builtin/source/lang/pt_BR.cpp b/plugins/builtin/source/lang/pt_BR.cpp index 553a727f2..f8d579dee 100644 --- a/plugins/builtin/source/lang/pt_BR.cpp +++ b/plugins/builtin/source/lang/pt_BR.cpp @@ -116,6 +116,8 @@ namespace hex::plugin::builtin { { "hex.builtin.popup.error.read_only", "Não foi possível obter acesso de gravação. Arquivo aberto no modo somente leitura." }, { "hex.builtin.popup.error.open", "Falha ao abrir o arquivo!" }, { "hex.builtin.popup.error.create", "Falha ao criar um novo arquivo!" }, + //{ "hex.builtin.popup.error.project.load", "Failed to load project!" }, + //{ "hex.builtin.popup.error.project.save", "Failed to save project!" }, //{ "hex.builtin.popup.error.task_exception", "Exception thrown in Task '{}':\n\n{}" }, { "hex.builtin.menu.file", "File" }, diff --git a/plugins/builtin/source/lang/zh_CN.cpp b/plugins/builtin/source/lang/zh_CN.cpp index e53d1c2d0..d7274f042 100644 --- a/plugins/builtin/source/lang/zh_CN.cpp +++ b/plugins/builtin/source/lang/zh_CN.cpp @@ -116,6 +116,8 @@ namespace hex::plugin::builtin { { "hex.builtin.popup.error.read_only", "无法获得写权限,文件以只读方式打开。" }, { "hex.builtin.popup.error.open", "打开文件失败!" }, { "hex.builtin.popup.error.create", "创建新文件失败!" }, + //{ "hex.builtin.popup.error.project.load", "Failed to load project!" }, + //{ "hex.builtin.popup.error.project.save", "Failed to save project!" }, { "hex.builtin.popup.error.task_exception", "任务 '{}' 异常:\n\n{}" }, { "hex.builtin.menu.file", "文件" }, diff --git a/plugins/builtin/source/lang/zh_TW.cpp b/plugins/builtin/source/lang/zh_TW.cpp index 06a722bcf..ceda82022 100644 --- a/plugins/builtin/source/lang/zh_TW.cpp +++ b/plugins/builtin/source/lang/zh_TW.cpp @@ -116,6 +116,8 @@ namespace hex::plugin::builtin { { "hex.builtin.popup.error.read_only", "無法取得寫入權限。檔案已以唯讀模式開啟。" }, { "hex.builtin.popup.error.open", "無法開啟檔案!" }, { "hex.builtin.popup.error.create", "無法建立新檔案!" }, + //{ "hex.builtin.popup.error.project.load", "Failed to load project!" }, + //{ "hex.builtin.popup.error.project.save", "Failed to save project!" }, //{ "hex.builtin.popup.error.task_exception", "Exception thrown in Task '{}':\n\n{}" }, { "hex.builtin.menu.file", "檔案" },