2026-03-08 22:17:14 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "CoreMinimal.h"
|
|
|
|
|
#include "MCPHandler.h"
|
2026-03-10 07:17:42 -04:00
|
|
|
#include "MCPFetcher.h"
|
2026-03-15 05:52:11 -04:00
|
|
|
#include "MCPTypes.h"
|
2026-03-08 22:17:14 -04:00
|
|
|
#include "MCPUtils.h"
|
|
|
|
|
#include "StructUtils/UserDefinedStruct.h"
|
|
|
|
|
#include "UserDefinedStructure/UserDefinedStructEditorData.h"
|
|
|
|
|
#include "UMCPHandler_AddStructField.generated.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
2026-03-11 22:03:32 -04:00
|
|
|
UCLASS(meta=(Group="Unclassified"))
|
2026-03-08 22:17:14 -04:00
|
|
|
class UMCPHandler_AddStructField : public UObject, public IMCPHandler
|
|
|
|
|
{
|
|
|
|
|
GENERATED_BODY()
|
|
|
|
|
|
|
|
|
|
public:
|
2026-03-10 07:17:42 -04:00
|
|
|
UPROPERTY(meta=(Description="Package path of the struct asset"))
|
|
|
|
|
FString Struct;
|
2026-03-08 22:17:14 -04:00
|
|
|
|
|
|
|
|
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.");
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-10 07:17:42 -04:00
|
|
|
virtual void Handle(const FJsonObject* Json, FStringBuilderBase& Result) override
|
2026-03-08 22:17:14 -04:00
|
|
|
{
|
2026-03-10 07:17:42 -04:00
|
|
|
// Find the struct
|
|
|
|
|
MCPFetcher F(Result);
|
|
|
|
|
UUserDefinedStruct* S = F.Walk(Struct).Cast<UUserDefinedStruct>();
|
|
|
|
|
if (!S) return;
|
2026-03-08 22:17:14 -04:00
|
|
|
|
2026-03-10 07:17:42 -04:00
|
|
|
// Resolve type
|
|
|
|
|
FEdGraphPinType PinType;
|
2026-03-15 05:52:11 -04:00
|
|
|
if (!UMCPTypes::TextToType(Type, PinType, Result))
|
2026-03-10 07:17:42 -04:00
|
|
|
return;
|
2026-03-08 22:17:14 -04:00
|
|
|
|
2026-03-10 07:17:42 -04:00
|
|
|
// Snapshot existing GUIDs so we can find the newly added one
|
|
|
|
|
TSet<FGuid> ExistingGuids;
|
|
|
|
|
for (const FStructVariableDescription& Var : FStructureEditorUtils::GetVarDesc(S))
|
|
|
|
|
ExistingGuids.Add(Var.VarGuid);
|
2026-03-08 22:17:14 -04:00
|
|
|
|
2026-03-10 07:17:42 -04:00
|
|
|
if (!FStructureEditorUtils::AddVariable(S, PinType))
|
|
|
|
|
{
|
|
|
|
|
Result.Append(TEXT("ERROR: Failed to add field to struct.\n"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-03-08 22:17:14 -04:00
|
|
|
|
2026-03-10 07:17:42 -04:00
|
|
|
// Find the new variable by diffing GUID sets and rename it
|
|
|
|
|
for (const FStructVariableDescription& Var : FStructureEditorUtils::GetVarDesc(S))
|
|
|
|
|
{
|
|
|
|
|
if (!ExistingGuids.Contains(Var.VarGuid))
|
|
|
|
|
{
|
|
|
|
|
FStructureEditorUtils::RenameVariable(S, Var.VarGuid, Name);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-08 22:17:14 -04:00
|
|
|
|
2026-03-10 07:17:42 -04:00
|
|
|
MCPUtils::SaveGenericPackage(S);
|
|
|
|
|
Result.Appendf(TEXT("Added field: %s %s\n"), *Type, *Name);
|
2026-03-08 22:17:14 -04:00
|
|
|
}
|
|
|
|
|
};
|