Skip to content

Commit 1d4cbbe

Browse files
committed
ux: Handle project loading errors better
1 parent e0e2996 commit 1d4cbbe

17 files changed

+43
-5
lines changed

lib/libimhex/include/hex/api/project_file_manager.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ namespace hex {
1919
struct Handler {
2020
using Function = std::function<bool(const std::fs::path &, Tar &tar)>;
2121
std::fs::path basePath;
22+
bool required;
2223
Function load, store;
2324
};
2425

2526
struct ProviderHandler {
2627
using Function = std::function<bool(prv::Provider *provider, const std::fs::path &, Tar &tar)>;
2728
std::fs::path basePath;
29+
bool required;
2830
Function load, store;
2931
};
3032

lib/libimhex/source/api/project_file_manager.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ namespace hex {
4545
log::info("{}", e.what());
4646
result = false;
4747
}
48+
49+
if (!result && handler.required) {
50+
return false;
51+
}
4852
}
4953

5054
for (const auto &provider : ImHexApi::Provider::getProviders()) {
@@ -57,12 +61,16 @@ namespace hex {
5761
log::info("{}", e.what());
5862
result = false;
5963
}
64+
65+
if (!result && handler.required) {
66+
return false;
67+
}
6068
}
6169
}
6270

6371
ProjectFile::s_currProjectPath = filePath;
6472

65-
return result;
73+
return true;
6674
}
6775

6876
bool ProjectFile::store(std::optional<std::fs::path> filePath) {

plugins/builtin/source/content/events.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ namespace hex::plugin::builtin {
7878
} else if (name == "Open Project") {
7979
fs::openFileBrowser(fs::DialogMode::Open, { {"Project File", "hexproj"} },
8080
[](const auto &path) {
81-
ProjectFile::load(path);
81+
if (!ProjectFile::load(path)) {
82+
View::showErrorPopup("hex.builtin.popup.error.project.load"_lang);
83+
}
8284
});
8385
}
8486
});

plugins/builtin/source/content/main_menu_items.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ namespace hex::plugin::builtin {
6464
fs::openFileBrowser(fs::DialogMode::Open, { {"Project File", "hexproj"}
6565
},
6666
[](const auto &path) {
67-
ProjectFile::load(path);
67+
if (!ProjectFile::load(path)) {
68+
View::showErrorPopup("hex.builtin.popup.error.project.load"_lang);
69+
}
6870
});
6971
}
7072

@@ -75,7 +77,9 @@ namespace hex::plugin::builtin {
7577
path.replace_extension(".hexproj");
7678
}
7779

78-
ProjectFile::store(path);
80+
if (!ProjectFile::load(path)) {
81+
View::showErrorPopup("hex.builtin.popup.error.project.load"_lang);
82+
}
7983
});
8084
}
8185
});

plugins/builtin/source/content/providers.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace hex::plugin::builtin {
2424

2525
ProjectFile::registerHandler({
2626
.basePath = "providers",
27+
.required = true,
2728
.load = [](const std::fs::path &basePath, Tar &tar) {
2829
auto json = nlohmann::json::parse(tar.readString(basePath / "providers.json"));
2930
auto providerIds = json["providers"].get<std::vector<int>>();
@@ -33,13 +34,14 @@ namespace hex::plugin::builtin {
3334
auto providerSettings = nlohmann::json::parse(tar.readString(basePath / hex::format("{}.json", id)));
3435

3536
auto provider = ImHexApi::Provider::createProvider(providerSettings["type"].get<std::string>(), true);
37+
ON_SCOPE_EXIT { if (!success) ImHexApi::Provider::remove(provider, true); };
3638
if (provider == nullptr) {
3739
success = false;
3840
continue;
3941
}
4042

4143
provider->loadSettings(providerSettings["settings"]);
42-
if (!provider->open())
44+
if (!provider->open() || !provider->isAvailable() || !provider->isReadable())
4345
success = false;
4446
else
4547
EventManager::post<EventProviderOpened>(provider);

plugins/builtin/source/content/views/view_bookmarks.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ namespace hex::plugin::builtin {
103103

104104
ProjectFile::registerPerProviderHandler({
105105
.basePath = "bookmarks.json",
106+
.required = false,
106107
.load = [](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) -> bool {
107108
auto fileContent = tar.read(basePath);
108109
if (fileContent.empty())

plugins/builtin/source/content/views/view_data_processor.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace hex::plugin::builtin {
3434

3535
ProjectFile::registerPerProviderHandler({
3636
.basePath = "data_processor.json",
37+
.required = false,
3738
.load = [this](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) {
3839
auto save = tar.readString(basePath);
3940

plugins/builtin/source/content/views/view_patches.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace hex::plugin::builtin {
1515

1616
ProjectFile::registerPerProviderHandler({
1717
.basePath = "patches.json",
18+
.required = false,
1819
.load = [](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) {
1920
auto json = nlohmann::json::parse(tar.read(basePath));
2021
provider->getPatches() = json["patches"].get<std::map<u64, u8>>();

plugins/builtin/source/content/views/view_pattern_editor.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ namespace hex::plugin::builtin {
302302

303303
ProjectFile::registerPerProviderHandler({
304304
.basePath = "pattern_source_code.hexpat",
305+
.required = false,
305306
.load = [this](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) {
306307
std::string sourceCode = tar.readString(basePath);
307308

plugins/builtin/source/lang/de_DE.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ namespace hex::plugin::builtin {
115115
{ "hex.builtin.popup.close_provider.desc", "Es wurden ungespeicherte Änderungen an diesem Provider vorgenommen.\nBist du sicher, dass du ihn schliessen willst?" },
116116
{ "hex.builtin.popup.error.read_only", "Schreibzugriff konnte nicht erlangt werden. Datei wurde im Lesemodus geöffnet." },
117117
{ "hex.builtin.popup.error.open", "Öffnen der Datei fehlgeschlagen!" },
118+
{ "hex.builtin.popup.error.project.load", "Laden des Projektes fehlgeschlagen!" },
119+
{ "hex.builtin.popup.error.project.save", "Speichern des Projektes fehlgeschlagen!!" },
118120
{ "hex.builtin.popup.error.create", "Erstellen der neuen Datei fehlgeschlagen!" },
119121
{ "hex.builtin.popup.error.task_exception", "Fehler in Task '{}':\n\n{}" },
120122

plugins/builtin/source/lang/en_US.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ namespace hex::plugin::builtin {
117117
{ "hex.builtin.popup.error.read_only", "Couldn't get write access. File opened in read-only mode." },
118118
{ "hex.builtin.popup.error.open", "Failed to open file!" },
119119
{ "hex.builtin.popup.error.create", "Failed to create new file!" },
120+
{ "hex.builtin.popup.error.project.load", "Failed to load project!" },
121+
{ "hex.builtin.popup.error.project.save", "Failed to save project!" },
120122
{ "hex.builtin.popup.error.task_exception", "Exception thrown in Task '{}':\n\n{}" },
121123

122124
{ "hex.builtin.menu.file", "File" },

plugins/builtin/source/lang/it_IT.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ namespace hex::plugin::builtin {
116116
{ "hex.builtin.popup.error.read_only", "Impossibile scrivere sul File. File aperto solo in modalità lettura" },
117117
{ "hex.builtin.popup.error.open", "Impossibile aprire il File!" },
118118
{ "hex.builtin.popup.error.create", "Impossibile creare il nuovo File!" },
119+
//{ "hex.builtin.popup.error.project.load", "Failed to load project!" },
120+
//{ "hex.builtin.popup.error.project.save", "Failed to save project!" },
119121
//{ "hex.builtin.popup.error.task_exception", "Exception thrown in Task '{}':\n\n{}" },
120122

121123
{ "hex.builtin.menu.file", "File" },

plugins/builtin/source/lang/ja_JP.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ namespace hex::plugin::builtin {
116116
{ "hex.builtin.popup.error.read_only", "書き込み権限を取得できませんでした。ファイルが読み取り専用で開かれました。" },
117117
{ "hex.builtin.popup.error.open", "ファイルを開けませんでした。" },
118118
{ "hex.builtin.popup.error.create", "新しいファイルを作成できませんでした。" },
119+
//{ "hex.builtin.popup.error.project.load", "Failed to load project!" },
120+
//{ "hex.builtin.popup.error.project.save", "Failed to save project!" },
119121
//{ "hex.builtin.popup.error.task_exception", "Exception thrown in Task '{}':\n\n{}" },
120122

121123
{ "hex.builtin.menu.file", "ファイル" },

plugins/builtin/source/lang/ko_KR.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ namespace hex::plugin::builtin {
116116
{ "hex.builtin.popup.error.read_only", "쓰기 권한을 가져올 수 없습니다. 파일이 읽기 전용 모드로 열렸습니다." },
117117
{ "hex.builtin.popup.error.open", "파일을 여는데 실패했습니다!" },
118118
{ "hex.builtin.popup.error.create", "새 파일을 만드는데 실패했습니다!" },
119+
//{ "hex.builtin.popup.error.project.load", "Failed to load project!" },
120+
//{ "hex.builtin.popup.error.project.save", "Failed to save project!" },
119121
//{ "hex.builtin.popup.error.task_exception", "Exception thrown in Task '{}':\n\n{}" },
120122

121123
{ "hex.builtin.menu.file", "파일" },

plugins/builtin/source/lang/pt_BR.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ namespace hex::plugin::builtin {
116116
{ "hex.builtin.popup.error.read_only", "Não foi possível obter acesso de gravação. Arquivo aberto no modo somente leitura." },
117117
{ "hex.builtin.popup.error.open", "Falha ao abrir o arquivo!" },
118118
{ "hex.builtin.popup.error.create", "Falha ao criar um novo arquivo!" },
119+
//{ "hex.builtin.popup.error.project.load", "Failed to load project!" },
120+
//{ "hex.builtin.popup.error.project.save", "Failed to save project!" },
119121
//{ "hex.builtin.popup.error.task_exception", "Exception thrown in Task '{}':\n\n{}" },
120122

121123
{ "hex.builtin.menu.file", "File" },

plugins/builtin/source/lang/zh_CN.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ namespace hex::plugin::builtin {
116116
{ "hex.builtin.popup.error.read_only", "无法获得写权限,文件以只读方式打开。" },
117117
{ "hex.builtin.popup.error.open", "打开文件失败!" },
118118
{ "hex.builtin.popup.error.create", "创建新文件失败!" },
119+
//{ "hex.builtin.popup.error.project.load", "Failed to load project!" },
120+
//{ "hex.builtin.popup.error.project.save", "Failed to save project!" },
119121
{ "hex.builtin.popup.error.task_exception", "任务 '{}' 异常:\n\n{}" },
120122

121123
{ "hex.builtin.menu.file", "文件" },

plugins/builtin/source/lang/zh_TW.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ namespace hex::plugin::builtin {
116116
{ "hex.builtin.popup.error.read_only", "無法取得寫入權限。檔案已以唯讀模式開啟。" },
117117
{ "hex.builtin.popup.error.open", "無法開啟檔案!" },
118118
{ "hex.builtin.popup.error.create", "無法建立新檔案!" },
119+
//{ "hex.builtin.popup.error.project.load", "Failed to load project!" },
120+
//{ "hex.builtin.popup.error.project.save", "Failed to save project!" },
119121
//{ "hex.builtin.popup.error.task_exception", "Exception thrown in Task '{}':\n\n{}" },
120122

121123
{ "hex.builtin.menu.file", "檔案" },

0 commit comments

Comments
 (0)