2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-11-30 00:07:47 +00:00

chore: track loaded zones in ui

This commit is contained in:
Jan Laupetin
2025-10-11 16:25:14 +01:00
parent 7cefaee41c
commit 49f2000bad
12 changed files with 166 additions and 31 deletions

View File

@@ -1,12 +1,42 @@
#include "FastFileContext.h"
#include "Web/Binds/FastFileBinds.h"
#include "Web/UiCommunication.h"
#include "ZoneLoading.h"
void FastFileContext::Destroy()
{
// Unload all zones
m_loaded_zones.clear();
}
result::Expected<Zone*, std::string> FastFileContext::LoadFastFile(const std::string& path)
{
auto zone = ZoneLoading::LoadZone(path);
if (!zone)
return result::Unexpected(std::move(zone.error()));
return m_loaded_zones.emplace_back(std::move(*zone)).get();
auto* result = m_loaded_zones.emplace_back(std::move(*zone)).get();
ui::NotifyZoneLoaded(result->m_name, path);
return result;
}
result::Expected<NoResult, std::string> FastFileContext::UnloadZone(const std::string& zoneName)
{
const auto existingZone = std::ranges::find_if(m_loaded_zones,
[&zoneName](const std::unique_ptr<Zone>& zone)
{
return zone->m_name == zoneName;
});
if (existingZone != m_loaded_zones.end())
{
m_loaded_zones.erase(existingZone);
ui::NotifyZoneUnloaded(zoneName);
return NoResult();
}
return result::Unexpected(std::format("No zone with name {} loaded", zoneName));
}

View File

@@ -9,7 +9,10 @@
class FastFileContext
{
public:
void Destroy();
result::Expected<Zone*, std::string> LoadFastFile(const std::string& path);
result::Expected<NoResult, std::string> UnloadZone(const std::string& zoneName);
std::vector<std::unique_ptr<Zone>> m_loaded_zones;
};

View File

@@ -13,5 +13,6 @@ void ModManContext::Startup()
void ModManContext::Destroy()
{
m_fast_file.Destroy();
m_db_thread.Terminate();
}

View File

@@ -8,6 +8,6 @@ namespace ui
void RegisterAllBinds(webview::webview& wv)
{
RegisterDialogHandlerBinds(wv);
RegisterFastFileBinds(wv);
RegisterZoneBinds(wv);
}
} // namespace ui

View File

@@ -3,8 +3,27 @@
#include "Context/ModManContext.h"
#include "Web/UiCommunication.h"
#include "Json/JsonExtension.h"
namespace
{
class ZoneLoadedDto
{
public:
std::string zoneName;
std::string filePath;
};
NLOHMANN_DEFINE_TYPE_EXTENSION(ZoneLoadedDto, zoneName, filePath);
class ZoneUnloadedDto
{
public:
std::string zoneName;
};
NLOHMANN_DEFINE_TYPE_EXTENSION(ZoneUnloadedDto, zoneName);
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(
@@ -24,11 +43,47 @@ namespace
}
});
}
void UnloadZone(webview::webview& wv, std::string id, std::string zoneName) // NOLINT(performance-unnecessary-value-param) Copy is made for thread safety
{
ModManContext::Get().m_db_thread.Dispatch(
[&wv, id, zoneName]
{
auto result = ModManContext::Get().m_fast_file.UnloadZone(zoneName);
if (result)
{
con::debug("Unloaded zone \"{}\"", zoneName);
ui::PromiseResolve(wv, id, true);
}
else
{
con::warn("Failed unloading zone {}: {}", zoneName, result.error());
ui::PromiseReject(wv, id, std::move(result).error());
}
});
}
} // namespace
namespace ui
{
void RegisterFastFileBinds(webview::webview& wv)
void NotifyZoneLoaded(std::string zoneName, std::string fastFilePath)
{
const ZoneLoadedDto dto{
.zoneName = std::move(zoneName),
.filePath = std::move(fastFilePath),
};
Notify(*ModManContext::Get().m_main_webview, "zoneLoaded", dto);
}
void NotifyZoneUnloaded(std::string zoneName)
{
const ZoneUnloadedDto dto{
.zoneName = std::move(zoneName),
};
Notify(*ModManContext::Get().m_main_webview, "zoneUnloaded", dto);
}
void RegisterZoneBinds(webview::webview& wv)
{
BindAsync<std::string>(wv,
"loadFastFile",
@@ -36,5 +91,12 @@ namespace ui
{
LoadFastFile(wv, id, std::move(path));
});
BindAsync<std::string>(wv,
"unloadZone",
[&wv](const std::string& id, std::string zoneName)
{
UnloadZone(wv, id, std::move(zoneName));
});
}
} // namespace ui

View File

@@ -4,5 +4,8 @@
namespace ui
{
void RegisterFastFileBinds(webview::webview& wv);
}
void NotifyZoneLoaded(std::string zoneName, std::string fastFilePath);
void NotifyZoneUnloaded(std::string zoneName);
void RegisterZoneBinds(webview::webview& wv);
} // namespace ui