ApplicationManager.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #if !defined(Q_MOC_RUN)
  10. #include <AzToolsFramework/Application/ToolsApplication.h>
  11. #include <type_traits>
  12. #include <QList>
  13. #include <QString>
  14. #include <QObject>
  15. #include <QDir>
  16. #include <QTimer>
  17. #include <QDateTime>
  18. #include "native/assetprocessor.h"
  19. #endif
  20. class QCoreApplication;
  21. namespace AZ
  22. {
  23. class Entity;
  24. }
  25. namespace AssetProcessor
  26. {
  27. class ThreadWorker;
  28. }
  29. class AssetProcessorAZApplication
  30. : public QObject
  31. , public AzToolsFramework::ToolsApplication
  32. {
  33. Q_OBJECT
  34. public:
  35. AZ_CLASS_ALLOCATOR(AssetProcessorAZApplication, AZ::SystemAllocator)
  36. AssetProcessorAZApplication(int* argc, char*** argv, QObject* parent = nullptr);
  37. AssetProcessorAZApplication(int* argc, char*** argv, QObject* parent, AZ::ComponentApplicationSettings componentAppSettings);
  38. AssetProcessorAZApplication(int* argc, char*** argv, AZ::ComponentApplicationSettings componentAppSettings);
  39. ~AssetProcessorAZApplication() override = default;
  40. /////////////////////////////////////////////////////////
  41. //// AzFramework::Application overrides
  42. AZ::ComponentTypeList GetRequiredSystemComponents() const override;
  43. void RegisterCoreComponents() override;
  44. void ResolveModulePath(AZ::OSString& modulePath) override;
  45. void SetSettingsRegistrySpecializations(AZ::SettingsRegistryInterface::Specializations& specializations) override;
  46. ///////////////////////////////////////////////////////////
  47. Q_SIGNALS:
  48. void AssetProcessorStatus(AssetProcessor::AssetProcessorStatusEntry entry);
  49. private:
  50. AZ::ModuleManagerRequests::PreModuleLoadEvent::Handler m_preModuleLoadHandler;
  51. };
  52. struct ApplicationDependencyInfo;
  53. //This global function is required, if we want to use uuid as a key in a QSet
  54. uint qHash(const AZ::Uuid& key, uint seed = 0);
  55. //! This class allows you to register any number of objects to it
  56. //! and when quit is requested, it will send a signal "QuitRequested()" to the registered object.
  57. //! (You must implement this slot in your object!)
  58. //! It will then expect each of those objects to send it the "ReadyToQuit(QObject*)" message when its ready
  59. //! once every object is ready, qApp() will be told to quit.
  60. //! the QObject parameter is the object that was originally registered and serves as the handle.
  61. //! if your registered object is destroyed, it will automatically remove it from the list for you, no need to
  62. //! unregister.
  63. class ApplicationManager
  64. : public QObject
  65. {
  66. Q_OBJECT
  67. public:
  68. //! This enum is used by the BeforeRun method and is useful in deciding whether we can run the application
  69. //! or whether we need to exit the application either because of an error or because we are restarting
  70. enum BeforeRunStatus
  71. {
  72. Status_Success = 0,
  73. Status_Restarting,
  74. Status_Failure,
  75. };
  76. ApplicationManager(int* argc, char*** argv, QObject* parent = nullptr);
  77. ApplicationManager(int* argc, char*** argv, AZ::ComponentApplicationSettings componentAppSettings);
  78. ApplicationManager(int* argc, char*** argv, QObject* parent, AZ::ComponentApplicationSettings componentAppSettings);
  79. virtual ~ApplicationManager();
  80. //! Prepares all the prerequisite needed for the main application functionality
  81. //! For eg Starts the AZ Framework,Activates logging ,Initialize Qt etc
  82. //! This method can return the following states success,failure and restarting.The latter two will cause the application to exit.
  83. virtual ApplicationManager::BeforeRunStatus BeforeRun();
  84. //! This method actually runs the main functionality of the application ,if BeforeRun method succeeds
  85. virtual bool Run() = 0;
  86. //! Returns a pointer to the QCoreApplication
  87. QCoreApplication* GetQtApplication();
  88. QDir GetSystemRoot() const;
  89. QString GetProjectPath() const;
  90. void RegisterComponentDescriptor(const AZ::ComponentDescriptor* descriptor);
  91. enum class RegistryCheckInstructions
  92. {
  93. Continue,
  94. Exit,
  95. Restart,
  96. };
  97. RegistryCheckInstructions CheckForRegistryProblems(QWidget* parentWidget, bool showPopupMessage);
  98. virtual bool IsAssetProcessorManagerIdle() const = 0;
  99. Q_SIGNALS:
  100. void AssetProcessorStatusChanged(AssetProcessor::AssetProcessorStatusEntry entry);
  101. public Q_SLOTS:
  102. void ReadyToQuit(QObject* source);
  103. virtual void QuitRequested();
  104. void ObjectDestroyed(QObject* source);
  105. void Restart();
  106. private Q_SLOTS:
  107. void CheckQuit();
  108. void CheckForUpdate();
  109. protected:
  110. //! Deactivate all your class member objects in this method
  111. virtual void Destroy() = 0;
  112. //! Prepares Qt Directories,Install Qt Translator etc
  113. virtual bool Activate();
  114. //! Runs late stage set up code
  115. virtual bool PostActivate();
  116. //! Override this method to create either QApplication or QCoreApplication
  117. virtual void CreateQtApplication() = 0;
  118. QString GetOrganizationName() const;
  119. QString GetApplicationName() const;
  120. void RegisterObjectForQuit(QObject* source, bool insertInFront = false);
  121. bool NeedRestart() const;
  122. void addRunningThread(AssetProcessor::ThreadWorker* thread);
  123. template<class BuilderClass>
  124. void RegisterInternalBuilder(const QString& builderName);
  125. //! Load the Modules (Such as Gems) and have them be reflected.
  126. bool ActivateModules();
  127. void PopulateApplicationDependencies();
  128. bool InitiatedShutdown() const;
  129. bool m_duringStartup = true;
  130. AssetProcessorAZApplication m_frameworkApp;
  131. QCoreApplication* m_qApp = nullptr;
  132. virtual void Reflect() {}
  133. virtual const char* GetLogBaseName() = 0;
  134. virtual RegistryCheckInstructions PopupRegistryProblemsMessage(QString warningText) = 0;
  135. private:
  136. bool StartAZFramework();
  137. // QuitPair - Object pointer and "is ready" boolean pair.
  138. typedef QPair<QObject*, bool> QuitPair;
  139. QList<QuitPair> m_objectsToNotify;
  140. bool m_duringShutdown = false;
  141. QList<ApplicationDependencyInfo*> m_appDependencies;
  142. QList<QString> m_filesOfInterest;
  143. QList<AssetProcessor::ThreadWorker*> m_runningThreads;
  144. QTimer m_updateTimer;
  145. bool m_needRestart = false;
  146. bool m_queuedCheckQuit = false;
  147. QDir m_systemRoot;
  148. AZ::Entity* m_entity = nullptr;
  149. };
  150. ///This class stores all the information of files that
  151. /// we need to monitor for relaunching assetprocessor
  152. struct ApplicationDependencyInfo
  153. {
  154. QString m_fileName;
  155. QDateTime m_timestamp;
  156. ApplicationDependencyInfo(QString fileName, QDateTime timestamp)
  157. : m_fileName(fileName)
  158. , m_timestamp(timestamp)
  159. {
  160. }
  161. public:
  162. QString FileName() const;
  163. void SetFileName(QString FileName);
  164. QDateTime Timestamp() const;
  165. void SetTimestamp(const QDateTime& Timestamp);
  166. };