ue-mcp 1.0.57 → 1.0.58
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/tool-counts.json
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
#include "JsonSerializer.h"
|
|
2
|
+
#include "HandlerJsonProperty.h"
|
|
3
|
+
#include "UObject/UnrealType.h"
|
|
4
|
+
#include "UObject/PropertyPortFlags.h"
|
|
5
|
+
#include "Dom/JsonObject.h"
|
|
6
|
+
#include "Dom/JsonValue.h"
|
|
7
|
+
|
|
8
|
+
TSharedPtr<FJsonValue> FMCPJsonSerializer::SerializeValue(const void* Value, FProperty* Property)
|
|
9
|
+
{
|
|
10
|
+
if (!Property || !Value)
|
|
11
|
+
{
|
|
12
|
+
return MakeShared<FJsonValueNull>();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return SerializePropertyValue(Value, Property);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
TSharedPtr<FJsonValue> FMCPJsonSerializer::SerializeVector(const FVector& Vector)
|
|
19
|
+
{
|
|
20
|
+
TSharedPtr<FJsonObject> JsonObject = MakeShared<FJsonObject>();
|
|
21
|
+
JsonObject->SetNumberField(TEXT("x"), Vector.X);
|
|
22
|
+
JsonObject->SetNumberField(TEXT("y"), Vector.Y);
|
|
23
|
+
JsonObject->SetNumberField(TEXT("z"), Vector.Z);
|
|
24
|
+
return MakeShared<FJsonValueObject>(JsonObject);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
TSharedPtr<FJsonValue> FMCPJsonSerializer::SerializeRotator(const FRotator& Rotator)
|
|
28
|
+
{
|
|
29
|
+
TSharedPtr<FJsonObject> JsonObject = MakeShared<FJsonObject>();
|
|
30
|
+
JsonObject->SetNumberField(TEXT("pitch"), Rotator.Pitch);
|
|
31
|
+
JsonObject->SetNumberField(TEXT("yaw"), Rotator.Yaw);
|
|
32
|
+
JsonObject->SetNumberField(TEXT("roll"), Rotator.Roll);
|
|
33
|
+
return MakeShared<FJsonValueObject>(JsonObject);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
TSharedPtr<FJsonValue> FMCPJsonSerializer::SerializeTransform(const FTransform& Transform)
|
|
37
|
+
{
|
|
38
|
+
TSharedPtr<FJsonObject> JsonObject = MakeShared<FJsonObject>();
|
|
39
|
+
TSharedPtr<FJsonValue> TranslationValue = SerializeVector(Transform.GetTranslation());
|
|
40
|
+
TSharedPtr<FJsonValue> RotationValue = SerializeRotator(Transform.GetRotation().Rotator());
|
|
41
|
+
TSharedPtr<FJsonValue> ScaleValue = SerializeVector(Transform.GetScale3D());
|
|
42
|
+
JsonObject->SetObjectField(TEXT("translation"), TranslationValue->AsObject());
|
|
43
|
+
JsonObject->SetObjectField(TEXT("rotation"), RotationValue->AsObject());
|
|
44
|
+
JsonObject->SetObjectField(TEXT("scale"), ScaleValue->AsObject());
|
|
45
|
+
return MakeShared<FJsonValueObject>(JsonObject);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
TSharedPtr<FJsonValue> FMCPJsonSerializer::SerializeLinearColor(const FLinearColor& Color)
|
|
49
|
+
{
|
|
50
|
+
TSharedPtr<FJsonObject> JsonObject = MakeShared<FJsonObject>();
|
|
51
|
+
JsonObject->SetNumberField(TEXT("r"), Color.R);
|
|
52
|
+
JsonObject->SetNumberField(TEXT("g"), Color.G);
|
|
53
|
+
JsonObject->SetNumberField(TEXT("b"), Color.B);
|
|
54
|
+
JsonObject->SetNumberField(TEXT("a"), Color.A);
|
|
55
|
+
return MakeShared<FJsonValueObject>(JsonObject);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
TSharedPtr<FJsonValue> FMCPJsonSerializer::SerializeString(const FString& String)
|
|
59
|
+
{
|
|
60
|
+
return MakeShared<FJsonValueString>(String);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
TSharedPtr<FJsonValue> FMCPJsonSerializer::SerializeObjectProperty(UObject* Object, FProperty* Property)
|
|
64
|
+
{
|
|
65
|
+
if (!Object || !Property)
|
|
66
|
+
{
|
|
67
|
+
return MakeShared<FJsonValueNull>();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const void* ValuePtr = Property->ContainerPtrToValuePtr<void>(Object);
|
|
71
|
+
return SerializePropertyValue(ValuePtr, Property);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
TSharedPtr<FJsonObject> FMCPJsonSerializer::SerializeObject(UObject* Object)
|
|
75
|
+
{
|
|
76
|
+
if (!Object)
|
|
77
|
+
{
|
|
78
|
+
return MakeShared<FJsonObject>();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
TSharedPtr<FJsonObject> JsonObject = MakeShared<FJsonObject>();
|
|
82
|
+
|
|
83
|
+
// Serialize basic object info
|
|
84
|
+
JsonObject->SetStringField(TEXT("name"), Object->GetName());
|
|
85
|
+
JsonObject->SetStringField(TEXT("class"), Object->GetClass()->GetName());
|
|
86
|
+
JsonObject->SetStringField(TEXT("path"), Object->GetPathName());
|
|
87
|
+
|
|
88
|
+
// Serialize properties
|
|
89
|
+
for (TFieldIterator<FProperty> PropIt(Object->GetClass()); PropIt; ++PropIt)
|
|
90
|
+
{
|
|
91
|
+
FProperty* Property = *PropIt;
|
|
92
|
+
if (Property && Property->HasAnyPropertyFlags(CPF_BlueprintVisible))
|
|
93
|
+
{
|
|
94
|
+
TSharedPtr<FJsonValue> PropValue = SerializeObjectProperty(Object, Property);
|
|
95
|
+
if (PropValue.IsValid())
|
|
96
|
+
{
|
|
97
|
+
JsonObject->SetField(Property->GetName(), PropValue);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return JsonObject;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
TSharedPtr<FJsonValue> FMCPJsonSerializer::SerializePropertyValue(const void* Value, FProperty* Property)
|
|
106
|
+
{
|
|
107
|
+
if (!Property || !Value)
|
|
108
|
+
{
|
|
109
|
+
return MakeShared<FJsonValueNull>();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (FStrProperty* StrProp = CastField<FStrProperty>(Property))
|
|
113
|
+
{
|
|
114
|
+
return MakeShared<FJsonValueString>(StrProp->GetPropertyValue(Value));
|
|
115
|
+
}
|
|
116
|
+
else if (FNameProperty* NameProp = CastField<FNameProperty>(Property))
|
|
117
|
+
{
|
|
118
|
+
return MakeShared<FJsonValueString>(NameProp->GetPropertyValue(Value).ToString());
|
|
119
|
+
}
|
|
120
|
+
else if (FTextProperty* TextProp = CastField<FTextProperty>(Property))
|
|
121
|
+
{
|
|
122
|
+
return MakeShared<FJsonValueString>(TextProp->GetPropertyValue(Value).ToString());
|
|
123
|
+
}
|
|
124
|
+
else if (FBoolProperty* BoolProp = CastField<FBoolProperty>(Property))
|
|
125
|
+
{
|
|
126
|
+
return MakeShared<FJsonValueBoolean>(BoolProp->GetPropertyValue(Value));
|
|
127
|
+
}
|
|
128
|
+
else if (FIntProperty* IntProp = CastField<FIntProperty>(Property))
|
|
129
|
+
{
|
|
130
|
+
return MakeShared<FJsonValueNumber>(IntProp->GetPropertyValue(Value));
|
|
131
|
+
}
|
|
132
|
+
else if (FInt64Property* Int64Prop = CastField<FInt64Property>(Property))
|
|
133
|
+
{
|
|
134
|
+
return MakeShared<FJsonValueNumber>(Int64Prop->GetPropertyValue(Value));
|
|
135
|
+
}
|
|
136
|
+
else if (FFloatProperty* FloatProp = CastField<FFloatProperty>(Property))
|
|
137
|
+
{
|
|
138
|
+
return MakeShared<FJsonValueNumber>(FloatProp->GetPropertyValue(Value));
|
|
139
|
+
}
|
|
140
|
+
else if (FDoubleProperty* DoubleProp = CastField<FDoubleProperty>(Property))
|
|
141
|
+
{
|
|
142
|
+
return MakeShared<FJsonValueNumber>(DoubleProp->GetPropertyValue(Value));
|
|
143
|
+
}
|
|
144
|
+
else if (FStructProperty* StructProp = CastField<FStructProperty>(Property))
|
|
145
|
+
{
|
|
146
|
+
if (StructProp->Struct == TBaseStructure<FVector>::Get())
|
|
147
|
+
{
|
|
148
|
+
return SerializeVector(*static_cast<const FVector*>(Value));
|
|
149
|
+
}
|
|
150
|
+
else if (StructProp->Struct == TBaseStructure<FRotator>::Get())
|
|
151
|
+
{
|
|
152
|
+
return SerializeRotator(*static_cast<const FRotator*>(Value));
|
|
153
|
+
}
|
|
154
|
+
else if (StructProp->Struct == TBaseStructure<FTransform>::Get())
|
|
155
|
+
{
|
|
156
|
+
return SerializeTransform(*static_cast<const FTransform*>(Value));
|
|
157
|
+
}
|
|
158
|
+
else if (StructProp->Struct == TBaseStructure<FLinearColor>::Get())
|
|
159
|
+
{
|
|
160
|
+
return SerializeLinearColor(*static_cast<const FLinearColor*>(Value));
|
|
161
|
+
}
|
|
162
|
+
else
|
|
163
|
+
{
|
|
164
|
+
// Generic struct: recursively serialize each field (#196, #199)
|
|
165
|
+
TSharedPtr<FJsonObject> StructJson = MakeShared<FJsonObject>();
|
|
166
|
+
for (TFieldIterator<FProperty> It(StructProp->Struct); It; ++It)
|
|
167
|
+
{
|
|
168
|
+
FProperty* FieldProp = *It;
|
|
169
|
+
const void* FieldValue = FieldProp->ContainerPtrToValuePtr<void>(Value);
|
|
170
|
+
TSharedPtr<FJsonValue> FieldJson = SerializePropertyValue(FieldValue, FieldProp);
|
|
171
|
+
if (FieldJson.IsValid())
|
|
172
|
+
{
|
|
173
|
+
StructJson->SetField(FieldProp->GetName(), FieldJson);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return MakeShared<FJsonValueObject>(StructJson);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
else if (FObjectPropertyBase* ObjProp = CastField<FObjectPropertyBase>(Property))
|
|
180
|
+
{
|
|
181
|
+
UObject* ObjValue = ObjProp->GetObjectPropertyValue(Value);
|
|
182
|
+
if (ObjValue)
|
|
183
|
+
{
|
|
184
|
+
return MakeShared<FJsonValueString>(ObjValue->GetPathName());
|
|
185
|
+
}
|
|
186
|
+
return MakeShared<FJsonValueNull>();
|
|
187
|
+
}
|
|
188
|
+
else if (FSoftObjectProperty* SoftObjProp = CastField<FSoftObjectProperty>(Property))
|
|
189
|
+
{
|
|
190
|
+
const FSoftObjectPtr& SoftPtr = SoftObjProp->GetPropertyValue(Value);
|
|
191
|
+
return MakeShared<FJsonValueString>(SoftPtr.ToString());
|
|
192
|
+
}
|
|
193
|
+
else if (FEnumProperty* EnumProp = CastField<FEnumProperty>(Property))
|
|
194
|
+
{
|
|
195
|
+
FString EnumStr;
|
|
196
|
+
EnumProp->ExportText_Direct(EnumStr, Value, Value, nullptr, PPF_None);
|
|
197
|
+
return MakeShared<FJsonValueString>(EnumStr);
|
|
198
|
+
}
|
|
199
|
+
else if (FByteProperty* ByteProp = CastField<FByteProperty>(Property))
|
|
200
|
+
{
|
|
201
|
+
if (ByteProp->Enum)
|
|
202
|
+
{
|
|
203
|
+
FString EnumStr;
|
|
204
|
+
ByteProp->ExportText_Direct(EnumStr, Value, Value, nullptr, PPF_None);
|
|
205
|
+
return MakeShared<FJsonValueString>(EnumStr);
|
|
206
|
+
}
|
|
207
|
+
return MakeShared<FJsonValueNumber>(ByteProp->GetPropertyValue(Value));
|
|
208
|
+
}
|
|
209
|
+
else if (FArrayProperty* ArrayProp = CastField<FArrayProperty>(Property))
|
|
210
|
+
{
|
|
211
|
+
TArray<TSharedPtr<FJsonValue>> JsonArray;
|
|
212
|
+
FScriptArrayHelper ArrayHelper(ArrayProp, Value);
|
|
213
|
+
for (int32 i = 0; i < ArrayHelper.Num(); ++i)
|
|
214
|
+
{
|
|
215
|
+
const void* ItemValue = ArrayHelper.GetRawPtr(i);
|
|
216
|
+
TSharedPtr<FJsonValue> ItemJson = SerializePropertyValue(ItemValue, ArrayProp->Inner);
|
|
217
|
+
if (ItemJson.IsValid())
|
|
218
|
+
{
|
|
219
|
+
JsonArray.Add(ItemJson);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return MakeShared<FJsonValueArray>(JsonArray);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Fallback: use ExportText for any remaining property types
|
|
226
|
+
FString StringValue;
|
|
227
|
+
Property->ExportText_Direct(StringValue, Value, Value, nullptr, PPF_None);
|
|
228
|
+
return MakeShared<FJsonValueString>(StringValue);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
bool FMCPJsonSerializer::DeserializeValue(FProperty* Property, void* ValueAddr, const TSharedPtr<FJsonValue>& JsonValue, FString& OutError)
|
|
232
|
+
{
|
|
233
|
+
return MCPJsonProperty::SetJsonOnProperty(Property, ValueAddr, JsonValue, OutError);
|
|
234
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "CoreMinimal.h"
|
|
4
|
+
#include "Dom/JsonValue.h"
|
|
5
|
+
#include "Dom/JsonObject.h"
|
|
6
|
+
#include "UObject/UnrealType.h"
|
|
7
|
+
|
|
8
|
+
class FMCPJsonSerializer
|
|
9
|
+
{
|
|
10
|
+
public:
|
|
11
|
+
// Serialize UE type to JSON value
|
|
12
|
+
static TSharedPtr<FJsonValue> SerializeValue(const void* Value, FProperty* Property);
|
|
13
|
+
|
|
14
|
+
// Serialize FVector to JSON
|
|
15
|
+
static TSharedPtr<FJsonValue> SerializeVector(const FVector& Vector);
|
|
16
|
+
|
|
17
|
+
// Serialize FRotator to JSON
|
|
18
|
+
static TSharedPtr<FJsonValue> SerializeRotator(const FRotator& Rotator);
|
|
19
|
+
|
|
20
|
+
// Serialize FTransform to JSON
|
|
21
|
+
static TSharedPtr<FJsonValue> SerializeTransform(const FTransform& Transform);
|
|
22
|
+
|
|
23
|
+
// Serialize FLinearColor to JSON
|
|
24
|
+
static TSharedPtr<FJsonValue> SerializeLinearColor(const FLinearColor& Color);
|
|
25
|
+
|
|
26
|
+
// Serialize FString to JSON
|
|
27
|
+
static TSharedPtr<FJsonValue> SerializeString(const FString& String);
|
|
28
|
+
|
|
29
|
+
// Serialize TArray to JSON
|
|
30
|
+
template<typename T>
|
|
31
|
+
static TSharedPtr<FJsonValue> SerializeArray(const TArray<T>& Array);
|
|
32
|
+
|
|
33
|
+
// Serialize UObject property to JSON
|
|
34
|
+
static TSharedPtr<FJsonValue> SerializeObjectProperty(UObject* Object, FProperty* Property);
|
|
35
|
+
|
|
36
|
+
// Serialize UObject to JSON (basic properties)
|
|
37
|
+
static TSharedPtr<FJsonObject> SerializeObject(UObject* Object);
|
|
38
|
+
|
|
39
|
+
// Deserialize a JSON value into a UE property (recursive; handles TArray,
|
|
40
|
+
// nested structs, UObject refs, soft refs, FGameplayTag, etc.).
|
|
41
|
+
// Delegates to MCPJsonProperty::SetJsonOnProperty — see HandlerJsonProperty.h.
|
|
42
|
+
static bool DeserializeValue(FProperty* Property, void* ValueAddr, const TSharedPtr<FJsonValue>& JsonValue, FString& OutError);
|
|
43
|
+
|
|
44
|
+
private:
|
|
45
|
+
// Helper to serialize property value
|
|
46
|
+
static TSharedPtr<FJsonValue> SerializePropertyValue(const void* Value, FProperty* Property);
|
|
47
|
+
};
|