using System; using System.Diagnostics; using System.Reflection; using UnityEngine; [Serializable] public class PropertyReference { public PropertyReference() { } public PropertyReference(Component target, string fieldName) { this.mTarget = target; this.mName = fieldName; } public Component target { get { return this.mTarget; } set { this.mTarget = value; this.mProperty = null; this.mField = null; } } public string name { get { return this.mName; } set { this.mName = value; this.mProperty = null; this.mField = null; } } public bool isValid { get { return this.mTarget != null && !string.IsNullOrEmpty(this.mName); } } public bool isEnabled { get { if (this.mTarget == null) { return false; } MonoBehaviour monoBehaviour = this.mTarget as MonoBehaviour; return monoBehaviour == null || monoBehaviour.enabled; } } public Type GetPropertyType() { if (this.mProperty == null && this.mField == null && this.isValid) { this.Cache(); } if (this.mProperty != null) { return this.mProperty.PropertyType; } if (this.mField != null) { return this.mField.FieldType; } return typeof(void); } public override bool Equals(object obj) { if (obj == null) { return !this.isValid; } if (obj is PropertyReference) { PropertyReference propertyReference = obj as PropertyReference; return this.mTarget == propertyReference.mTarget && string.Equals(this.mName, propertyReference.mName); } return false; } public override int GetHashCode() { return PropertyReference.s_Hash; } public void Set(Component target, string methodName) { this.mTarget = target; this.mName = methodName; } public void Clear() { this.mTarget = null; this.mName = null; } public void Reset() { this.mField = null; this.mProperty = null; } public override string ToString() { return PropertyReference.ToString(this.mTarget, this.name); } public static string ToString(Component comp, string property) { if (!(comp != null)) { return null; } string text = comp.GetType().ToString(); int num = text.LastIndexOf('.'); if (num > 0) { text = text.Substring(num + 1); } if (!string.IsNullOrEmpty(property)) { return text + "." + property; } return text + ".[property]"; } [DebuggerHidden] [DebuggerStepThrough] public object Get() { if (this.mProperty == null && this.mField == null && this.isValid) { this.Cache(); } if (this.mProperty != null) { if (this.mProperty.CanRead) { return this.mProperty.GetValue(this.mTarget, null); } } else if (this.mField != null) { return this.mField.GetValue(this.mTarget); } return null; } [DebuggerHidden] [DebuggerStepThrough] public bool Set(object value) { if (this.mProperty == null && this.mField == null && this.isValid) { this.Cache(); } if (this.mProperty == null && this.mField == null) { return false; } if (value == null) { try { if (this.mProperty == null) { this.mField.SetValue(this.mTarget, null); return true; } if (this.mProperty.CanWrite) { this.mProperty.SetValue(this.mTarget, null, null); return true; } } catch (Exception) { return false; } } if (!this.Convert(ref value)) { if (Application.isPlaying) { UnityEngine.Debug.LogError(string.Concat(new object[] { "Unable to convert ", value.GetType(), " to ", this.GetPropertyType() })); } } else { if (this.mField != null) { this.mField.SetValue(this.mTarget, value); return true; } if (this.mProperty.CanWrite) { this.mProperty.SetValue(this.mTarget, value, null); return true; } } return false; } [DebuggerHidden] [DebuggerStepThrough] private bool Cache() { if (this.mTarget != null && !string.IsNullOrEmpty(this.mName)) { Type type = this.mTarget.GetType(); this.mField = type.GetField(this.mName); this.mProperty = type.GetProperty(this.mName); } else { this.mField = null; this.mProperty = null; } return this.mField != null || this.mProperty != null; } private bool Convert(ref object value) { if (this.mTarget == null) { return false; } Type propertyType = this.GetPropertyType(); Type from; if (value == null) { if (!propertyType.IsClass) { return false; } from = propertyType; } else { from = value.GetType(); } return PropertyReference.Convert(ref value, from, propertyType); } public static bool Convert(Type from, Type to) { object obj = null; return PropertyReference.Convert(ref obj, from, to); } public static bool Convert(object value, Type to) { if (value == null) { value = null; return PropertyReference.Convert(ref value, to, to); } return PropertyReference.Convert(ref value, value.GetType(), to); } public static bool Convert(ref object value, Type from, Type to) { if (to.IsAssignableFrom(from)) { return true; } if (to == typeof(string)) { value = ((value == null) ? "null" : value.ToString()); return true; } if (value == null) { return false; } float num2; if (to == typeof(int)) { if (from == typeof(string)) { int num; if (int.TryParse((string)value, out num)) { value = num; return true; } } else if (from == typeof(float)) { value = Mathf.RoundToInt((float)value); return true; } } else if (to == typeof(float) && from == typeof(string) && float.TryParse((string)value, out num2)) { value = num2; return true; } return false; } [SerializeField] private Component mTarget; [SerializeField] private string mName; private FieldInfo mField; private PropertyInfo mProperty; private static int s_Hash = "PropertyBinding".GetHashCode(); }