3
0

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_env.pMovieSystem; };
  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. //////////////////////////////////////////////////////////////////////////
  149. // retrieves the perlin noise singleton instance
  150. CPNoise3* GetNoiseGen() override;
  151. void DetectGameFolderAccessRights();
  152. void ExecuteCommandLine(bool deferred=true) override;
  153. void GetUpdateStats(SSystemUpdateStats& stats) override;
  154. //////////////////////////////////////////////////////////////////////////
  155. XmlNodeRef CreateXmlNode(const char* sNodeName = "", bool bReuseStrings = false, bool bIsProcessingInstruction = false) override;
  156. XmlNodeRef LoadXmlFromFile(const char* sFilename, bool bReuseStrings = false) override;
  157. XmlNodeRef LoadXmlFromBuffer(const char* buffer, size_t size, bool bReuseStrings = false, bool bSuppressWarnings = false) override;
  158. IXmlUtils* GetXmlUtils() override;
  159. //////////////////////////////////////////////////////////////////////////
  160. void IgnoreUpdates(bool bIgnore) override { m_bIgnoreUpdates = bIgnore; };
  161. bool IsTestMode() const override { return m_bTestMode; }
  162. //@}
  163. void SleepIfNeeded();
  164. void FatalError(const char* format, ...) override PRINTF_PARAMS(2, 3);
  165. void ReportBug(const char* format, ...) override PRINTF_PARAMS(2, 3);
  166. // Validator Warning.
  167. void WarningV(EValidatorModule module, EValidatorSeverity severity, int flags, const char* file, const char* format, va_list args) override;
  168. void Warning(EValidatorModule module, EValidatorSeverity severity, int flags, const char* file, const char* format, ...) override;
  169. void ShowMessage(const char* text, const char* caption, unsigned int uType) override;
  170. bool CheckLogVerbosity(int verbosity) override;
  171. //! Return pointer to user defined callback.
  172. ISystemUserCallback* GetUserCallback() const { return m_pUserCallback; };
  173. //////////////////////////////////////////////////////////////////////////
  174. void SaveConfiguration() override;
  175. void LoadConfiguration(const char* sFilename, ILoadConfigurationEntrySink* pSink = nullptr, bool warnIfMissing = true) override;
  176. ESystemConfigPlatform GetConfigPlatform() const override;
  177. void SetConfigPlatform(ESystemConfigPlatform platform) override;
  178. //////////////////////////////////////////////////////////////////////////
  179. bool IsPaused() const override { return m_bPaused; };
  180. ILocalizationManager* GetLocalizationManager() override;
  181. void debug_GetCallStack(const char** pFunctions, int& nCount) override;
  182. void debug_LogCallStack(int nMaxFuncs = 32, int nFlags = 0) override;
  183. public:
  184. #if !defined(RELEASE)
  185. void SetVersionInfo(const char* const szVersion);
  186. #endif
  187. #if defined(WIN32)
  188. friend LRESULT WINAPI WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  189. #endif
  190. void RegisterWindowMessageHandler(IWindowMessageHandler* pHandler) override;
  191. void UnregisterWindowMessageHandler(IWindowMessageHandler* pHandler) override;
  192. // IWindowMessageHandler
  193. #if defined(WIN32)
  194. bool HandleMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT* pResult) override;
  195. #endif
  196. // ~IWindowMessageHandler
  197. private:
  198. // Release all resources.
  199. void ShutDown();
  200. //! @name Initialization routines
  201. //@{
  202. bool InitConsole();
  203. bool InitFileSystem();
  204. bool InitFileSystem_LoadEngineFolders(const SSystemInitParams& initParams);
  205. bool InitAudioSystem();
  206. //@}
  207. //////////////////////////////////////////////////////////////////////////
  208. // Helper functions.
  209. //////////////////////////////////////////////////////////////////////////
  210. void CreateSystemVars();
  211. void QueryVersionInfo();
  212. void LogVersion();
  213. void LogBuildInfo();
  214. void SetDevMode(bool bEnable);
  215. #ifndef _RELEASE
  216. static void SystemVersionChanged(ICVar* pCVar);
  217. #endif // #ifndef _RELEASE
  218. void UpdateAudioSystems();
  219. void AddCVarGroupDirectory(const AZStd::string& sPath) override;
  220. #if defined(AZ_RESTRICTED_PLATFORM)
  221. #define AZ_RESTRICTED_SECTION SYSTEM_H_SECTION_3
  222. #include AZ_RESTRICTED_FILE(System_h)
  223. #elif defined(WIN32)
  224. bool GetWinGameFolder(char* szMyDocumentsPath, int maxPathSize);
  225. #endif
  226. public:
  227. void EnableFloatExceptions(int type);
  228. // interface ISystem -------------------------------------------
  229. bool IsDevMode() const override { return m_bInDevMode; }
  230. // -------------------------------------------------------------
  231. //! attaches the given variable to the given container;
  232. //! recreates the variable if necessary
  233. ICVar* attachVariable (const char* szVarName, int* pContainer, const char* szComment, int dwFlags = 0);
  234. const CTimeValue& GetLastTickTime() const { return m_lastTickTime; }
  235. private: // ------------------------------------------------------
  236. // System environment.
  237. SSystemGlobalEnvironment m_env;
  238. bool m_bInitializedSuccessfully; //!< true if the system completed all initialization steps
  239. bool m_bRelaunch; //!< relaunching the app or not (true beforerelaunch)
  240. int m_iLoadingMode; //!< Game is loading w/o changing context (0 not, 1 quickloading, 2 full loading)
  241. bool m_bTestMode; //!< If running in testing mode.
  242. bool m_bEditor; //!< If running in Editor.
  243. bool m_bNoCrashDialog;
  244. bool m_bNoErrorReportWindow;
  245. bool m_bPreviewMode; //!< If running in Preview mode.
  246. bool m_bDedicatedServer; //!< If running as Dedicated server.
  247. bool m_bIgnoreUpdates; //!< When set to true will ignore Update and Render calls,
  248. bool m_bInDevMode; //!< Set to true if was in dev mode.
  249. bool m_bGameFolderWritable; //!< True when verified that current game folder have write access.
  250. CTimeValue m_lastTickTime;
  251. //! system event dispatcher
  252. ISystemEventDispatcher* m_pSystemEventDispatcher;
  253. //! System to manage levels.
  254. ILevelSystem* m_pLevelSystem;
  255. // XML Utils interface.
  256. class CXmlUtils* m_pXMLUtils;
  257. int m_iApplicationInstance;
  258. //! to hold the values stored in system.cfg
  259. //! because editor uses it's own values,
  260. //! and then saves them to file, overwriting the user's resolution.
  261. int m_iHeight;
  262. int m_iWidth;
  263. int m_iColorBits;
  264. // System console variables.
  265. //////////////////////////////////////////////////////////////////////////
  266. #if AZ_LOADSCREENCOMPONENT_ENABLED
  267. ICVar* m_game_load_screen_uicanvas_path;
  268. ICVar* m_level_load_screen_uicanvas_path;
  269. ICVar* m_game_load_screen_sequence_to_auto_play;
  270. ICVar* m_level_load_screen_sequence_to_auto_play;
  271. ICVar* m_game_load_screen_sequence_fixed_fps;
  272. ICVar* m_level_load_screen_sequence_fixed_fps;
  273. ICVar* m_game_load_screen_max_fps;
  274. ICVar* m_level_load_screen_max_fps;
  275. ICVar* m_game_load_screen_minimum_time{};
  276. ICVar* m_level_load_screen_minimum_time{};
  277. #endif // if AZ_LOADSCREENCOMPONENT_ENABLED
  278. ICVar* m_sysNoUpdate;
  279. ICVar* m_svDedicatedMaxRate;
  280. ICVar* m_sys_firstlaunch;
  281. ICVar* m_sys_load_files_to_memory;
  282. #if defined(AZ_RESTRICTED_PLATFORM)
  283. #define AZ_RESTRICTED_SECTION SYSTEM_H_SECTION_4
  284. #include AZ_RESTRICTED_FILE(System_h)
  285. #endif
  286. ICVar* m_gpu_particle_physics;
  287. AZStd::string m_sSavedRDriver; //!< to restore the driver when quitting the dedicated server
  288. //////////////////////////////////////////////////////////////////////////
  289. //! User define callback for system events.
  290. ISystemUserCallback* m_pUserCallback;
  291. SFileVersion m_fileVersion;
  292. SFileVersion m_productVersion;
  293. SFileVersion m_buildVersion;
  294. class CLocalizedStringsManager* m_pLocalizationManager;
  295. ESystemConfigPlatform m_ConfigPlatform;
  296. // Pause mode.
  297. bool m_bPaused;
  298. bool m_bNoUpdate;
  299. bool m_executedCommandLine = false;
  300. AZStd::unique_ptr<AzFramework::MissingAssetLogger> m_missingAssetLogger;
  301. public:
  302. //////////////////////////////////////////////////////////////////////////
  303. // File version.
  304. //////////////////////////////////////////////////////////////////////////
  305. const SFileVersion& GetFileVersion() override;
  306. const SFileVersion& GetProductVersion() override;
  307. const SFileVersion& GetBuildVersion() override;
  308. void OpenPlatformPaks();
  309. void OpenLanguagePak(const char* sLanguage);
  310. void OpenLanguageAudioPak(const char* sLanguage);
  311. void GetLocalizedPath(const char* sLanguage, AZStd::string& sLocalizedPath);
  312. void GetLocalizedAudioPath(const char* sLanguage, AZStd::string& sLocalizedPath);
  313. void CloseLanguagePak(const char* sLanguage);
  314. void CloseLanguageAudioPak(const char* sLanguage);
  315. void UpdateMovieSystem(const int updateFlags, const float fFrameTime, const bool bPreUpdate);
  316. //////////////////////////////////////////////////////////////////////////
  317. // CryAssert and error related.
  318. bool RegisterErrorObserver(IErrorObserver* errorObserver) override;
  319. bool UnregisterErrorObserver(IErrorObserver* errorObserver) override;
  320. void OnAssert(const char* condition, const char* message, const char* fileName, unsigned int fileLineNumber) override;
  321. void OnFatalError(const char* message);
  322. bool IsAssertDialogVisible() const override;
  323. void SetAssertVisible(bool bAssertVisble) override;
  324. //////////////////////////////////////////////////////////////////////////
  325. void ClearErrorMessages() override
  326. {
  327. m_ErrorMessages.clear();
  328. }
  329. bool IsLoading()
  330. {
  331. return m_eRuntimeState == ESYSTEM_EVENT_LEVEL_LOAD_START_LOADINGSCREEN;
  332. }
  333. ESystemGlobalState GetSystemGlobalState() override;
  334. void SetSystemGlobalState(ESystemGlobalState systemGlobalState) override;
  335. #if !defined(_RELEASE)
  336. bool IsSavingResourceList() const override { return (g_cvars.archiveVars.nSaveLevelResourceList != 0); }
  337. #endif
  338. private:
  339. std::vector<IErrorObserver*> m_errorObservers;
  340. ESystemGlobalState m_systemGlobalState;
  341. static const char* GetSystemGlobalStateName(const ESystemGlobalState systemGlobalState);
  342. public:
  343. void InitLocalization();
  344. protected: // -------------------------------------------------------------
  345. CCmdLine* m_pCmdLine;
  346. AZStd::string m_currentLanguageAudio;
  347. std::vector< std::pair<CTimeValue, float> > m_updateTimes;
  348. struct SErrorMessage
  349. {
  350. AZStd::string m_Message;
  351. float m_fTimeToShow;
  352. float m_Color[4];
  353. bool m_HardFailure;
  354. };
  355. using TErrorMessages = std::list<SErrorMessage>;
  356. TErrorMessages m_ErrorMessages;
  357. bool m_bHasRenderedErrorMessage;
  358. ESystemEvent m_eRuntimeState;
  359. bool m_bIsAsserting;
  360. std::vector<IWindowMessageHandler*> m_windowMessageHandlers;
  361. };