System.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // Description : CryENGINE system core-handle all subsystems
  9. #include "CrySystem_precompiled.h"
  10. #include "System.h"
  11. #include <time.h>
  12. #include <AzCore/Console/Console.h>
  13. #include <AzCore/IO/IStreamer.h>
  14. #include <AzCore/IO/SystemFile.h>
  15. #include <AzCore/Debug/Budget.h>
  16. #include <CryPath.h>
  17. #include <CrySystemBus.h>
  18. #include <CryCommon/IFont.h>
  19. #include <CryCommon/MiniQueue.h>
  20. #include <AzFramework/API/ApplicationAPI.h>
  21. #include <AzFramework/API/ApplicationAPI_Platform.h>
  22. #include <AzFramework/Input/Devices/Keyboard/InputDeviceKeyboard.h>
  23. #include <AzFramework/Spawnable/RootSpawnableInterface.h>
  24. #include <AzCore/Debug/Profiler.h>
  25. #include <AzCore/Debug/Trace.h>
  26. #include <AzCore/Interface/Interface.h>
  27. #include <AzCore/std/algorithm.h>
  28. #include <AzCore/Time/ITime.h>
  29. #include <AzFramework/Logging/MissingAssetLogger.h>
  30. #include <AzFramework/Entity/EntityDebugDisplayBus.h>
  31. #include <AzCore/Interface/Interface.h>
  32. AZ_DEFINE_BUDGET(CrySystem);
  33. #if defined(AZ_RESTRICTED_PLATFORM)
  34. #undef AZ_RESTRICTED_SECTION
  35. #define SYSTEM_CPP_SECTION_1 1
  36. #define SYSTEM_CPP_SECTION_2 2
  37. #define SYSTEM_CPP_SECTION_3 3
  38. #define SYSTEM_CPP_SECTION_4 4
  39. #define SYSTEM_CPP_SECTION_5 5
  40. #define SYSTEM_CPP_SECTION_6 6
  41. #define SYSTEM_CPP_SECTION_7 7
  42. #define SYSTEM_CPP_SECTION_8 8
  43. #define SYSTEM_CPP_SECTION_9 9
  44. #endif
  45. #if defined(_RELEASE) && AZ_LEGACY_CRYSYSTEM_TRAIT_USE_EXCLUDEUPDATE_ON_CONSOLE
  46. //exclude some not needed functionality for release console builds
  47. #define EXCLUDE_UPDATE_ON_CONSOLE
  48. #endif
  49. #ifdef WIN32
  50. #include <AzCore/PlatformIncl.h>
  51. LRESULT WINAPI WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  52. {
  53. CSystem* pSystem = 0;
  54. if (gEnv)
  55. {
  56. pSystem = static_cast<CSystem*>(gEnv->pSystem);
  57. }
  58. if (pSystem && !pSystem->IsQuitting())
  59. {
  60. LRESULT result = 0;
  61. bool bAny = false;
  62. for (std::vector<IWindowMessageHandler*>::const_iterator it = pSystem->m_windowMessageHandlers.begin(); it != pSystem->m_windowMessageHandlers.end(); ++it)
  63. {
  64. IWindowMessageHandler* pHandler = *it;
  65. LRESULT maybeResult = 0xDEADDEAD;
  66. if (pHandler->HandleMessage(hWnd, uMsg, wParam, lParam, &maybeResult))
  67. {
  68. assert(maybeResult != 0xDEADDEAD && "Message handler indicated a resulting value, but no value was written");
  69. if (bAny)
  70. {
  71. assert(result == maybeResult && "Two window message handlers tried to return different result values");
  72. }
  73. else
  74. {
  75. bAny = true;
  76. result = maybeResult;
  77. }
  78. }
  79. }
  80. if (bAny)
  81. {
  82. // One of the registered handlers returned something
  83. return result;
  84. }
  85. }
  86. // Handle with the default procedure
  87. assert(IsWindowUnicode(hWnd) && "Window should be Unicode when compiling with UNICODE");
  88. return DefWindowProcW(hWnd, uMsg, wParam, lParam);
  89. }
  90. #endif
  91. #if defined(LINUX) && !defined(ANDROID)
  92. #include <execinfo.h> // for backtrace
  93. #endif
  94. #if defined(ANDROID)
  95. #include <unwind.h> // for _Unwind_Backtrace and _Unwind_GetIP
  96. #endif
  97. #if defined(AZ_RESTRICTED_PLATFORM)
  98. #define AZ_RESTRICTED_SECTION SYSTEM_CPP_SECTION_1
  99. #include AZ_RESTRICTED_FILE(System_cpp)
  100. #endif
  101. #include <IRenderer.h>
  102. #include <IMovieSystem.h>
  103. #include <ILog.h>
  104. #include <IAudioSystem.h>
  105. #include <LoadScreenBus.h>
  106. #include <AzFramework/Archive/Archive.h>
  107. #include "XConsole.h"
  108. #include "Log.h"
  109. #include "XML/xml.h"
  110. #include "LocalizedStringManager.h"
  111. #include "XML/XmlUtils.h"
  112. #include "SystemEventDispatcher.h"
  113. #include "RemoteConsole/RemoteConsole.h"
  114. #include <AzFramework/Asset/AssetSystemBus.h>
  115. #include <AzFramework/Input/Buses/Requests/InputSystemRequestBus.h>
  116. #ifdef WIN32
  117. #include <AzFramework/Input/Buses/Notifications/RawInputNotificationBus_Platform.h>
  118. #include <process.h>
  119. #include <malloc.h>
  120. #endif
  121. #include <ILevelSystem.h>
  122. #include <AzFramework/IO/LocalFileIO.h>
  123. // Define global cvars.
  124. SSystemCVars g_cvars;
  125. #include <AzCore/Module/Environment.h>
  126. #include <AzCore/Component/ComponentApplication.h>
  127. #include "AZCoreLogSink.h"
  128. namespace
  129. {
  130. float GetMovieFrameDeltaTime()
  131. {
  132. // Use GetRealTickDeltaTimeUs for CryMovie, because it should not be affected by pausing game time
  133. const AZ::TimeUs delta = AZ::GetRealTickDeltaTimeUs();
  134. return AZ::TimeUsToSeconds(delta);
  135. }
  136. }
  137. /////////////////////////////////////////////////////////////////////////////////
  138. // System Implementation.
  139. //////////////////////////////////////////////////////////////////////////
  140. CSystem::CSystem()
  141. {
  142. CrySystemRequestBus::Handler::BusConnect();
  143. m_systemGlobalState = ESYSTEM_GLOBAL_STATE_UNKNOWN;
  144. m_iHeight = 0;
  145. m_iWidth = 0;
  146. m_iColorBits = 0;
  147. // CRT ALLOCATION threshold
  148. m_bIsAsserting = false;
  149. m_pSystemEventDispatcher = new CSystemEventDispatcher(); // Must be first.
  150. if (m_pSystemEventDispatcher)
  151. {
  152. m_pSystemEventDispatcher->RegisterListener(this);
  153. }
  154. //////////////////////////////////////////////////////////////////////////
  155. // Clear environment.
  156. //////////////////////////////////////////////////////////////////////////
  157. memset(&m_env, 0, sizeof(m_env));
  158. //////////////////////////////////////////////////////////////////////////
  159. // Initialize global environment interface pointers.
  160. m_env.pSystem = this;
  161. m_env.bIgnoreAllAsserts = false;
  162. m_env.bNoAssertDialog = false;
  163. //////////////////////////////////////////////////////////////////////////
  164. m_sysNoUpdate = NULL;
  165. m_pCmdLine = NULL;
  166. m_pLevelSystem = NULL;
  167. m_pLocalizationManager = NULL;
  168. #if defined(AZ_RESTRICTED_PLATFORM)
  169. #define AZ_RESTRICTED_SECTION SYSTEM_CPP_SECTION_2
  170. #include AZ_RESTRICTED_FILE(System_cpp)
  171. #endif
  172. m_pUserCallback = NULL;
  173. m_sys_firstlaunch = NULL;
  174. m_gpu_particle_physics = NULL;
  175. m_bInitializedSuccessfully = false;
  176. m_bRelaunch = false;
  177. m_iLoadingMode = 0;
  178. m_bTestMode = false;
  179. m_bEditor = false;
  180. m_bPreviewMode = false;
  181. m_bIgnoreUpdates = false;
  182. m_bNoCrashDialog = false;
  183. m_bNoErrorReportWindow = false;
  184. m_bInDevMode = false;
  185. m_bGameFolderWritable = false;
  186. m_bPaused = false;
  187. m_bNoUpdate = false;
  188. m_iApplicationInstance = -1;
  189. m_pXMLUtils = new CXmlUtils(this);
  190. m_eRuntimeState = ESYSTEM_EVENT_LEVEL_UNLOAD;
  191. m_bHasRenderedErrorMessage = false;
  192. #if AZ_LEGACY_CRYSYSTEM_TRAIT_USE_MESSAGE_HANDLER
  193. RegisterWindowMessageHandler(this);
  194. #endif
  195. m_ConfigPlatform = CONFIG_INVALID_PLATFORM;
  196. m_movieSystem = AZ::Interface<IMovieSystem>::Get();
  197. }
  198. /////////////////////////////////////////////////////////////////////////////////
  199. /////////////////////////////////////////////////////////////////////////////////
  200. CSystem::~CSystem()
  201. {
  202. ShutDown();
  203. #if AZ_LEGACY_CRYSYSTEM_TRAIT_USE_MESSAGE_HANDLER
  204. UnregisterWindowMessageHandler(this);
  205. #endif
  206. CRY_ASSERT(m_windowMessageHandlers.empty() && "There exists a dangling window message handler somewhere");
  207. SAFE_DELETE(m_pXMLUtils);
  208. SAFE_DELETE(m_pSystemEventDispatcher);
  209. AZCoreLogSink::Disconnect();
  210. m_env.pSystem = 0;
  211. gEnv = 0;
  212. }
  213. //////////////////////////////////////////////////////////////////////////
  214. void CSystem::Release()
  215. {
  216. delete this;
  217. }
  218. //////////////////////////////////////////////////////////////////////////
  219. IRemoteConsole* CSystem::GetIRemoteConsole()
  220. {
  221. return CRemoteConsole::GetInst();
  222. }
  223. //////////////////////////////////////////////////////////////////////////
  224. void CSystem::SetDevMode(bool bEnable)
  225. {
  226. m_bInDevMode = bEnable;
  227. }
  228. ///////////////////////////////////////////////////
  229. void CSystem::ShutDown()
  230. {
  231. CryLogAlways("System Shutdown");
  232. // Disconnect any networking connections at the beginning of shutting down.
  233. // This needs to happen before unloading the level because the network connection might queue
  234. // references to entities and assets that could prevent cleanup from happening during the level unload.
  235. if (const auto console = AZ::Interface<AZ::IConsole>::Get())
  236. {
  237. console->PerformCommand("disconnect");
  238. }
  239. // On shutdown, we need to start by unloading the level spawnable and processing the spawnable queue
  240. // to clean up any remaining references. Otherwise, we'll get lots of errors on shutdown due to assets still
  241. // being in use as various subsystems get shut down. By the time the spawnable system shuts down, it will try
  242. // to clean up the assets, but the asset handlers for those assets will also be deregistered and destroyed by then,
  243. // causing even more errors.
  244. // By unloading the level before shutting down any subsystems, we can avoid the cascade of errors.
  245. ILevelSystem* levelSystem = GetILevelSystem();
  246. if (levelSystem)
  247. {
  248. levelSystem->UnloadLevel();
  249. if (auto spawnableInterface = AzFramework::RootSpawnableInterface::Get(); spawnableInterface)
  250. {
  251. spawnableInterface->ProcessSpawnableQueueUntilEmpty();
  252. }
  253. }
  254. // don't broadcast OnCrySystemShutdown unless
  255. // we'd previously broadcast OnCrySystemInitialized
  256. if (m_bInitializedSuccessfully)
  257. {
  258. EBUS_EVENT(CrySystemEventBus, OnCrySystemShutdown, *this);
  259. }
  260. if (m_pUserCallback)
  261. {
  262. m_pUserCallback->OnShutdown();
  263. }
  264. if (GetIRemoteConsole()->IsStarted())
  265. {
  266. GetIRemoteConsole()->Stop();
  267. }
  268. if (m_sys_firstlaunch)
  269. {
  270. m_sys_firstlaunch->Set("0");
  271. }
  272. if ((m_bEditor) && (m_env.pConsole))
  273. {
  274. // restore the old saved cvars
  275. if (m_env.pConsole->GetCVar("r_Width"))
  276. {
  277. m_env.pConsole->GetCVar("r_Width")->Set(m_iWidth);
  278. }
  279. if (m_env.pConsole->GetCVar("r_Height"))
  280. {
  281. m_env.pConsole->GetCVar("r_Height")->Set(m_iHeight);
  282. }
  283. if (m_env.pConsole->GetCVar("r_ColorBits"))
  284. {
  285. m_env.pConsole->GetCVar("r_ColorBits")->Set(m_iColorBits);
  286. }
  287. }
  288. if (m_bEditor && !m_bRelaunch)
  289. {
  290. SaveConfiguration();
  291. }
  292. // Dispatch the full-shutdown event in case this is not a fast-shutdown.
  293. if (m_pSystemEventDispatcher != NULL)
  294. {
  295. m_pSystemEventDispatcher->OnSystemEvent(ESYSTEM_EVENT_FULL_SHUTDOWN, 0, 0);
  296. }
  297. SAFE_RELEASE(m_env.pCryFont);
  298. if (m_env.pConsole)
  299. {
  300. ((CXConsole*)m_env.pConsole)->FreeRenderResources();
  301. }
  302. SAFE_RELEASE(m_pLevelSystem);
  303. if (m_env.pLog)
  304. {
  305. m_env.pLog->UnregisterConsoleVariables();
  306. }
  307. GetIRemoteConsole()->UnregisterConsoleVariables();
  308. // Release console variables.
  309. SAFE_RELEASE(m_sys_firstlaunch);
  310. #if defined(AZ_RESTRICTED_PLATFORM)
  311. #define AZ_RESTRICTED_SECTION SYSTEM_CPP_SECTION_3
  312. #include AZ_RESTRICTED_FILE(System_cpp)
  313. #endif
  314. SAFE_DELETE(m_pLocalizationManager);
  315. delete m_pCmdLine;
  316. m_pCmdLine = 0;
  317. // Audio System Shutdown!
  318. // Shut down audio as late as possible but before the streaming system and console get released!
  319. Audio::Gem::SystemRequestBus::Broadcast(&Audio::Gem::SystemRequestBus::Events::Release);
  320. // Shut down console as late as possible and after audio!
  321. SAFE_RELEASE(m_env.pConsole);
  322. // Log must be last thing released.
  323. if (m_env.pLog)
  324. {
  325. m_env.pLog->Flush();
  326. }
  327. SAFE_RELEASE(m_env.pLog); // creates log backup
  328. ShutdownFileSystem();
  329. EBUS_EVENT(CrySystemEventBus, OnCrySystemPostShutdown);
  330. }
  331. /////////////////////////////////////////////////////////////////////////////////
  332. /////////////////////////////////////////////////////////////////////////////////
  333. void CSystem::Quit()
  334. {
  335. CryLogAlways("CSystem::Quit invoked from thread %" PRI_THREADID " (main is %" PRI_THREADID ")", GetCurrentThreadId(), gEnv->mMainThreadId);
  336. AzFramework::ApplicationRequests::Bus::Broadcast(&AzFramework::ApplicationRequests::ExitMainLoop);
  337. // If this was set from anywhere but the main thread, bail and let the main thread handle shutdown
  338. if (GetCurrentThreadId() != gEnv->mMainThreadId)
  339. {
  340. return;
  341. }
  342. if (m_pUserCallback)
  343. {
  344. m_pUserCallback->OnQuit();
  345. }
  346. gEnv->pLog->Flush();
  347. #ifdef WIN32
  348. //Post a WM_QUIT message to the Win32 api which causes the message loop to END
  349. //This is not the same as handling a WM_DESTROY event which destroys a window
  350. //but keeps the message loop alive.
  351. PostQuitMessage(0);
  352. #endif
  353. }
  354. /////////////////////////////////////////////////////////////////////////////////
  355. /////////////////////////////////////////////////////////////////////////////////
  356. bool CSystem::IsQuitting() const
  357. {
  358. bool wasExitMainLoopRequested = false;
  359. AzFramework::ApplicationRequests::Bus::BroadcastResult(wasExitMainLoopRequested, &AzFramework::ApplicationRequests::WasExitMainLoopRequested);
  360. return wasExitMainLoopRequested;
  361. }
  362. //////////////////////////////////////////////////////////////////////////
  363. ISystem* CSystem::GetCrySystem()
  364. {
  365. return this;
  366. }
  367. //////////////////////////////////////////////////////////////////////////
  368. void CSystem::SleepIfNeeded()
  369. {
  370. static bool firstCall = true;
  371. typedef MiniQueue<CTimeValue, 32> PrevNow;
  372. static PrevNow prevNow;
  373. if (firstCall)
  374. {
  375. const AZ::TimeMs timeMs = AZ::GetRealElapsedTimeMs();
  376. const double timeSec = AZ::TimeMsToSecondsDouble(timeMs);
  377. m_lastTickTime = CTimeValue(timeSec);
  378. prevNow.Push(m_lastTickTime);
  379. firstCall = false;
  380. return;
  381. }
  382. const float maxRate = m_svDedicatedMaxRate->GetFVal();
  383. const float minTime = 1.0f / maxRate;
  384. const AZ::TimeMs nowTimeMs = AZ::GetRealElapsedTimeMs();
  385. const double nowTimeSec = AZ::TimeMsToSecondsDouble(nowTimeMs);
  386. const CTimeValue now = CTimeValue(nowTimeSec);
  387. const float elapsed = (now - m_lastTickTime).GetSeconds();
  388. if (prevNow.Full())
  389. {
  390. prevNow.Pop();
  391. }
  392. prevNow.Push(now);
  393. static bool allowStallCatchup = true;
  394. if (elapsed > minTime && allowStallCatchup)
  395. {
  396. allowStallCatchup = false;
  397. const AZ::TimeMs lastTimeMs = AZ::GetRealElapsedTimeMs();
  398. const double lastTimeSec = AZ::TimeMsToSecondsDouble(lastTimeMs);
  399. m_lastTickTime = CTimeValue(lastTimeSec);
  400. return;
  401. }
  402. allowStallCatchup = true;
  403. float totalElapsed = (now - prevNow.Front()).GetSeconds();
  404. float wantSleepTime = AZStd::clamp(minTime * (prevNow.Size() - 1) - totalElapsed, 0.0f, (minTime - elapsed) * 0.9f);
  405. static float sleepTime = 0;
  406. sleepTime = (15 * sleepTime + wantSleepTime) / 16;
  407. int sleepMS = (int)(1000.0f * sleepTime + 0.5f);
  408. if (sleepMS > 0)
  409. {
  410. AZ_PROFILE_FUNCTION(CrySystem);
  411. Sleep(sleepMS);
  412. }
  413. const AZ::TimeMs lastTimeMs = AZ::GetRealElapsedTimeMs();
  414. const double lastTimeSec = AZ::TimeMsToSecondsDouble(lastTimeMs);
  415. m_lastTickTime = CTimeValue(lastTimeSec);
  416. }
  417. extern DWORD g_idDebugThreads[];
  418. extern int g_nDebugThreads;
  419. int prev_sys_float_exceptions = -1;
  420. //////////////////////////////////////////////////////////////////////
  421. bool CSystem::UpdatePreTickBus(int updateFlags, int nPauseMode)
  422. {
  423. // If we detect the quit flag at the start of Update, that means it was set
  424. // from another thread, and we should quit immediately. Otherwise, it will
  425. // be set by game logic or the console during Update and we will quit later
  426. if (IsQuitting())
  427. {
  428. Quit();
  429. return false;
  430. }
  431. #ifndef EXCLUDE_UPDATE_ON_CONSOLE
  432. // do the dedicated sleep earlier than the frame profiler to avoid having it counted
  433. if (gEnv->IsDedicated())
  434. {
  435. SleepIfNeeded();
  436. }
  437. #endif //EXCLUDE_UPDATE_ON_CONSOLE
  438. #ifdef WIN32
  439. // enable/disable SSE fp exceptions (#nan and /0)
  440. // need to do it each frame since sometimes they are being reset
  441. _mm_setcsr(_mm_getcsr() & ~0x280 | (g_cvars.sys_float_exceptions > 0 ? 0 : 0x280));
  442. #endif //WIN32
  443. AZ_PROFILE_FUNCTION(CrySystem);
  444. #ifndef EXCLUDE_UPDATE_ON_CONSOLE
  445. if (m_pUserCallback)
  446. {
  447. m_pUserCallback->OnUpdate();
  448. }
  449. //////////////////////////////////////////////////////////////////////////
  450. // Enable/Disable floating exceptions.
  451. //////////////////////////////////////////////////////////////////////////
  452. prev_sys_float_exceptions += 1 + g_cvars.sys_float_exceptions & prev_sys_float_exceptions >> 31;
  453. if (prev_sys_float_exceptions != g_cvars.sys_float_exceptions)
  454. {
  455. prev_sys_float_exceptions = g_cvars.sys_float_exceptions;
  456. EnableFloatExceptions(g_cvars.sys_float_exceptions);
  457. }
  458. #endif //EXCLUDE_UPDATE_ON_CONSOLE
  459. //////////////////////////////////////////////////////////////////////////
  460. if (m_env.pLog)
  461. {
  462. m_env.pLog->Update();
  463. }
  464. #ifdef USE_REMOTE_CONSOLE
  465. GetIRemoteConsole()->Update();
  466. #endif
  467. if (nPauseMode != 0)
  468. {
  469. m_bPaused = true;
  470. }
  471. else
  472. {
  473. m_bPaused = false;
  474. }
  475. #ifndef EXCLUDE_UPDATE_ON_CONSOLE
  476. if (m_bIgnoreUpdates)
  477. {
  478. return true;
  479. }
  480. #endif //EXCLUDE_UPDATE_ON_CONSOLE
  481. //static bool sbPause = false;
  482. //bool bPause = false;
  483. bool bNoUpdate = false;
  484. #ifndef EXCLUDE_UPDATE_ON_CONSOLE
  485. if (m_sysNoUpdate && m_sysNoUpdate->GetIVal())
  486. {
  487. bNoUpdate = true;
  488. }
  489. m_bNoUpdate = bNoUpdate;
  490. #endif //EXCLUDE_UPDATE_ON_CONSOLE
  491. //check if we are quitting from the game
  492. if (IsQuitting())
  493. {
  494. Quit();
  495. return (false);
  496. }
  497. //limit frame rate if vsync is turned off
  498. //for consoles this is done inside renderthread to be vsync dependent
  499. {
  500. static ICVar* pSysMaxFPS = NULL;
  501. static ICVar* pVSync = NULL;
  502. if (pSysMaxFPS == NULL && gEnv && gEnv->pConsole)
  503. {
  504. pSysMaxFPS = gEnv->pConsole->GetCVar("sys_MaxFPS");
  505. }
  506. if (pVSync == NULL && gEnv && gEnv->pConsole)
  507. {
  508. pVSync = gEnv->pConsole->GetCVar("vsync_interval");
  509. }
  510. if (pSysMaxFPS && pVSync)
  511. {
  512. int32 maxFPS = pSysMaxFPS->GetIVal();
  513. uint32 vSync = pVSync->GetIVal();
  514. if (maxFPS == 0 && vSync == 0)
  515. {
  516. ILevelSystem* pLvlSys = GetILevelSystem();
  517. const bool inLevel = pLvlSys && pLvlSys->IsLevelLoaded();
  518. maxFPS = !inLevel || IsPaused() ? 60 : 0;
  519. }
  520. if (maxFPS > 0 && vSync == 0)
  521. {
  522. const float safeMarginFPS = 0.5f;//save margin to not drop below 30 fps
  523. static AZ::TimeMs sTimeLast = AZ::GetRealElapsedTimeMs();
  524. const AZ::TimeMs timeFrameMax(static_cast<AZ::TimeMs>(
  525. (int64)(1000.f / ((float)maxFPS + safeMarginFPS))
  526. ));
  527. const AZ::TimeMs timeLast = timeFrameMax + sTimeLast;
  528. while (timeLast > AZ::GetRealElapsedTimeMs())
  529. {
  530. CrySleep(0);
  531. }
  532. sTimeLast = AZ::GetRealElapsedTimeMs();
  533. }
  534. }
  535. }
  536. //////////////////////////////////////////////////////////////////////
  537. //update console system
  538. if (m_env.pConsole)
  539. {
  540. m_env.pConsole->Update();
  541. }
  542. if (IsQuitting())
  543. {
  544. Quit();
  545. return false;
  546. }
  547. // Run movie system pre-update
  548. if (!bNoUpdate)
  549. {
  550. UpdateMovieSystem(updateFlags, GetMovieFrameDeltaTime(), true);
  551. }
  552. return !IsQuitting();
  553. }
  554. //////////////////////////////////////////////////////////////////////
  555. bool CSystem::UpdatePostTickBus(int updateFlags, int /*nPauseMode*/)
  556. {
  557. const AZ::TimeMs updateStartTimeMs = AZ::GetRealElapsedTimeMs();
  558. const double updateStartTimeSec = AZ::TimeMsToSecondsDouble(updateStartTimeMs);
  559. const CTimeValue updateStart(updateStartTimeSec);
  560. // Run movie system post-update
  561. if (!m_bNoUpdate)
  562. {
  563. UpdateMovieSystem(updateFlags, GetMovieFrameDeltaTime(), false);
  564. }
  565. //////////////////////////////////////////////////////////////////////
  566. // Update sound system
  567. if (!m_bNoUpdate)
  568. {
  569. UpdateAudioSystems();
  570. }
  571. //Now update frame statistics
  572. const AZ::TimeMs curTimeMs = AZ::GetRealElapsedTimeMs();
  573. const double curTimeSec = AZ::TimeMsToSecondsDouble(curTimeMs);
  574. const CTimeValue cur_time(curTimeSec);
  575. CTimeValue a_second(g_cvars.sys_update_profile_time);
  576. std::vector< std::pair<CTimeValue, float> >::iterator it = m_updateTimes.begin();
  577. for (std::vector< std::pair<CTimeValue, float> >::iterator eit = m_updateTimes.end(); it != eit; ++it)
  578. {
  579. if ((cur_time - it->first) < a_second)
  580. {
  581. break;
  582. }
  583. }
  584. {
  585. if (it != m_updateTimes.begin())
  586. {
  587. m_updateTimes.erase(m_updateTimes.begin(), it);
  588. }
  589. float updateTime = (cur_time - updateStart).GetMilliSeconds();
  590. m_updateTimes.push_back(std::make_pair(cur_time, updateTime));
  591. }
  592. m_pSystemEventDispatcher->Update();
  593. if (!gEnv->IsEditing() && m_eRuntimeState == ESYSTEM_EVENT_LEVEL_GAMEPLAY_START)
  594. {
  595. gEnv->pCryPak->DisableRuntimeFileAccess(true);
  596. }
  597. // Also broadcast for anyone else that needs to draw global debug to do so now
  598. AzFramework::DebugDisplayEventBus::Broadcast(&AzFramework::DebugDisplayEvents::DrawGlobalDebugInfo);
  599. return !IsQuitting();
  600. }
  601. bool CSystem::UpdateLoadtime()
  602. {
  603. return !IsQuitting();
  604. }
  605. void CSystem::UpdateAudioSystems()
  606. {
  607. if (auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get(); audioSystem != nullptr)
  608. {
  609. audioSystem->ExternalUpdate();
  610. }
  611. }
  612. //////////////////////////////////////////////////////////////////////////
  613. void CSystem::GetUpdateStats(SSystemUpdateStats& stats)
  614. {
  615. if (m_updateTimes.empty())
  616. {
  617. stats = SSystemUpdateStats();
  618. }
  619. else
  620. {
  621. stats.avgUpdateTime = 0;
  622. stats.maxUpdateTime = -FLT_MAX;
  623. stats.minUpdateTime = +FLT_MAX;
  624. for (std::vector< std::pair<CTimeValue, float> >::const_iterator it = m_updateTimes.begin(), eit = m_updateTimes.end(); it != eit; ++it)
  625. {
  626. const float t = it->second;
  627. stats.avgUpdateTime += t;
  628. stats.maxUpdateTime = max(stats.maxUpdateTime, t);
  629. stats.minUpdateTime = min(stats.minUpdateTime, t);
  630. }
  631. stats.avgUpdateTime /= m_updateTimes.size();
  632. }
  633. }
  634. //////////////////////////////////////////////////////////////////////////
  635. void CSystem::UpdateMovieSystem(const int updateFlags, const float fFrameTime, const bool bPreUpdate)
  636. {
  637. if (!m_movieSystem)
  638. {
  639. m_movieSystem = AZ::Interface<IMovieSystem>::Get();
  640. }
  641. if (m_movieSystem && !(updateFlags & ESYSUPDATE_EDITOR) && g_cvars.sys_trackview)
  642. {
  643. float fMovieFrameTime = fFrameTime;
  644. if (fMovieFrameTime > g_cvars.sys_maxTimeStepForMovieSystem)
  645. {
  646. fMovieFrameTime = g_cvars.sys_maxTimeStepForMovieSystem;
  647. }
  648. if (bPreUpdate)
  649. {
  650. m_movieSystem->PreUpdate(fMovieFrameTime);
  651. }
  652. else
  653. {
  654. m_movieSystem->PostUpdate(fMovieFrameTime);
  655. }
  656. }
  657. }
  658. //////////////////////////////////////////////////////////////////////////
  659. // XML stuff
  660. //////////////////////////////////////////////////////////////////////////
  661. //////////////////////////////////////////////////////////////////////////
  662. XmlNodeRef CSystem::CreateXmlNode(const char* sNodeName, bool bReuseStrings, bool bIsProcessingInstruction)
  663. {
  664. return new CXmlNode(sNodeName, bReuseStrings, bIsProcessingInstruction);
  665. }
  666. //////////////////////////////////////////////////////////////////////////
  667. IXmlUtils* CSystem::GetXmlUtils()
  668. {
  669. return m_pXMLUtils;
  670. }
  671. //////////////////////////////////////////////////////////////////////////
  672. XmlNodeRef CSystem::LoadXmlFromFile(const char* sFilename, bool bReuseStrings)
  673. {
  674. return m_pXMLUtils->LoadXmlFromFile(sFilename, bReuseStrings);
  675. }
  676. //////////////////////////////////////////////////////////////////////////
  677. XmlNodeRef CSystem::LoadXmlFromBuffer(const char* buffer, size_t size, bool bReuseStrings, bool bSuppressWarnings)
  678. {
  679. return m_pXMLUtils->LoadXmlFromBuffer(buffer, size, bReuseStrings, bSuppressWarnings);
  680. }
  681. //////////////////////////////////////////////////////////////////////////
  682. bool CSystem::CheckLogVerbosity(int verbosity)
  683. {
  684. if (verbosity <= m_env.pLog->GetVerbosityLevel())
  685. {
  686. return true;
  687. }
  688. return false;
  689. }
  690. //////////////////////////////////////////////////////////////////////////
  691. void CSystem::Warning(EValidatorModule module, EValidatorSeverity severity, int flags, const char* file, const char* format, ...)
  692. {
  693. va_list args;
  694. va_start(args, format);
  695. WarningV(module, severity, flags, file, format, args);
  696. va_end(args);
  697. }
  698. //////////////////////////////////////////////////////////////////////////
  699. void CSystem::ShowMessage(const char* text, const char* caption, unsigned int uType)
  700. {
  701. if (m_pUserCallback)
  702. {
  703. m_pUserCallback->ShowMessage(text, caption, uType);
  704. }
  705. else
  706. {
  707. CryMessageBox(text, caption, uType);
  708. }
  709. }
  710. inline const char* ValidatorModuleToString(EValidatorModule module)
  711. {
  712. switch (module)
  713. {
  714. case VALIDATOR_MODULE_RENDERER:
  715. return "Renderer";
  716. case VALIDATOR_MODULE_3DENGINE:
  717. return "3DEngine";
  718. case VALIDATOR_MODULE_ASSETS:
  719. return "Assets";
  720. case VALIDATOR_MODULE_SYSTEM:
  721. return "System";
  722. case VALIDATOR_MODULE_AUDIO:
  723. return "Audio";
  724. case VALIDATOR_MODULE_MOVIE:
  725. return "Movie";
  726. case VALIDATOR_MODULE_EDITOR:
  727. return "Editor";
  728. case VALIDATOR_MODULE_NETWORK:
  729. return "Network";
  730. case VALIDATOR_MODULE_PHYSICS:
  731. return "Physics";
  732. case VALIDATOR_MODULE_ONLINE:
  733. return "Online";
  734. case VALIDATOR_MODULE_FEATURETESTS:
  735. return "FeatureTests";
  736. case VALIDATOR_MODULE_SHINE:
  737. return "UI";
  738. }
  739. return "";
  740. }
  741. //////////////////////////////////////////////////////////////////////////
  742. void CSystem::WarningV(EValidatorModule module, EValidatorSeverity severity, int flags, const char* file, const char* format, va_list args)
  743. {
  744. // Fran: No logging in a testing environment
  745. if (m_env.pLog == 0)
  746. {
  747. return;
  748. }
  749. const char* sModuleFilter = m_env.pLog->GetModuleFilter();
  750. if (sModuleFilter && *sModuleFilter != 0)
  751. {
  752. const char* sModule = ValidatorModuleToString(module);
  753. if (strlen(sModule) > 1 || AZ::StringFunc::Find(sModule, sModuleFilter) == AZStd::string::npos)
  754. {
  755. // Filter out warnings from other modules.
  756. return;
  757. }
  758. }
  759. bool bDbgBreak = false;
  760. if (severity == VALIDATOR_ERROR_DBGBRK)
  761. {
  762. bDbgBreak = true;
  763. severity = VALIDATOR_ERROR; // change it to a standard VALIDATOR_ERROR for simplicity in the rest of the system
  764. }
  765. IMiniLog::ELogType ltype = ILog::eComment;
  766. switch (severity)
  767. {
  768. case VALIDATOR_ERROR:
  769. ltype = ILog::eError;
  770. break;
  771. case VALIDATOR_WARNING:
  772. ltype = ILog::eWarning;
  773. break;
  774. case VALIDATOR_COMMENT:
  775. ltype = ILog::eComment;
  776. break;
  777. default:
  778. break;
  779. }
  780. AZStd::fixed_string<MAX_WARNING_LENGTH> fmt;
  781. vsnprintf_s(fmt.data(), MAX_WARNING_LENGTH, MAX_WARNING_LENGTH - 1, format, args);
  782. if (file && *file)
  783. {
  784. fmt += " [File=";
  785. fmt += file;
  786. fmt += "]";
  787. }
  788. m_env.pLog->LogWithType(ltype, flags | VALIDATOR_FLAG_SKIP_VALIDATOR, "%s", fmt.c_str());
  789. if (bDbgBreak && g_cvars.sys_error_debugbreak)
  790. {
  791. AZ::Debug::Trace::Instance().Break();
  792. }
  793. }
  794. //////////////////////////////////////////////////////////////////////////
  795. void CSystem::GetLocalizedPath(const char* sLanguage, AZStd::string& sLocalizedPath)
  796. {
  797. // Omit the trailing slash!
  798. AZStd::string sLocalizationFolder(PathUtil::GetLocalizationFolder());
  799. sLocalizationFolder.pop_back();
  800. int locFormat = 0;
  801. LocalizationManagerRequestBus::BroadcastResult(locFormat, &LocalizationManagerRequestBus::Events::GetLocalizationFormat);
  802. if(locFormat == 1)
  803. {
  804. sLocalizedPath = sLocalizationFolder + "/" + sLanguage + ".loc.agsxml";
  805. }
  806. else
  807. {
  808. if (AZ::StringFunc::Equal(sLocalizationFolder, "Languages", false))
  809. {
  810. sLocalizedPath = sLocalizationFolder + "/" + sLanguage + "_xml.pak";
  811. }
  812. else
  813. {
  814. sLocalizedPath = AZStd::string("Localized/") + sLanguage + "_xml.pak";
  815. }
  816. }
  817. }
  818. //////////////////////////////////////////////////////////////////////////
  819. void CSystem::GetLocalizedAudioPath(const char* sLanguage, AZStd::string& sLocalizedPath)
  820. {
  821. // Omit the trailing slash!
  822. AZStd::string sLocalizationFolder(PathUtil::GetLocalizationFolder());
  823. sLocalizationFolder.pop_back();
  824. if (AZ::StringFunc::Equal(sLocalizationFolder, "Languages", false))
  825. {
  826. sLocalizedPath = sLocalizationFolder + "/" + sLanguage + ".pak";
  827. }
  828. else
  829. {
  830. sLocalizedPath = AZStd::string("Localized/") + sLanguage + ".pak";
  831. }
  832. }
  833. //////////////////////////////////////////////////////////////////////////
  834. void CSystem::CloseLanguagePak(const char* sLanguage)
  835. {
  836. AZStd::string sLocalizedPath;
  837. GetLocalizedPath(sLanguage, sLocalizedPath);
  838. m_env.pCryPak->ClosePacks({ sLocalizedPath.c_str(), sLocalizedPath.size() });
  839. }
  840. //////////////////////////////////////////////////////////////////////////
  841. void CSystem::CloseLanguageAudioPak(const char* sLanguage)
  842. {
  843. AZStd::string sLocalizedPath;
  844. GetLocalizedAudioPath(sLanguage, sLocalizedPath);
  845. m_env.pCryPak->ClosePacks({ sLocalizedPath.c_str(), sLocalizedPath.size() });
  846. }
  847. //////////////////////////////////////////////////////////////////////////
  848. void CSystem::Relaunch(bool bRelaunch)
  849. {
  850. if (m_sys_firstlaunch)
  851. {
  852. m_sys_firstlaunch->Set("0");
  853. }
  854. m_bRelaunch = bRelaunch;
  855. SaveConfiguration();
  856. }
  857. //////////////////////////////////////////////////////////////////////////
  858. ILocalizationManager* CSystem::GetLocalizationManager()
  859. {
  860. return m_pLocalizationManager;
  861. }
  862. //////////////////////////////////////////////////////////////////////////
  863. void CSystem::ExecuteCommandLine(bool deferred)
  864. {
  865. if (m_executedCommandLine)
  866. {
  867. return;
  868. }
  869. m_executedCommandLine = true;
  870. // execute command line arguments e.g. +g_gametype ASSAULT +LoadLevel "testy"
  871. ICmdLine* pCmdLine = GetICmdLine();
  872. assert(pCmdLine);
  873. const int iCnt = pCmdLine->GetArgCount();
  874. for (int i = 0; i < iCnt; ++i)
  875. {
  876. const ICmdLineArg* pCmd = pCmdLine->GetArg(i);
  877. if (pCmd->GetType() == eCLAT_Post)
  878. {
  879. AZStd::string sLine = pCmd->GetName();
  880. {
  881. if (pCmd->GetValue())
  882. {
  883. sLine += AZStd::string(" ") + pCmd->GetValue();
  884. }
  885. GetILog()->Log("Executing command from command line: \n%s\n", sLine.c_str()); // - the actual command might be executed much later (e.g. level load pause)
  886. GetIConsole()->ExecuteString(sLine.c_str(), false, deferred);
  887. }
  888. }
  889. }
  890. //gEnv->pConsole->ExecuteString("sys_RestoreSpec test*"); // to get useful debugging information about current spec settings to the log file
  891. }
  892. //////////////////////////////////////////////////////////////////////////
  893. void CSystem::SetConfigPlatform(const ESystemConfigPlatform platform)
  894. {
  895. m_ConfigPlatform = platform;
  896. }
  897. //////////////////////////////////////////////////////////////////////////
  898. ESystemConfigPlatform CSystem::GetConfigPlatform() const
  899. {
  900. return m_ConfigPlatform;
  901. }
  902. //////////////////////////////////////////////////////////////////////
  903. void CSystem::OnLanguageCVarChanged(ICVar* language)
  904. {
  905. if (language && language->GetType() == CVAR_STRING)
  906. {
  907. CSystem* pSys = static_cast<CSystem*>(gEnv->pSystem);
  908. if (pSys && pSys->GetLocalizationManager())
  909. {
  910. const char* lang = language->GetString();
  911. // Hook up Localization initialization
  912. int locFormat = 0;
  913. LocalizationManagerRequestBus::BroadcastResult(locFormat, &LocalizationManagerRequestBus::Events::GetLocalizationFormat);
  914. if (locFormat == 0)
  915. {
  916. const char* locLanguage = nullptr;
  917. LocalizationManagerRequestBus::BroadcastResult(locLanguage, &LocalizationManagerRequestBus::Events::GetLanguage);
  918. pSys->OpenLanguagePak(lang);
  919. }
  920. LocalizationManagerRequestBus::Broadcast(&LocalizationManagerRequestBus::Events::SetLanguage, lang);
  921. LocalizationManagerRequestBus::Broadcast(&LocalizationManagerRequestBus::Events::ReloadData);
  922. }
  923. }
  924. }
  925. //////////////////////////////////////////////////////////////////////////
  926. void CSystem::OnLocalizationFolderCVarChanged(ICVar* const pLocalizationFolder)
  927. {
  928. if (pLocalizationFolder && pLocalizationFolder->GetType() == CVAR_STRING)
  929. {
  930. CSystem* const pSystem = static_cast<CSystem* const>(gEnv->pSystem);
  931. if (pSystem != NULL && gEnv->pCryPak != NULL)
  932. {
  933. CLocalizedStringsManager* const pLocalizationManager = static_cast<CLocalizedStringsManager* const>(pSystem->GetLocalizationManager());
  934. if (pLocalizationManager)
  935. {
  936. // Get what is currently loaded
  937. CLocalizedStringsManager::TLocalizationTagVec tagVec;
  938. pLocalizationManager->GetLoadedTags(tagVec);
  939. // Release the old localization data.
  940. CLocalizedStringsManager::TLocalizationTagVec::const_iterator end = tagVec.end();
  941. for (CLocalizedStringsManager::TLocalizationTagVec::const_iterator it = tagVec.begin(); it != end; ++it)
  942. {
  943. pLocalizationManager->ReleaseLocalizationDataByTag(it->c_str());
  944. }
  945. // Close the paks situated in the previous localization folder.
  946. pSystem->CloseLanguagePak(pLocalizationManager->GetLanguage());
  947. pSystem->CloseLanguageAudioPak(pSystem->m_currentLanguageAudio.c_str());
  948. // Set the new localization folder.
  949. gEnv->pCryPak->SetLocalizationFolder(pLocalizationFolder->GetString());
  950. // Now open the paks situated in the new localization folder.
  951. pSystem->OpenLanguagePak(pLocalizationManager->GetLanguage());
  952. pSystem->OpenLanguageAudioPak(pSystem->m_currentLanguageAudio.c_str());
  953. // And load the new data.
  954. for (CLocalizedStringsManager::TLocalizationTagVec::const_iterator it = tagVec.begin(); it != end; ++it)
  955. {
  956. pLocalizationManager->LoadLocalizationDataByTag(it->c_str());
  957. }
  958. }
  959. }
  960. }
  961. }
  962. // Catch changes to assert verbosity and update the global used to track it
  963. void CSystem::SetAssertLevel(int _assertlevel)
  964. {
  965. AZ::EnvironmentVariable<int> assertVerbosityLevel = AZ::Environment::FindVariable<int>("assertVerbosityLevel");
  966. if (assertVerbosityLevel)
  967. {
  968. assertVerbosityLevel.Set(_assertlevel);
  969. }
  970. }
  971. void CSystem::OnAssertLevelCvarChanged(ICVar* pArgs)
  972. {
  973. SetAssertLevel(pArgs->GetIVal());
  974. }
  975. void CSystem::SetLogLevel(int _loglevel)
  976. {
  977. AZ::EnvironmentVariable<int> logVerbosityLevel = AZ::Environment::FindVariable<int>("sys_LogLevel");
  978. if (logVerbosityLevel.IsConstructed())
  979. {
  980. logVerbosityLevel.Set(_loglevel);
  981. }
  982. }
  983. void CSystem::OnLogLevelCvarChanged(ICVar* pArgs)
  984. {
  985. if (pArgs)
  986. {
  987. SetLogLevel(pArgs->GetIVal());
  988. }
  989. }
  990. void CSystem::OnSystemEvent(ESystemEvent event, [[maybe_unused]] UINT_PTR wparam, [[maybe_unused]] UINT_PTR lparam)
  991. {
  992. switch (event)
  993. {
  994. case ESYSTEM_EVENT_LEVEL_LOAD_START_LOADINGSCREEN:
  995. case ESYSTEM_EVENT_LEVEL_UNLOAD:
  996. gEnv->pCryPak->DisableRuntimeFileAccess(false);
  997. case ESYSTEM_EVENT_LEVEL_GAMEPLAY_START:
  998. m_eRuntimeState = event;
  999. break;
  1000. }
  1001. }
  1002. ESystemGlobalState CSystem::GetSystemGlobalState(void)
  1003. {
  1004. return m_systemGlobalState;
  1005. }
  1006. const char* CSystem::GetSystemGlobalStateName(const ESystemGlobalState systemGlobalState)
  1007. {
  1008. static const char* const s_systemGlobalStateNames[] = {
  1009. "UNKNOWN", // ESYSTEM_GLOBAL_STATE_UNKNOWN,
  1010. "INIT", // ESYSTEM_GLOBAL_STATE_INIT,
  1011. "RUNNING", // ESYSTEM_GLOBAL_STATE_RUNNING,
  1012. "LEVEL_LOAD_PREPARE", // ESYSTEM_GLOBAL_STATE_LEVEL_LOAD_START_PREPARE,
  1013. "LEVEL_LOAD_START", // ESYSTEM_GLOBAL_STATE_LEVEL_LOAD_START,
  1014. "LEVEL_LOAD_MATERIALS", // ESYSTEM_GLOBAL_STATE_LEVEL_LOAD_START_MATERIALS,
  1015. "LEVEL_LOAD_OBJECTS", // ESYSTEM_GLOBAL_STATE_LEVEL_LOAD_START_OBJECTS,
  1016. "LEVEL_LOAD_STATIC_WORLD", // ESYSTEM_GLOBAL_STATE_LEVEL_LOAD_START_STATIC_WORLD,
  1017. "LEVEL_LOAD_PRECACHE", // ESYSTEM_GLOBAL_STATE_LEVEL_LOAD_START_PRECACHE,
  1018. "LEVEL_LOAD_TEXTURES", // ESYSTEM_GLOBAL_STATE_LEVEL_LOAD_START_TEXTURES,
  1019. "LEVEL_LOAD_END", // ESYSTEM_GLOBAL_STATE_LEVEL_LOAD_END,
  1020. "LEVEL_LOAD_COMPLETE" // ESYSTEM_GLOBAL_STATE_LEVEL_LOAD_COMPLETE
  1021. };
  1022. const size_t numElements = sizeof(s_systemGlobalStateNames) / sizeof(s_systemGlobalStateNames[0]);
  1023. const size_t index = (size_t)systemGlobalState;
  1024. if (index >= numElements)
  1025. {
  1026. return "INVALID INDEX";
  1027. }
  1028. return s_systemGlobalStateNames[index];
  1029. }
  1030. void CSystem::SetSystemGlobalState(const ESystemGlobalState systemGlobalState)
  1031. {
  1032. static AZ::TimeMs s_startTime = AZ::Time::ZeroTimeMs;
  1033. if (systemGlobalState != m_systemGlobalState)
  1034. {
  1035. const AZ::TimeMs endTime = AZ::GetRealElapsedTimeMs();
  1036. [[maybe_unused]] const double numSeconds = AZ::TimeMsToSecondsDouble(endTime - s_startTime);
  1037. CryLog("SetGlobalState %d->%d '%s'->'%s' %3.1f seconds",
  1038. m_systemGlobalState, systemGlobalState,
  1039. CSystem::GetSystemGlobalStateName(m_systemGlobalState), CSystem::GetSystemGlobalStateName(systemGlobalState),
  1040. numSeconds);
  1041. s_startTime = AZ::GetRealElapsedTimeMs();
  1042. }
  1043. m_systemGlobalState = systemGlobalState;
  1044. #if AZ_LOADSCREENCOMPONENT_ENABLED
  1045. if (m_systemGlobalState == ESYSTEM_GLOBAL_STATE_LEVEL_LOAD_COMPLETE)
  1046. {
  1047. EBUS_EVENT(LoadScreenBus, Stop);
  1048. }
  1049. #endif // if AZ_LOADSCREENCOMPONENT_ENABLED
  1050. }
  1051. //////////////////////////////////////////////////////////////////////////
  1052. void CSystem::RegisterWindowMessageHandler(IWindowMessageHandler* pHandler)
  1053. {
  1054. #if AZ_LEGACY_CRYSYSTEM_TRAIT_USE_MESSAGE_HANDLER
  1055. assert(pHandler && !stl::find(m_windowMessageHandlers, pHandler) && "This IWindowMessageHandler is already registered");
  1056. m_windowMessageHandlers.push_back(pHandler);
  1057. #else
  1058. CRY_ASSERT(false && "This platform does not support window message handlers");
  1059. #endif
  1060. }
  1061. //////////////////////////////////////////////////////////////////////////
  1062. void CSystem::UnregisterWindowMessageHandler(IWindowMessageHandler* pHandler)
  1063. {
  1064. #if AZ_LEGACY_CRYSYSTEM_TRAIT_USE_MESSAGE_HANDLER
  1065. [[maybe_unused]] bool bRemoved = stl::find_and_erase(m_windowMessageHandlers, pHandler);
  1066. assert(pHandler && bRemoved && "This IWindowMessageHandler was not registered");
  1067. #else
  1068. CRY_ASSERT(false && "This platform does not support window message handlers");
  1069. #endif
  1070. }
  1071. //////////////////////////////////////////////////////////////////////////
  1072. #if defined(WIN32)
  1073. bool CSystem::HandleMessage([[maybe_unused]] HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
  1074. {
  1075. static bool sbInSizingModalLoop;
  1076. int x = LOWORD(lParam);
  1077. int y = HIWORD(lParam);
  1078. *pResult = 0;
  1079. switch (uMsg)
  1080. {
  1081. // System event translation
  1082. case WM_CLOSE:
  1083. /*
  1084. Trigger CSystem to call Quit() the next time
  1085. it calls Update(). HandleMessages can get messages
  1086. pumped to it from SyncMainWithRender which would
  1087. be called recurively by Quit(). Doing so would
  1088. cause the render thread to deadlock and the main
  1089. thread to spin in SRenderThread::WaitFlushFinishedCond.
  1090. */
  1091. AzFramework::ApplicationRequests::Bus::Broadcast(&AzFramework::ApplicationRequests::ExitMainLoop);
  1092. return false;
  1093. case WM_MOVE:
  1094. GetISystemEventDispatcher()->OnSystemEvent(ESYSTEM_EVENT_MOVE, x, y);
  1095. return false;
  1096. case WM_SIZE:
  1097. GetISystemEventDispatcher()->OnSystemEvent(ESYSTEM_EVENT_RESIZE, x, y);
  1098. switch (wParam)
  1099. {
  1100. case SIZE_MINIMIZED:
  1101. EBUS_EVENT(AzFramework::WindowsLifecycleEvents::Bus, OnMinimized);
  1102. break;
  1103. case SIZE_MAXIMIZED:
  1104. EBUS_EVENT(AzFramework::WindowsLifecycleEvents::Bus, OnMaximized);
  1105. break;
  1106. case SIZE_RESTORED:
  1107. EBUS_EVENT(AzFramework::WindowsLifecycleEvents::Bus, OnRestored);
  1108. break;
  1109. }
  1110. return false;
  1111. case WM_WINDOWPOSCHANGED:
  1112. GetISystemEventDispatcher()->OnSystemEvent(ESYSTEM_EVENT_POS_CHANGED, 1, 0);
  1113. return false;
  1114. case WM_STYLECHANGED:
  1115. GetISystemEventDispatcher()->OnSystemEvent(ESYSTEM_EVENT_STYLE_CHANGED, 1, 0);
  1116. return false;
  1117. case WM_ACTIVATE:
  1118. // Pass HIWORD(wParam) as well to indicate whether this window is minimized or not
  1119. // HIWORD(wParam) != 0 is minimized, HIWORD(wParam) == 0 is not minimized
  1120. GetISystemEventDispatcher()->OnSystemEvent(ESYSTEM_EVENT_ACTIVATE, LOWORD(wParam) != WA_INACTIVE, HIWORD(wParam));
  1121. return true;
  1122. case WM_SETFOCUS:
  1123. EBUS_EVENT(AzFramework::WindowsLifecycleEvents::Bus, OnSetFocus);
  1124. GetISystemEventDispatcher()->OnSystemEvent(ESYSTEM_EVENT_CHANGE_FOCUS, 1, 0);
  1125. return false;
  1126. case WM_KILLFOCUS:
  1127. EBUS_EVENT(AzFramework::WindowsLifecycleEvents::Bus, OnKillFocus);
  1128. GetISystemEventDispatcher()->OnSystemEvent(ESYSTEM_EVENT_CHANGE_FOCUS, 0, 0);
  1129. return false;
  1130. case WM_INPUTLANGCHANGE:
  1131. GetISystemEventDispatcher()->OnSystemEvent(ESYSTEM_EVENT_LANGUAGE_CHANGE, wParam, lParam);
  1132. return false;
  1133. case WM_SYSCOMMAND:
  1134. if ((wParam & 0xFFF0) == SC_SCREENSAVE)
  1135. {
  1136. // Check if screen saver is allowed
  1137. IConsole* const pConsole = gEnv->pConsole;
  1138. const ICVar* const pVar = pConsole ? pConsole->GetCVar("sys_screensaver_allowed") : 0;
  1139. return pVar && pVar->GetIVal() == 0;
  1140. }
  1141. return false;
  1142. // Mouse activation
  1143. case WM_MOUSEACTIVATE:
  1144. *pResult = MA_ACTIVATEANDEAT;
  1145. return true;
  1146. // Hardware mouse counters
  1147. case WM_ENTERSIZEMOVE:
  1148. sbInSizingModalLoop = true;
  1149. // Fall through intended
  1150. case WM_ENTERMENULOOP:
  1151. {
  1152. return true;
  1153. }
  1154. case WM_CAPTURECHANGED:
  1155. // If WM_CAPTURECHANGED is received after WM_ENTERSIZEMOVE (ie, moving/resizing begins).
  1156. // but no matching WM_EXITSIZEMOVE is received (this can happen if the window is not actually moved).
  1157. // we still need to decrement the hardware mouse counter that was incremented when WM_ENTERSIZEMOVE was seen.
  1158. // So in this case, we effectively treat WM_CAPTURECHANGED as if it was the WM_EXITSIZEMOVE message.
  1159. // This behavior has only been reproduced the window is deactivated during the modal loop (ie, breakpoint triggered and focus moves to VS).
  1160. case WM_EXITSIZEMOVE:
  1161. if (!sbInSizingModalLoop)
  1162. {
  1163. return false;
  1164. }
  1165. sbInSizingModalLoop = false;
  1166. // Fall through intended
  1167. case WM_EXITMENULOOP:
  1168. {
  1169. return (uMsg != WM_CAPTURECHANGED);
  1170. }
  1171. case WM_SYSKEYUP:
  1172. case WM_SYSKEYDOWN:
  1173. {
  1174. const bool bAlt = (lParam & (1 << 29)) != 0;
  1175. if (bAlt && wParam == VK_F4)
  1176. {
  1177. return false; // Pass though ALT+F4
  1178. }
  1179. // Prevent game from entering menu loop! Editor does allow menu loop.
  1180. return !m_bEditor;
  1181. }
  1182. case WM_INPUT:
  1183. {
  1184. UINT rawInputSize;
  1185. const UINT rawInputHeaderSize = sizeof(RAWINPUTHEADER);
  1186. GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &rawInputSize, rawInputHeaderSize);
  1187. AZStd::array<BYTE, sizeof(RAWINPUT)> rawInputBytesArray;
  1188. LPBYTE rawInputBytes = rawInputBytesArray.data();
  1189. [[maybe_unused]] const UINT bytesCopied = GetRawInputData((HRAWINPUT)lParam, RID_INPUT, rawInputBytes, &rawInputSize, rawInputHeaderSize);
  1190. CRY_ASSERT(bytesCopied == rawInputSize);
  1191. [[maybe_unused]] RAWINPUT* rawInput = (RAWINPUT*)rawInputBytes;
  1192. CRY_ASSERT(rawInput);
  1193. AzFramework::RawInputNotificationBusWindows::Broadcast(&AzFramework::RawInputNotificationsWindows::OnRawInputEvent, *rawInput);
  1194. return false;
  1195. }
  1196. case WM_DEVICECHANGE:
  1197. {
  1198. if (wParam == 0x0007) // DBT_DEVNODES_CHANGED
  1199. {
  1200. AzFramework::RawInputNotificationBusWindows::Broadcast(&AzFramework::RawInputNotificationsWindows::OnRawInputDeviceChangeEvent);
  1201. }
  1202. return true;
  1203. }
  1204. case WM_CHAR:
  1205. {
  1206. const unsigned short codeUnitUTF16 = static_cast<unsigned short>(wParam);
  1207. AzFramework::RawInputNotificationBusWindows::Broadcast(&AzFramework::RawInputNotificationsWindows::OnRawInputCodeUnitUTF16Event, codeUnitUTF16);
  1208. return true;
  1209. }
  1210. // Any other event doesn't interest us
  1211. default:
  1212. return false;
  1213. }
  1214. }
  1215. #endif
  1216. ILevelSystem* CSystem::GetILevelSystem()
  1217. {
  1218. return m_pLevelSystem;
  1219. }
  1220. #undef EXCLUDE_UPDATE_ON_CONSOLE