From e61490c1433ebc0306982ee374cd18c1ab9afc08 Mon Sep 17 00:00:00 2001 From: uurha Date: Sun, 8 Sep 2024 17:34:00 +0200 Subject: [PATCH 1/2] Rework GetPropertyContainers --- .../SerializedPropertyExtensions.cs | 88 ++++++++++--------- .../Editor/Helpers/CachePropertyKey.cs | 36 -------- .../Editor/Helpers/CachePropertyKey.cs.meta | 11 --- .../Editor/Helpers/PropertyItemInfo.cs | 8 -- .../Editor/Helpers/PropertyItemInfo.cs.meta | 11 --- .../Editor/Helpers/PropertyParent.cs | 17 ++++ .../Editor/Helpers/PropertyParent.cs.meta | 3 + .../Utility/SerializedPropertyUtility.cs | 31 +++++++ 8 files changed, 97 insertions(+), 108 deletions(-) delete mode 100644 Assets/BetterCommons/Editor/Helpers/CachePropertyKey.cs delete mode 100644 Assets/BetterCommons/Editor/Helpers/CachePropertyKey.cs.meta delete mode 100644 Assets/BetterCommons/Editor/Helpers/PropertyItemInfo.cs delete mode 100644 Assets/BetterCommons/Editor/Helpers/PropertyItemInfo.cs.meta create mode 100644 Assets/BetterCommons/Editor/Helpers/PropertyParent.cs create mode 100644 Assets/BetterCommons/Editor/Helpers/PropertyParent.cs.meta diff --git a/Assets/BetterCommons/Editor/Extensions/SerializedPropertyExtensions.cs b/Assets/BetterCommons/Editor/Extensions/SerializedPropertyExtensions.cs index c3672e3..61209cb 100644 --- a/Assets/BetterCommons/Editor/Extensions/SerializedPropertyExtensions.cs +++ b/Assets/BetterCommons/Editor/Extensions/SerializedPropertyExtensions.cs @@ -22,6 +22,12 @@ public static class SerializedPropertyExtensions private static readonly MethodInfo VerifyMethod; private static readonly FieldInfo PropertyPrtInfo; private static readonly FieldInfo ObjectPrtInfo; + + private struct PropertyItemInfo + { + public string PropertyName { get; set; } + public int ElementIndex { get; set; } + } static SerializedPropertyExtensions() { @@ -177,7 +183,7 @@ public static string GetPropertyParentList(this SerializedProperty self) return string.Empty; } - string propertyPath = self.propertyPath; + var propertyPath = self.propertyPath; return SerializedPropertyUtility.GetPropertyParentList(propertyPath); } @@ -316,9 +322,9 @@ public static object GetValue(this SerializedProperty self) return null; } - string propertyPath = self.propertyPath; + var propertyPath = self.propertyPath; object value = self.serializedObject.targetObject; - int i = 0; + var i = 0; while (NextPathComponent(propertyPath, ref i, out var token)) value = GetPathComponentValue(value, token); return value; @@ -342,12 +348,12 @@ public static void SetValueNoRecord(this SerializedProperty self, object value) return; } - var container = GetPropertyContainer(self, out var deferredToken); + var container = GetPropertyParent(self, out var deferredToken); SetPathComponentValue(container, deferredToken, value); } - public static object GetPropertyContainer(this SerializedProperty self) + public static object GetPropertyParent(this SerializedProperty self) { if (self == null) { @@ -355,10 +361,10 @@ public static object GetPropertyContainer(this SerializedProperty self) return null; } - return GetPropertyContainer(self, out _); + return GetPropertyParent(self, out _); } - public static object GetLastNonCollectionContainer(this SerializedProperty self) + public static object GetLastNonCollectionParent(this SerializedProperty self) { if (self == null) { @@ -366,7 +372,7 @@ public static object GetLastNonCollectionContainer(this SerializedProperty self) return null; } - var containers = self.GetPropertyContainers(); + var containers = self.GetPropertyParents(); for (var index = containers.Count - 1; index >= 0; index--) { var container = containers[index]; @@ -377,45 +383,43 @@ public static object GetLastNonCollectionContainer(this SerializedProperty self) return containers.FirstOrDefault(); } - public static List GetPropertyContainers(this SerializedProperty self) + public static List GetPropertyParents(this SerializedProperty self) + { + var list = new List(); + self.CollectPropertyParents(ref list); + return list; + } + + public static void CollectPropertyParents(this SerializedProperty self, ref List propertyParents) { if (self == null) { DebugUtility.LogException(nameof(self)); - return new List(); + return; } - string propertyPath = self.propertyPath; - object container = self.serializedObject.targetObject; + var propertyPath = self.propertyPath; + object parent = self.serializedObject.targetObject; + + var i = 0; + NextPathComponent(propertyPath, ref i, out var deferredToken); + + propertyParents.Add(new PropertyParent(parent, deferredToken.PropertyName, deferredToken.ElementIndex)); - int i = 0; - PropertyItemInfo deferredToken; - var list = new List(); - list.Add(container); - NextPathComponent(propertyPath, ref i, out deferredToken); while (NextPathComponent(propertyPath, ref i, out var token)) { - container = GetPathComponentValue(container, deferredToken); + parent = GetPathComponentValue(parent, deferredToken); deferredToken = token; - list.Add(container); - } - - if (container.GetType().IsValueType) - { - var message = - $"Cannot use SerializedObject.SetValue on a struct object, as the result will be set on a temporary. Either change {container.GetType().Name} to a class, or use SetValue with a parent member."; - Debug.LogWarning(message); + propertyParents.Add(new PropertyParent(parent, deferredToken.PropertyName, deferredToken.ElementIndex)); } - - return list; } - private static object GetPropertyContainer(SerializedProperty self, out PropertyItemInfo deferredToken) + private static object GetPropertyParent(SerializedProperty self, out PropertyItemInfo deferredToken) { - string propertyPath = self.propertyPath; + var propertyPath = self.propertyPath; object container = self.serializedObject.targetObject; - int i = 0; + var i = 0; NextPathComponent(propertyPath, ref i, out deferredToken); while (NextPathComponent(propertyPath, ref i, out var token)) { @@ -441,7 +445,7 @@ private static bool NextPathComponent(string propertyPath, ref int index, out Pr return true; } - int dot = propertyPath.IndexOf('.', index); + var dot = propertyPath.IndexOf('.', index); if (dot == -1) { component.PropertyName = propertyPath.Substring(index); @@ -456,20 +460,20 @@ private static bool NextPathComponent(string propertyPath, ref int index, out Pr return true; } - private static object GetPathComponentValue(object container, PropertyItemInfo component) + private static object GetPathComponentValue(object container, PropertyItemInfo propertyItemInfo) { - if (component.PropertyName == null) - return ((IList)container)[component.ElementIndex]; + if (propertyItemInfo.PropertyName == null) + return ((IList)container)[propertyItemInfo.ElementIndex]; - return GetMemberValue(container, component.PropertyName); + return GetMemberValue(container, propertyItemInfo.PropertyName); } - private static void SetPathComponentValue(object container, PropertyItemInfo component, object value) + private static void SetPathComponentValue(object container, PropertyItemInfo propertyItemInfo, object value) { - if (component.PropertyName == null) - ((IList)container)[component.ElementIndex] = value; + if (propertyItemInfo.PropertyName == null) + ((IList)container)[propertyItemInfo.ElementIndex] = value; else - SetMemberValue(container, component.PropertyName, value); + SetMemberValue(container, propertyItemInfo.PropertyName, value); } private static object GetMemberValue(object container, string name) @@ -478,7 +482,7 @@ private static object GetMemberValue(object container, string name) return null; var type = container.GetType(); var members = TraverseBaseClasses(type, name); - for (int i = 0; i < members.Count; ++i) + for (var i = 0; i < members.Count; ++i) { if (members[i] is FieldInfo field) return field.GetValue(container); @@ -512,7 +516,7 @@ private static void SetMemberValue(object container, string name, object value) { var type = container.GetType(); var members = type.GetMember(name, Defines.FieldsFlags); - for (int i = 0; i < members.Length; ++i) + for (var i = 0; i < members.Length; ++i) { if (members[i] is FieldInfo field) { diff --git a/Assets/BetterCommons/Editor/Helpers/CachePropertyKey.cs b/Assets/BetterCommons/Editor/Helpers/CachePropertyKey.cs deleted file mode 100644 index 7c40b50..0000000 --- a/Assets/BetterCommons/Editor/Helpers/CachePropertyKey.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using Better.Commons.Runtime.Extensions; - -namespace Better.Commons.EditorAddons.Helpers -{ - public struct CachePropertyKey : IEquatable - { - private readonly Type _type; - private readonly string _propertyPath; - - public CachePropertyKey(Type type, string propertyPath) - { - _type = type; - _propertyPath = propertyPath; - } - - public bool Equals(CachePropertyKey other) - { - return _type == other._type && _propertyPath.CompareOrdinal(other._propertyPath); - } - - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is CachePropertyKey key && Equals(key); - } - - public override int GetHashCode() - { - unchecked - { - return ((_type != null ? _type.GetHashCode() : 0) * 397) ^ (_propertyPath != null ? _propertyPath.GetHashCode() : 0); - } - } - } -} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Helpers/CachePropertyKey.cs.meta b/Assets/BetterCommons/Editor/Helpers/CachePropertyKey.cs.meta deleted file mode 100644 index e939bd5..0000000 --- a/Assets/BetterCommons/Editor/Helpers/CachePropertyKey.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: f010485a16537de4fa7d3752da6c2076 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/BetterCommons/Editor/Helpers/PropertyItemInfo.cs b/Assets/BetterCommons/Editor/Helpers/PropertyItemInfo.cs deleted file mode 100644 index beb6bb9..0000000 --- a/Assets/BetterCommons/Editor/Helpers/PropertyItemInfo.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Better.Commons.EditorAddons.Helpers -{ - public struct PropertyItemInfo - { - public string PropertyName { get; set; } - public int ElementIndex { get; set; } - } -} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Helpers/PropertyItemInfo.cs.meta b/Assets/BetterCommons/Editor/Helpers/PropertyItemInfo.cs.meta deleted file mode 100644 index f53d637..0000000 --- a/Assets/BetterCommons/Editor/Helpers/PropertyItemInfo.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: d08fc8f80709ae84da7b30cd657c4205 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/BetterCommons/Editor/Helpers/PropertyParent.cs b/Assets/BetterCommons/Editor/Helpers/PropertyParent.cs new file mode 100644 index 0000000..f64730d --- /dev/null +++ b/Assets/BetterCommons/Editor/Helpers/PropertyParent.cs @@ -0,0 +1,17 @@ +namespace Better.Commons.EditorAddons.Helpers +{ + public class PropertyParent + { + public object ParentInstance { get; } + + public string FieldName { get; } + public int ElementIndex { get; } + + public PropertyParent(object parentInstance, string fieldName, int elementIndex) + { + ParentInstance = parentInstance; + FieldName = fieldName; + ElementIndex = elementIndex; + } + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Helpers/PropertyParent.cs.meta b/Assets/BetterCommons/Editor/Helpers/PropertyParent.cs.meta new file mode 100644 index 0000000..d1cb049 --- /dev/null +++ b/Assets/BetterCommons/Editor/Helpers/PropertyParent.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2a825ff7c97d4346b1a12ca4d3b787fd +timeCreated: 1725761183 \ No newline at end of file diff --git a/Assets/BetterCommons/Editor/Utility/SerializedPropertyUtility.cs b/Assets/BetterCommons/Editor/Utility/SerializedPropertyUtility.cs index 77faba2..864433d 100644 --- a/Assets/BetterCommons/Editor/Utility/SerializedPropertyUtility.cs +++ b/Assets/BetterCommons/Editor/Utility/SerializedPropertyUtility.cs @@ -27,6 +27,37 @@ public static class SerializedPropertyUtility private static readonly Dictionary FieldInfoFromPropertyPathCache = new Dictionary(); + private struct CachePropertyKey : IEquatable + { + private readonly Type _type; + private readonly string _propertyPath; + + public CachePropertyKey(Type type, string propertyPath) + { + _type = type; + _propertyPath = propertyPath; + } + + public bool Equals(CachePropertyKey other) + { + return _type == other._type && _propertyPath.CompareOrdinal(other._propertyPath); + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is CachePropertyKey key && Equals(key); + } + + public override int GetHashCode() + { + unchecked + { + return ((_type != null ? _type.GetHashCode() : 0) * 397) ^ (_propertyPath != null ? _propertyPath.GetHashCode() : 0); + } + } + } + [DidReloadScripts] private static void Reload() { From 3d597b288d337554182956040d505fd6f8165eb0 Mon Sep 17 00:00:00 2001 From: uurha Date: Sun, 8 Sep 2024 17:34:12 +0200 Subject: [PATCH 2/2] Update package.json --- Assets/BetterCommons/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/BetterCommons/package.json b/Assets/BetterCommons/package.json index 50ccb51..f851d2d 100644 --- a/Assets/BetterCommons/package.json +++ b/Assets/BetterCommons/package.json @@ -1,7 +1,7 @@ { "name": "com.tdw.better.commons", "displayName": "Better Commons", - "version": "0.0.51", + "version": "0.0.52", "unity": "2021.3", "description": " ", "dependencies": {