|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using MelonLoader; |
| 9 | +using UnhollowerBaseLib; |
| 10 | + |
| 11 | +namespace Explorer |
| 12 | +{ |
| 13 | + public class PropertyInfoHolder : MemberInfoHolder |
| 14 | + { |
| 15 | + public PropertyInfo propInfo; |
| 16 | + public object m_value; |
| 17 | + |
| 18 | + public PropertyInfoHolder(Type _type, PropertyInfo _propInfo) |
| 19 | + { |
| 20 | + classType = _type; |
| 21 | + propInfo = _propInfo; |
| 22 | + } |
| 23 | + |
| 24 | + public override void Draw(ReflectionWindow window) |
| 25 | + { |
| 26 | + UIStyles.DrawMember(ref m_value, ref this.IsExpanded, ref this.arrayOffset, this.propInfo, window.m_rect, window.m_object, SetValue); |
| 27 | + } |
| 28 | + |
| 29 | + public override void UpdateValue(object obj) |
| 30 | + { |
| 31 | + try |
| 32 | + { |
| 33 | + if (obj is Il2CppSystem.Object ilObject) |
| 34 | + { |
| 35 | + var declaringType = this.propInfo.DeclaringType; |
| 36 | + if (declaringType == typeof(Il2CppObjectBase)) |
| 37 | + { |
| 38 | + m_value = ilObject.Pointer; |
| 39 | + } |
| 40 | + else |
| 41 | + { |
| 42 | + var cast = CppExplorer.Il2CppCast(obj, declaringType); |
| 43 | + m_value = this.propInfo.GetValue(cast, null); |
| 44 | + } |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + m_value = this.propInfo.GetValue(obj, null); |
| 49 | + } |
| 50 | + } |
| 51 | + catch (Exception e) |
| 52 | + { |
| 53 | + MelonLogger.Log("Exception on PropertyInfoHolder.UpdateValue, Name: " + this.propInfo.Name); |
| 54 | + MelonLogger.Log(e.GetType() + ", " + e.Message); |
| 55 | + |
| 56 | + var inner = e.InnerException; |
| 57 | + while (inner != null) |
| 58 | + { |
| 59 | + MelonLogger.Log("inner: " + inner.GetType() + ", " + inner.Message); |
| 60 | + inner = inner.InnerException; |
| 61 | + } |
| 62 | + |
| 63 | + m_value = null; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public override void SetValue(object obj) |
| 68 | + { |
| 69 | + try |
| 70 | + { |
| 71 | + if (propInfo.PropertyType.IsEnum) |
| 72 | + { |
| 73 | + if (System.Enum.Parse(propInfo.PropertyType, m_value.ToString()) is object enumValue && enumValue != null) |
| 74 | + { |
| 75 | + m_value = enumValue; |
| 76 | + } |
| 77 | + } |
| 78 | + else if (propInfo.PropertyType.IsPrimitive) |
| 79 | + { |
| 80 | + if (propInfo.PropertyType == typeof(float)) |
| 81 | + { |
| 82 | + if (float.TryParse(m_value.ToString(), out float f)) |
| 83 | + { |
| 84 | + m_value = f; |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + MelonLogger.LogWarning("Cannot parse " + m_value.ToString() + " to a float!"); |
| 89 | + } |
| 90 | + } |
| 91 | + else if (propInfo.PropertyType == typeof(double)) |
| 92 | + { |
| 93 | + if (double.TryParse(m_value.ToString(), out double d)) |
| 94 | + { |
| 95 | + m_value = d; |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + MelonLogger.LogWarning("Cannot parse " + m_value.ToString() + " to a double!"); |
| 100 | + } |
| 101 | + } |
| 102 | + else if (propInfo.PropertyType != typeof(bool)) |
| 103 | + { |
| 104 | + if (int.TryParse(m_value.ToString(), out int i)) |
| 105 | + { |
| 106 | + m_value = i; |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + MelonLogger.LogWarning("Cannot parse " + m_value.ToString() + " to an integer! type: " + propInfo.PropertyType); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + var declaring = propInfo.DeclaringType; |
| 116 | + var cast = CppExplorer.Il2CppCast(obj, declaring); |
| 117 | + |
| 118 | + propInfo.SetValue(propInfo.GetAccessors()[0].IsStatic ? null : cast, m_value, null); |
| 119 | + } |
| 120 | + catch |
| 121 | + { |
| 122 | + //MelonLogger.Log("Exception trying to set property " + this.propInfo.Name); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments