Introduce a namespace in Loader

All functions had a `LDR_` prefix. The prefix is removed.
Functions and globals which are only used in Loader.cpp are now
contained in an anonymous namespace.
This commit is contained in:
phosit 2025-10-20 21:35:40 +02:00
parent f257ce8f9c
commit 1917d034fd
No known key found for this signature in database
GPG key ID: C9430B600671C268
15 changed files with 94 additions and 81 deletions

View file

@ -206,17 +206,17 @@ CMiniMapTexture& CGameView::GetMiniMapTexture()
void CGameView::RegisterInit()
{
// CGameView init
LDR_Register([this]
PS::Loader::Register([this]
{
m->CameraController->LoadConfig();
return 0;
}, L"CGameView init", 1);
LDR_Register([]
PS::Loader::Register([]
{
return g_TexMan.StartTerrainTextures();
}, L"StartTerrainTextures", 1);
LDR_Register([]
PS::Loader::Register([]
{
return g_TexMan.PollTerrainTextures();
}, L"PollTerrainTextures", 60);

View file

@ -150,55 +150,55 @@ void CMapReader::LoadMap(const VfsPath& pathname, const ScriptContext& cx, JS::
// load map or script settings script
if (settings.isUndefined())
LDR_Register([this]
PS::Loader::Register([this]
{
return LoadScriptSettings();
}, L"CMapReader::LoadScriptSettings", 50);
else
LDR_Register([this]
PS::Loader::Register([this]
{
return LoadRMSettings();
}, L"CMapReader::LoadRMSettings", 50);
// load player settings script (must be done before reading map)
LDR_Register([this]
PS::Loader::Register([this]
{
return LoadPlayerSettings();
}, L"CMapReader::LoadPlayerSettings", 50);
// unpack the data
if (!only_xml)
LDR_Register([this]
PS::Loader::Register([this]
{
return UnpackTerrain();
}, L"CMapReader::UnpackMap", 1200);
// read the corresponding XML file
LDR_Register([this]
PS::Loader::Register([this]
{
return ReadXML();
}, L"CMapReader::ReadXML", 50);
// apply terrain data to the world
LDR_Register([this]
PS::Loader::Register([this]
{
return ApplyTerrainData();
}, L"CMapReader::ApplyTerrainData", 5);
// read entities
LDR_Register([this]
PS::Loader::Register([this]
{
return ReadXMLEntities();
}, L"CMapReader::ReadXMLEntities", 5800);
// apply misc data to the world
LDR_Register([this]
PS::Loader::Register([this]
{
return ApplyData();
}, L"CMapReader::ApplyData", 5);
// load map settings script (must be done after reading map)
LDR_Register([this]
PS::Loader::Register([this]
{
return LoadMapSettings();
}, L"CMapReader::LoadMapSettings", 5);
@ -232,70 +232,70 @@ void CMapReader::LoadRandomMap(const CStrW& scriptFile, const ScriptContext& cx,
only_xml = false;
// copy random map settings (before entity creation)
LDR_Register([this]
PS::Loader::Register([this]
{
return LoadRMSettings();
}, L"CMapReader::LoadRMSettings", 50);
// load player settings script (must be done before reading map)
LDR_Register([this]
PS::Loader::Register([this]
{
return LoadPlayerSettings();
}, L"CMapReader::LoadPlayerSettings", 50);
// load map generator with random map script
LDR_Register([this, scriptFile]
PS::Loader::Register([this, scriptFile]
{
return StartMapGeneration(scriptFile);
}, L"CMapReader::StartMapGeneration", 1);
LDR_Register([this]
PS::Loader::Register([this]
{
return PollMapGeneration();
}, L"CMapReader::PollMapGeneration", 19999);
// parse RMS results into terrain structure
LDR_Register([this]
PS::Loader::Register([this]
{
return ParseTerrain();
}, L"CMapReader::ParseTerrain", 500);
// parse RMS results into environment settings
LDR_Register([this]
PS::Loader::Register([this]
{
return ParseEnvironment();
}, L"CMapReader::ParseEnvironment", 5);
// parse RMS results into camera settings
LDR_Register([this]
PS::Loader::Register([this]
{
return ParseCamera();
}, L"CMapReader::ParseCamera", 5);
// apply terrain data to the world
LDR_Register([this]
PS::Loader::Register([this]
{
return ApplyTerrainData();
}, L"CMapReader::ApplyTerrainData", 5);
// parse RMS results into entities
LDR_Register([this]
PS::Loader::Register([this]
{
return StartParseEntities();
}, L"CMapReader::StartParseEntities", 10);
LDR_Register([this]
PS::Loader::Register([this]
{
return PollParseEntities();
}, L"CMapReader::PollParseEntities", 1000);
// apply misc data to the world
LDR_Register([this]
PS::Loader::Register([this]
{
return ApplyData();
}, L"CMapReader::ApplyData", 5);
// load map settings script (must be done after reading map)
LDR_Register([this]
PS::Loader::Register([this]
{
return LoadMapSettings();
}, L"CMapReader::LoadMapSettings", 5);

View file

@ -329,7 +329,7 @@ static int ProgressiveLoad()
int progressPercent{0};
try
{
const LDR_ProgressiveLoadResult result{LDR_ProgressiveLoad(10e-3)};
const PS::Loader::ProgressiveLoadResult result{PS::Loader::ProgressiveLoad(10e-3)};
description = result.nextDescription;
progressPercent = result.progressPercent;
switch(result.status)

View file

@ -189,7 +189,7 @@ public:
for (size_t j = 0; j < clients.size(); ++j)
{
clients[j]->Poll();
TS_ASSERT_OK(LDR_NonprogressiveLoad());
TS_ASSERT_OK(PS::Loader::NonprogressiveLoad());
clients[j]->LoadFinished();
}
@ -272,7 +272,7 @@ public:
for (size_t j = 0; j < clients.size(); ++j)
{
clients[j]->Poll();
TS_ASSERT_OK(LDR_NonprogressiveLoad());
TS_ASSERT_OK(PS::Loader::NonprogressiveLoad());
clients[j]->LoadFinished();
}
@ -367,7 +367,7 @@ public:
clients[2]->Poll();
TS_ASSERT_OK(LDR_NonprogressiveLoad());
TS_ASSERT_OK(PS::Loader::NonprogressiveLoad());
clients[2]->LoadFinished();
wait(clients, 100);

View file

@ -251,9 +251,9 @@ void CGame::RegisterInit(const JS::HandleValue attribs, const std::string& saved
LOGERROR("GameSpeed could not be parsed.");
}
LDR_BeginRegistering();
PS::Loader::BeginRegistering();
LDR_Register([this]
PS::Loader::Register([this]
{
return m_Simulation2->ProgressiveLoad();
}, L"Simulation init", 1000);
@ -282,24 +282,24 @@ void CGame::RegisterInit(const JS::HandleValue attribs, const std::string& saved
m_World->RegisterInit(mapFile, scriptInterface.GetContext(), settings, m_PlayerID);
}
if (m_GameView)
LDR_Register([&waterManager = g_Renderer.GetSceneRenderer().GetWaterManager()]
PS::Loader::Register([&waterManager = g_Renderer.GetSceneRenderer().GetWaterManager()]
{
return waterManager.LoadWaterTextures();
}, L"LoadWaterTextures", 80);
if (m_IsSavedGame)
LDR_Register([this, savedState]
PS::Loader::Register([this, savedState]
{
return LoadInitialState(savedState);
}, L"Loading game", 1000);
if (m_IsVisualReplay)
LDR_Register([this]
PS::Loader::Register([this]
{
return LoadVisualReplayData();
}, L"Loading visual replay data", 1000);
LDR_EndRegistering();
PS::Loader::EndRegistering();
}
int CGame::LoadInitialState(const std::string& savedState)

View file

@ -868,7 +868,7 @@ bool Autostart(const CmdLineArgs& args)
if (args.Has("autostart-nonvisual"))
{
LDR_NonprogressiveLoad();
PS::Loader::NonprogressiveLoad();
g_Game->ReallyStartGame();
}
@ -914,7 +914,7 @@ void CancelLoad(const CStrW& message)
JS::RootedValue global(rq.cx, rq.globalValue());
LDR_Cancel();
PS::Loader::Cancel();
if (g_GUI &&
g_GUI->GetPageCount() &&

View file

@ -32,24 +32,28 @@
#include <string>
#include <utility>
namespace PS::Loader
{
namespace
{
// set by LDR_EndRegistering; may be 0 during development when
// estimated task durations haven't yet been set.
static double total_estimated_duration;
double total_estimated_duration;
// total time spent loading so far, set by LDR_ProgressiveLoad.
// we need a persistent counter so it can be reset after each load.
// this also accumulates less errors than:
// progress += task_estimated / total_estimated.
static double estimated_duration_tally;
double estimated_duration_tally;
// needed for report of how long each individual task took.
static double task_elapsed_time;
double task_elapsed_time;
// main purpose is to indicate whether a load is in progress, so that
// LDR_ProgressiveLoad can return 0 iff loading just completed.
// the REGISTERING state allows us to detect 2 simultaneous loads (bogus);
// FIRST_LOAD is used to skip the first timeslice (see LDR_ProgressiveLoad).
static enum
enum
{
IDLE,
REGISTERING,
@ -78,21 +82,22 @@ struct LoadRequest
}
};
typedef std::deque<LoadRequest> LoadRequests;
static LoadRequests load_requests;
using LoadRequests = std::deque<LoadRequest>;
LoadRequests load_requests;
// Returns true if the return code indicates that the `LoadRequest` didn't
// finish and should be reinvoked in the next frame.
static bool ldr_was_interrupted(const int ret)
bool WasInterrupted(const int ret)
{
return 0 < ret && ret <= 100;
}
} // anonymous namespace
// call before starting to register load requests.
// this routine is provided so we can prevent 2 simultaneous load operations,
// which is bogus. that can happen by clicking the load button quickly,
// or issuing via console while already loading.
void LDR_BeginRegistering()
void BeginRegistering()
{
ENSURE(state == IDLE);
@ -109,7 +114,7 @@ void LDR_BeginRegistering()
// <estimated_duration_ms>: used to calculate progress, and when checking
// whether there is enough of the time budget left to process this task
// (reduces timeslice overruns, making the main loop more responsive).
void LDR_Register(LoadFunc func, std::wstring description, int estimatedDurationMs)
void Register(LoadFunc func, std::wstring description, int estimatedDurationMs)
{
ENSURE(state == REGISTERING); // must be called between LDR_(Begin|End)Register
@ -119,7 +124,7 @@ void LDR_Register(LoadFunc func, std::wstring description, int estimatedDuration
// call when finished registering tasks; subsequent calls to
// LDR_ProgressiveLoad will then work off the queued entries.
void LDR_EndRegistering()
void EndRegistering()
{
ENSURE(state == REGISTERING);
ENSURE(!load_requests.empty());
@ -135,7 +140,7 @@ void LDR_EndRegistering()
// immediately cancel this load; no further tasks will be processed.
// used to abort loading upon user request or failure.
// note: no special notification will be returned by LDR_ProgressiveLoad.
void LDR_Cancel()
void Cancel()
{
// the queue doesn't need to be emptied now; that'll happen during the
// next LDR_StartRegistering. for now, it is sufficient to set the
@ -143,9 +148,11 @@ void LDR_Cancel()
state = IDLE;
}
namespace
{
// helper routine for LDR_ProgressiveLoad.
// tries to prevent starting a long task when at the end of a timeslice.
static bool HaveTimeForNextTask(double time_left, double time_budget, int estimated_duration_ms)
bool HaveTimeForNextTask(double time_left, double time_budget, int estimated_duration_ms)
{
// have already exceeded our time budget
if(time_left <= 0.0)
@ -164,10 +171,11 @@ static bool HaveTimeForNextTask(double time_left, double time_budget, int estima
return true;
}
}
LDR_ProgressiveLoadResult LDR_ProgressiveLoad(double time_budget)
ProgressiveLoadResult ProgressiveLoad(double time_budget)
{
LDR_ProgressiveLoadResult ret;
ProgressiveLoadResult ret;
double progress = 0.0; // used to set progress_percent
double time_left = time_budget;
@ -201,7 +209,7 @@ LDR_ProgressiveLoadResult LDR_ProgressiveLoad(double time_budget)
// call this task's function and bill elapsed time.
const double t0 = timer_Time();
const int status = lr.func();
const bool timed_out = ldr_was_interrupted(status);
const bool timed_out = WasInterrupted(status);
const double elapsed_time = timer_Time() - t0;
time_left -= elapsed_time;
task_elapsed_time += elapsed_time;
@ -278,7 +286,7 @@ done:
// immediately process all queued load requests.
// returns 0 on success or a negative error code.
Status LDR_NonprogressiveLoad()
Status NonprogressiveLoad()
{
const double time_budget = 100.0;
// large enough so that individual functions won't time out
@ -286,7 +294,7 @@ Status LDR_NonprogressiveLoad()
for(;;)
{
const auto [ret, description, progress_percent] = LDR_ProgressiveLoad(time_budget);
const auto [ret, description, progress_percent] = ProgressiveLoad(time_budget);
switch(ret)
{
case INFO::OK:
@ -301,3 +309,4 @@ Status LDR_NonprogressiveLoad()
}
}
}
} // namespace PS::Loader

View file

@ -27,6 +27,8 @@
#include <functional>
#include <string>
namespace PS::Loader
{
/*
[KEEP IN SYNC WITH WIKI!]
@ -102,7 +104,7 @@ Then in the main loop, call LDR_ProgressiveLoad().
// this routine is provided so we can prevent 2 simultaneous load operations,
// which is bogus. that can happen by clicking the load button quickly,
// or issuing via console while already loading.
extern void LDR_BeginRegistering();
void BeginRegistering();
// callback function of a task; performs the actual work.
@ -125,20 +127,20 @@ using LoadFunc = std::function<int()>;
// <estimated_duration_ms>: used to calculate progress, and when checking
// whether there is enough of the time budget left to process this task
// (reduces timeslice overruns, making the main loop more responsive).
void LDR_Register(LoadFunc func, std::wstring description, int estimated_duration_ms);
void Register(LoadFunc func, std::wstring description, int estimated_duration_ms);
// call when finished registering tasks; subsequent calls to
// LDR_ProgressiveLoad will then work off the queued entries.
extern void LDR_EndRegistering();
void EndRegistering();
// immediately cancel this load; no further tasks will be processed.
// used to abort loading upon user request or failure.
// note: no special notification will be returned by LDR_ProgressiveLoad.
extern void LDR_Cancel();
void Cancel();
struct LDR_ProgressiveLoadResult
struct ProgressiveLoadResult
{
/**
* @c INFO::All_COMPLETE if the final load task just completed.
@ -163,11 +165,11 @@ struct LDR_ProgressiveLoadResult
* Process as many of the queued tasks as possible within @c timeBudget [s].
* if a task is lengthy, the budget may be exceeded. call from the main loop.
*/
LDR_ProgressiveLoadResult LDR_ProgressiveLoad(double time_budget);
ProgressiveLoadResult ProgressiveLoad(double time_budget);
// immediately process all queued load requests.
// returns 0 on success or a negative error code.
extern Status LDR_NonprogressiveLoad();
Status NonprogressiveLoad();
// boilerplate check-if-timed-out and return-progress-percent code.
@ -185,4 +187,6 @@ extern Status LDR_NonprogressiveLoad();
return (int)progress_percent;\
}
} // namespace PS::Loader
#endif // #ifndef INCLUDED_LOADER

View file

@ -268,7 +268,7 @@ void CReplayPlayer::Replay(const bool serializationtest, const int rejointesttur
g_Game->StartGame(&attribs, "");
// TODO: Non progressive load can fail - need a decent way to handle this
LDR_NonprogressiveLoad();
PS::Loader::NonprogressiveLoad();
PSRETURN ret = g_Game->ReallyStartGame();
ENSURE(ret == PSRETURN_OK);

View file

@ -86,7 +86,7 @@ void CWorld::RegisterInit(const CStrW& mapFile, const ScriptContext& cx, JS::Han
m_Game.GetSimulation2(), &m_Game.GetSimulation2()->GetSimContext(), playerID,
false);
// fails immediately, or registers for delay loading
LDR_Register([this]
PS::Loader::Register([this]
{
return DeleteMapReader();
}, L"CWorld::DeleteMapReader", 5);
@ -111,7 +111,7 @@ void CWorld::RegisterInitRMS(const CStrW& scriptFile, const ScriptContext& cx, J
pTriggerManager, CRenderer::IsInitialised() ? &g_Renderer.GetPostprocManager() : nullptr,
m_Game.GetSimulation2(), playerID);
// registers for delay loading
LDR_Register([this]
PS::Loader::Register([this]
{
return DeleteMapReader();
}, L"CWorld::DeleteMapReader", 5);

View file

@ -363,7 +363,7 @@ void Interface::ApplyMessage(const GameMessage& msg)
if (nonVisual)
{
LDR_NonprogressiveLoad();
PS::Loader::NonprogressiveLoad();
ENSURE(g_Game->ReallyStartGame() == PSRETURN_OK);
m_ReturnValue = GetGameState();
m_MsgApplied.notify_one();

View file

@ -449,7 +449,7 @@ void CSimulation2Impl::Update(int turnLength, const std::vector<SimulationComman
// Load the map into the secondary simulation
LDR_BeginRegistering();
PS::Loader::BeginRegistering();
std::unique_ptr<CMapReader> mapReader = std::make_unique<CMapReader>();
std::string mapType;
@ -470,8 +470,8 @@ void CSimulation2Impl::Update(int turnLength, const std::vector<SimulationComman
NULL, NULL, m_SecondaryContext.get(), INVALID_PLAYER, true); // throws exception on failure
}
LDR_EndRegistering();
ENSURE(LDR_NonprogressiveLoad() == INFO::OK);
PS::Loader::EndRegistering();
ENSURE(PS::Loader::NonprogressiveLoad() == INFO::OK);
ENSURE(m_SecondaryComponentManager->DeserializeState(primaryStateBefore.state));
}

View file

@ -164,13 +164,13 @@ public:
std::unique_ptr<CMapReader> mapReader = std::make_unique<CMapReader>();
LDR_BeginRegistering();
PS::Loader::BeginRegistering();
mapReader->LoadMap(L"maps/skirmishes/Median Oasis (2).pmp",
sim2.GetScriptInterface().GetContext(), JS::UndefinedHandleValue,
&terrain, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
&sim2, &sim2.GetSimContext(), -1, false);
LDR_EndRegistering();
TS_ASSERT_OK(LDR_NonprogressiveLoad());
PS::Loader::EndRegistering();
TS_ASSERT_OK(PS::Loader::NonprogressiveLoad());
sim2.PreInitGame();
sim2.InitGame();
@ -278,13 +278,13 @@ public:
std::unique_ptr<CMapReader> mapReader = std::make_unique<CMapReader>();
LDR_BeginRegistering();
PS::Loader::BeginRegistering();
mapReader->LoadMap(L"maps/scenarios/Peloponnese.pmp",
sim2.GetScriptInterface().GetContext(), JS::UndefinedHandleValue,
&terrain, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
&sim2, &sim2.GetSimContext(), -1, false);
LDR_EndRegistering();
TS_ASSERT_OK(LDR_NonprogressiveLoad());
PS::Loader::EndRegistering();
TS_ASSERT_OK(PS::Loader::NonprogressiveLoad());
sim2.PreInitGame();
sim2.InitGame();
@ -335,13 +335,13 @@ public:
std::unique_ptr<CMapReader> mapReader = std::make_unique<CMapReader>();
LDR_BeginRegistering();
PS::Loader::BeginRegistering();
mapReader->LoadMap(L"maps/scenarios/Peloponnese.pmp",
sim2.GetScriptInterface().GetContext(), JS::UndefinedHandleValue,
&terrain, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
&sim2, &sim2.GetSimContext(), -1, false);
LDR_EndRegistering();
TS_ASSERT_OK(LDR_NonprogressiveLoad());
PS::Loader::EndRegistering();
TS_ASSERT_OK(PS::Loader::NonprogressiveLoad());
sim2.PreInitGame();
sim2.InitGame();

View file

@ -907,13 +907,13 @@ public:
std::unique_ptr<CMapReader> mapReader = std::make_unique<CMapReader>();
LDR_BeginRegistering();
PS::Loader::BeginRegistering();
mapReader->LoadMap(L"maps/skirmishes/Greek Acropolis (2).pmp",
sim2.GetScriptInterface().GetContext(), JS::UndefinedHandleValue,
&terrain, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
&sim2, &sim2.GetSimContext(), -1, false);
LDR_EndRegistering();
TS_ASSERT_OK(LDR_NonprogressiveLoad());
PS::Loader::EndRegistering();
TS_ASSERT_OK(PS::Loader::NonprogressiveLoad());
sim2.Update(0);

View file

@ -109,7 +109,7 @@ namespace
g_Game->StartGame(attrs, "");
// TODO: Non progressive load can fail - need a decent way to handle this
LDR_NonprogressiveLoad();
PS::Loader::NonprogressiveLoad();
// Disable fog-of-war - this must be done before starting the game,
// as visual actors cache their visibility state on first render.
@ -153,7 +153,7 @@ QUERYHANDLER(GenerateMap)
catch (std::exception&)
{
// Cancel loading
LDR_Cancel();
PS::Loader::Cancel();
// Since map generation failed and we don't know why, use the blank map as a fallback