-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Version 0.0.60
- Loading branch information
There are no files selected for viewing
This file was deleted.
This file was deleted.
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,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.
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; | ||
} | ||
} | ||
} |