-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathGameObjectComponentHelpers.cs
More file actions
435 lines (388 loc) · 18.1 KB
/
Copy pathGameObjectComponentHelpers.cs
File metadata and controls
435 lines (388 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using MCPForUnity.Editor.Helpers;
using MCPForUnity.Editor.Tools;
using MCPForUnity.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEditor;
using UnityEngine;
namespace MCPForUnity.Editor.Tools.GameObjects
{
internal static class GameObjectComponentHelpers
{
internal static object AddComponentInternal(GameObject targetGo, string typeName, JObject properties)
{
Type componentType = FindType(typeName);
if (componentType == null)
{
return new ErrorResponse($"Component type '{typeName}' not found or is not a valid Component.");
}
if (!typeof(Component).IsAssignableFrom(componentType))
{
return new ErrorResponse($"Type '{typeName}' is not a Component.");
}
if (componentType == typeof(Transform))
{
return new ErrorResponse("Cannot add another Transform component.");
}
// A 2D-vs-3D conflict is only possible when both physics modules are installed;
// if either is absent, its component types can't exist, so there's nothing to check.
#if MCP_HAS_PHYSICS && MCP_HAS_PHYSICS_2D
bool isAdding2DPhysics = typeof(Rigidbody2D).IsAssignableFrom(componentType) || typeof(Collider2D).IsAssignableFrom(componentType);
bool isAdding3DPhysics = typeof(Rigidbody).IsAssignableFrom(componentType) || typeof(Collider).IsAssignableFrom(componentType);
if (isAdding2DPhysics)
{
if (targetGo.GetComponent<Rigidbody>() != null || targetGo.GetComponent<Collider>() != null)
{
return new ErrorResponse($"Cannot add 2D physics component '{typeName}' because the GameObject '{targetGo.name}' already has a 3D Rigidbody or Collider.");
}
}
else if (isAdding3DPhysics)
{
if (targetGo.GetComponent<Rigidbody2D>() != null || targetGo.GetComponent<Collider2D>() != null)
{
return new ErrorResponse($"Cannot add 3D physics component '{typeName}' because the GameObject '{targetGo.name}' already has a 2D Rigidbody or Collider.");
}
}
#endif
Component existingComponent = targetGo.GetComponent(componentType);
if (existingComponent != null && !AllowsMultiple(componentType))
{
return new ErrorResponse(
$"Component '{typeName}' already exists on '{targetGo.name}' and this type does not allow multiple instances."
);
}
try
{
Component newComponent = Undo.AddComponent(targetGo, componentType);
if (newComponent == null)
{
if (targetGo.GetComponent(componentType) != null && !AllowsMultiple(componentType))
{
return new ErrorResponse(
$"Component '{typeName}' already exists on '{targetGo.name}' and this type does not allow multiple instances."
);
}
return new ErrorResponse(
$"Failed to add component '{typeName}' to '{targetGo.name}'. Unity may restrict this component on the current target."
);
}
if (newComponent is Light light)
{
light.type = LightType.Directional;
}
if (properties != null)
{
var setResult = SetComponentPropertiesInternal(targetGo, typeName, properties, newComponent);
if (setResult != null)
{
Undo.DestroyObjectImmediate(newComponent);
return setResult;
}
}
return null;
}
catch (Exception e)
{
return new ErrorResponse($"Error adding component '{typeName}' to '{targetGo.name}': {e.Message}");
}
}
private static bool AllowsMultiple(Type componentType)
{
if (componentType == null)
{
return false;
}
return !Attribute.IsDefined(componentType, typeof(DisallowMultipleComponent), inherit: true);
}
internal static object RemoveComponentInternal(GameObject targetGo, string typeName)
{
if (targetGo == null)
{
return new ErrorResponse("Target GameObject is null.");
}
Type componentType = FindType(typeName);
if (componentType == null)
{
return new ErrorResponse($"Component type '{typeName}' not found for removal.");
}
if (componentType == typeof(Transform))
{
return new ErrorResponse("Cannot remove the Transform component.");
}
Component componentToRemove = targetGo.GetComponent(componentType);
if (componentToRemove == null)
{
return new ErrorResponse($"Component '{typeName}' not found on '{targetGo.name}' to remove.");
}
try
{
Undo.DestroyObjectImmediate(componentToRemove);
return null;
}
catch (Exception e)
{
return new ErrorResponse($"Error removing component '{typeName}' from '{targetGo.name}': {e.Message}");
}
}
internal static object SetComponentPropertiesInternal(GameObject targetGo, string componentTypeName, JObject properties, Component targetComponentInstance = null)
{
Component targetComponent = targetComponentInstance;
if (targetComponent == null)
{
if (ComponentResolver.TryResolve(componentTypeName, out var compType, out var compError))
{
targetComponent = targetGo.GetComponent(compType);
}
else
{
targetComponent = targetGo.GetComponent(componentTypeName);
}
}
if (targetComponent == null)
{
return new ErrorResponse($"Component '{componentTypeName}' not found on '{targetGo.name}' to set properties.");
}
Undo.RecordObject(targetComponent, "Set Component Properties");
var failures = new List<string>();
foreach (var prop in properties.Properties())
{
string propName = prop.Name;
JToken propValue = prop.Value;
try
{
bool setResult;
string setError;
// Nested paths (e.g. "transform.position") need local handling
// since ComponentOps doesn't support dot/bracket notation.
if (propName.Contains('.') || propName.Contains('['))
{
setResult = SetNestedProperty(targetComponent, propName, propValue, InputSerializer, out setError);
}
else
{
// ComponentOps handles reflection + SerializedProperty fallback
setResult = ComponentOps.SetProperty(targetComponent, propName, propValue, out setError);
}
if (!setResult)
{
string msg = setError;
if (msg == null || msg.Contains("not found"))
{
var availableProperties = ComponentResolver.GetAllComponentProperties(targetComponent.GetType());
var suggestions = ComponentResolver.GetFuzzyPropertySuggestions(propName, availableProperties);
msg = suggestions.Any()
? $"Property '{propName}' not found. Did you mean: {string.Join(", ", suggestions)}? Available: [{string.Join(", ", availableProperties)}]"
: $"Property '{propName}' not found. Available: [{string.Join(", ", availableProperties)}]";
}
McpLog.Warn($"[ManageGameObject] {msg}");
failures.Add(msg);
}
}
catch (Exception e)
{
McpLog.Error($"[ManageGameObject] Error setting property '{propName}' on '{componentTypeName}': {e.Message}");
failures.Add($"Error setting '{propName}': {e.Message}");
}
}
EditorUtility.SetDirty(targetComponent);
return failures.Count == 0
? null
: new ErrorResponse($"One or more properties failed on '{componentTypeName}'.", new { errors = failures });
}
private static JsonSerializer InputSerializer => UnityJsonSerializer.Instance;
private static bool SetNestedProperty(object target, string path, JToken value, JsonSerializer inputSerializer, out string error)
{
error = null;
try
{
string[] pathParts = SplitPropertyPath(path);
if (pathParts.Length == 0)
{
error = $"Invalid nested property path '{path}'.";
return false;
}
object currentObject = target;
Type currentType = currentObject.GetType();
BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase;
for (int i = 0; i < pathParts.Length - 1; i++)
{
string part = pathParts[i];
bool isArray = false;
int arrayIndex = -1;
if (part.Contains("["))
{
int startBracket = part.IndexOf('[');
int endBracket = part.IndexOf(']');
if (startBracket > 0 && endBracket > startBracket)
{
string indexStr = part.Substring(startBracket + 1, endBracket - startBracket - 1);
if (int.TryParse(indexStr, out arrayIndex))
{
isArray = true;
part = part.Substring(0, startBracket);
}
}
}
PropertyInfo propInfo = currentType.GetProperty(part, flags);
FieldInfo fieldInfo = null;
if (propInfo == null)
{
fieldInfo = currentType.GetField(part, flags);
if (fieldInfo == null)
{
error = $"Could not find property or field '{part}' on type '{currentType.Name}' in path '{path}'.";
return false;
}
}
currentObject = propInfo != null ? propInfo.GetValue(currentObject) : fieldInfo.GetValue(currentObject);
if (currentObject == null)
{
error = $"Property '{part}' is null in path '{path}', cannot access nested properties.";
return false;
}
if (isArray)
{
if (currentObject is Material[])
{
var materials = currentObject as Material[];
if (materials.Length == 0)
{
error = $"Material array is empty in path '{path}', cannot access index {arrayIndex}.";
return false;
}
if (arrayIndex < 0 || arrayIndex >= materials.Length)
{
error = $"Material index {arrayIndex} out of range (0-{materials.Length - 1}) in path '{path}'.";
return false;
}
currentObject = materials[arrayIndex];
}
else if (currentObject is System.Collections.IList)
{
var list = currentObject as System.Collections.IList;
if (list.Count == 0)
{
error = $"List is empty in path '{path}', cannot access index {arrayIndex}.";
return false;
}
if (arrayIndex < 0 || arrayIndex >= list.Count)
{
error = $"Index {arrayIndex} out of range (0-{list.Count - 1}) in path '{path}'.";
return false;
}
currentObject = list[arrayIndex];
}
else
{
error = $"Property '{part}' is not an array or list in path '{path}', cannot access by index.";
return false;
}
}
currentType = currentObject.GetType();
}
string finalPart = pathParts[pathParts.Length - 1];
if (currentObject is Material material && finalPart.StartsWith("_"))
{
if (!MaterialOps.TrySetShaderProperty(material, finalPart, value, inputSerializer))
{
error = $"Failed to set shader property '{finalPart}' on material '{material.name}' in path '{path}'.";
return false;
}
return true;
}
PropertyInfo finalPropInfo = currentType.GetProperty(finalPart, flags);
if (finalPropInfo != null && finalPropInfo.CanWrite)
{
object convertedValue = ConvertJTokenToType(value, finalPropInfo.PropertyType, inputSerializer);
if (convertedValue != null || value.Type == JTokenType.Null)
{
finalPropInfo.SetValue(currentObject, convertedValue);
return true;
}
error = $"Failed to convert value for '{finalPart}' to type '{finalPropInfo.PropertyType.Name}' in path '{path}'.";
return false;
}
FieldInfo finalFieldInfo = currentType.GetField(finalPart, flags);
if (finalFieldInfo != null)
{
object convertedValue = ConvertJTokenToType(value, finalFieldInfo.FieldType, inputSerializer);
if (convertedValue != null || value.Type == JTokenType.Null)
{
finalFieldInfo.SetValue(currentObject, convertedValue);
return true;
}
error = $"Failed to convert value for '{finalPart}' to type '{finalFieldInfo.FieldType.Name}' in path '{path}'.";
return false;
}
// Try non-public [SerializeField] fields (nested paths need this too)
FieldInfo serializedField = ComponentOps.FindSerializedFieldInHierarchy(currentType, finalPart);
if (serializedField != null)
{
object convertedValue = ConvertJTokenToType(value, serializedField.FieldType, inputSerializer);
if (convertedValue != null || value.Type == JTokenType.Null)
{
serializedField.SetValue(currentObject, convertedValue);
return true;
}
error = $"Failed to convert value for '{finalPart}' to type '{serializedField.FieldType.Name}' in path '{path}'.";
return false;
}
error = $"Property or field '{finalPart}' not found on type '{currentType.Name}' in path '{path}'.";
}
catch (Exception ex)
{
error = $"Error setting nested property '{path}': {ex.Message}";
}
return false;
}
private static string[] SplitPropertyPath(string path)
{
List<string> parts = new List<string>();
int startIndex = 0;
bool inBrackets = false;
for (int i = 0; i < path.Length; i++)
{
char c = path[i];
if (c == '[')
{
inBrackets = true;
}
else if (c == ']')
{
inBrackets = false;
}
else if (c == '.' && !inBrackets)
{
parts.Add(path.Substring(startIndex, i - startIndex));
startIndex = i + 1;
}
}
if (startIndex < path.Length)
{
parts.Add(path.Substring(startIndex));
}
return parts.ToArray();
}
private static object ConvertJTokenToType(JToken token, Type targetType, JsonSerializer inputSerializer)
{
return PropertyConversion.ConvertToType(token, targetType);
}
private static Type FindType(string typeName)
{
if (ComponentResolver.TryResolve(typeName, out Type resolvedType, out string error))
{
return resolvedType;
}
if (!string.IsNullOrEmpty(error))
{
McpLog.Warn($"[FindType] {error}");
}
return null;
}
}
}