3
0

ApplicationManager.h 6.3 KB

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