99 lines
2.8 KiB
C++
99 lines
2.8 KiB
C++
#pragma once
|
|
|
|
#include "CoreMinimal.h"
|
|
#include "MCPServer.h"
|
|
#include "MCPHandler.h"
|
|
#include "MCPTypes.h"
|
|
#include "MCPUtils.h"
|
|
#include "StructUtils/UserDefinedStruct.h"
|
|
#include "Kismet2/BlueprintEditorUtils.h"
|
|
#include "UserDefinedStructure/UserDefinedStructEditorData.h"
|
|
#include "Factories/StructureFactory.h"
|
|
#include "MCPPackageMaker.h"
|
|
#include "Struct_Create.generated.h"
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------
|
|
|
|
USTRUCT()
|
|
struct FStructPropertyEntry
|
|
{
|
|
GENERATED_BODY()
|
|
|
|
UPROPERTY()
|
|
FString Name;
|
|
|
|
UPROPERTY()
|
|
FString Type;
|
|
};
|
|
|
|
UCLASS(meta=(HalfBaked))
|
|
class UMCP_Struct_Create : public UObject, public IMCPHandler
|
|
{
|
|
GENERATED_BODY()
|
|
|
|
public:
|
|
UPROPERTY(meta=(Description="Full asset path for the new struct (e.g. '/Game/DataTypes/S_MyStruct')"))
|
|
FString AssetPath;
|
|
|
|
UPROPERTY(meta=(Optional, Description="Array of initial properties, each with 'name' and 'type' fields"))
|
|
FMCPJsonArray Properties;
|
|
|
|
virtual FString GetDescription() const override
|
|
{
|
|
return TEXT("Create a new UserDefinedStruct asset with optional initial properties.");
|
|
}
|
|
|
|
virtual void Handle() override
|
|
{
|
|
MCPPackageMaker Maker(AssetPath);
|
|
if (!Maker.Ok()) return;
|
|
|
|
// Create the struct using the AssetTools factory.
|
|
UUserDefinedStruct* NewStruct = Maker.CreateAsset<UUserDefinedStruct, UStructureFactory>();
|
|
if (!NewStruct) return;
|
|
|
|
// Add properties if specified.
|
|
int32 PropsAdded = 0;
|
|
for (const TSharedPtr<FJsonValue>& PropVal : Properties.Array)
|
|
{
|
|
FStructPropertyEntry Entry;
|
|
if (!MCPUtils::PopulateFromJson(FStructPropertyEntry::StaticStruct(), &Entry, PropVal)) return;
|
|
if (Entry.Name.IsEmpty() || Entry.Type.IsEmpty()) continue;
|
|
|
|
FEdGraphPinType PinType;
|
|
if (!UMCPTypes::TextToType(Entry.Type, PinType))
|
|
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);
|
|
|
|
UMCPServer::Printf(TEXT("Created %s\n"), *NewStruct->GetPathName());
|
|
if (PropsAdded > 0)
|
|
UMCPServer::Printf(TEXT("Properties added: %d\n"), PropsAdded);
|
|
if (!bSaved)
|
|
UMCPServer::Print(TEXT("WARNING: Package save failed\n"));
|
|
}
|
|
};
|