Use default equality comparer

This commit is contained in:
VladV 2022-07-12 13:09:21 +03:00
parent 63734b20a5
commit a56ae84197
5 changed files with 53 additions and 26 deletions

View File

@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TriInspector;
@ -94,7 +93,7 @@ namespace TriInspector.Drawers
var items = _resolver.GetDropdownItems(_property);
_currentText = items
.FirstOrDefault(it => _resolver.EqualityComparer.Equals(it.Value, _property.Value))
.FirstOrDefault(it => _property.Comparer.Equals(it.Value, _property.Value))
?.Text ?? "";
}
@ -105,7 +104,7 @@ namespace TriInspector.Drawers
foreach (var item in items)
{
var isOn = _resolver.EqualityComparer.Equals(item.Value, _property.Value);
var isOn = _property.Comparer.Equals(item.Value, _property.Value);
menu.AddItem(new GUIContent(item.Text), isOn, _property.SetValue, item.Value);
}
@ -115,8 +114,6 @@ namespace TriInspector.Drawers
private abstract class DropdownResolver
{
public abstract IEqualityComparer EqualityComparer { get; }
public abstract void Initialize(TriPropertyDefinition propertyDefinition, string expression);
public abstract bool TryGetErrorString(out string error);
@ -137,8 +134,6 @@ namespace TriInspector.Drawers
{
private ValueResolver<IEnumerable<TriDropdownItem<T>>> _resolver;
public override IEqualityComparer EqualityComparer { get; } = EqualityComparer<T>.Default;
public override void Initialize(TriPropertyDefinition propertyDefinition, string expression)
{
_resolver = ValueResolver.Resolve<IEnumerable<TriDropdownItem<T>>>(propertyDefinition, expression);
@ -164,8 +159,6 @@ namespace TriInspector.Drawers
{
private ValueResolver<IEnumerable<T>> _resolver;
public override IEqualityComparer EqualityComparer { get; } = EqualityComparer<T>.Default;
public override void Initialize(TriPropertyDefinition propertyDefinition, string expression)
{
_resolver = ValueResolver.Resolve<IEnumerable<T>>(propertyDefinition, expression);

View File

@ -88,6 +88,8 @@ namespace TriInspector
[PublicAPI]
public string DisplayName => DisplayNameContent.text;
public IEqualityComparer Comparer => TriEqualityComparer.Of(ValueType);
[PublicAPI]
public GUIContent DisplayNameContent
{
@ -536,21 +538,6 @@ namespace TriInspector
return false;
}
public static bool AreValuesEqual(Type type, object a, object b)
{
if (type == typeof(string))
{
return string.Equals((string) a, (string) b);
}
if (type.IsValueType || type == typeof(string))
{
return a.Equals(b);
}
return ReferenceEquals(a, b);
}
private static void SetValueRecursive(TriProperty property, object value, int targetIndex)
{
// for value types we must recursively set all parent objects
@ -624,7 +611,7 @@ namespace TriInspector
for (var i = 1; i < property.PropertyTree.TargetsCount; i++)
{
var otherValue = property.GetValue(i);
if (!AreValuesEqual(property.FieldType, otherValue, newValue))
if (!property.Comparer.Equals(otherValue, newValue))
{
isMixed = true;
return;

View File

@ -25,7 +25,7 @@ namespace TriInspector
get => (T) Property.Value;
set
{
if (TriProperty.AreValuesEqual(Property.FieldType, Property.Value, value))
if (Property.Comparer.Equals(Property.Value, value))
{
return;
}

View File

@ -0,0 +1,36 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
namespace TriInspector.Utilities
{
public static class TriEqualityComparer
{
private static readonly Dictionary<Type, IEqualityComparer> Cache = new Dictionary<Type, IEqualityComparer>();
public static IEqualityComparer Of(Type type)
{
if (!Cache.TryGetValue(type, out var comparer))
{
Cache[type] = comparer = CreateDefaultEqualityComparer(type);
}
return comparer;
}
private static IEqualityComparer CreateDefaultEqualityComparer(Type type)
{
var comparerType = typeof(EqualityComparer<>).MakeGenericType(type);
var comparerProperty = comparerType.GetProperty("Default", BindingFlags.Static | BindingFlags.Public);
var comparer = (IEqualityComparer) comparerProperty?.GetValue(null);
if (comparer == null)
{
throw new InvalidOperationException($"Failed to create default comparer for type {type}");
}
return comparer;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 47b9c566490541bcbfcf7fc93bf7bdea
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: