Split MCP handlers
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "MCPHandler.h"
|
||||
#include "MCPAssetFinder.h"
|
||||
#include "MCPServer.h"
|
||||
#include "MCPUtils.h"
|
||||
#include "Engine/Blueprint.h"
|
||||
#include "EdGraph/EdGraph.h"
|
||||
#include "Kismet2/BlueprintEditorUtils.h"
|
||||
#include "UObject/UObjectIterator.h"
|
||||
#include "UMCPHandler_RemoveBlueprintInterface.generated.h"
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
UCLASS()
|
||||
class UMCPHandler_RemoveBlueprintInterface : public UObject, public IMCPHandler
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
UPROPERTY(meta=(Description="Blueprint name or package path"))
|
||||
FString Blueprint;
|
||||
|
||||
UPROPERTY(meta=(Description="Interface name to remove"))
|
||||
FString InterfaceName;
|
||||
|
||||
UPROPERTY(meta=(Optional, Description="If true, keep the function graphs as regular functions"))
|
||||
bool PreserveFunctions = false;
|
||||
|
||||
virtual FString GetDescription() const override
|
||||
{
|
||||
return TEXT("Remove a Blueprint Interface implementation from a Blueprint. "
|
||||
"Optionally preserve the function graphs as regular functions.");
|
||||
}
|
||||
|
||||
virtual void Handle(const FJsonObject* Json, FJsonObject* Result) override
|
||||
{
|
||||
|
||||
MCPAssets<UBlueprint> Assets;
|
||||
if (!Assets.Exact(Blueprint).Errors(Result).ENone().ETwo().Load()) return;
|
||||
UBlueprint* BP = Assets.Object();
|
||||
|
||||
// Find the interface in ImplementedInterfaces by name (case-insensitive)
|
||||
UClass* FoundInterface = nullptr;
|
||||
for (const FBPInterfaceDescription& IfaceDesc : BP->ImplementedInterfaces)
|
||||
{
|
||||
if (!IfaceDesc.Interface)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
FString ClassName = IfaceDesc.Interface->GetName();
|
||||
if (ClassName.Equals(InterfaceName, ESearchCase::IgnoreCase))
|
||||
{
|
||||
FoundInterface = IfaceDesc.Interface;
|
||||
break;
|
||||
}
|
||||
// Strip "_C" suffix for comparison
|
||||
FString TrimmedName = ClassName;
|
||||
if (TrimmedName.EndsWith(TEXT("_C")))
|
||||
{
|
||||
TrimmedName = TrimmedName.LeftChop(2);
|
||||
}
|
||||
if (TrimmedName.Equals(InterfaceName, ESearchCase::IgnoreCase))
|
||||
{
|
||||
FoundInterface = IfaceDesc.Interface;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!FoundInterface)
|
||||
{
|
||||
// Build helpful error with list of implemented interfaces
|
||||
TArray<TSharedPtr<FJsonValue>> IfaceList;
|
||||
for (const FBPInterfaceDescription& IfaceDesc : BP->ImplementedInterfaces)
|
||||
{
|
||||
if (IfaceDesc.Interface)
|
||||
{
|
||||
IfaceList.Add(MakeShared<FJsonValueString>(IfaceDesc.Interface->GetName()));
|
||||
}
|
||||
}
|
||||
|
||||
MCPUtils::MakeErrorJson(Result, FString::Printf(
|
||||
TEXT("Interface '%s' is not implemented by Blueprint '%s'"),
|
||||
*InterfaceName, *Blueprint));
|
||||
Result->SetArrayField(TEXT("implementedInterfaces"), IfaceList);
|
||||
return;
|
||||
}
|
||||
|
||||
FTopLevelAssetPath InterfacePath = FoundInterface->GetClassPathName();
|
||||
|
||||
UE_LOG(LogTemp, Display, TEXT("BlueprintMCP: Removing interface '%s' from Blueprint '%s' (preserveFunctions: %s)"),
|
||||
*FoundInterface->GetName(), *Blueprint, PreserveFunctions ? TEXT("true") : TEXT("false"));
|
||||
|
||||
FBlueprintEditorUtils::RemoveInterface(BP, InterfacePath, PreserveFunctions);
|
||||
|
||||
FBlueprintEditorUtils::MarkBlueprintAsStructurallyModified(BP);
|
||||
|
||||
|
||||
UE_LOG(LogTemp, Display, TEXT("BlueprintMCP: Removed interface '%s' from '%s'"),
|
||||
*FoundInterface->GetName(), *Blueprint);
|
||||
|
||||
Result->SetStringField(TEXT("interfaceName"), FoundInterface->GetName());
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user