DllMain.cpp 16 KB

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