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

feat: combine loading bar and zone list

This commit is contained in:
Jan Laupetin
2025-10-16 23:04:03 +01:00
parent 78538d68f5
commit 23f5ad67e9
11 changed files with 276 additions and 129 deletions

View File

@@ -10,7 +10,7 @@ namespace fs = std::filesystem;
namespace
{
constexpr double MIN_PROGRESS_TO_REPORT = 0.005;
constexpr double MIN_PROGRESS_TO_REPORT = 0.5;
class LoadingEventProgressReporter : public ProgressCallback
{
@@ -23,7 +23,7 @@ namespace
void OnProgress(const size_t current, const size_t total) override
{
const double percentage = static_cast<double>(current) / static_cast<double>(total);
const double percentage = static_cast<double>(current) / static_cast<double>(total) * 100.0;
if (percentage - m_last_progress >= MIN_PROGRESS_TO_REPORT)
{
@@ -38,38 +38,53 @@ namespace
};
} // namespace
LoadedZone::LoadedZone(std::unique_ptr<Zone> zone, std::string filePath)
: m_zone(std::move(zone)),
m_file_path(std::move(filePath))
{
}
void FastFileContext::Destroy()
{
// Unload all zones
m_loaded_zones.clear();
}
result::Expected<Zone*, std::string> FastFileContext::LoadFastFile(const std::string& path)
result::Expected<LoadedZone*, std::string> FastFileContext::LoadFastFile(const std::string& path)
{
auto zone = ZoneLoading::LoadZone(path, std::make_unique<LoadingEventProgressReporter>(fs::path(path).filename().replace_extension().string()));
if (!zone)
return result::Unexpected(std::move(zone.error()));
auto* result = m_loaded_zones.emplace_back(std::move(*zone)).get();
auto loadedZone = std::make_unique<LoadedZone>(std::move(*zone), path);
ui::NotifyZoneLoaded(result->m_name, path);
LoadedZone* result;
{
std::lock_guard lock(m_zone_lock);
result = m_loaded_zones.emplace_back(std::move(loadedZone)).get();
}
ui::NotifyZoneLoaded(*result);
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();
std::lock_guard lock(m_zone_lock);
const auto existingZone = std::ranges::find_if(m_loaded_zones,
[&zoneName](const std::unique_ptr<LoadedZone>& loadedZone)
{
return loadedZone->m_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));