LevelSystem.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "ILevelSystem.h"
  10. #include <AzFramework/API/ApplicationAPI.h>
  11. #include <AzFramework/Archive/IArchive.h>
  12. #include <CryCommon/TimeValue.h>
  13. // [LYN-2376] Remove the entire file once legacy slice support is removed
  14. namespace LegacyLevelSystem
  15. {
  16. class CLevelInfo
  17. : public ILevelInfo
  18. {
  19. friend class CLevelSystem;
  20. public:
  21. CLevelInfo() = default;
  22. // ILevelInfo
  23. const char* GetName() const override { return m_levelName.c_str(); }
  24. const char* GetPath() const override { return m_levelPath.c_str(); }
  25. const char* GetAssetName() const override { return m_levelAssetName.c_str(); }
  26. // ~ILevelInfo
  27. private:
  28. bool ReadInfo();
  29. bool OpenLevelPak();
  30. void CloseLevelPak();
  31. AZStd::string m_defaultGameTypeName;
  32. AZStd::string m_levelName;
  33. AZStd::string m_levelPath;
  34. AZStd::string m_levelAssetName;
  35. AZStd::string m_levelPakFullPath;
  36. bool m_isPak = false;
  37. };
  38. struct ILevel
  39. {
  40. virtual ~ILevel() = default;
  41. virtual void Release() = 0;
  42. virtual ILevelInfo* GetLevelInfo() = 0;
  43. };
  44. class CLevel
  45. : public ILevel
  46. {
  47. friend class CLevelSystem;
  48. public:
  49. CLevel() {}
  50. virtual ~CLevel() = default;
  51. void Release() override { delete this; }
  52. ILevelInfo* GetLevelInfo() override { return &m_levelInfo; }
  53. private:
  54. CLevelInfo m_levelInfo;
  55. };
  56. class CLevelSystem
  57. : public ILevelSystem
  58. , AzFramework::LevelSystemLifecycleInterface::Registrar
  59. {
  60. public:
  61. CLevelSystem(ISystem* pSystem, const char* levelsFolder);
  62. virtual ~CLevelSystem();
  63. void Release() override { delete this; };
  64. // ILevelSystem
  65. void Rescan(const char* levelsFolder) override;
  66. int GetLevelCount() override;
  67. ILevelInfo* GetLevelInfo(int level) override;
  68. ILevelInfo* GetLevelInfo(const char* levelName) override;
  69. void AddListener(ILevelSystemListener* pListener) override;
  70. void RemoveListener(ILevelSystemListener* pListener) override;
  71. bool LoadLevel(const char* levelName) override;
  72. void UnloadLevel() override;
  73. //! AzFramework::LevelSystemLifecycleInterface overrides.
  74. //! @{
  75. bool IsLevelLoaded() const override { return m_bLevelLoaded; }
  76. const char* GetCurrentLevelName() const override
  77. {
  78. if (m_pCurrentLevel && m_pCurrentLevel->GetLevelInfo())
  79. {
  80. return m_pCurrentLevel->GetLevelInfo()->GetName();
  81. }
  82. return "";
  83. }
  84. //! @}
  85. // If the level load failed then we need to have a different shutdown procedure vs when a level is naturally unloaded
  86. void SetLevelLoadFailed(bool loadFailed) override { m_levelLoadFailed = loadFailed; }
  87. bool GetLevelLoadFailed() override { return m_levelLoadFailed; }
  88. // Unsupported by legacy level system.
  89. AZ::Data::AssetType GetLevelAssetType() const override { return {}; }
  90. // ~ILevelSystem
  91. private:
  92. float GetLastLevelLoadTime() { return m_fLastLevelLoadTime; }
  93. void ScanFolder(const char* subfolder, bool modFolder);
  94. void PopulateLevels(
  95. AZStd::string searchPattern, const AZStd::string& folder, AZ::IO::IArchive* pPak, bool& modFolder, bool fromFileSystemOnly);
  96. void PrepareNextLevel(const char* levelName);
  97. ILevel* LoadLevelInternal(const char* _levelName);
  98. // Methods to notify ILevelSystemListener
  99. void OnLevelNotFound(const char* levelName);
  100. void OnLoadingStart(const char* levelName);
  101. void OnLoadingComplete(const char* levelName);
  102. void OnLoadingError(const char* levelName, const char* error);
  103. void OnLoadingProgress(const char* levelName, int progressAmount);
  104. void OnUnloadComplete(const char* levelName);
  105. void LogLoadingTime();
  106. bool LoadLevelInfo(CLevelInfo& levelInfo);
  107. // internal get functions for the level infos ... they preserve the type and don't
  108. // directly cast to the interface
  109. CLevelInfo* GetLevelInfoInternal(int level);
  110. CLevelInfo* GetLevelInfoInternal(const AZStd::string& levelName);
  111. ISystem* m_pSystem;
  112. AZStd::vector<CLevelInfo> m_levelInfos;
  113. AZStd::string m_levelsFolder;
  114. ILevel* m_pCurrentLevel;
  115. ILevelInfo* m_pLoadingLevelInfo;
  116. AZStd::string m_lastLevelName;
  117. float m_fLastLevelLoadTime;
  118. float m_fLastTime;
  119. bool m_bLevelLoaded;
  120. bool m_levelLoadFailed = false;
  121. int m_nLoadedLevelsCount;
  122. CTimeValue m_levelLoadStartTime;
  123. AZStd::vector<ILevelSystemListener*> m_listeners;
  124. AZ::IO::IArchive::LevelPackOpenEvent::Handler m_levelPackOpenHandler;
  125. AZ::IO::IArchive::LevelPackCloseEvent::Handler m_levelPackCloseHandler;
  126. static constexpr const char* LevelPakName = "level.pak";
  127. };
  128. } // namespace LegacyLevelSystem