System.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. #pragma once
  9. #include <ISystem.h>
  10. #include <IRenderer.h>
  11. #include <IWindowMessageHandler.h>
  12. #include <CryVersion.h>
  13. #include "CmdLine.h"
  14. #include <AzFramework/Archive/ArchiveVars.h>
  15. #include <CryCommon/LoadScreenBus.h>
  16. #include <AzCore/Module/DynamicModuleHandle.h>
  17. #include <AzCore/Math/Crc.h>
  18. #include <CryCommon/TimeValue.h>
  19. #include <list>
  20. #include <map>
  21. namespace AzFramework
  22. {
  23. class MissingAssetLogger;
  24. }
  25. struct IConsoleCmdArgs;
  26. struct ICVar;
  27. struct IFFont;
  28. class CWatchdogThread;
  29. #if defined(AZ_RESTRICTED_PLATFORM)
  30. #undef AZ_RESTRICTED_SECTION
  31. #define SYSTEM_H_SECTION_1 1
  32. #define SYSTEM_H_SECTION_2 2
  33. #define SYSTEM_H_SECTION_3 3
  34. #define SYSTEM_H_SECTION_4 4
  35. #endif
  36. #if defined(AZ_RESTRICTED_PLATFORM)
  37. #define AZ_RESTRICTED_SECTION SYSTEM_H_SECTION_1
  38. #include AZ_RESTRICTED_FILE(System_h)
  39. #else
  40. #if defined(WIN32) || defined(LINUX) || defined(APPLE)
  41. #define AZ_LEGACY_CRYSYSTEM_TRAIT_ALLOW_CREATE_BACKUP_LOG_FILE 1
  42. #endif
  43. #if defined(LINUX) || defined(APPLE)
  44. #define AZ_LEGACY_CRYSYSTEM_TRAIT_FORWARD_EXCEPTION_POINTERS 1
  45. #endif
  46. #if !defined(_WIN32)
  47. #define AZ_LEGACY_CRYSYSTEM_TRAIT_DEBUGCALLSTACK_SINGLETON 1
  48. #endif
  49. #if !defined(LINUX) && !defined(APPLE)
  50. #define AZ_LEGACY_CRYSYSTEM_TRAIT_DEBUGCALLSTACK_TRANSLATE 1
  51. #endif
  52. #if !defined(LINUX) && !defined(APPLE)
  53. #define AZ_LEGACY_CRYSYSTEM_TRAIT_DEBUGCALLSTACK_APPEND_MODULENAME 1
  54. #endif
  55. #define AZ_LEGACY_CRYSYSTEM_TRAIT_USE_EXCLUDEUPDATE_ON_CONSOLE 0
  56. #if defined(WIN32)
  57. #define AZ_LEGACY_CRYSYSTEM_TRAIT_USE_MESSAGE_HANDLER 1
  58. #endif
  59. //////////////////////////////////////////////////////////////////////////
  60. #endif
  61. #ifdef WIN32
  62. using WIN_HMODULE = void*;
  63. #else
  64. typedef void* WIN_HMODULE;
  65. #endif
  66. #define PHSYICS_OBJECT_ENTITY 0
  67. struct SSystemCVars
  68. {
  69. ICVar* sys_localization_folder;
  70. int sys_float_exceptions;
  71. int sys_no_crash_dialog;
  72. int sys_no_error_report_window;
  73. int sys_dump_aux_threads;
  74. int sys_WER;
  75. int sys_dump_type;
  76. int sys_trackview;
  77. float sys_update_profile_time;
  78. int sys_MaxFPS;
  79. float sys_maxTimeStepForMovieSystem;
  80. int sys_asserts;
  81. int sys_error_debugbreak;
  82. AZ::IO::ArchiveVars archiveVars;
  83. };
  84. extern SSystemCVars g_cvars;
  85. class CSystem;
  86. class AssetSystem;
  87. /*
  88. ===========================================
  89. The System interface Class
  90. ===========================================
  91. */
  92. class CXConsole;
  93. //////////////////////////////////////////////////////////////////////
  94. //! ISystem implementation
  95. class CSystem
  96. : public ISystem
  97. , public ILoadConfigurationEntrySink
  98. , public ISystemEventListener
  99. , public IWindowMessageHandler
  100. , public CrySystemRequestBus::Handler
  101. {
  102. public:
  103. CSystem();
  104. ~CSystem();
  105. static void OnLanguageCVarChanged(ICVar* language);
  106. static void OnLocalizationFolderCVarChanged(ICVar* const pLocalizationFolder);
  107. // adding CVAR to toggle assert verbosity level
  108. static void OnAssertLevelCvarChanged(ICVar* pArgs);
  109. static void SetAssertLevel(int _assertlevel);
  110. static void OnLogLevelCvarChanged(ICVar* pArgs);
  111. static void SetLogLevel(int _logLevel);
  112. // interface ILoadConfigurationEntrySink ----------------------------------
  113. void OnLoadConfigurationEntry(const char* szKey, const char* szValue, const char* szGroup) override;
  114. // ISystemEventListener
  115. void OnSystemEvent(ESystemEvent event, UINT_PTR wparam, UINT_PTR lparam) override;
  116. ///////////////////////////////////////////////////////////////////////////
  117. //! @name ISystem implementation
  118. //@{
  119. virtual bool Init(const SSystemInitParams& startupParams);
  120. void Release() override;
  121. SSystemGlobalEnvironment* GetGlobalEnvironment() override { return &m_env; }
  122. bool UpdatePreTickBus(int updateFlags = 0, int nPauseMode = 0) override;
  123. bool UpdatePostTickBus(int updateFlags = 0, int nPauseMode = 0) override;
  124. bool UpdateLoadtime() override;
  125. ////////////////////////////////////////////////////////////////////////
  126. // CrySystemRequestBus interface implementation
  127. ISystem* GetCrySystem() override;
  128. ////////////////////////////////////////////////////////////////////////
  129. void Relaunch(bool bRelaunch) override;
  130. bool IsRelaunch() const override { return m_bRelaunch; };
  131. void SerializingFile(int mode) override { m_iLoadingMode = mode; }
  132. int IsSerializingFile() const override { return m_iLoadingMode; }
  133. void Quit() override;
  134. bool IsQuitting() const override;
  135. void ShutdownFileSystem(); // used to cleanup any file resources, such as cache handle.
  136. const char* GetUserName() override;
  137. int GetApplicationInstance() override;
  138. int GetApplicationLogInstance(const char* logFilePath) override;
  139. AZ::IO::IArchive* GetIPak() override { return m_env.pCryPak; };
  140. IConsole* GetIConsole() override { return m_env.pConsole; };
  141. IRemoteConsole* GetIRemoteConsole() override;
  142. IMovieSystem* GetIMovieSystem() override { return m_movieSystem; };
  143. ICryFont* GetICryFont() override{ return m_env.pCryFont; }
  144. ILog* GetILog() override{ return m_env.pLog; }
  145. ICmdLine* GetICmdLine() override{ return m_pCmdLine; }
  146. ILevelSystem* GetILevelSystem() override;
  147. ISystemEventDispatcher* GetISystemEventDispatcher() override { return m_pSystemEventDispatcher; }
  148. void DetectGameFolderAccessRights();
  149. void ExecuteCommandLine(bool deferred=true) override;
  150. void GetUpdateStats(SSystemUpdateStats& stats) override;
  151. //////////////////////////////////////////////////////////////////////////
  152. XmlNodeRef CreateXmlNode(const char* sNodeName = "", bool bReuseStrings = false, bool bIsProcessingInstruction = false) override;
  153. XmlNodeRef LoadXmlFromFile(const char* sFilename, bool bReuseStrings = false) override;
  154. XmlNodeRef LoadXmlFromBuffer(const char* buffer, size_t size, bool bReuseStrings = false, bool bSuppressWarnings = false) override;
  155. IXmlUtils* GetXmlUtils() override;
  156. //////////////////////////////////////////////////////////////////////////
  157. void IgnoreUpdates(bool bIgnore) override { m_bIgnoreUpdates = bIgnore; };
  158. bool IsTestMode() const override { return m_bTestMode; }
  159. //@}
  160. void SleepIfNeeded();
  161. void FatalError(const char* format, ...) override PRINTF_PARAMS(2, 3);
  162. void ReportBug(const char* format, ...) override PRINTF_PARAMS(2, 3);
  163. // Validator Warning.
  164. void WarningV(EValidatorModule module, EValidatorSeverity severity, int flags, const char* file, const char* format, va_list args) override;
  165. void Warning(EValidatorModule module, EValidatorSeverity severity, int flags, const char* file, const char* format, ...) override;
  166. void ShowMessage(const char* text, const char* caption, unsigned int uType) override;
  167. bool CheckLogVerbosity(int verbosity) override;
  168. //! Return pointer to user defined callback.
  169. ISystemUserCallback* GetUserCallback() const { return m_pUserCallback; };
  170. //////////////////////////////////////////////////////////////////////////
  171. void SaveConfiguration() override;
  172. void LoadConfiguration(const char* sFilename, ILoadConfigurationEntrySink* pSink = nullptr, bool warnIfMissing = true) override;
  173. ESystemConfigPlatform GetConfigPlatform() const override;
  174. void SetConfigPlatform(ESystemConfigPlatform platform) override;
  175. //////////////////////////////////////////////////////////////////////////
  176. bool IsPaused() const override { return m_bPaused; };
  177. ILocalizationManager* GetLocalizationManager() override;
  178. void debug_GetCallStack(const char** pFunctions, int& nCount) override;
  179. void debug_LogCallStack(int nMaxFuncs = 32, int nFlags = 0) override;
  180. public:
  181. #if !defined(RELEASE)
  182. void SetVersionInfo(const char* const szVersion);
  183. #endif
  184. #if defined(WIN32)
  185. friend LRESULT WINAPI WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  186. #endif
  187. void RegisterWindowMessageHandler(IWindowMessageHandler* pHandler) override;
  188. void UnregisterWindowMessageHandler(IWindowMessageHandler* pHandler) override;
  189. // IWindowMessageHandler
  190. #if defined(WIN32)
  191. bool HandleMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT* pResult) override;
  192. #endif
  193. // ~IWindowMessageHandler
  194. private:
  195. // Release all resources.
  196. void ShutDown();
  197. //! @name Initialization routines
  198. //@{
  199. bool InitConsole();
  200. bool InitFileSystem();
  201. bool InitFileSystem_LoadEngineFolders(const SSystemInitParams& initParams);
  202. bool InitAudioSystem();
  203. //@}
  204. //////////////////////////////////////////////////////////////////////////
  205. // Helper functions.
  206. //////////////////////////////////////////////////////////////////////////
  207. void CreateSystemVars();
  208. void QueryVersionInfo();
  209. void LogVersion();
  210. void LogBuildInfo();
  211. void SetDevMode(bool bEnable);
  212. #ifndef _RELEASE
  213. static void SystemVersionChanged(ICVar* pCVar);
  214. #endif // #ifndef _RELEASE
  215. void UpdateAudioSystems();
  216. void AddCVarGroupDirectory(const AZStd::string& sPath) override;
  217. #if defined(AZ_RESTRICTED_PLATFORM)
  218. #define AZ_RESTRICTED_SECTION SYSTEM_H_SECTION_3
  219. #include AZ_RESTRICTED_FILE(System_h)
  220. #elif defined(WIN32)
  221. bool GetWinGameFolder(char* szMyDocumentsPath, int maxPathSize);
  222. #endif
  223. public:
  224. void EnableFloatExceptions(int type);
  225. // interface ISystem -------------------------------------------
  226. bool IsDevMode() const override { return m_bInDevMode; }
  227. // -------------------------------------------------------------
  228. //! attaches the given variable to the given container;
  229. //! recreates the variable if necessary
  230. ICVar* attachVariable (const char* szVarName, int* pContainer, const char* szComment, int dwFlags = 0);
  231. const CTimeValue& GetLastTickTime() const { return m_lastTickTime; }
  232. private: // ------------------------------------------------------
  233. // System environment.
  234. SSystemGlobalEnvironment m_env;
  235. bool m_bInitializedSuccessfully; //!< true if the system completed all initialization steps
  236. bool m_bRelaunch; //!< relaunching the app or not (true beforerelaunch)
  237. int m_iLoadingMode; //!< Game is loading w/o changing context (0 not, 1 quickloading, 2 full loading)
  238. bool m_bTestMode; //!< If running in testing mode.
  239. bool m_bEditor; //!< If running in Editor.
  240. bool m_bNoCrashDialog;
  241. bool m_bNoErrorReportWindow;
  242. bool m_bPreviewMode; //!< If running in Preview mode.
  243. bool m_bDedicatedServer; //!< If running as Dedicated server.
  244. bool m_bIgnoreUpdates; //!< When set to true will ignore Update and Render calls,
  245. bool m_bInDevMode; //!< Set to true if was in dev mode.
  246. bool m_bGameFolderWritable; //!< True when verified that current game folder have write access.
  247. CTimeValue m_lastTickTime;
  248. //! system event dispatcher
  249. ISystemEventDispatcher* m_pSystemEventDispatcher;
  250. //! System to manage levels.
  251. ILevelSystem* m_pLevelSystem;
  252. // XML Utils interface.
  253. class CXmlUtils* m_pXMLUtils;
  254. int m_iApplicationInstance;
  255. //! to hold the values stored in system.cfg
  256. //! because editor uses it's own values,
  257. //! and then saves them to file, overwriting the user's resolution.
  258. int m_iHeight;
  259. int m_iWidth;
  260. int m_iColorBits;
  261. // System console variables.
  262. //////////////////////////////////////////////////////////////////////////
  263. #if AZ_LOADSCREENCOMPONENT_ENABLED
  264. ICVar* m_game_load_screen_uicanvas_path;
  265. ICVar* m_level_load_screen_uicanvas_path;
  266. ICVar* m_game_load_screen_sequence_to_auto_play;
  267. ICVar* m_level_load_screen_sequence_to_auto_play;
  268. ICVar* m_game_load_screen_sequence_fixed_fps;
  269. ICVar* m_level_load_screen_sequence_fixed_fps;
  270. ICVar* m_game_load_screen_max_fps;
  271. ICVar* m_level_load_screen_max_fps;
  272. ICVar* m_game_load_screen_minimum_time{};
  273. ICVar* m_level_load_screen_minimum_time{};
  274. #endif // if AZ_LOADSCREENCOMPONENT_ENABLED
  275. ICVar* m_sysNoUpdate;
  276. ICVar* m_svDedicatedMaxRate;
  277. ICVar* m_sys_firstlaunch;
  278. ICVar* m_sys_load_files_to_memory;
  279. #if defined(AZ_RESTRICTED_PLATFORM)
  280. #define AZ_RESTRICTED_SECTION SYSTEM_H_SECTION_4
  281. #include AZ_RESTRICTED_FILE(System_h)
  282. #endif
  283. ICVar* m_gpu_particle_physics;
  284. AZStd::string m_sSavedRDriver; //!< to restore the driver when quitting the dedicated server
  285. //////////////////////////////////////////////////////////////////////////
  286. //! User define callback for system events.
  287. ISystemUserCallback* m_pUserCallback;
  288. SFileVersion m_fileVersion;
  289. SFileVersion m_productVersion;
  290. SFileVersion m_buildVersion;
  291. class CLocalizedStringsManager* m_pLocalizationManager;
  292. ESystemConfigPlatform m_ConfigPlatform;
  293. // Pause mode.
  294. bool m_bPaused;
  295. bool m_bNoUpdate;
  296. bool m_executedCommandLine = false;
  297. AZStd::unique_ptr<AzFramework::MissingAssetLogger> m_missingAssetLogger;
  298. IMovieSystem* m_movieSystem;
  299. public:
  300. //////////////////////////////////////////////////////////////////////////
  301. // File version.
  302. //////////////////////////////////////////////////////////////////////////
  303. const SFileVersion& GetFileVersion() override;
  304. const SFileVersion& GetProductVersion() override;
  305. const SFileVersion& GetBuildVersion() override;
  306. void OpenPlatformPaks();
  307. void OpenLanguagePak(const char* sLanguage);
  308. void OpenLanguageAudioPak(const char* sLanguage);
  309. void GetLocalizedPath(const char* sLanguage, AZStd::string& sLocalizedPath);
  310. void GetLocalizedAudioPath(const char* sLanguage, AZStd::string& sLocalizedPath);
  311. void CloseLanguagePak(const char* sLanguage);
  312. void CloseLanguageAudioPak(const char* sLanguage);
  313. void UpdateMovieSystem(const int updateFlags, const float fFrameTime, const bool bPreUpdate);
  314. //////////////////////////////////////////////////////////////////////////
  315. // CryAssert and error related.
  316. bool RegisterErrorObserver(IErrorObserver* errorObserver) override;
  317. bool UnregisterErrorObserver(IErrorObserver* errorObserver) override;
  318. void OnAssert(const char* condition, const char* message, const char* fileName, unsigned int fileLineNumber) override;
  319. void OnFatalError(const char* message);
  320. bool IsAssertDialogVisible() const override;
  321. void SetAssertVisible(bool bAssertVisble) override;
  322. //////////////////////////////////////////////////////////////////////////
  323. void ClearErrorMessages() override
  324. {
  325. m_ErrorMessages.clear();
  326. }
  327. bool IsLoading()
  328. {
  329. return m_eRuntimeState == ESYSTEM_EVENT_LEVEL_LOAD_START_LOADINGSCREEN;
  330. }
  331. ESystemGlobalState GetSystemGlobalState() override;
  332. void SetSystemGlobalState(ESystemGlobalState systemGlobalState) override;
  333. #if !defined(_RELEASE)
  334. bool IsSavingResourceList() const override { return (g_cvars.archiveVars.nSaveLevelResourceList != 0); }
  335. #endif
  336. private:
  337. std::vector<IErrorObserver*> m_errorObservers;
  338. ESystemGlobalState m_systemGlobalState;
  339. static const char* GetSystemGlobalStateName(const ESystemGlobalState systemGlobalState);
  340. public:
  341. void InitLocalization();
  342. protected: // -------------------------------------------------------------
  343. CCmdLine* m_pCmdLine;
  344. AZStd::string m_currentLanguageAudio;
  345. std::vector< std::pair<CTimeValue, float> > m_updateTimes;
  346. struct SErrorMessage
  347. {
  348. AZStd::string m_Message;
  349. float m_fTimeToShow;
  350. float m_Color[4];
  351. bool m_HardFailure;
  352. };
  353. using TErrorMessages = std::list<SErrorMessage>;
  354. TErrorMessages m_ErrorMessages;
  355. bool m_bHasRenderedErrorMessage;
  356. ESystemEvent m_eRuntimeState;
  357. bool m_bIsAsserting;
  358. std::vector<IWindowMessageHandler*> m_windowMessageHandlers;
  359. };