LmbrCentral.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. #include "LmbrCentral.h"
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. #include <AzCore/std/containers/list.h>
  13. #include <AzFramework/API/ApplicationAPI.h>
  14. #include <AzFramework/Asset/AssetCatalogBus.h>
  15. #include <AzFramework/Metrics/MetricsPlainTextNameRegistration.h>
  16. // Component descriptors
  17. #include "Audio/AudioAreaEnvironmentComponent.h"
  18. #include "Audio/AudioEnvironmentComponent.h"
  19. #include "Audio/AudioListenerComponent.h"
  20. #include "Audio/AudioMultiPositionComponent.h"
  21. #include "Audio/AudioPreloadComponent.h"
  22. #include "Audio/AudioProxyComponent.h"
  23. #include "Audio/AudioRtpcComponent.h"
  24. #include "Audio/AudioSwitchComponent.h"
  25. #include "Audio/AudioSystemComponent.h"
  26. #include "Audio/AudioTriggerComponent.h"
  27. #include "Bundling/BundlingSystemComponent.h"
  28. #include "Scripting/TagComponent.h"
  29. #include "Scripting/SimpleStateComponent.h"
  30. #include "Scripting/SpawnerComponent.h"
  31. #include "Scripting/LookAtComponent.h"
  32. #include "Scripting/RandomTimedSpawnerComponent.h"
  33. #include "Geometry/GeometrySystemComponent.h"
  34. #include <Asset/AssetSystemDebugComponent.h>
  35. // Other
  36. #include "Unhandled/Other/AudioAssetTypeInfo.h"
  37. #include "Unhandled/Other/CharacterPhysicsAssetTypeInfo.h"
  38. #include "Unhandled/Other/GroupAssetTypeInfo.h"
  39. #include "Unhandled/Other/PrefabsLibraryAssetTypeInfo.h"
  40. #include "Unhandled/Other/GameTokenAssetTypeInfo.h"
  41. #include "Unhandled/Other/EntityPrototypeLibraryAssetTypeInfo.h"
  42. // Texture
  43. #include "Unhandled/Texture/SubstanceAssetTypeInfo.h"
  44. #include "Unhandled/Texture/TextureAssetTypeInfo.h"
  45. // Hidden
  46. #include "Unhandled/Hidden/TextureMipmapAssetTypeInfo.h"
  47. //UI
  48. #include <Unhandled/UI/EntityIconAssetTypeInfo.h>
  49. #include "Unhandled/UI/FontAssetTypeInfo.h"
  50. #include "Unhandled/UI/UICanvasAssetTypeInfo.h"
  51. // Asset types
  52. #include <AzCore/Slice/SliceAsset.h>
  53. #include <LmbrCentral/Rendering/TextureAsset.h>
  54. // Scriptable Ebus Registration
  55. #include "Events/ReflectScriptableEvents.h"
  56. // Shape components
  57. #include "Shape/SphereShapeComponent.h"
  58. #include "Shape/DiskShapeComponent.h"
  59. #include "Shape/AxisAlignedBoxShapeComponent.h"
  60. #include "Shape/BoxShapeComponent.h"
  61. #include "Shape/QuadShapeComponent.h"
  62. #include "Shape/CylinderShapeComponent.h"
  63. #include "Shape/CapsuleShapeComponent.h"
  64. #include "Shape/TubeShapeComponent.h"
  65. #include "Shape/CompoundShapeComponent.h"
  66. #include "Shape/SplineComponent.h"
  67. #include "Shape/PolygonPrismShapeComponent.h"
  68. #include "Shape/ReferenceShapeComponent.h"
  69. namespace LmbrCentral
  70. {
  71. // This component boots the required allocators for LmbrCentral everywhere but AssetBuilders
  72. class LmbrCentralAllocatorComponent
  73. : public AZ::Component
  74. {
  75. public:
  76. AZ_COMPONENT(LmbrCentralAllocatorComponent, "{B0512A75-AC4A-423A-BB55-C3355C0B186A}", AZ::Component);
  77. LmbrCentralAllocatorComponent() = default;
  78. ~LmbrCentralAllocatorComponent() override = default;
  79. void Activate() override
  80. {
  81. }
  82. void Deactivate() override
  83. {
  84. }
  85. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  86. {
  87. provided.push_back(AZ_CRC_CE("MemoryAllocators"));
  88. }
  89. static void Reflect(AZ::ReflectContext* context)
  90. {
  91. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  92. if (serializeContext)
  93. {
  94. serializeContext->Class<LmbrCentralAllocatorComponent, AZ::Component>()->Version(1);
  95. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  96. {
  97. editContext->Class<LmbrCentralAllocatorComponent>(
  98. "LmbrCentral Allocator Component", "Manages initialization of memory allocators required by LmbrCentral")
  99. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  100. ->Attribute(AZ::Edit::Attributes::Category, "Engine")
  101. ;
  102. }
  103. }
  104. }
  105. };
  106. // This component is opted in to AssetBuilders
  107. class LmbrCentralAssetBuilderAllocatorComponent
  108. : public LmbrCentralAllocatorComponent
  109. {
  110. public:
  111. AZ_COMPONENT(LmbrCentralAssetBuilderAllocatorComponent, "{030B63DE-7DC1-4E08-9AAF-1D089D3D0C46}", LmbrCentralAllocatorComponent);
  112. LmbrCentralAssetBuilderAllocatorComponent() = default;
  113. ~LmbrCentralAssetBuilderAllocatorComponent() override = default;
  114. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  115. {
  116. provided.push_back(AZ_CRC_CE("MemoryAllocators"));
  117. }
  118. static void Reflect(AZ::ReflectContext* context)
  119. {
  120. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  121. if (serializeContext)
  122. {
  123. serializeContext->Class<LmbrCentralAssetBuilderAllocatorComponent, LmbrCentralAllocatorComponent>()->Version(1)
  124. ->Attribute(AZ::Edit::Attributes::SystemComponentTags, AZStd::vector<AZ::Crc32>({ AZ_CRC_CE("AssetBuilder") }));
  125. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  126. {
  127. editContext->Class<LmbrCentralAssetBuilderAllocatorComponent>(
  128. "LmbrCentral Asset Builder Allocator Component", "Manages initialization of memory allocators required by LmbrCentral during asset building")
  129. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  130. ->Attribute(AZ::Edit::Attributes::Category, "Engine")
  131. ;
  132. }
  133. }
  134. }
  135. };
  136. ////////////////////////////////////////////////////////////////////////////
  137. // LmbrCentral::LmbrCentralModule
  138. ////////////////////////////////////////////////////////////////////////////
  139. //! Create ComponentDescriptors and add them to the list.
  140. //! The descriptors will be registered at the appropriate time.
  141. //! The descriptors will be destroyed (and thus unregistered) at the appropriate time.
  142. LmbrCentralModule::LmbrCentralModule()
  143. : AZ::Module()
  144. {
  145. m_descriptors.insert(m_descriptors.end(), {
  146. AudioAreaEnvironmentComponent::CreateDescriptor(),
  147. AudioEnvironmentComponent::CreateDescriptor(),
  148. AudioListenerComponent::CreateDescriptor(),
  149. AudioMultiPositionComponent::CreateDescriptor(),
  150. AudioPreloadComponent::CreateDescriptor(),
  151. AudioProxyComponent::CreateDescriptor(),
  152. AudioRtpcComponent::CreateDescriptor(),
  153. AudioSwitchComponent::CreateDescriptor(),
  154. AudioSystemComponent::CreateDescriptor(),
  155. AudioTriggerComponent::CreateDescriptor(),
  156. BundlingSystemComponent::CreateDescriptor(),
  157. LmbrCentralAllocatorComponent::CreateDescriptor(),
  158. LmbrCentralAssetBuilderAllocatorComponent::CreateDescriptor(),
  159. LmbrCentralSystemComponent::CreateDescriptor(),
  160. SimpleStateComponent::CreateDescriptor(),
  161. SpawnerComponent::CreateDescriptor(),
  162. LookAtComponent::CreateDescriptor(),
  163. TagComponent::CreateDescriptor(),
  164. SphereShapeComponent::CreateDescriptor(),
  165. DiskShapeComponent::CreateDescriptor(),
  166. BoxShapeComponent::CreateDescriptor(),
  167. AxisAlignedBoxShapeComponent::CreateDescriptor(),
  168. QuadShapeComponent::CreateDescriptor(),
  169. CylinderShapeComponent::CreateDescriptor(),
  170. CapsuleShapeComponent::CreateDescriptor(),
  171. TubeShapeComponent::CreateDescriptor(),
  172. CompoundShapeComponent::CreateDescriptor(),
  173. ReferenceShapeComponent::CreateDescriptor(),
  174. SplineComponent::CreateDescriptor(),
  175. PolygonPrismShapeComponent::CreateDescriptor(),
  176. GeometrySystemComponent::CreateDescriptor(),
  177. RandomTimedSpawnerComponent::CreateDescriptor(),
  178. SphereShapeDebugDisplayComponent::CreateDescriptor(),
  179. DiskShapeDebugDisplayComponent::CreateDescriptor(),
  180. BoxShapeDebugDisplayComponent::CreateDescriptor(),
  181. AxisAlignedBoxShapeDebugDisplayComponent::CreateDescriptor(),
  182. QuadShapeDebugDisplayComponent::CreateDescriptor(),
  183. CapsuleShapeDebugDisplayComponent::CreateDescriptor(),
  184. CylinderShapeDebugDisplayComponent::CreateDescriptor(),
  185. PolygonPrismShapeDebugDisplayComponent::CreateDescriptor(),
  186. TubeShapeDebugDisplayComponent::CreateDescriptor(),
  187. AssetSystemDebugComponent::CreateDescriptor(),
  188. });
  189. // This is an internal Amazon gem, so register it's components for metrics tracking, otherwise the name of the component won't get sent back.
  190. // IF YOU ARE A THIRDPARTY WRITING A GEM, DO NOT REGISTER YOUR COMPONENTS WITH EditorMetricsComponentRegistrationBus
  191. AZStd::vector<AZ::Uuid> typeIds;
  192. typeIds.reserve(m_descriptors.size());
  193. for (AZ::ComponentDescriptor* descriptor : m_descriptors)
  194. {
  195. typeIds.emplace_back(descriptor->GetUuid());
  196. }
  197. AzFramework::MetricsPlainTextNameRegistrationBus::Broadcast(
  198. &AzFramework::MetricsPlainTextNameRegistrationBus::Events::RegisterForNameSending, typeIds);
  199. }
  200. //! Request system components on the system entity.
  201. //! These components' memory is owned by the system entity.
  202. AZ::ComponentTypeList LmbrCentralModule::GetRequiredSystemComponents() const
  203. {
  204. return {
  205. azrtti_typeid<LmbrCentralAllocatorComponent>(),
  206. azrtti_typeid<LmbrCentralAssetBuilderAllocatorComponent>(),
  207. azrtti_typeid<LmbrCentralSystemComponent>(),
  208. azrtti_typeid<GeometrySystemComponent>(),
  209. azrtti_typeid<AudioSystemComponent>(),
  210. azrtti_typeid<BundlingSystemComponent>(),
  211. azrtti_typeid<AssetSystemDebugComponent>(),
  212. };
  213. }
  214. ////////////////////////////////////////////////////////////////////////////
  215. // LmbrCentralSystemComponent
  216. void LmbrCentralSystemComponent::Reflect(AZ::ReflectContext* context)
  217. {
  218. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  219. {
  220. serializeContext->ClassDeprecate(
  221. "SimpleAssetReference_TextureAsset",
  222. AZ::Uuid("{68E92460-5C0C-4031-9620-6F1A08763243}"),
  223. [](AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& rootElement)
  224. {
  225. AZStd::vector<AZ::SerializeContext::DataElementNode> childNodeElements;
  226. for (int index = 0; index < rootElement.GetNumSubElements(); ++index)
  227. {
  228. childNodeElements.push_back(rootElement.GetSubElement(index));
  229. }
  230. // Convert the rootElement now, the existing child DataElmentNodes are now removed
  231. rootElement.Convert<AzFramework::SimpleAssetReference<TextureAsset>>(context);
  232. for (AZ::SerializeContext::DataElementNode& childNodeElement : childNodeElements)
  233. {
  234. rootElement.AddElement(AZStd::move(childNodeElement));
  235. }
  236. return true;
  237. });
  238. AzFramework::SimpleAssetReference<TextureAsset>::Register(*serializeContext);
  239. serializeContext->Class<LmbrCentralSystemComponent, AZ::Component>()
  240. ->Version(1)
  241. ;
  242. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  243. {
  244. editContext->Class<LmbrCentralSystemComponent>(
  245. "LmbrCentral", "Coordinates initialization of systems within LmbrCentral")
  246. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  247. ->Attribute(AZ::Edit::Attributes::Category, "Game")
  248. ;
  249. }
  250. }
  251. ReflectScriptableEvents::Reflect(context);
  252. }
  253. void LmbrCentralSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  254. {
  255. provided.push_back(AZ_CRC_CE("LmbrCentralService"));
  256. }
  257. void LmbrCentralSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  258. {
  259. incompatible.push_back(AZ_CRC_CE("LmbrCentralService"));
  260. }
  261. void LmbrCentralSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  262. {
  263. required.push_back(AZ_CRC_CE("AssetDatabaseService"));
  264. }
  265. void LmbrCentralSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  266. {
  267. dependent.push_back(AZ_CRC_CE("MemoryAllocators"));
  268. dependent.push_back(AZ_CRC_CE("AssetCatalogService"));
  269. }
  270. void LmbrCentralSystemComponent::Activate()
  271. {
  272. // Register asset handlers. Requires "AssetDatabaseService"
  273. AZ_Assert(AZ::Data::AssetManager::IsReady(), "Asset manager isn't ready!");
  274. // Add asset types and extensions to AssetCatalog. Uses "AssetCatalogService".
  275. if (auto assetCatalog = AZ::Data::AssetCatalogRequestBus::FindFirstHandler(); assetCatalog)
  276. {
  277. // Sprite files are only used by LyShine and should be moved there at some point
  278. assetCatalog->AddExtension("sprite");
  279. }
  280. AZ::Data::AssetManagerNotificationBus::Handler::BusConnect();
  281. // Other
  282. auto audioAssetTypeInfo = aznew AudioAssetTypeInfo();
  283. audioAssetTypeInfo->Register();
  284. m_unhandledAssetInfo.emplace_back(audioAssetTypeInfo);
  285. auto characterPhysicsAssetTypeInfo = aznew CharacterPhysicsAssetTypeInfo();
  286. characterPhysicsAssetTypeInfo->Register();
  287. m_unhandledAssetInfo.emplace_back(characterPhysicsAssetTypeInfo);
  288. auto groupAssetTypeInfo = aznew GroupAssetTypeInfo();
  289. groupAssetTypeInfo->Register();
  290. m_unhandledAssetInfo.emplace_back(groupAssetTypeInfo);
  291. auto prefabsLibraryAssetTypeInfo = aznew PrefabsLibraryAssetTypeInfo();
  292. prefabsLibraryAssetTypeInfo->Register();
  293. m_unhandledAssetInfo.emplace_back(prefabsLibraryAssetTypeInfo);
  294. auto entityPrototypeAssetTypeInfo = aznew EntityPrototypeLibraryAssetTypeInfo();
  295. entityPrototypeAssetTypeInfo->Register();
  296. m_unhandledAssetInfo.emplace_back(entityPrototypeAssetTypeInfo);
  297. auto gameTokenAssetTypeInfo = aznew GameTokenAssetTypeInfo();
  298. gameTokenAssetTypeInfo->Register();
  299. m_unhandledAssetInfo.emplace_back(gameTokenAssetTypeInfo);
  300. // Texture
  301. auto substanceAssetTypeInfo = aznew SubstanceAssetTypeInfo();
  302. substanceAssetTypeInfo->Register();
  303. m_unhandledAssetInfo.emplace_back(substanceAssetTypeInfo);
  304. auto textureAssetTypeInfo = aznew TextureAssetTypeInfo();
  305. textureAssetTypeInfo->Register();
  306. m_unhandledAssetInfo.emplace_back(textureAssetTypeInfo);
  307. // Hidden
  308. auto textureMipmapAssetTypeInfo = aznew TextureMipmapAssetTypeInfo();
  309. textureMipmapAssetTypeInfo->Register();
  310. m_unhandledAssetInfo.emplace_back(textureMipmapAssetTypeInfo);
  311. // UI
  312. auto fontAssetTypeInfo = aznew FontAssetTypeInfo();
  313. fontAssetTypeInfo->Register();
  314. m_unhandledAssetInfo.emplace_back(fontAssetTypeInfo);
  315. auto uiCanvasAssetTypeInfo = aznew UICanvasAssetTypeInfo();
  316. uiCanvasAssetTypeInfo->Register();
  317. m_unhandledAssetInfo.emplace_back(uiCanvasAssetTypeInfo);
  318. auto entityIconAssetTypeInfo = aznew EntityIconAssetTypeInfo();
  319. entityIconAssetTypeInfo->Register();
  320. m_unhandledAssetInfo.emplace_back(entityIconAssetTypeInfo);
  321. }
  322. void LmbrCentralSystemComponent::Deactivate()
  323. {
  324. // AssetTypeInfo's destructor calls Unregister()
  325. m_unhandledAssetInfo.clear();
  326. AZ::Data::AssetManagerNotificationBus::Handler::BusDisconnect();
  327. // AssetHandler's destructor calls Unregister()
  328. m_assetHandlers.clear();
  329. for (auto allocatorIt = m_allocatorShutdowns.rbegin(); allocatorIt != m_allocatorShutdowns.rend(); ++allocatorIt)
  330. {
  331. (*allocatorIt)();
  332. }
  333. m_allocatorShutdowns.clear();
  334. }
  335. } // namespace LmbrCentral
  336. #if !defined(LMBR_CENTRAL_EDITOR)
  337. #if defined(O3DE_GEM_NAME)
  338. AZ_DECLARE_MODULE_CLASS(AZ_JOIN(Gem_, O3DE_GEM_NAME), LmbrCentral::LmbrCentralModule)
  339. #else
  340. AZ_DECLARE_MODULE_CLASS(Gem_LmbrCentral, LmbrCentral::LmbrCentralModule)
  341. #endif
  342. #endif