/* * Copyright (c) Contributors to the Open 3D Engine Project. * For complete copyright and license terms please see the LICENSE at the root of this distribution. * * SPDX-License-Identifier: Apache-2.0 OR MIT * */ #include #include #include #include #include #include #if defined(PHYSX_EDITOR) #include #include #include #endif // defined(PHYSX_EDITOR) #include #include #include namespace PhysX { class Module : public AZ::Module { public: AZ_RTTI(PhysX::Module, "{160C59B1-FA68-4CDC-8562-D1204AB78FC1}", AZ::Module); Module() : AZ::Module() #if defined(PHYSX_EDITOR) , m_physXSystem(new PhysXEditorSettingsRegistryManager(), PxCooking::GetEditTimeCookingParams()) #else , m_physXSystem(new PhysXSettingsRegistryManager(), PxCooking::GetRealTimeCookingParams()) #endif { LoadModules(); AZStd::list descriptorsToAdd = GetDescriptors(); m_descriptors.insert(m_descriptors.end(), descriptorsToAdd.begin(), descriptorsToAdd.end()); #if defined(PHYSX_EDITOR) AZStd::list editorDescriptorsToAdd = GetEditorDescriptors(); m_descriptors.insert(m_descriptors.end(), editorDescriptorsToAdd.begin(), editorDescriptorsToAdd.end()); #endif // defined(PHYSX_EDITOR) } virtual ~Module() { m_physXSystem.Shutdown(); UnloadModules(); } AZ::ComponentTypeList GetRequiredSystemComponents() const override { return AZ::ComponentTypeList{ azrtti_typeid() #if defined(PHYSX_EDITOR) , azrtti_typeid() #endif }; } private: void LoadModules() { #if defined(PHYSX_EDITOR) { AZStd::unique_ptr sceneCoreModule = AZ::DynamicModuleHandle::Create("SceneCore"); [[maybe_unused]] bool ok = sceneCoreModule->Load(true/*isInitializeFunctionRequired*/); AZ_Error("PhysX::Module", ok, "Error loading SceneCore module"); m_modules.push_back(AZStd::move(sceneCoreModule)); } #endif // defined(PHYSX_EDITOR) // Load PhysX SDK dynamic libraries when running on a non-monolithic build. // The PhysX Gem module was linked with the PhysX SDK dynamic libraries, but // some platforms may not detect the dependency when the gem is loaded, so we // may have to load them ourselves. #if AZ_TRAIT_PHYSX_FORCE_LOAD_MODULES && !defined(AZ_MONOLITHIC_BUILD) { const AZStd::vector physXModuleNames = { "PhysX", "PhysXCooking", "PhysXFoundation", "PhysXCommon" }; for (const auto& physXModuleName : physXModuleNames) { AZ::OSString modulePathName = physXModuleName; AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::ResolveModulePath, modulePathName); AZStd::unique_ptr physXModule = AZ::DynamicModuleHandle::Create(modulePathName.c_str()); bool ok = physXModule->Load(false/*isInitializeFunctionRequired*/); AZ_Error("PhysX::Module", ok, "Error loading %s module", physXModuleName.c_str()); m_modules.push_back(AZStd::move(physXModule)); } } #endif } void UnloadModules() { // Unload modules in reserve order that were loaded for (auto it = m_modules.rbegin(); it != m_modules.rend(); ++it) { it->reset(); } m_modules.clear(); } /// Required modules to load/unload when PhysX Gem module is created/destroyed AZStd::vector> m_modules; PhysXSystem m_physXSystem; }; } // namespace PhysX // DO NOT MODIFY THIS LINE UNLESS YOU RENAME THE GEM // The first parameter should be GemName_GemIdLower // The second should be the fully qualified name of the class above AZ_DECLARE_MODULE_CLASS(Gem_PhysX, PhysX::Module)