2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-11-17 18:52:06 +00:00

chore: add error handling for fastfile bind

This commit is contained in:
Jan Laupetin
2025-10-11 14:42:52 +01:00
parent 2037cf3258
commit 098be53559
4 changed files with 36 additions and 14 deletions

View File

@@ -2,8 +2,11 @@
#include "ZoneLoading.h" #include "ZoneLoading.h"
bool FastFileContext::LoadFastFile(const std::string& path) std::optional<Zone*> FastFileContext::LoadFastFile(const std::string& path)
{ {
m_loaded_zones.emplace_back(ZoneLoading::LoadZone(path)); auto zone = ZoneLoading::LoadZone(path);
return true; if (!zone)
return std::nullopt;
return m_loaded_zones.emplace_back(std::move(zone)).get();
} }

View File

@@ -2,12 +2,13 @@
#include "Zone/Zone.h" #include "Zone/Zone.h"
#include <memory> #include <memory>
#include <optional>
#include <vector> #include <vector>
class FastFileContext class FastFileContext
{ {
public: public:
bool LoadFastFile(const std::string& path); std::optional<Zone*> LoadFastFile(const std::string& path);
std::vector<std::unique_ptr<Zone>> m_loaded_zones; std::vector<std::unique_ptr<Zone>> m_loaded_zones;
}; };

View File

@@ -5,8 +5,26 @@
namespace namespace
{ {
void LoadFastFile(webview::webview& wv, std::string id, std::string path) // NOLINT(performance-unnecessary-value-param) Copy is made for thread safety
{
ModManContext::Get().m_db_thread.Dispatch(
[&wv, id, path]
{
const auto maybeZone = ModManContext::Get().m_fast_file.LoadFastFile(path);
} if (maybeZone)
{
ui::PromiseResolve(wv, id, true);
con::debug("Loaded zone \"{}\"", maybeZone.value()->m_name);
}
else
{
con::warn("Failed to load zone \"{}\"", path);
ui::PromiseReject(wv, id, false);
}
});
}
} // namespace
namespace ui namespace ui
{ {
@@ -16,13 +34,7 @@ namespace ui
"loadFastFile", "loadFastFile",
[&wv](const std::string& id, std::string path) [&wv](const std::string& id, std::string path)
{ {
std::string idMove(id); LoadFastFile(wv, id, std::move(path));
ModManContext::Get().m_db_thread.Dispatch(
[&wv, idMove, path]
{
ModManContext::Get().m_fast_file.LoadFastFile(path);
PromiseResolve(wv, idMove, true);
});
}); });
} }
} // namespace ui } // namespace ui

View File

@@ -10,8 +10,14 @@ async function onOpenFastfileClick() {
(await webviewBinds.openFileDialog({ filters: [{ name: "Fastfiles", filter: "*.ff" }] })) ?? ""; (await webviewBinds.openFileDialog({ filters: [{ name: "Fastfiles", filter: "*.ff" }] })) ?? "";
loadingFastFile.value = true; loadingFastFile.value = true;
await webviewBinds.loadFastFile(lastPath.value);
webviewBinds.loadFastFile(lastPath.value)
.catch((e) => {
console.error("Failed to load fastfile", e);
})
.finally(() => {
loadingFastFile.value = false; loadingFastFile.value = false;
});
} }
</script> </script>