DllMain.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. #if !defined(AZ_MONOLITHIC_BUILD)
  9. #include <AzCore/Component/EntityBus.h>
  10. #include <AzCore/Component/Entity.h>
  11. #include <AzCore/EBus/EBus.h>
  12. #include <AzCore/Module/Environment.h>
  13. #include <AzCore/RTTI/BehaviorContext.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. #include <SceneAPI/SceneCore/Components/BehaviorComponent.h>
  16. #include <SceneAPI/SceneCore/Components/LoadingComponent.h>
  17. #include <SceneAPI/SceneCore/Components/GenerationComponent.h>
  18. #include <SceneAPI/SceneCore/Components/ExportingComponent.h>
  19. #include <SceneAPI/SceneCore/Components/Utilities/EntityConstructor.h>
  20. #include <SceneAPI/SceneCore/Components/SceneSystemComponent.h>
  21. #include <SceneAPI/SceneCore/Containers/RuleContainer.h>
  22. #include <SceneAPI/SceneCore/Containers/Scene.h>
  23. #include <SceneAPI/SceneCore/Containers/SceneManifest.h>
  24. #include <SceneAPI/SceneCore/DataTypes/IManifestObject.h>
  25. #include <SceneAPI/SceneCore/DataTypes/IGraphObject.h>
  26. #include <SceneAPI/SceneCore/DataTypes/Groups/IGroup.h>
  27. #include <SceneAPI/SceneCore/DataTypes/Groups/IImportGroup.h>
  28. #include <SceneAPI/SceneCore/DataTypes/Groups/IMeshGroup.h>
  29. #include <SceneAPI/SceneCore/DataTypes/Groups/ISkeletonGroup.h>
  30. #include <SceneAPI/SceneCore/DataTypes/Groups/ISkinGroup.h>
  31. #include <SceneAPI/SceneCore/DataTypes/Groups/IAnimationGroup.h>
  32. #include <SceneAPI/SceneCore/DataTypes/Rules/IRule.h>
  33. #include <SceneAPI/SceneCore/DataTypes/Rules/IBlendShapeRule.h>
  34. #include <SceneAPI/SceneCore/DataTypes/Rules/ICommentRule.h>
  35. #include <SceneAPI/SceneCore/DataTypes/Rules/IMaterialRule.h>
  36. #include <SceneAPI/SceneCore/DataTypes/Rules/IMeshAdvancedRule.h>
  37. #include <SceneAPI/SceneCore/DataTypes/Rules/ILodRule.h>
  38. #include <SceneAPI/SceneCore/DataTypes/Rules/ISkeletonProxyRule.h>
  39. #include <SceneAPI/SceneCore/DataTypes/Rules/IScriptProcessorRule.h>
  40. #include <SceneAPI/SceneCore/DataTypes/Rules/IUnmodifiableRule.h>
  41. #include <SceneAPI/SceneCore/DataTypes/Rules/ITagRule.h>
  42. #include <SceneAPI/SceneCore/DataTypes/GraphData/IAnimationData.h>
  43. #include <SceneAPI/SceneCore/DataTypes/GraphData/IBlendShapeData.h>
  44. #include <SceneAPI/SceneCore/DataTypes/GraphData/IBoneData.h>
  45. #include <SceneAPI/SceneCore/DataTypes/GraphData/IMaterialData.h>
  46. #include <SceneAPI/SceneCore/DataTypes/GraphData/IMeshData.h>
  47. #include <SceneAPI/SceneCore/DataTypes/GraphData/IMeshVertexColorData.h>
  48. #include <SceneAPI/SceneCore/DataTypes/GraphData/IMeshVertexUVData.h>
  49. #include <SceneAPI/SceneCore/DataTypes/GraphData/ISkinWeightData.h>
  50. #include <SceneAPI/SceneCore/DataTypes/GraphData/ITransform.h>
  51. #include <SceneAPI/SceneCore/DataTypes/ManifestBase/ISceneNodeSelectionList.h>
  52. #include <SceneAPI/SceneCore/Import/ManifestImportRequestHandler.h>
  53. #include <SceneAPI/SceneCore/Import/SceneImportSettings.h>
  54. #include <SceneAPI/SceneCore/Utilities/PatternMatcher.h>
  55. #include <SceneAPI/SceneCore/Utilities/Reporting.h>
  56. namespace AZ
  57. {
  58. namespace SceneAPI
  59. {
  60. namespace SceneCore
  61. {
  62. static class EntityMonitor* g_entityMonitor = nullptr;
  63. static AZ::Entity* g_behaviors = nullptr;
  64. static AZ::EntityId g_behaviorsId;
  65. static AZStd::vector<AZ::ComponentDescriptor*> g_componentDescriptors;
  66. static AZ::SceneAPI::Import::ManifestImportRequestHandler* g_manifestImporter = nullptr;
  67. class EntityMonitor
  68. : public AZ::EntityBus::Handler
  69. {
  70. public:
  71. AZ_CLASS_ALLOCATOR(EntityMonitor, AZ::SystemAllocator);
  72. EntityMonitor()
  73. {
  74. AZ::EntityBus::Handler::BusConnect(g_behaviorsId);
  75. }
  76. ~EntityMonitor()
  77. {
  78. AZ::EntityBus::Handler::BusDisconnect(g_behaviorsId);
  79. }
  80. void OnEntityDestruction(const AZ::EntityId& entityId) override
  81. {
  82. if (entityId == g_behaviorsId)
  83. {
  84. // Another part of the code has claimed and deleted this entity already.
  85. g_behaviors = nullptr;
  86. AZ::EntityBus::Handler::BusDisconnect(g_behaviorsId);
  87. g_behaviorsId.SetInvalid();
  88. }
  89. }
  90. };
  91. void Initialize()
  92. {
  93. // Explicitly creating this component early as this currently needs to be available to the
  94. // RC before Gems are loaded in order to know the file extension.
  95. if (!g_manifestImporter)
  96. {
  97. g_manifestImporter = aznew AZ::SceneAPI::Import::ManifestImportRequestHandler();
  98. g_manifestImporter->Activate();
  99. }
  100. }
  101. bool IMeshGroupConverter(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
  102. {
  103. if (classElement.GetVersion() == 1)
  104. {
  105. // There have been 2 version of IMeshGroup, one that directly inherited from IGroup and one that
  106. // inherited as IMeshGroup : ISceneNodeGroup (was IMeshBaseGroup) : IGroup. To fix this, check
  107. // if {1D20FA11-B184-429E-8C86-745852234845} (ISceneNodeGroup) is present and if not add it.
  108. AZ::SerializeContext::DataElementNode& baseClass = classElement.GetSubElement(0);
  109. if (baseClass.GetId() != AZ::SceneAPI::DataTypes::ISceneNodeGroup::TYPEINFO_Uuid())
  110. {
  111. if (!baseClass.Convert<AZ::SceneAPI::DataTypes::ISceneNodeGroup>(context))
  112. {
  113. AZ_TracePrintf(AZ::SceneAPI::Utilities::ErrorWindow, "Failed to upgrade IMeshGroup from version 1.");
  114. return false;
  115. }
  116. }
  117. }
  118. return true;
  119. }
  120. void ReflectTypes(AZ::SerializeContext* context)
  121. {
  122. if (!context)
  123. {
  124. AZ::ComponentApplicationBus::BroadcastResult(context, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  125. }
  126. // Check if this library hasn't already been reflected. This can happen as the ResourceCompilerScene needs
  127. // to explicitly load and reflect the SceneAPI libraries to discover the available extension, while
  128. // Gems with system components need to do the same in the Project Manager.
  129. if (context && (context->IsRemovingReflection() || !context->FindClassData(AZ::SceneAPI::DataTypes::IGroup::TYPEINFO_Uuid())))
  130. {
  131. AZ::SceneAPI::DataTypes::IManifestObject::Reflect(context);
  132. AZ::SceneAPI::Events::CallProcessorBinder::Reflect(context);
  133. // Register components
  134. AZ::SceneAPI::SceneCore::BehaviorComponent::Reflect(context);
  135. AZ::SceneAPI::SceneCore::LoadingComponent::Reflect(context);
  136. AZ::SceneAPI::SceneCore::GenerationComponent::Reflect(context);
  137. AZ::SceneAPI::SceneCore::ExportingComponent::Reflect(context);
  138. AZ::SceneAPI::SceneCore::SceneSystemComponent::Reflect(context);
  139. // Register group interfaces
  140. context->Class<AZ::SceneAPI::DataTypes::IGroup, AZ::SceneAPI::DataTypes::IManifestObject>()->Version(1);
  141. context->Class<AZ::SceneAPI::DataTypes::ISceneNodeGroup, AZ::SceneAPI::DataTypes::IGroup>()->Version(1);
  142. context->Class<AZ::SceneAPI::DataTypes::IMeshGroup, AZ::SceneAPI::DataTypes::ISceneNodeGroup>()->Version(2, &IMeshGroupConverter);
  143. context->Class<AZ::SceneAPI::DataTypes::ISkeletonGroup, AZ::SceneAPI::DataTypes::IGroup>()->Version(1);
  144. context->Class<AZ::SceneAPI::DataTypes::ISkinGroup, AZ::SceneAPI::DataTypes::ISceneNodeGroup>()->Version(1);
  145. context->Class<AZ::SceneAPI::DataTypes::IAnimationGroup, AZ::SceneAPI::DataTypes::IGroup>()->Version(1);
  146. context->Class<AZ::SceneAPI::DataTypes::IImportGroup, AZ::SceneAPI::DataTypes::ISceneNodeGroup>()->Version(0);
  147. // Register rule interfaces
  148. context->Class<AZ::SceneAPI::DataTypes::IRule, AZ::SceneAPI::DataTypes::IManifestObject>()->Version(1);
  149. context->Class<AZ::SceneAPI::DataTypes::IBlendShapeRule, AZ::SceneAPI::DataTypes::IRule>()->Version(1);
  150. context->Class<AZ::SceneAPI::DataTypes::ICommentRule, AZ::SceneAPI::DataTypes::IRule>()->Version(1);
  151. context->Class<AZ::SceneAPI::DataTypes::IMaterialRule, AZ::SceneAPI::DataTypes::IRule>()->Version(1);
  152. context->Class<AZ::SceneAPI::DataTypes::IMeshAdvancedRule, AZ::SceneAPI::DataTypes::IRule>()->Version(1);
  153. context->Class<AZ::SceneAPI::DataTypes::ILodRule, AZ::SceneAPI::DataTypes::IRule>()->Version(1);
  154. context->Class<AZ::SceneAPI::DataTypes::ISkeletonProxyRule, AZ::SceneAPI::DataTypes::IRule>()->Version(1);
  155. context->Class<AZ::SceneAPI::DataTypes::IScriptProcessorRule, AZ::SceneAPI::DataTypes::IRule>()->Version(1);
  156. context->Class<AZ::SceneAPI::DataTypes::IUnmodifiableRule, AZ::SceneAPI::DataTypes::IRule>()->Version(1);
  157. context->Class<AZ::SceneAPI::DataTypes::ITagRule, AZ::SceneAPI::DataTypes::IRule>()->Version(1);
  158. // Register graph data interfaces
  159. context->Class<AZ::SceneAPI::DataTypes::IAnimationData, AZ::SceneAPI::DataTypes::IGraphObject>()->Version(1);
  160. context->Class<AZ::SceneAPI::DataTypes::IBlendShapeData, AZ::SceneAPI::DataTypes::IGraphObject>()->Version(1);
  161. context->Class<AZ::SceneAPI::DataTypes::IBoneData, AZ::SceneAPI::DataTypes::IGraphObject>()->Version(1);
  162. context->Class<AZ::SceneAPI::DataTypes::IMaterialData, AZ::SceneAPI::DataTypes::IGraphObject>()->Version(1);
  163. context->Class<AZ::SceneAPI::DataTypes::IMeshData, AZ::SceneAPI::DataTypes::IGraphObject>()->Version(1);
  164. context->Class<AZ::SceneAPI::DataTypes::IMeshVertexColorData, AZ::SceneAPI::DataTypes::IGraphObject>()->Version(1);
  165. context->Class<AZ::SceneAPI::DataTypes::IMeshVertexUVData, AZ::SceneAPI::DataTypes::IGraphObject>()->Version(1);
  166. context->Class<AZ::SceneAPI::DataTypes::ISkinWeightData, AZ::SceneAPI::DataTypes::IGraphObject>()->Version(1);
  167. context->Class<AZ::SceneAPI::DataTypes::ITransform, AZ::SceneAPI::DataTypes::IGraphObject>()->Version(1);
  168. // Register base manifest types
  169. context->Class<AZ::SceneAPI::DataTypes::ISceneNodeSelectionList>()->Version(1);
  170. // Register containers
  171. AZ::SceneAPI::Containers::RuleContainer::Reflect(context);
  172. AZ::SceneAPI::Containers::SceneManifest::Reflect(context);
  173. // Register utilities
  174. AZ::SceneAPI::SceneCore::PatternMatcher::Reflect(context);
  175. AZ::SceneAPI::Utilities::DebugSceneGraph::Reflect(context);
  176. // Register import settings
  177. AZ::SceneAPI::SceneImportSettings::Reflect(context);
  178. }
  179. }
  180. void Reflect(AZ::SerializeContext* context)
  181. {
  182. ReflectTypes(context);
  183. // Descriptor registration is done in Reflect instead of Initialize because the ResourceCompilerScene initializes the libraries before
  184. // there's an application.
  185. if (g_componentDescriptors.empty())
  186. {
  187. for (AZ::ComponentDescriptor* descriptor : g_componentDescriptors)
  188. {
  189. AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Handler::RegisterComponentDescriptor, descriptor);
  190. }
  191. }
  192. }
  193. void ReflectBehavior(AZ::BehaviorContext* context)
  194. {
  195. AZ::SceneAPI::Containers::Scene::Reflect(context);
  196. AZ::SceneAPI::Containers::SceneGraph::Reflect(context);
  197. AZ::SceneAPI::Containers::SceneManifest::Reflect(context);
  198. AZ::SceneAPI::Containers::RuleContainer::Reflect(context);
  199. AZ::SceneAPI::SceneCore::ExportingComponent::Reflect(context);
  200. }
  201. void Activate()
  202. {
  203. if (g_behaviors)
  204. {
  205. return;
  206. }
  207. g_behaviors = AZ::SceneAPI::SceneCore::EntityConstructor::BuildEntityRaw("Scene Behaviors",
  208. AZ::SceneAPI::SceneCore::BehaviorComponent::TYPEINFO_Uuid());
  209. g_behaviorsId = g_behaviors->GetId();
  210. AZ_Error("SceneCore", !g_entityMonitor, "The EntityMonitor has not been deactivated properly, cannot complete activation");
  211. if (!g_entityMonitor)
  212. {
  213. g_entityMonitor = aznew EntityMonitor();
  214. }
  215. }
  216. void Deactivate()
  217. {
  218. if (g_entityMonitor)
  219. {
  220. delete g_entityMonitor;
  221. g_entityMonitor = nullptr;
  222. }
  223. if (g_behaviors)
  224. {
  225. g_behaviors->Deactivate();
  226. delete g_behaviors;
  227. g_behaviors = nullptr;
  228. g_behaviorsId.SetInvalid();
  229. }
  230. }
  231. void Uninitialize()
  232. {
  233. AZ::SerializeContext* context = nullptr;
  234. AZ::ComponentApplicationBus::BroadcastResult(context, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  235. if (context)
  236. {
  237. context->EnableRemoveReflection();
  238. Reflect(context);
  239. context->DisableRemoveReflection();
  240. context->CleanupModuleGenericClassInfo();
  241. }
  242. if (!g_componentDescriptors.empty())
  243. {
  244. for (AZ::ComponentDescriptor* descriptor : g_componentDescriptors)
  245. {
  246. descriptor->ReleaseDescriptor();
  247. }
  248. g_componentDescriptors.clear();
  249. g_componentDescriptors.shrink_to_fit();
  250. }
  251. if (g_manifestImporter)
  252. {
  253. g_manifestImporter->Deactivate();
  254. delete g_manifestImporter;
  255. g_manifestImporter = nullptr;
  256. }
  257. }
  258. } // namespace SceneCore
  259. } // namespace SceneAPI
  260. } // namespace AZ
  261. static bool g_sceneCoreInitialized = false;
  262. extern "C" AZ_DLL_EXPORT void InitializeDynamicModule()
  263. {
  264. if (g_sceneCoreInitialized)
  265. {
  266. return;
  267. }
  268. g_sceneCoreInitialized = true;
  269. AZ::SceneAPI::SceneCore::Initialize();
  270. }
  271. extern "C" AZ_DLL_EXPORT void Reflect(AZ::SerializeContext* context)
  272. {
  273. AZ::SceneAPI::SceneCore::Reflect(context);
  274. }
  275. extern "C" AZ_DLL_EXPORT void ReflectBehavior(AZ::BehaviorContext * context)
  276. {
  277. AZ::SceneAPI::SceneCore::ReflectBehavior(context);
  278. }
  279. extern "C" AZ_DLL_EXPORT void ReflectTypes(AZ::SerializeContext * context)
  280. {
  281. AZ::SceneAPI::SceneCore::ReflectTypes(context);
  282. }
  283. extern "C" AZ_DLL_EXPORT void CleanUpSceneCoreGenericClassInfo()
  284. {
  285. AZ::GetCurrentSerializeContextModule().Cleanup();
  286. }
  287. extern "C" AZ_DLL_EXPORT void Activate()
  288. {
  289. AZ::SceneAPI::SceneCore::Activate();
  290. }
  291. extern "C" AZ_DLL_EXPORT void Deactivate()
  292. {
  293. AZ::SceneAPI::SceneCore::Deactivate();
  294. }
  295. extern "C" AZ_DLL_EXPORT void UninitializeDynamicModule()
  296. {
  297. if (!g_sceneCoreInitialized)
  298. {
  299. return;
  300. }
  301. g_sceneCoreInitialized = false;
  302. AZ::SceneAPI::SceneCore::Uninitialize();
  303. }
  304. #endif // !defined(AZ_MONOLITHIC_BUILD)