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).
This commit is contained in:
Sebastian 2018-10-17 23:16:28 +01:00
parent 36cdd0c337
commit b4e959ccd3
3 changed files with 65 additions and 1 deletions

View File

@ -16,7 +16,7 @@
"Modules": [
{
"Name": "ImGui",
"Type": "RuntimeNoCommandlet",
"Type": "Developer",
"LoadingPhase": "PreDefault"
}
]

View File

@ -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<string> PrivateDefinitions = Definitions;
#endif
PrivateDefinitions.Add(string.Format("RUNTIME_LOADER_ENABLED = {0}", bEnableRuntimeLoader ? 1 : 0));
}
}

View File

@ -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<IModuleInterface>& 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
//----------------------------------------------------------------------------------------------------