Skip to content

Commit

Permalink
Merge pull request #60 from techno-dwarf-works/feature/refactoring
Browse files Browse the repository at this point in the history
Version 0.0.57
  • Loading branch information
uurha committed Oct 6, 2024
1 parent 110f80a commit 2a0f166
Show file tree
Hide file tree
Showing 28 changed files with 568 additions and 138 deletions.
25 changes: 25 additions & 0 deletions Editor/Comparers/CachedSerializedPropertyComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Generic;
using Better.Commons.EditorAddons.Drawers.Base;
using Better.Commons.Runtime.Comparers;

namespace Better.Commons.EditorAddons.Comparers
{
public class CachedSerializedPropertyComparer : BaseComparer<CachedSerializedPropertyComparer, CachedSerializedProperty>,
IEqualityComparer<CachedSerializedProperty>
{
public bool Equals(CachedSerializedProperty x, CachedSerializedProperty y)
{
if (ReferenceEquals(x, y)) return true;
if (ReferenceEquals(x, null)) return false;
if (ReferenceEquals(y, null)) return false;
if (x.GetType() != y.GetType()) return false;
return x == y;
}

public int GetHashCode(CachedSerializedProperty obj)
{
return obj.GetHashCode();
}

}
}
3 changes: 3 additions & 0 deletions Editor/Comparers/CachedSerializedPropertyComparer.cs.meta

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

2 changes: 1 addition & 1 deletion Editor/Comparers/SerializedPropertyComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public bool Equals(SerializedProperty x, SerializedProperty y)

public int GetHashCode(SerializedProperty obj)
{
return !obj.IsDisposed() && obj.propertyPath != null ? obj.propertyPath.GetHashCode() : 0;
return obj.Verify() && !obj.IsDisposed() && obj.propertyPath != null ? obj.propertyPath.GetHashCode() : 0;
}
}
}
87 changes: 87 additions & 0 deletions Editor/Drawers/Base/CachedSerializedProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using Better.Commons.EditorAddons.Extensions;
using UnityEditor;

namespace Better.Commons.EditorAddons.Drawers.Base
{
public class CachedSerializedProperty : IEquatable<CachedSerializedProperty>
{
private readonly int _hashCode;
private readonly SerializedProperty _serializedProperty;

public SerializedProperty SerializedProperty => _serializedProperty;

public CachedSerializedProperty(SerializedProperty serializedProperty)
{
_hashCode = serializedProperty.GetHashCode();
_serializedProperty = serializedProperty;
}

public override int GetHashCode()
{
return HashCode.Combine(_hashCode, _serializedProperty);
}

public bool IsValid()
{
try
{
if (_serializedProperty == null)
{
return false;
}

if (!_serializedProperty.Verify())
{
return false;
}

if (_serializedProperty.IsDisposed())
{
return false;
}

return _serializedProperty.serializedObject.targetObject != null;
}
catch
{
return false;
}
}

public bool Equals(CachedSerializedProperty other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return _hashCode == other._hashCode;
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((CachedSerializedProperty)obj);
}

public static implicit operator SerializedProperty(CachedSerializedProperty property)
{
return property.SerializedProperty;
}

public static explicit operator CachedSerializedProperty(SerializedProperty property)
{
return new CachedSerializedProperty(property);
}

public static bool operator ==(CachedSerializedProperty left, CachedSerializedProperty right)
{
return !ReferenceEquals(left, null) && left.Equals(right);
}

public static bool operator !=(CachedSerializedProperty left, CachedSerializedProperty right)
{
return !ReferenceEquals(left, null) && !left.Equals(right);
}
}
}
3 changes: 3 additions & 0 deletions Editor/Drawers/Base/CachedSerializedProperty.cs.meta

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

23 changes: 20 additions & 3 deletions Editor/Drawers/Base/HandlerCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

namespace Better.Commons.EditorAddons.Drawers.Base
{
public class HandlerCollection<T> : Dictionary<SerializedProperty, CollectionValue<T>> where T : SerializedPropertyHandler
public class HandlerCollection<T> : Dictionary<CachedSerializedProperty, CollectionValue<T>> where T : SerializedPropertyHandler
{
public HandlerCollection() : base(SerializedPropertyComparer.Instance)
public HandlerCollection() : base(CachedSerializedPropertyComparer.Instance)
{
}

/// <summary>
/// Deconstruct method for stored wrappers
/// ContainerReleased method for stored wrappers
/// </summary>
public void Deconstruct()
{
Expand All @@ -21,5 +21,22 @@ public void Deconstruct()
value.Handler.Deconstruct();
}
}

public void Revalidate()
{
var listToRemove = new List<CachedSerializedProperty>();
foreach (var property in Keys)
{
if (!property.IsValid())
{
listToRemove.Add(property);
}
}

foreach (var property in listToRemove)
{
Remove(property);
}
}
}
}
32 changes: 23 additions & 9 deletions Editor/Drawers/BasePropertyDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public abstract class BasePropertyDrawer<THandler, TAttribute> : PropertyDrawer
protected TAttribute Attribute { get; private set; }

protected HandlerCollection<THandler> Handlers { get; private set; }
protected ElementsContainer Container { get; private set; }
protected TypeHandlerBinder<THandler> TypeHandlersBinder { get; private set; }

protected BasePropertyDrawer()
Expand Down Expand Up @@ -53,9 +54,11 @@ protected THandler GetHandler(SerializedProperty property)
var attributeType = Attribute.GetType();
var fieldType = GetFieldOrElementType();

if (Handlers.TryGetValue(property, out var value))
var cached = new CachedSerializedProperty(property);
Handlers.Revalidate();

if (Handlers.TryGetValue(cached, out var value))
{
ValidationUtility.ValidateCachedProperties(Handlers);
return value.Handler;
}

Expand All @@ -66,25 +69,31 @@ protected THandler GetHandler(SerializedProperty property)
}

var collectionValue = new CollectionValue<THandler>(handler, fieldType);
Handlers.Add(property, collectionValue);
Handlers.Add(cached, collectionValue);

return handler;
}

public sealed override VisualElement CreatePropertyGUI(SerializedProperty property)
{
var container = new ElementsContainer(property);
if (Container != null)
{
ContainerReleased(Container);
Container = null;
}

Container = new ElementsContainer(property);
FieldInfo = fieldInfo;
Attribute = (TAttribute)attribute;
Handlers = new HandlerCollection<THandler>();
TypeHandlersBinder = HandlerBinderRegistry.GetMap<THandler>();

PopulateContainer(container);
container.Use();
PopulateContainer(Container);
Container.Use();

var subState = StyleDefinition.CombineSubState(typeof(TAttribute).Name, GetType().Name);
container.RootElement.AddToClassList(subState);
return container.RootElement;
Container.RootElement.AddToClassList(subState);
return Container.RootElement;
}

protected abstract void PopulateContainer(ElementsContainer container);
Expand All @@ -111,9 +120,14 @@ protected virtual Type GetFieldOrElementType()
return fieldType;
}

protected virtual void Deconstruct()
private void Deconstruct()
{
Handlers?.Deconstruct();
ContainerReleased(Container);
}

protected virtual void ContainerReleased(ElementsContainer container)
{
}
}
}
32 changes: 32 additions & 0 deletions Editor/Drawers/Container/ContainerPrewarmElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.Linq;

namespace Better.Commons.EditorAddons.Drawers.Container
{
public class ContainerPrewarmElement : SubPrewarmElement
{
private List<SubPrewarmElement> PrewarmChildren { get; }

public ContainerPrewarmElement() : base()
{
PrewarmChildren = new List<SubPrewarmElement>();
}

public void Add(SubPrewarmElement prewarmElement)
{
PrewarmChildren.Add(prewarmElement);
base.Add(prewarmElement);
}

public IEnumerable<SubPrewarmElement> GetByTags(IEnumerable<object> tag)
{
return PrewarmChildren.Where(x => x.ContainsAnyTags(tag));
}

public bool TryGetByTag(object tag, out SubPrewarmElement element)
{
element = PrewarmChildren.FirstOrDefault(x => x.ContainsTag(tag));
return element != null;
}
}
}
3 changes: 3 additions & 0 deletions Editor/Drawers/Container/ContainerPrewarmElement.cs.meta

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

Loading

0 comments on commit 2a0f166

Please sign in to comment.