1
0
forked from mirrors/0ad

Don't use PS::Loader::Task outside of Loader

PS::Loader::Task is made for the Loader and it's combersome to use it
outside. Also there is an overhead.
This commit is contained in:
phosit
2025-11-03 12:34:54 +01:00
parent 2fc61dd172
commit 067a7abc72
+18 -40
View File
@@ -79,6 +79,7 @@
#include <atomic>
#include <functional>
#include <js/PropertyAndElement.h>
#include <numeric>
#include <ranges>
#include <string>
#include <string_view>
@@ -487,7 +488,7 @@ public:
CXeromyces xmb_file;
XMBElementList nodes; // children of root
PS::Loader::Task ReadEntities(XMBElement parent);
void ReadEntities(XMBElement parent, CSimulation2& sim);
private:
@@ -509,9 +510,6 @@ private:
int at_seed;
int at_turret;
// # entities+nonentities processed and total (for progress calc)
int completed_jobs, total_jobs;
// maximum used entity ID, so we can safely allocate new ones
entity_id_t max_uid;
@@ -559,13 +557,6 @@ void CXMLReader::Init(const VfsPath& xml_filename)
ENSURE(xmb_file.GetElementStringView(root.GetNodeName()) == "Scenario");
nodes = root.GetChildNodes();
// find out total number of entities+nonentities
// (used when calculating progress)
completed_jobs = 0;
total_jobs = 0;
for (XMBElement node : nodes)
total_jobs += node.GetChildNodes().size();
// Find the maximum entity ID, so we can safely allocate new IDs without conflicts
max_uid = SYSTEM_ENTITY;
@@ -995,16 +986,8 @@ void CXMLReader::ReadTriggers(XMBElement /*parent*/)
{
}
PS::Loader::Task CXMLReader::ReadEntities(XMBElement parent)
void CXMLReader::ReadEntities(XMBElement entity, CSimulation2& sim)
{
XMBElementList entities = parent.GetChildNodes();
ENSURE(m_MapReader.pSimulation2);
CSimulation2& sim = *m_MapReader.pSimulation2;
CmpPtr<ICmpPlayerManager> cmpPlayerManager(sim, SYSTEM_ENTITY);
for (XMBElement entity : entities)
{
ENSURE(entity.GetNodeName() == el_entity);
XMBAttributeList attrs = entity.GetAttributes();
@@ -1104,25 +1087,21 @@ PS::Loader::Task CXMLReader::ReadEntities(XMBElement parent)
debug_warn(L"Invalid map XML data");
}
CmpPtr<ICmpPlayerManager> cmpPlayerManager(sim, SYSTEM_ENTITY);
entity_id_t player = cmpPlayerManager->GetPlayerByID(PlayerID);
CmpPtr<ICmpPlayer> cmpPlayer(sim, player);
// Don't add entities for removed players.
if (cmpPlayer && cmpPlayer->IsRemoved())
{
completed_jobs++;
co_yield 100 * completed_jobs / total_jobs;
continue;
}
return;
entity_id_t ent = sim.AddEntity(TemplateName, EntityUid);
if (ent == INVALID_ENTITY || player == INVALID_ENTITY)
{
// Don't add entities with invalid player IDs
LOGERROR("Failed to load entity template '%s'", utf8_from_wstring(TemplateName));
return;
}
else
{
CmpPtr<ICmpPosition> cmpPosition(sim, ent);
if (cmpPosition)
{
@@ -1179,13 +1158,6 @@ PS::Loader::Task CXMLReader::ReadEntities(XMBElement parent)
// Focus on civil centre or first entity owned by player
m_MapReader.m_StartingCameraTarget = ent;
}
}
completed_jobs++;
co_yield 100 * completed_jobs / total_jobs;
}
co_return 0;
}
void CXMLReader::ReadXML()
@@ -1281,26 +1253,32 @@ int CMapReader::ReadXML()
// progressive
PS::Loader::Task CMapReader::ReadXMLEntities()
{
ENSURE(pSimulation2);
if (m_SkipEntities)
co_return 0;
if (!m_XmlReader)
m_XmlReader = std::make_unique<CXMLReader>(m_FilenameXml, *this);
const std::size_t totalJobs{std::transform_reduce(m_XmlReader->nodes.begin(),
m_XmlReader->nodes.end(), static_cast<std::size_t>(0), std::plus<>{}, [](XMBElement node)
{
return node.GetChildNodes().size();
})};
std::size_t completedJobs{0};
for (XMBElement node : m_XmlReader->nodes)
{
CStr name = m_XmlReader->xmb_file.GetElementString(node.GetNodeName());
if (name != "Entities")
continue;
PS::Loader::Task subTask{m_XmlReader->ReadEntities(node)};
while (!subTask.IsDone())
for (XMBElement child : node.GetChildNodes())
{
co_yield subTask.GetProgress();
subTask.Step(-1);
m_XmlReader->ReadEntities(child, *pSimulation2);
completedJobs++;
co_yield 100 * completedJobs / totalJobs;
}
if (subTask.Get() < 0)
co_return subTask.Get();
}
m_XmlReader.reset();