Skip to content

Instantly share code, notes, and snippets.

@seriema
Last active January 14, 2024 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seriema/be30822411941e7239296137ffb9302d to your computer and use it in GitHub Desktop.
Save seriema/be30822411941e7239296137ffb9302d to your computer and use it in GitHub Desktop.
Module setup with UDeveloperSetting validation
#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)
#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