Files
integration/Plugins/BlueprintMCP/Source/BlueprintMCP/Handlers/Struct_Create.h

117 lines
3.5 KiB
C
Raw Normal View History

2026-03-08 22:17:14 -04:00
#pragma once
#include "CoreMinimal.h"
#include "MCPHandler.h"
#include "MCPAssetFinder.h"
#include "MCPUtils.h"
#include "StructUtils/UserDefinedStruct.h"
#include "Kismet2/BlueprintEditorUtils.h"
#include "UserDefinedStructure/UserDefinedStructEditorData.h"
#include "AssetToolsModule.h"
#include "IAssetTools.h"
#include "Factories/StructureFactory.h"
2026-03-12 00:44:17 -04:00
#include "Struct_Create.generated.h"
2026-03-08 22:17:14 -04:00
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
USTRUCT()
struct FStructPropertyEntry
{
GENERATED_BODY()
UPROPERTY()
FString Name;
UPROPERTY()
FString Type;
};
2026-03-12 00:44:17 -04:00
UCLASS()
class UMCP_Struct_Create : public UObject, public IMCPHandler
2026-03-08 22:17:14 -04:00
{
GENERATED_BODY()
public:
2026-03-10 07:17:42 -04:00
UPROPERTY(meta=(Description="Name for the new struct asset"))
FString Name;
UPROPERTY(meta=(Description="Package path where the asset will be created (must start with /Game)"))
FString PackagePath;
2026-03-08 22:17:14 -04:00
UPROPERTY(meta=(Optional, Description="Array of initial properties, each with 'name' and 'type' fields"))
FMCPJsonArray Properties;
virtual FString GetDescription() const override
{
2026-03-10 07:17:42 -04:00
return TEXT("Create a new UserDefinedStruct asset with optional initial properties.");
2026-03-08 22:17:14 -04:00
}
2026-03-12 17:48:11 -04:00
virtual void Handle(FStringBuilderBase& Result) override
2026-03-08 22:17:14 -04:00
{
2026-03-10 07:17:42 -04:00
if (!PackagePath.StartsWith(TEXT("/Game")))
{
Result.Append(TEXT("ERROR: PackagePath must start with '/Game'\n"));
return;
}
// Check if an asset with this name already exists.
MCPAssets<UUserDefinedStruct> ExistCheck;
if (!ExistCheck.Exact(Name).Errors(Result).EAny().Info()) return;
// Create the struct using the AssetTools factory.
IAssetTools& AssetTools = FModuleManager::LoadModuleChecked<FAssetToolsModule>("AssetTools").Get();
UStructureFactory* Factory = NewObject<UStructureFactory>();
UObject* NewAsset = AssetTools.CreateAsset(Name, PackagePath, UUserDefinedStruct::StaticClass(), Factory);
UUserDefinedStruct* NewStruct = Cast<UUserDefinedStruct>(NewAsset);
if (!NewStruct)
{
Result.Appendf(TEXT("ERROR: Failed to create struct '%s' in '%s'\n"), *Name, *PackagePath);
return;
}
// Add properties if specified.
int32 PropsAdded = 0;
for (const TSharedPtr<FJsonValue>& PropVal : Properties.Array)
{
FStructPropertyEntry Entry;
if (!MCPUtils::PopulateFromJson(FStructPropertyEntry::StaticStruct(), &Entry, PropVal, Result)) return;
if (Entry.Name.IsEmpty() || Entry.Type.IsEmpty()) continue;
FEdGraphPinType PinType;
if (!MCPUtils::ResolveTypeFromString(Entry.Type, PinType, Result))
continue;
// Snapshot existing GUIDs so we can find the newly added one.
TSet<FGuid> ExistingGuids;
for (const FStructVariableDescription& Var : FStructureEditorUtils::GetVarDesc(NewStruct))
ExistingGuids.Add(Var.VarGuid);
if (!FStructureEditorUtils::AddVariable(NewStruct, PinType))
continue;
// Find the new variable by diffing GUID sets.
for (const FStructVariableDescription& Var : FStructureEditorUtils::GetVarDesc(NewStruct))
{
if (!ExistingGuids.Contains(Var.VarGuid))
{
FStructureEditorUtils::RenameVariable(NewStruct, Var.VarGuid, Entry.Name);
break;
}
}
PropsAdded++;
}
bool bSaved = MCPUtils::SaveGenericPackage(NewStruct);
Result.Appendf(TEXT("Created %s\n"), *NewStruct->GetPathName());
if (PropsAdded > 0)
Result.Appendf(TEXT("Properties added: %d\n"), PropsAdded);
if (!bSaved)
Result.Append(TEXT("WARNING: Package save failed\n"));
2026-03-08 22:17:14 -04:00
}
};