using System; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using MCPForUnity.Editor.Helpers; using UnityEditor; using UnityEngine; namespace MCPForUnity.Editor.Helpers { /// /// Unified property conversion from JSON to Unity types. /// Uses UnityJsonSerializer for consistent type handling. /// public static class PropertyConversion { /// /// Converts a JToken to the specified target type using Unity type converters. /// /// The JSON token to convert /// The target type to convert to /// The converted object, or null if conversion fails public static object ConvertToType(JToken token, Type targetType) { if (token == null || token.Type == JTokenType.Null) { if (targetType.IsValueType && Nullable.GetUnderlyingType(targetType) == null) { McpLog.Warn($"[PropertyConversion] Cannot assign null to non-nullable value type {targetType.Name}. Returning default value."); return Activator.CreateInstance(targetType); } return null; } try { // Use the shared Unity serializer with custom converters return token.ToObject(targetType, UnityJsonSerializer.Instance); } catch (Exception ex) { McpLog.Error($"Error converting token to {targetType.FullName}: {ex.Message}\nToken: {token.ToString(Formatting.None)}"); throw; } } /// /// Tries to convert a JToken to the specified target type. /// Returns null and logs warning on failure (does not throw). /// public static object TryConvertToType(JToken token, Type targetType) { try { return ConvertToType(token, targetType); } catch { return null; } } /// /// Generic version of ConvertToType. /// public static T ConvertTo(JToken token) { return (T)ConvertToType(token, typeof(T)); } /// /// Converts a JToken to a Unity asset by loading from path. /// /// JToken containing asset path /// Expected asset type /// The loaded asset, or null if not found public static UnityEngine.Object LoadAssetFromToken(JToken token, Type targetType) { if (token == null || token.Type != JTokenType.String) return null; string assetPath = AssetPathUtility.SanitizeAssetPath(token.ToString()); UnityEngine.Object loadedAsset = AssetDatabase.LoadAssetAtPath(assetPath, targetType); if (loadedAsset == null) { McpLog.Warn($"[PropertyConversion] Could not load asset of type {targetType.Name} from path: {assetPath}"); } return loadedAsset; } } }