Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
WorldRunnable.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
9
10#include "BattlegroundMgr.h"
11#include "Common.h"
13#include "Errors.h"
14#include "MapManager.h"
15#include "ObjectAccessor.h"
16#include "OutdoorPvPMgr.h"
17#include "Platform/TimeUtils.h"
18#include "ScriptMgr.h"
19#include "Timer.h"
20#include "World.h"
21#include "WorldRunnable.h"
23#include "WorldSocketMgr.h"
24
25#define WORLD_SLEEP_CONST 50
26
27#ifdef _WIN32
28#include "ServiceWin32.h"
29extern int m_ServiceStatus;
30#endif
31
32namespace
33{
34 void AdvanceShutdownStep(Skyfire::WorldShutdown::WorldShutdownStep& current,
36 {
38 current = next;
39 }
40}
41
44{
45 uint32 realCurrTime = 0;
46 uint32 realPrevTime = getMSTime();
47
48 uint32 prevSleepTime = 0; // used for balanced full tick time length near WORLD_SLEEP_CONST
49
50 sScriptMgr->OnStartup();
51
53 while (!World::IsStopped())
54 {
56 realCurrTime = getMSTime();
57
58 uint32 diff = getMSTimeDiff(realPrevTime, realCurrTime);
59
60 sWorld->Update(diff);
61 realPrevTime = realCurrTime;
62
63 // diff (D0) include time of previous sleep (d0) + tick time (t0)
64 // we want that next d1 + t1 == WORLD_SLEEP_CONST
65 // we can't know next t1 and then can use (t0 + d1) == WORLD_SLEEP_CONST requirement
66 // d1 = WORLD_SLEEP_CONST - t0 = WORLD_SLEEP_CONST - (D0 - d0) = WORLD_SLEEP_CONST + d0 - D0
67 if (diff <= WORLD_SLEEP_CONST + prevSleepTime)
68 {
69 prevSleepTime = WORLD_SLEEP_CONST + prevSleepTime - diff;
70 Skyfire::SleepForMilliseconds(prevSleepTime);
71 }
72 else
73 prevSleepTime = 0;
74
75#ifdef _WIN32
76 if (m_ServiceStatus == 0)
78
79 while (m_ServiceStatus == 2)
81#endif
82 }
83
85
86 AdvanceShutdownStep(shutdownStep, Skyfire::WorldShutdown::WORLD_SHUTDOWN_SCRIPT_SHUTDOWN);
87 sScriptMgr->OnShutdown();
88
89 // Players must be kicked and sessions updated before maps can be unloaded.
90 AdvanceShutdownStep(shutdownStep, Skyfire::WorldShutdown::WORLD_SHUTDOWN_KICK_PLAYERS);
91 sWorld->KickAll(); // save and kick all players
92 AdvanceShutdownStep(shutdownStep, Skyfire::WorldShutdown::WORLD_SHUTDOWN_UPDATE_SESSIONS);
93 sWorld->UpdateSessions(1); // real players unload required UpdateSessions call
94
95 // Battleground and network teardown must finish before map/object storage is destroyed.
96 AdvanceShutdownStep(shutdownStep, Skyfire::WorldShutdown::WORLD_SHUTDOWN_DELETE_BATTLEGROUNDS);
97 sBattlegroundMgr->DeleteAllBattlegrounds();
98 AdvanceShutdownStep(shutdownStep, Skyfire::WorldShutdown::WORLD_SHUTDOWN_STOP_NETWORK);
99 sWorldSocketMgr->StopNetwork();
100
101 // Maps own live world objects; ObjectAccessor corpse storage is unloaded after maps.
102 AdvanceShutdownStep(shutdownStep, Skyfire::WorldShutdown::WORLD_SHUTDOWN_UNLOAD_MAPS);
103 sMapMgr->UnloadAll(); // unload all grids (including locked in memory)
104 AdvanceShutdownStep(shutdownStep, Skyfire::WorldShutdown::WORLD_SHUTDOWN_UNLOAD_OBJECT_ACCESSOR);
105 sObjectAccessor->UnloadAll(); // unload 'i_player2corpse' storage and remove from world
106 AdvanceShutdownStep(shutdownStep, Skyfire::WorldShutdown::WORLD_SHUTDOWN_UNLOAD_SCRIPTS);
107 sScriptMgr->Unload();
108 AdvanceShutdownStep(shutdownStep, Skyfire::WorldShutdown::WORLD_SHUTDOWN_OUTDOOR_PVP_DIE);
109 sOutdoorPvPMgr->Die();
110 AdvanceShutdownStep(shutdownStep, Skyfire::WorldShutdown::WORLD_SHUTDOWN_COMPLETE);
111}
#define sBattlegroundMgr
std::uint32_t uint32
Definition Define.h:77
#define ASSERT
Definition Errors.h:29
#define sMapMgr
Definition MapManager.h:145
#define sObjectAccessor
#define sOutdoorPvPMgr
#define sScriptMgr
Definition ScriptMgr.h:764
uint32 getMSTime()
Definition Timer.h:12
uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
Definition Timer.h:17
#define WORLD_SLEEP_CONST
static void StopNow(uint8 exitcode)
Definition World.h:674
static bool IsStopped()
Definition World.h:675
static std::atomic< uint32 > m_worldLoopCounter
Definition World.h:559
void Run()
Heartbeat for the World.
#define sWorldSocketMgr
#define sWorld
Definition World.h:910
@ SHUTDOWN_EXIT_CODE
Definition World.h:64
bool IsExpectedShutdownTransition(WorldShutdownStep current, WorldShutdownStep next)
void SleepForMilliseconds(uint32 milliseconds)
Definition TimeUtils.h:42