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

79 lines
2.3 KiB
C++

#pragma once
#include "CoreMinimal.h"
#include "MCPHandler.h"
#include "MCPFetcher.h"
#include "MCPProperty.h"
#include "MCPUtils.h"
#include "Property_Set.generated.h"
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
UCLASS()
class UMCP_Property_Set : public UObject, public IMCPHandler
{
GENERATED_BODY()
public:
UPROPERTY(meta=(Description="MCPFetcher path to the object (e.g. /Game/Materials/M_Gold or /Game/Tangibles/TAN_Char,component:Mesh0)"))
FString Path;
UPROPERTY(meta=(Description="Object mapping property names to new values in Unreal text format"))
FMCPJsonObject Properties;
virtual FString GetDescription() const override
{
return TEXT("Set one or more editable properties on an object resolved via MCPFetcher path. "
"Properties is a JSON object like {\"TwoSided\": \"true\", \"BlendMode\": \"BLEND_Translucent\"}.");
}
virtual void Handle(FStringBuilderBase& Result) override
{
// Resolve the path to an object and get its editable template.
MCPFetcher F;
UObject* Template = F.Walk(Path).Template().Cast<UObject>();
if (!Template) return;
if (!Properties.Json || Properties.Json->Values.Num() == 0)
{
Result.Append(TEXT("Error: No properties specified\n"));
return;
}
// Validation pass — resolve all properties and values before modifying anything.
TArray<TPair<MCPProperty, FString>> Resolved;
for (const auto& Pair : Properties.Json->Values)
{
MCPProperty P = MCPProperty::GetOneExactMatch(Template, CPF_Edit, Pair.Key, Result);
if (!P) return;
FString ValueStr;
if (!Pair.Value->TryGetString(ValueStr))
{
Result.Appendf(TEXT("Error: Value for '%s' must be a string\n"), *Pair.Key);
return;
}
Resolved.Emplace(P, ValueStr);
}
// Apply all changes.
int32 SuccessCount = 0;
for (auto& [P, ValueStr] : Resolved)
{
if (!P.SetText(ValueStr, Result))
continue;
SuccessCount++;
}
// Save.
bool bSaved = MCPUtils::SaveGenericPackage(Template);
Result.Appendf(TEXT("Set %d/%d properties.\n"), SuccessCount, Properties.Json->Values.Num());
if (!bSaved)
Result.Append(TEXT("Warning: Save failed\n"));
}
};