Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 0.0.52 #55

Merged
merged 2 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand All @@ -342,31 +348,31 @@ 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)
{
DebugUtility.LogException<ArgumentNullException>(nameof(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)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return null;
}

var containers = self.GetPropertyContainers();
var containers = self.GetPropertyParents();
for (var index = containers.Count - 1; index >= 0; index--)
{
var container = containers[index];
Expand All @@ -377,45 +383,43 @@ public static object GetLastNonCollectionContainer(this SerializedProperty self)
return containers.FirstOrDefault();
}

public static List<object> GetPropertyContainers(this SerializedProperty self)
public static List<PropertyParent> GetPropertyParents(this SerializedProperty self)
{
var list = new List<PropertyParent>();
self.CollectPropertyParents(ref list);
return list;
}

public static void CollectPropertyParents(this SerializedProperty self, ref List<PropertyParent> propertyParents)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return new List<object>();
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<object>();
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))
{
Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down
36 changes: 0 additions & 36 deletions Assets/BetterCommons/Editor/Helpers/CachePropertyKey.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Assets/BetterCommons/Editor/Helpers/CachePropertyKey.cs.meta

This file was deleted.

8 changes: 0 additions & 8 deletions Assets/BetterCommons/Editor/Helpers/PropertyItemInfo.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Assets/BetterCommons/Editor/Helpers/PropertyItemInfo.cs.meta

This file was deleted.

17 changes: 17 additions & 0 deletions Assets/BetterCommons/Editor/Helpers/PropertyParent.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
3 changes: 3 additions & 0 deletions Assets/BetterCommons/Editor/Helpers/PropertyParent.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions Assets/BetterCommons/Editor/Utility/SerializedPropertyUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,37 @@ public static class SerializedPropertyUtility

private static readonly Dictionary<CachePropertyKey, CachedFieldInfo> FieldInfoFromPropertyPathCache = new Dictionary<CachePropertyKey, CachedFieldInfo>();

private struct CachePropertyKey : IEquatable<CachePropertyKey>
{
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()
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/BetterCommons/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
Loading