Start thinking about types
This commit is contained in:
140
Plugins/BlueprintMCP/Source/BlueprintMCP/Private/MCPTypes.cpp
Normal file
140
Plugins/BlueprintMCP/Source/BlueprintMCP/Private/MCPTypes.cpp
Normal file
@@ -0,0 +1,140 @@
|
||||
#include "MCPTypes.h"
|
||||
#include "EdGraphSchema_K2.h"
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helper: format a UField name with module prefix if not in /Script/Engine.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static FString FormatFieldName(UField* Field, const FString& TypePrefix)
|
||||
{
|
||||
// Package name is /Script/ModuleName
|
||||
FString ModuleName = Field->GetOutermost()->GetName().Mid(8);
|
||||
|
||||
FString Name = TypePrefix + Field->GetName();
|
||||
if (ModuleName != TEXT("Engine"))
|
||||
return ModuleName + TEXT("::") + Name;
|
||||
return Name;
|
||||
}
|
||||
|
||||
static FString FormatClassName(UClass* Class)
|
||||
{
|
||||
FString Prefix = Class->IsChildOf(AActor::StaticClass()) ? TEXT("A") : TEXT("U");
|
||||
return FormatFieldName(Class, Prefix);
|
||||
}
|
||||
|
||||
static FString FormatStructName(UScriptStruct* Struct)
|
||||
{
|
||||
return FormatFieldName(Struct, TEXT("F"));
|
||||
}
|
||||
|
||||
static FString FormatEnumName(UEnum* Enum)
|
||||
{
|
||||
return FormatFieldName(Enum, TEXT("E"));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// TypeToText
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static FString TerminalToText(const FEdGraphPinType& PinType)
|
||||
{
|
||||
const FName& Cat = PinType.PinCategory;
|
||||
UObject* SubObj = PinType.PinSubCategoryObject.Get();
|
||||
|
||||
if (Cat == UEdGraphSchema_K2::PC_Boolean) return TEXT("bool");
|
||||
if (Cat == UEdGraphSchema_K2::PC_Int) return TEXT("int32");
|
||||
if (Cat == UEdGraphSchema_K2::PC_Int64) return TEXT("int64");
|
||||
if (Cat == UEdGraphSchema_K2::PC_Name) return TEXT("FName");
|
||||
if (Cat == UEdGraphSchema_K2::PC_String) return TEXT("FString");
|
||||
if (Cat == UEdGraphSchema_K2::PC_Text) return TEXT("FText");
|
||||
|
||||
if (Cat == UEdGraphSchema_K2::PC_Real)
|
||||
{
|
||||
if (PinType.PinSubCategory == UEdGraphSchema_K2::PC_Float)
|
||||
return TEXT("float");
|
||||
return TEXT("double");
|
||||
}
|
||||
|
||||
if (Cat == UEdGraphSchema_K2::PC_Byte)
|
||||
{
|
||||
if (UEnum* Enum = Cast<UEnum>(SubObj))
|
||||
return FormatEnumName(Enum);
|
||||
return TEXT("uint8");
|
||||
}
|
||||
|
||||
if (Cat == UEdGraphSchema_K2::PC_Enum)
|
||||
{
|
||||
if (UEnum* Enum = Cast<UEnum>(SubObj))
|
||||
return FormatEnumName(Enum);
|
||||
return TEXT("uint8");
|
||||
}
|
||||
|
||||
if (Cat == UEdGraphSchema_K2::PC_Struct)
|
||||
{
|
||||
if (UScriptStruct* Struct = Cast<UScriptStruct>(SubObj))
|
||||
return FormatStructName(Struct);
|
||||
return FString();
|
||||
}
|
||||
|
||||
if (Cat == UEdGraphSchema_K2::PC_Object)
|
||||
{
|
||||
if (UClass* Class = Cast<UClass>(SubObj))
|
||||
return FormatClassName(Class) + TEXT("*");
|
||||
return FString();
|
||||
}
|
||||
|
||||
if (Cat == UEdGraphSchema_K2::PC_Class)
|
||||
{
|
||||
if (UClass* Class = Cast<UClass>(SubObj))
|
||||
return FString::Printf(TEXT("TSubclassOf<%s>"), *FormatClassName(Class));
|
||||
return FString();
|
||||
}
|
||||
|
||||
if (Cat == UEdGraphSchema_K2::PC_SoftObject)
|
||||
{
|
||||
if (UClass* Class = Cast<UClass>(SubObj))
|
||||
return FString::Printf(TEXT("TSoftObjectPtr<%s>"), *FormatClassName(Class));
|
||||
return FString();
|
||||
}
|
||||
|
||||
if (Cat == UEdGraphSchema_K2::PC_SoftClass)
|
||||
{
|
||||
if (UClass* Class = Cast<UClass>(SubObj))
|
||||
return FString::Printf(TEXT("TSoftClassPtr<%s>"), *FormatClassName(Class));
|
||||
return FString();
|
||||
}
|
||||
|
||||
if (Cat == UEdGraphSchema_K2::PC_Interface)
|
||||
{
|
||||
if (UClass* Class = Cast<UClass>(SubObj))
|
||||
return FString::Printf(TEXT("TScriptInterface<%s>"), *FormatClassName(Class));
|
||||
return FString();
|
||||
}
|
||||
|
||||
return FString();
|
||||
}
|
||||
|
||||
FString MCPTypes::TypeToText(const FEdGraphPinType& PinType)
|
||||
{
|
||||
FString Inner = TerminalToText(PinType);
|
||||
if (Inner.IsEmpty())
|
||||
return FString();
|
||||
|
||||
if (PinType.IsArray())
|
||||
return FString::Printf(TEXT("TArray<%s>"), *Inner);
|
||||
if (PinType.IsSet())
|
||||
return FString::Printf(TEXT("TSet<%s>"), *Inner);
|
||||
if (PinType.IsMap())
|
||||
{
|
||||
FEdGraphPinType ValueType;
|
||||
ValueType.PinCategory = PinType.PinValueType.TerminalCategory;
|
||||
ValueType.PinSubCategory = PinType.PinValueType.TerminalSubCategory;
|
||||
ValueType.PinSubCategoryObject = PinType.PinValueType.TerminalSubCategoryObject;
|
||||
FString ValueInner = TerminalToText(ValueType);
|
||||
if (ValueInner.IsEmpty())
|
||||
return FString();
|
||||
return FString::Printf(TEXT("TMap<%s, %s>"), *Inner, *ValueInner);
|
||||
}
|
||||
|
||||
return Inner;
|
||||
}
|
||||
Reference in New Issue
Block a user