Use a coroutine for Loader tasks

Some tasks are invoked multiple times. Normally those tasks are broken
up inside a loop and had to be continued there. With coroutines that is
easier as it's possible to suspend inside a loop.

Coroutines which are lambdas should not capture anythig as the lifetime
of the captured values might end before the coroutine completes. For
that purpose `std::bind_front` is used.
This commit is contained in:
phosit
2025-10-20 21:55:24 +02:00
parent b67faf35c3
commit 5586802b86
12 changed files with 421 additions and 322 deletions
+6 -15
View File
@@ -132,7 +132,7 @@ public:
return static_cast<CSimulation2Impl*>(param)->ReloadChangedFile(path);
}
int ProgressiveLoad();
PS::Loader::Task ProgressiveLoad();
void Update(int turnLength, const std::vector<SimulationCommand>& commands);
static void UpdateComponents(CSimContext& simContext, fixed turnLengthFixed, const std::vector<SimulationCommand>& commands);
void Interpolate(float simFrameLength, float frameOffset, float realFrameLength);
@@ -267,15 +267,9 @@ Status CSimulation2Impl::ReloadChangedFile(const VfsPath& path)
return INFO::OK;
}
int CSimulation2Impl::ProgressiveLoad()
PS::Loader::Task CSimulation2Impl::ProgressiveLoad()
{
// yield after this time is reached. balances increased progress bar
// smoothness vs. slowing down loading.
const double end_time = timer_Time() + 200e-3;
int ret;
do
while (true)
{
bool progressed = false;
int total = 0;
@@ -286,13 +280,10 @@ int CSimulation2Impl::ProgressiveLoad()
m_ComponentManager.BroadcastMessage(msg);
if (!progressed || total == 0)
return 0; // we have nothing left to load
co_return 0; // we have nothing left to load
ret = Clamp(100*progress / total, 1, 100);
co_yield Clamp(100*progress / total, 1, 100);
}
while (timer_Time() < end_time);
return ret;
}
void CSimulation2Impl::DumpSerializationTestState(SerializationTestState& state, const OsPath& path, const OsPath::String& suffix)
@@ -849,7 +840,7 @@ void CSimulation2::LoadMapSettings()
m->LoadTriggerScripts(m->m_ComponentManager, m->m_MapSettings, &m->m_LoadedScripts);
}
int CSimulation2::ProgressiveLoad()
PS::Loader::Task CSimulation2::ProgressiveLoad()
{
return m->ProgressiveLoad();
}
+2 -1
View File
@@ -21,6 +21,7 @@
#include "lib/code_annotation.h"
#include "lib/file/vfs/vfs_path.h"
#include "lib/status.h"
#include "ps/Loader.h"
#include "simulation2/system/DebugOptions.h"
#include "simulation2/system/Entity.h"
@@ -140,7 +141,7 @@ public:
/**
* RegMemFun incremental loader function.
*/
int ProgressiveLoad();
PS::Loader::Task ProgressiveLoad();
/**
* Reload any scripts that were loaded from the given filename.