Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented UCLASS/USTRUCT debug print for basic properties #52

Merged
merged 7 commits into from
Nov 6, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
#46 Added USTRUCT to printer
xthebat committed Nov 5, 2023
commit 567e7e589c3b2bffa8676beafbcc224163983579
1 change: 1 addition & 0 deletions Config/DefaultGame.ini
Original file line number Diff line number Diff line change
@@ -16,4 +16,5 @@ bIsDrawDeprojectedCursorLine=False
bIsShowMouseCursor=False
NetGraph=1
UnUsedEnum=Everything
UnUsedStruct=(IntField0=3,FloatField1=0.000000)

4 changes: 3 additions & 1 deletion Source/Cloud9/Game/Cloud9DeveloperSettings.cpp
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@ const UCloud9DeveloperSettings* UCloud9DeveloperSettings::GetCloud9DeveloperSett

if (!bIsInitialized)
{
let X = FUnUsedStruct();
let q = X.StaticStruct();
Cast<UCloud9DeveloperSettings>(Settings)->Log();
bIsInitialized = true;
}
@@ -25,5 +27,5 @@ void UCloud9DeveloperSettings::Save()

void UCloud9DeveloperSettings::Log() const
{
UE_LOG(LogCloud9, Display, TEXT("%s"), *UCloud9ReflectionLibrary::UObjectToString(this));
UE_LOG(LogCloud9, Display, TEXT("%s"), *UCloud9ReflectionLibrary::UObjectToString(this, GetClass()));
}
16 changes: 16 additions & 0 deletions Source/Cloud9/Game/Cloud9DeveloperSettings.h
Original file line number Diff line number Diff line change
@@ -3,6 +3,19 @@
#include "CoreMinimal.h"
#include "Cloud9DeveloperSettings.generated.h"

USTRUCT(BlueprintType)
struct FUnUsedStruct
{
GENERATED_BODY()

public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int IntField0 = 0;

UPROPERTY(EditAnywhere, BlueprintReadWrite)
float FloatField1 = 0.0f;
};

UENUM()
enum class EUnUsedEnum : int32
{
@@ -43,4 +56,7 @@ class CLOUD9_API UCloud9DeveloperSettings : public UDeveloperSettings

UPROPERTY(Config, EditAnywhere, BlueprintReadWrite, Category = "Debug")
EUnUsedEnum UnUsedEnum;

UPROPERTY(Config, EditAnywhere, BlueprintReadWrite, Category = "Debug")
FUnUsedStruct UnUsedStruct;
};
108 changes: 63 additions & 45 deletions Source/Cloud9/Tools/Cloud9ReflectionLibrary.cpp
Original file line number Diff line number Diff line change
@@ -27,29 +27,29 @@

auto LexToString(const FText& Text) { return *Text.ToString(); }

template <typename TValue>
FString UCloud9ReflectionLibrary::FormatProperty(const FProperty* Property, TValue Value)
{
let Name = Property->GetFName();
let TypeName = Property->GetCPPType();
return FString::Printf(TEXT("%s: %s = %s"), *Name.ToString(), *TypeName, *LexToString(Value));
}

template <typename TType, typename TResult>
TResult UCloud9ReflectionLibrary::UPropertyGetValue(const UObject* Object, const TType* Property)
TResult UCloud9ReflectionLibrary::UPropertyGetValue(const void* Object, const TType* Property)
{
let Ptr = Property->template ContainerPtrToValuePtr<void>(Object);
return TType::GetPropertyValue(Ptr);
}

template <>
bool UCloud9ReflectionLibrary::UPropertyGetValue(const UObject* Object, const FBoolProperty* Property)
bool UCloud9ReflectionLibrary::UPropertyGetValue(const void* Object, const FBoolProperty* Property)
{
return Property->GetPropertyValue_InContainer(Object);
}

template <typename TValue>
FString UCloud9ReflectionLibrary::FormatProperty(const FProperty* Property, TValue Value)
{
let Name = Property->GetFName();
let TypeName = Property->GetCPPType();
return FString::Printf(TEXT("%s: %s = %s"), *Name.ToString(), *TypeName, *LexToString(Value));
}

template <typename TType>
TOptional<FString> UCloud9ReflectionLibrary::UPropertyConvert(const UObject* Object, const FProperty* Property)
TOptional<FString> UCloud9ReflectionLibrary::UPropertyConvert(const void* Object, const FProperty* Property)
{
if (let TypedProperty = CastField<TType>(Property))
{
@@ -61,7 +61,7 @@ TOptional<FString> UCloud9ReflectionLibrary::UPropertyConvert(const UObject* Obj

template <>
TOptional<FString> UCloud9ReflectionLibrary::UPropertyConvert<FEnumProperty>(
const UObject* Object,
const void* Object,
const FProperty* Property)
{
if (let TypedProperty = CastField<FEnumProperty>(Property))
@@ -76,10 +76,23 @@ TOptional<FString> UCloud9ReflectionLibrary::UPropertyConvert<FEnumProperty>(
return {};
}

template <>
TOptional<FString> UCloud9ReflectionLibrary::UPropertyConvert<FStructProperty>(
const void* Object,
const FProperty* Property)
{
if (let TypedProperty = CastField<FStructProperty>(Property))
{
let Ptr = TypedProperty->ContainerPtrToValuePtr<UObject>(Object);
return UObjectToString(Ptr, TypedProperty->Struct);
}
return {};
}

template <typename TType>
bool UCloud9ReflectionLibrary::UPropertyAppendTo(
FTextBuilder& Builder,
const UObject* Object,
const void* Object,
const FProperty* Property)
{
if (let String = UPropertyConvert<TType>(Object, Property))
@@ -90,44 +103,49 @@ bool UCloud9ReflectionLibrary::UPropertyAppendTo(
return false;
}

FString UCloud9ReflectionLibrary::UObjectToString(const UObject* Object)
bool UCloud9ReflectionLibrary::UPropertyAppendTo(
FTextBuilder& Builder,
const void* Object,
const FProperty* Property)
{
return UPropertyAppendTo<FInt16Property>(Builder, Object, Property)
|| UPropertyAppendTo<FIntProperty>(Builder, Object, Property)
|| UPropertyAppendTo<FInt64Property>(Builder, Object, Property)
|| UPropertyAppendTo<FUInt16Property>(Builder, Object, Property)
|| UPropertyAppendTo<FUInt32Property>(Builder, Object, Property)
|| UPropertyAppendTo<FUInt64Property>(Builder, Object, Property)
|| UPropertyAppendTo<FFloatProperty>(Builder, Object, Property)
|| UPropertyAppendTo<FDoubleProperty>(Builder, Object, Property)
|| UPropertyAppendTo<FBoolProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FObjectProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FWeakObjectProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FLazyObjectProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FSoftObjectProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FClassProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FSoftClassProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FInterfaceProperty>(Builder, Object, Property)
|| UPropertyAppendTo<FNameProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FArrayProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FMapProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FSetProperty>(Builder, Object, Property)
|| UPropertyAppendTo<FStructProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FDelegateProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FMulticastDelegateProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FMulticastInlineDelegateProperty>(Builder, Object, Property)
|| UPropertyAppendTo<FEnumProperty>(Builder, Object, Property)
|| UPropertyAppendTo<FTextProperty>(Builder, Object, Property);
}

FString UCloud9ReflectionLibrary::UObjectToString(const UObject* Object, const UStruct* Type)
{
FTextBuilder Builder;
let Class = Object->GetClass();

Builder.AppendLine(FString::Printf(TEXT("class %s {"), *Class->GetName()));
Builder.AppendLine(FString::Printf(TEXT("class %s {"), *Type->GetName()));
Builder.Indent();

for (TFieldIterator<FProperty> PropertyIterator(Class); PropertyIterator; ++PropertyIterator)
for (TFieldIterator<FProperty> It(Type); It; ++It)
{
let Property = *PropertyIterator;

UPropertyAppendTo<FInt16Property>(Builder, Object, Property)
|| UPropertyAppendTo<FIntProperty>(Builder, Object, Property)
|| UPropertyAppendTo<FInt64Property>(Builder, Object, Property)
|| UPropertyAppendTo<FUInt16Property>(Builder, Object, Property)
|| UPropertyAppendTo<FUInt32Property>(Builder, Object, Property)
|| UPropertyAppendTo<FUInt64Property>(Builder, Object, Property)
|| UPropertyAppendTo<FFloatProperty>(Builder, Object, Property)
|| UPropertyAppendTo<FDoubleProperty>(Builder, Object, Property)
|| UPropertyAppendTo<FBoolProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FObjectProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FWeakObjectProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FLazyObjectProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FSoftObjectProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FClassProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FSoftClassProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FInterfaceProperty>(Builder, Object, Property)
|| UPropertyAppendTo<FNameProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FArrayProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FMapProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FSetProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FStructProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FDelegateProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FMulticastDelegateProperty>(Builder, Object, Property)
// || UPropertyAppendTo<FMulticastInlineDelegateProperty>(Builder, Object, Property)
|| UPropertyAppendTo<FEnumProperty>(Builder, Object, Property)
|| UPropertyAppendTo<FTextProperty>(Builder, Object, Property);
UPropertyAppendTo(Builder, Object, *It);
}

Builder.Unindent();
14 changes: 8 additions & 6 deletions Source/Cloud9/Tools/Cloud9ReflectionLibrary.h
Original file line number Diff line number Diff line change
@@ -36,18 +36,20 @@ class CLOUD9_API UCloud9ReflectionLibrary : public UBlueprintFunctionLibrary
GENERATED_BODY()

public:
template <typename TType, typename TResult = typename TType::TCppType>
static TResult UPropertyGetValue(const UObject* Object, const TType* Property);

template <typename TValue>
static FString FormatProperty(const FProperty* Property, TValue Value);

template <typename TType, typename TResult = typename TType::TCppType>
static TResult UPropertyGetValue(const void* Object, const TType* Property);

template <typename TType>
static TOptional<FString> UPropertyConvert(const UObject* Object, const FProperty* Property);
static TOptional<FString> UPropertyConvert(const void* Object, const FProperty* Property);

template <typename TType>
static bool UPropertyAppendTo(FTextBuilder& Builder, const UObject* Object, const FProperty* Property);
static bool UPropertyAppendTo(FTextBuilder& Builder, const void* Object, const FProperty* Property);

static bool UPropertyAppendTo(FTextBuilder& Builder, const void* Object, const FProperty* Property);

UFUNCTION(BlueprintCallable)
static FString UObjectToString(const UObject* Object);
static FString UObjectToString(const UObject* Object, const UStruct* Type);
};