mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-06-21 01:29:50 +00:00
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:
+6
-6
@@ -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)
|
||||
|
||||
@@ -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() &&
|
||||
|
||||
+26
-17
@@ -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
|
||||
|
||||
+11
-7
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
+2
-2
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user