From b4e959ccd3f46d7d924f0800525cd28517858702 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 17 Oct 2018 23:16:28 +0100 Subject: [PATCH] Changed module type to allow stripping it from selected builds: - Changed module type to 'Developer'. - Added runtime loader that handles automatic module loading in runtime builds (feature can be selectively enabled). --- ImGui.uplugin | 2 +- Source/ImGui/ImGui.Build.cs | 12 +++++++ Source/ImGui/Private/ImGuiModule.cpp | 52 ++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/ImGui.uplugin b/ImGui.uplugin index 5c515ae..586d533 100644 --- a/ImGui.uplugin +++ b/ImGui.uplugin @@ -16,7 +16,7 @@ "Modules": [ { "Name": "ImGui", - "Type": "RuntimeNoCommandlet", + "Type": "Developer", "LoadingPhase": "PreDefault" } ] diff --git a/Source/ImGui/ImGui.Build.cs b/Source/ImGui/ImGui.Build.cs index bc92c60..2ac7ce8 100644 --- a/Source/ImGui/ImGui.Build.cs +++ b/Source/ImGui/ImGui.Build.cs @@ -1,5 +1,6 @@ // Distributed under the MIT License (MIT) (see accompanying LICENSE file) +using System.Collections.Generic; using System.IO; using UnrealBuildTool; @@ -18,6 +19,11 @@ public class ImGui : ModuleRules bool bBuildEditor = (Target.Type == TargetRules.TargetType.Editor); #endif + // Developer modules are automatically loaded only in editor builds but can be stripped out from other builds. + // Enable runtime loader, if you want this module to be automatically loaded in runtime builds (monolithic). + bool bEnableRuntimeLoader = true; + + PublicIncludePaths.AddRange( new string[] { Path.Combine(ModuleDirectory, "../ThirdParty/ImGuiLibrary/Include"), @@ -78,5 +84,11 @@ public class ImGui : ModuleRules // ... add any modules that your module loads dynamically here ... } ); + + +#if !UE_4_19_OR_LATER + List PrivateDefinitions = Definitions; +#endif + PrivateDefinitions.Add(string.Format("RUNTIME_LOADER_ENABLED = {0}", bEnableRuntimeLoader ? 1 : 0)); } } diff --git a/Source/ImGui/Private/ImGuiModule.cpp b/Source/ImGui/Private/ImGuiModule.cpp index 90efc75..b77b6f2 100644 --- a/Source/ImGui/Private/ImGuiModule.cpp +++ b/Source/ImGui/Private/ImGuiModule.cpp @@ -221,6 +221,58 @@ void FImGuiModule::ToggleShowDemo() SetShowDemo(!IsShowingDemo()); } + +//---------------------------------------------------------------------------------------------------- +// Runtime loader +//---------------------------------------------------------------------------------------------------- + +#if !WITH_EDITOR && RUNTIME_LOADER_ENABLED + +class FImGuiModuleLoader +{ + FImGuiModuleLoader() + { + if (!Load()) + { + FModuleManager::Get().OnModulesChanged().AddRaw(this, &FImGuiModuleLoader::LoadAndRelease); + } + } + + // For different engine versions. + static FORCEINLINE bool IsValid(const TSharedPtr& Ptr) { return Ptr.IsValid(); } + static FORCEINLINE bool IsValid(const IModuleInterface* const Ptr) { return Ptr != nullptr; } + + bool Load() + { + return IsValid(FModuleManager::Get().LoadModule(ModuleName)); + } + + void LoadAndRelease(FName Name, EModuleChangeReason Reason) + { + // Avoid handling own load event. + if (Name != ModuleName) + { + // Try loading until success and then release. + if (Load()) + { + FModuleManager::Get().OnModulesChanged().RemoveAll(this); + } + } + } + + static FName ModuleName; + + static FImGuiModuleLoader Instance; +}; + +FName FImGuiModuleLoader::ModuleName = "ImGui"; + +// In monolithic builds this will start loading process. +FImGuiModuleLoader FImGuiModuleLoader::Instance; + +#endif // !WITH_EDITOR && RUNTIME_LOADER_ENABLED + + //---------------------------------------------------------------------------------------------------- // Partial implementations of other classes that needs access to ImGuiModuleManager //----------------------------------------------------------------------------------------------------