Skip to content

Commit

Permalink
Merge pull request #63 from techno-dwarf-works/feature/refactoring
Browse files Browse the repository at this point in the history
Version 0.0.60
  • Loading branch information
uurha committed Dec 1, 2024
1 parent f2721d8 commit 85a503a
Show file tree
Hide file tree
Showing 36 changed files with 403 additions and 309 deletions.
3 changes: 1 addition & 2 deletions 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
3 changes: 0 additions & 3 deletions Editor/Drawers/BasePropertyDrawer.cs.meta

This file was deleted.

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

This file was deleted.

3 changes: 3 additions & 0 deletions 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 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;
}
}
}
3 changes: 3 additions & 0 deletions Editor/Drawers/HandlerBinding/Binding.cs.meta

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

18 changes: 18 additions & 0 deletions 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; }
}
}
3 changes: 3 additions & 0 deletions Editor/Drawers/HandlerBinding/BindingInfo.cs.meta

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

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

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

27 changes: 27 additions & 0 deletions Editor/Drawers/HandlerBinding/Filters/AttributeHandlersFilter.cs
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.

24 changes: 24 additions & 0 deletions Editor/Drawers/HandlerBinding/Filters/FieldHandlersFilter.cs
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.

35 changes: 35 additions & 0 deletions Editor/Drawers/HandlerBinding/Filters/HandlersFilter.cs
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);
}
}
3 changes: 3 additions & 0 deletions Editor/Drawers/HandlerBinding/Filters/HandlersFilter.cs.meta

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

20 changes: 20 additions & 0 deletions Editor/Drawers/HandlerBinding/HandlerBindingAttribute.cs
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);
}
}
}
3 changes: 3 additions & 0 deletions Editor/Drawers/HandlerBinding/HandlerBindingAttribute.cs.meta

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

33 changes: 33 additions & 0 deletions Editor/Drawers/HandlerBinding/HandlerBindingRegistry.cs
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

0 comments on commit 85a503a

Please sign in to comment.