From 1917d034fd9eb65e04bbce5d9cf7c9b51db79b95 Mon Sep 17 00:00:00 2001 From: phosit Date: Mon, 20 Oct 2025 21:35:40 +0200 Subject: [PATCH] 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. --- source/graphics/GameView.cpp | 6 +-- source/graphics/MapReader.cpp | 42 +++++++++--------- source/main.cpp | 2 +- source/network/tests/test_Net.h | 6 +-- source/ps/Game.cpp | 12 +++--- source/ps/GameSetup/GameSetup.cpp | 4 +- source/ps/Loader.cpp | 43 +++++++++++-------- source/ps/Loader.h | 18 +++++--- source/ps/Replay.cpp | 2 +- source/ps/World.cpp | 4 +- source/rlinterface/RLInterface.cpp | 2 +- source/simulation2/Simulation2.cpp | 6 +-- .../components/tests/test_Pathfinder.h | 18 ++++---- source/simulation2/tests/test_Serializer.h | 6 +-- .../GameInterface/Handlers/MapHandlers.cpp | 4 +- 15 files changed, 94 insertions(+), 81 deletions(-) diff --git a/source/graphics/GameView.cpp b/source/graphics/GameView.cpp index f4a3db0469..d9ff5a66c1 100644 --- a/source/graphics/GameView.cpp +++ b/source/graphics/GameView.cpp @@ -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); diff --git a/source/graphics/MapReader.cpp b/source/graphics/MapReader.cpp index 2845611ff2..3d30fe09e3 100644 --- a/source/graphics/MapReader.cpp +++ b/source/graphics/MapReader.cpp @@ -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); diff --git a/source/main.cpp b/source/main.cpp index 32fe39a6de..5d63317241 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -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) diff --git a/source/network/tests/test_Net.h b/source/network/tests/test_Net.h index 96a7972961..797c75e329 100644 --- a/source/network/tests/test_Net.h +++ b/source/network/tests/test_Net.h @@ -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); diff --git a/source/ps/Game.cpp b/source/ps/Game.cpp index 2e9d6c9457..25989d1cd1 100644 --- a/source/ps/Game.cpp +++ b/source/ps/Game.cpp @@ -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) diff --git a/source/ps/GameSetup/GameSetup.cpp b/source/ps/GameSetup/GameSetup.cpp index eaea7879c0..0cd5aef637 100644 --- a/source/ps/GameSetup/GameSetup.cpp +++ b/source/ps/GameSetup/GameSetup.cpp @@ -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() && diff --git a/source/ps/Loader.cpp b/source/ps/Loader.cpp index b29c2a12ca..57f5d021da 100644 --- a/source/ps/Loader.cpp +++ b/source/ps/Loader.cpp @@ -32,24 +32,28 @@ #include #include +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 LoadRequests; -static LoadRequests load_requests; +using LoadRequests = std::deque; +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() // : 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 diff --git a/source/ps/Loader.h b/source/ps/Loader.h index 91a1fa7c67..d0425e4c29 100644 --- a/source/ps/Loader.h +++ b/source/ps/Loader.h @@ -27,6 +27,8 @@ #include #include +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; // : 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 diff --git a/source/ps/Replay.cpp b/source/ps/Replay.cpp index e88c8a1c8e..0c6bac94c7 100644 --- a/source/ps/Replay.cpp +++ b/source/ps/Replay.cpp @@ -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); diff --git a/source/ps/World.cpp b/source/ps/World.cpp index 6b468bbdfe..725480e5b7 100644 --- a/source/ps/World.cpp +++ b/source/ps/World.cpp @@ -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); diff --git a/source/rlinterface/RLInterface.cpp b/source/rlinterface/RLInterface.cpp index a4328e164e..57c5993583 100644 --- a/source/rlinterface/RLInterface.cpp +++ b/source/rlinterface/RLInterface.cpp @@ -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(); diff --git a/source/simulation2/Simulation2.cpp b/source/simulation2/Simulation2.cpp index 3ebb88793b..74ccd70ab5 100644 --- a/source/simulation2/Simulation2.cpp +++ b/source/simulation2/Simulation2.cpp @@ -449,7 +449,7 @@ void CSimulation2Impl::Update(int turnLength, const std::vector mapReader = std::make_unique(); std::string mapType; @@ -470,8 +470,8 @@ void CSimulation2Impl::Update(int turnLength, const std::vectorDeserializeState(primaryStateBefore.state)); } diff --git a/source/simulation2/components/tests/test_Pathfinder.h b/source/simulation2/components/tests/test_Pathfinder.h index 19f369a9dd..9117c87df1 100644 --- a/source/simulation2/components/tests/test_Pathfinder.h +++ b/source/simulation2/components/tests/test_Pathfinder.h @@ -164,13 +164,13 @@ public: std::unique_ptr mapReader = std::make_unique(); - 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 mapReader = std::make_unique(); - 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 mapReader = std::make_unique(); - 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(); diff --git a/source/simulation2/tests/test_Serializer.h b/source/simulation2/tests/test_Serializer.h index d3de02dd94..446ab27283 100644 --- a/source/simulation2/tests/test_Serializer.h +++ b/source/simulation2/tests/test_Serializer.h @@ -907,13 +907,13 @@ public: std::unique_ptr mapReader = std::make_unique(); - 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); diff --git a/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp b/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp index 28ceab96be..d09d624658 100644 --- a/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp +++ b/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp @@ -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