75 lines
2.0 KiB
C++
75 lines
2.0 KiB
C++
#pragma once
|
|
|
|
#include "CoreMinimal.h"
|
|
#include "WingServer.h"
|
|
#include "WingHandler.h"
|
|
#include "WingFetcher.h"
|
|
#include "WingProperty.h"
|
|
#include "WingUtils.h"
|
|
#include "Property_Set.generated.h"
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------
|
|
|
|
UCLASS()
|
|
class UWing_Property_Set : public UObject, public IWingHandler
|
|
{
|
|
GENERATED_BODY()
|
|
|
|
public:
|
|
UPROPERTY(meta=(Description="Target object"))
|
|
FString Object;
|
|
|
|
UPROPERTY(meta=(Description="Object mapping property names to new values in Unreal text format"))
|
|
FWingJsonObject Properties;
|
|
|
|
virtual FString GetDescription() const override
|
|
{
|
|
return TEXT("Set one or more editable properties. Values use Unreal text format.");
|
|
}
|
|
|
|
virtual void Handle() override
|
|
{
|
|
// Resolve the path to an object and get its editable template.
|
|
WingFetcher F;
|
|
UObject* Obj = F.Walk(Object).Cast<UObject>();
|
|
if (!Obj) return;
|
|
|
|
if (!Properties.Json || Properties.Json->Values.Num() == 0)
|
|
{
|
|
UWingServer::Print(TEXT("Error: No properties specified\n"));
|
|
return;
|
|
}
|
|
|
|
// Validation pass — resolve all properties and values before modifying anything.
|
|
TArray<FWingProperty> All = FWingProperty::GetAll(Obj, CPF_Edit);
|
|
TArray<TPair<FWingProperty, FString>> Resolved;
|
|
for (const auto& Pair : Properties.Json->Values)
|
|
{
|
|
FWingProperty P = FWingProperty::FindOneExactMatch(All, Pair.Key);
|
|
if (!P) return;
|
|
|
|
FString ValueStr;
|
|
if (!Pair.Value->TryGetString(ValueStr))
|
|
{
|
|
UWingServer::Printf(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))
|
|
continue;
|
|
SuccessCount++;
|
|
}
|
|
|
|
UWingServer::Printf(TEXT("Set %d/%d properties.\n"), SuccessCount, Properties.Json->Values.Num());
|
|
}
|
|
};
|