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.60 #63

Merged
merged 2 commits into from
Dec 1, 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
3 changes: 1 addition & 2 deletions Assets/BetterCommons/Editor/CustomEditors/MultiEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ bool WherePredicate((Type type, MultiEditorAttribute attribute) x)
return att.EditorFor == targetType;
}

return typeof(ExtendedEditor).GetAllInheritedTypesWithoutUnityObject()
.Select(type => (type, type.GetCustomAttribute<MultiEditorAttribute>()))
return typeof(ExtendedEditor).GetAllInheritedTypesWithoutUnityObject().Select(type => (type, type.GetCustomAttribute<MultiEditorAttribute>()))
.Where(WherePredicate).OrderBy(x => x.Item2.Order).ToArray();
}

Expand Down

This file was deleted.

34 changes: 0 additions & 34 deletions Assets/BetterCommons/Editor/Drawers/HandlerBinderRegistry.cs

This file was deleted.

3 changes: 3 additions & 0 deletions Assets/BetterCommons/Editor/Drawers/HandlerBinding.meta

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

102 changes: 102 additions & 0 deletions Assets/BetterCommons/Editor/Drawers/HandlerBinding/Binding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.Linq;
using System.Reflection;

namespace Better.Commons.EditorAddons.Drawers.Handlers
{
public class Binding
{
public BindingInfo[] Binds { get; }
public Type HandlerType { get; }

public Binding(Type handlerType)
{
HandlerType = handlerType;
var handlerBindings = HandlerType.GetCustomAttributes<HandlerBindingAttribute>(true).Select(attribute => attribute.BindingInfo);

Binds = handlerBindings.ToArray();
}

public int GetBindingPriority(Type attributeType, Type fieldType)
{
var value = GetFieldPriority(fieldType);
if (value < 0)
{
return value;
}

var buffer = GetAttributePriority(attributeType);
if (buffer < 0)
{
return buffer;
}

return value + buffer;
}

private int GetFieldPriority(Type fieldType)
{
if (!IsFieldTypeSupported(fieldType))
{
return -1;
}

if (Binds.Any(bind => bind.FieldType != null && bind.FieldType == fieldType))
{
return 2;
}

if (Binds.Any(bind => bind.FieldType != null && bind.FieldType.IsAssignableFrom(fieldType)))
{
return 1;
}

if (Binds.Any(bind => bind.AnyFieldType))
{
return 0;
}

return 0;
}

private int GetAttributePriority(Type attributeType)
{
if (attributeType == null)
{
return 0;
}

if (Binds.All(bind => bind.AttributeType == null))
{
return -1;
}

if (Binds.Any(bind => bind.AttributeType != null && bind.AttributeType == attributeType))
{
return 2;
}

if (Binds.Any(bind => bind.AttributeType != null && bind.AttributeType.IsAssignableFrom(attributeType)))
{
return 1;
}

return 0;
}

public bool IsFieldTypeSupported(Type fieldType)
{
var fieldSupported = Binds.Select(bind => bind.FieldType)
.Where(bindFieldType => bindFieldType != null && fieldType != null)
.Any(bindFieldType => bindFieldType.IsAssignableFrom(fieldType));

if (fieldSupported)
{
return true;
}

var anyFieldSupported = Binds.Any(bind => bind.AnyFieldType);
return anyFieldSupported;
}
}
}

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

18 changes: 18 additions & 0 deletions Assets/BetterCommons/Editor/Drawers/HandlerBinding/BindingInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace Better.Commons.EditorAddons.Drawers
{
public class BindingInfo
{
public BindingInfo(Type fieldType, Type attributeType, bool anyFieldType)
{
AttributeType = attributeType;
FieldType = fieldType;
AnyFieldType = anyFieldType;
}

public Type AttributeType { get; }
public Type FieldType { get; }
public bool AnyFieldType { get; }
}
}

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Better.Commons.EditorAddons.Drawers.Handlers
{
public class AttributeHandlersFilter : FieldHandlersFilter
{
protected readonly Type _attributeType;

public AttributeHandlersFilter(Type fieldType, Type attributeType) : base(fieldType)
{
_attributeType = attributeType;
}

protected override int GetBindingPriority(Binding bind)
{
return bind.GetBindingPriority(_attributeType, _fieldType);
}

protected override bool TryFilter(Binding binding)
{
return binding.Binds.Any(bind => bind.AttributeType.IsAssignableFrom(_attributeType))
&& base.TryFilter(binding);
}
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace Better.Commons.EditorAddons.Drawers.Handlers
{
public class FieldHandlersFilter : HandlersFilter
{
protected readonly Type _fieldType;

public FieldHandlersFilter(Type fieldType)
{
_fieldType = fieldType;
}

protected override int GetBindingPriority(Binding bind)
{
return bind.GetBindingPriority(null, _fieldType);
}

protected override bool TryFilter(Binding binding)
{
return binding.IsFieldTypeSupported(_fieldType);
}
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections.Generic;
using System.Linq;

namespace Better.Commons.EditorAddons.Drawers.Handlers
{
public abstract class HandlersFilter
{
public bool TryFilter(HashSet<Binding> bindings, out Binding filteredBinding)
{
var candidates = new HashSet<Binding>();

foreach (var binding in bindings)
{
if (!TryFilter(binding)) continue;
candidates.Add(binding);
}

var sortedCandidates = SortCandidates(candidates);

filteredBinding = sortedCandidates.FirstOrDefault();
return filteredBinding != null;
}

protected virtual IEnumerable<Binding> SortCandidates(IEnumerable<Binding> candidates)
{
candidates = candidates.OrderByDescending(GetBindingPriority);
return candidates;
}

protected abstract int GetBindingPriority(Binding bind);


protected abstract bool TryFilter(Binding binding);
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace Better.Commons.EditorAddons.Drawers
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class HandlerBindingAttribute : Attribute
{
public BindingInfo BindingInfo { get; }

public HandlerBindingAttribute(Type fieldType, Type attributeType)
{
BindingInfo = new BindingInfo(fieldType, attributeType, false);
}

public HandlerBindingAttribute(Type attributeType)
{
BindingInfo = new BindingInfo(null, attributeType, true);
}
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Better.Commons.EditorAddons.Drawers.Handlers;
using Better.Commons.Runtime.Extensions;

namespace Better.Commons.EditorAddons.Drawers
{
public static class BindingRegistry
{
private static readonly HashSet<Binding> _bindings;

static BindingRegistry()
{
_bindings = GetBindings();
}

private static HashSet<Binding> GetBindings()
{
var handlerTypes = typeof(SerializedPropertyHandler).GetAllInheritedTypes().Where(type => !type.IsAbstract);
var boundHandlers = handlerTypes.Where(type => type.IsDefined(typeof(HandlerBindingAttribute)));
var bindings = boundHandlers.Select(type => new Binding(type));
return bindings.ToHashSet();
}

public static TypeHandlerBinder<THandler> GetBinder<THandler>() where THandler : SerializedPropertyHandler
{
var bindings = _bindings.Where(binding => typeof(THandler).IsAssignableFrom(binding.HandlerType)).ToHashSet();
var binder = new TypeHandlerBinder<THandler>(bindings);
return binder;
}
}
}
Loading
Loading