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 <atomic>
#include <functional> #include <functional>
#include <js/PropertyAndElement.h> #include <js/PropertyAndElement.h>
#include <numeric>
#include <ranges> #include <ranges>
#include <string> #include <string>
#include <string_view> #include <string_view>
@@ -487,7 +488,7 @@ public:
CXeromyces xmb_file; CXeromyces xmb_file;
XMBElementList nodes; // children of root XMBElementList nodes; // children of root
PS::Loader::Task ReadEntities(XMBElement parent); void ReadEntities(XMBElement parent, CSimulation2& sim);
private: private:
@@ -509,9 +510,6 @@ private:
int at_seed; int at_seed;
int at_turret; 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 // maximum used entity ID, so we can safely allocate new ones
entity_id_t max_uid; entity_id_t max_uid;
@@ -559,13 +557,6 @@ void CXMLReader::Init(const VfsPath& xml_filename)
ENSURE(xmb_file.GetElementStringView(root.GetNodeName()) == "Scenario"); ENSURE(xmb_file.GetElementStringView(root.GetNodeName()) == "Scenario");
nodes = root.GetChildNodes(); 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 // Find the maximum entity ID, so we can safely allocate new IDs without conflicts
max_uid = SYSTEM_ENTITY; max_uid = SYSTEM_ENTITY;
@@ -995,15 +986,7 @@ 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); ENSURE(entity.GetNodeName() == el_entity);
@@ -1104,25 +1087,21 @@ PS::Loader::Task CXMLReader::ReadEntities(XMBElement parent)
debug_warn(L"Invalid map XML data"); debug_warn(L"Invalid map XML data");
} }
CmpPtr<ICmpPlayerManager> cmpPlayerManager(sim, SYSTEM_ENTITY);
entity_id_t player = cmpPlayerManager->GetPlayerByID(PlayerID); entity_id_t player = cmpPlayerManager->GetPlayerByID(PlayerID);
CmpPtr<ICmpPlayer> cmpPlayer(sim, player); CmpPtr<ICmpPlayer> cmpPlayer(sim, player);
// Don't add entities for removed players. // Don't add entities for removed players.
if (cmpPlayer && cmpPlayer->IsRemoved()) if (cmpPlayer && cmpPlayer->IsRemoved())
{ return;
completed_jobs++;
co_yield 100 * completed_jobs / total_jobs;
continue;
}
entity_id_t ent = sim.AddEntity(TemplateName, EntityUid); entity_id_t ent = sim.AddEntity(TemplateName, EntityUid);
if (ent == INVALID_ENTITY || player == INVALID_ENTITY) if (ent == INVALID_ENTITY || player == INVALID_ENTITY)
{ {
// Don't add entities with invalid player IDs // Don't add entities with invalid player IDs
LOGERROR("Failed to load entity template '%s'", utf8_from_wstring(TemplateName)); LOGERROR("Failed to load entity template '%s'", utf8_from_wstring(TemplateName));
return;
} }
else
{
CmpPtr<ICmpPosition> cmpPosition(sim, ent); CmpPtr<ICmpPosition> cmpPosition(sim, ent);
if (cmpPosition) if (cmpPosition)
{ {
@@ -1181,13 +1160,6 @@ PS::Loader::Task CXMLReader::ReadEntities(XMBElement parent)
} }
} }
completed_jobs++;
co_yield 100 * completed_jobs / total_jobs;
}
co_return 0;
}
void CXMLReader::ReadXML() void CXMLReader::ReadXML()
{ {
for (XMBElement node : nodes) for (XMBElement node : nodes)
@@ -1281,26 +1253,32 @@ int CMapReader::ReadXML()
// progressive // progressive
PS::Loader::Task CMapReader::ReadXMLEntities() PS::Loader::Task CMapReader::ReadXMLEntities()
{ {
ENSURE(pSimulation2);
if (m_SkipEntities) if (m_SkipEntities)
co_return 0; co_return 0;
if (!m_XmlReader) if (!m_XmlReader)
m_XmlReader = std::make_unique<CXMLReader>(m_FilenameXml, *this); 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) for (XMBElement node : m_XmlReader->nodes)
{ {
CStr name = m_XmlReader->xmb_file.GetElementString(node.GetNodeName()); CStr name = m_XmlReader->xmb_file.GetElementString(node.GetNodeName());
if (name != "Entities") if (name != "Entities")
continue; continue;
PS::Loader::Task subTask{m_XmlReader->ReadEntities(node)}; for (XMBElement child : node.GetChildNodes())
while (!subTask.IsDone())
{ {
co_yield subTask.GetProgress(); m_XmlReader->ReadEntities(child, *pSimulation2);
subTask.Step(-1);
completedJobs++;
co_yield 100 * completedJobs / totalJobs;
} }
if (subTask.Get() < 0)
co_return subTask.Get();
} }
m_XmlReader.reset(); m_XmlReader.reset();