Last active
January 14, 2024 13:40
-
-
Save seriema/be30822411941e7239296137ffb9302d to your computer and use it in GitHub Desktop.
Module setup with UDeveloperSetting validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MyConfigModule.h" | |
#include <Core/Public/Misc/MessageDialog.h> | |
#include <CoreUObject/Public/Misc/DataValidation.h> | |
#include <CoreUObject/Public/Misc/UObjectToken.h> | |
#include <UnrealEd/Public/Editor.h> | |
#include "MyConfig/ProjectSetting/MyConfig.h" | |
namespace | |
{ | |
const auto RunConfigValidation = []() -> FMessageLog | |
{ | |
FMessageLog ConfigLoadLog("LoadErrors"); | |
// Need a mutable version of the config to call IsDataValid | |
const auto Config = GetMutableDefault<UMyConfig>(); | |
if (!IsValid(Config)) | |
{ | |
return ConfigLoadLog; | |
} | |
FDataValidationContext ValidationContext; | |
const auto ValidationResult = Config->IsDataValid(ValidationContext); | |
if (ValidationResult == EDataValidationResult::Valid) | |
{ | |
return ConfigLoadLog; | |
} | |
ConfigLoadLog | |
.Error() // | |
->AddToken(FUObjectToken::Create(Config)) | |
->AddToken(FTextToken::Create(INVTEXT("has validation issues! See messages below:"))); | |
for (auto& Issue : ValidationContext.GetIssues()) | |
{ | |
ConfigLoadLog | |
.Message(Issue.Severity) // | |
->AddToken(FUObjectToken::Create(Config)) | |
->AddToken(FTextToken::Create(Issue.Message)); | |
} | |
return ConfigLoadLog; | |
}; | |
} // namespace | |
void FMyConfig::StartupModule() | |
{ | |
FCoreDelegates::OnPostEngineInit.AddLambda( | |
[] | |
{ | |
auto ResultLog = RunConfigValidation(); | |
if (ResultLog.NumMessages(EMessageSeverity::Warning) > 0) | |
{ | |
FMessageDialog::Open(EAppMsgType::Ok, INVTEXT("UI Config is invalid! UI will be unpredictable. Check the file Config/DefaultUIConfig.ini")); | |
} | |
}); | |
FCoreDelegates::OnEnginePreExit.AddLambda( | |
[] | |
{ | |
auto ResultLog = RunConfigValidation(); | |
if (ResultLog.NumMessages(EMessageSeverity::Warning) > 0) | |
{ | |
FMessageDialog::Open(EAppMsgType::Ok, INVTEXT("UI Config is invalid! Check the file Config/DefaultUIConfig.ini")); | |
} | |
}); | |
#if WITH_EDITOR | |
FEditorDelegates::BeginPIE.AddLambda( | |
[](bool bIsSimulating) | |
{ | |
auto ResultLog = RunConfigValidation(); | |
if (ResultLog.NumMessages(EMessageSeverity::Warning) > 0) | |
{ | |
ResultLog.Notify(INVTEXT("UI Config has validations issues that needs to be fixed! UI will be unpredictable."), EMessageSeverity::Warning); | |
FMessageDialog::Open(EAppMsgType::Ok, INVTEXT("UI Config is invalid! See the LoadErrors message log.")); | |
} | |
}); | |
#endif | |
} | |
void FMyConfig::ShutdownModule() | |
{ | |
#if WITH_EDITOR | |
FEditorDelegates::BeginPIE.RemoveAll(this); | |
#endif | |
FCoreDelegates::OnEnginePreExit.RemoveAll(this); | |
FCoreDelegates::OnPostEngineInit.RemoveAll(this); | |
} | |
IMPLEMENT_MODULE(FMyConfig, MyConfig) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <CoreMinimal.h> | |
#include <Modules/ModuleManager.h> | |
class FMyConfig : public IModuleInterface | |
{ | |
public: | |
// Begin IModuleInterface | |
void StartupModule() override; | |
void ShutdownModule() override; | |
// End IModuleInterface | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment