Split MCP handlers
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "MCPHandler.h"
|
||||
#include "MCPAssetFinder.h"
|
||||
#include "MCPUtils.h"
|
||||
#include "StructUtils/UserDefinedStruct.h"
|
||||
#include "Engine/UserDefinedEnum.h"
|
||||
#include "Kismet2/BlueprintEditorUtils.h"
|
||||
#include "UserDefinedStructure/UserDefinedStructEditorData.h"
|
||||
#include "Kismet2/EnumEditorUtils.h"
|
||||
#include "AssetRegistry/AssetRegistryModule.h"
|
||||
#include "AssetRegistry/IAssetRegistry.h"
|
||||
#include "AssetToolsModule.h"
|
||||
#include "IAssetTools.h"
|
||||
#include "Factories/StructureFactory.h"
|
||||
#include "Factories/EnumFactory.h"
|
||||
#include "UMCPHandler_AddStructField.generated.h"
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
UCLASS()
|
||||
class UMCPHandler_AddStructField : public UObject, public IMCPHandler
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
UPROPERTY(meta=(Description="Name or package path of the struct asset"))
|
||||
FString AssetPath;
|
||||
|
||||
UPROPERTY(meta=(Description="Name for the new field"))
|
||||
FString Name;
|
||||
|
||||
UPROPERTY(meta=(Description="Type for the new field (e.g. 'int32', 'FString', 'FVector')"))
|
||||
FString Type;
|
||||
|
||||
virtual FString GetDescription() const override
|
||||
{
|
||||
return TEXT("Add a new field to a UserDefinedStruct asset.");
|
||||
}
|
||||
|
||||
virtual void Handle(const FJsonObject* Json, FJsonObject* Result) override
|
||||
{
|
||||
// Find the struct
|
||||
MCPAssets<UUserDefinedStruct> Assets;
|
||||
if (!Assets.Exact(AssetPath).Errors(Result).ENone().ETwo().Load()) return;
|
||||
UUserDefinedStruct* Struct = Assets.Object();
|
||||
|
||||
// Resolve type
|
||||
FEdGraphPinType PinType;
|
||||
if (!MCPUtils::ResolveTypeFromString(Type, PinType, Result))
|
||||
return;
|
||||
|
||||
// Snapshot existing GUIDs so we can find the newly added one
|
||||
TSet<FGuid> ExistingGuids;
|
||||
for (const FStructVariableDescription& Var : FStructureEditorUtils::GetVarDesc(Struct))
|
||||
{
|
||||
ExistingGuids.Add(Var.VarGuid);
|
||||
}
|
||||
|
||||
bool bAdded = FStructureEditorUtils::AddVariable(Struct, PinType);
|
||||
if (!bAdded)
|
||||
{
|
||||
return MCPUtils::MakeErrorJson(Result, TEXT("Failed to add property to struct"));
|
||||
}
|
||||
|
||||
// Find the new variable by diffing GUID sets and rename it
|
||||
for (const FStructVariableDescription& Var : FStructureEditorUtils::GetVarDesc(Struct))
|
||||
{
|
||||
if (!ExistingGuids.Contains(Var.VarGuid))
|
||||
{
|
||||
FStructureEditorUtils::RenameVariable(Struct, Var.VarGuid, Name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Save
|
||||
bool bSaved = MCPUtils::SaveGenericPackage(Struct);
|
||||
|
||||
Result->SetBoolField(TEXT("saved"), bSaved);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user