From 370d8ad8101cd142f5e41ae086e5cc82b9284701 Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Wed, 1 Apr 2020 23:26:15 -0500 Subject: [PATCH] Reverted implicit converters --- IPA.Loader/Config/Stores/Converters.cs | 153 +++++++++--------- .../GeneratedStoreImpl/ObjectStructure.cs | 6 - 2 files changed, 72 insertions(+), 87 deletions(-) diff --git a/IPA.Loader/Config/Stores/Converters.cs b/IPA.Loader/Config/Stores/Converters.cs index e572a7df..6c425288 100644 --- a/IPA.Loader/Config/Stores/Converters.cs +++ b/IPA.Loader/Config/Stores/Converters.cs @@ -36,75 +36,16 @@ namespace IPA.Config.Stores.Converters val is Integer inte ? inte.AsFloat()?.Value : null; - internal static Type GetDefaultConverterType(Type t, bool returnSimpleConverters = true) - { - if (t.IsEnum) - { - return typeof(CaseInsensitiveEnumConverter<>).MakeGenericType(t); - } - if (t.IsGenericType) - { - var generic = t.GetGenericTypeDefinition(); - var args = t.GetGenericArguments(); - if (generic == typeof(List<>)) - return (typeof(ListConverter<>).MakeGenericType(args)); - else if (generic == typeof(IList<>)) - return (typeof(IListConverter<>).MakeGenericType(args)); - else if (generic == typeof(Dictionary<,>) && args[0] == typeof(string)) - return (typeof(DictionaryConverter<>).MakeGenericType(args[1])); - else if (generic == typeof(IDictionary<,>) && args[0] == typeof(string)) - return (typeof(IDictionaryConverter<>).MakeGenericType(args[1])); -#if NET4 - else if (generic == typeof(ISet<>)) - return (typeof(ISetConverter<>).MakeGenericType(args)); - else if (generic == typeof(IReadOnlyDictionary<,>) && args[0] == typeof(string)) - return (typeof(IReadOnlyDictionaryConverter<>).MakeGenericType(args[1])); -#endif - } - var iCollBase = t.GetInterfaces() - .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICollection<>)); - if (iCollBase != null && t.GetConstructor(Type.EmptyTypes) != null) - { // if it implements ICollection and has a default constructor - var valueType = iCollBase.GetGenericArguments().First(); - return (typeof(CollectionConverter<,>).MakeGenericType(valueType, t)); - } - if (!returnSimpleConverters) return null; - if (t == typeof(string)) - { - //Logger.log.Debug($"gives StringConverter"); - return typeof(StringConverter); - } - if (t.IsValueType) - { // we have to do this garbo to make it accept the thing that we know is a value type at instantiation time - if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)) - { // this is a Nullable - //Logger.log.Debug($"gives NullableConverter<{Nullable.GetUnderlyingType(t)}>"); - return (typeof(NullableConverter<>).MakeGenericType(Nullable.GetUnderlyingType(t))); - } - - //Logger.log.Debug($"gives converter for value type {t}"); - var valConv = Activator.CreateInstance(typeof(ValConv<>).MakeGenericType(t)) as IValConv; - return valConv.Get(); - } - - //Logger.log.Debug($"gives CustomObjectConverter<{t}>"); - return (typeof(CustomObjectConverter<>).MakeGenericType(t)); - } - - internal interface IValConv - { - Type Get(); - } internal interface IValConv { - Type Get(); + ValueConverter Get(); } - internal class ValConv : IValConv, IValConv where T : struct + internal class ValConv : IValConv where T : struct { private static readonly IValConv Impl = ValConvImpls.Impl as IValConv ?? new ValConv(); - public Type Get() => Impl.Get(); - Type IValConv.Get() - => typeof(CustomValueTypeConverter); + public ValueConverter Get() => Impl.Get(); + ValueConverter IValConv.Get() + => new CustomValueTypeConverter(); } private class ValConvImpls : IValConv, IValConv, IValConv, @@ -116,21 +57,21 @@ namespace IPA.Config.Stores.Converters IValConv, IValConv { internal static readonly ValConvImpls Impl = new ValConvImpls(); - Type IValConv.Get() => typeof(CharConverter); - Type IValConv.Get() => typeof(LongConverter); - Type IValConv.Get() => typeof(ULongConverter); - Type IValConv.Get() => typeof(IntPtrConverter); - Type IValConv.Get() => typeof(UIntPtrConverter); - Type IValConv.Get() => typeof(IntConverter); - Type IValConv.Get() => typeof(UIntConverter); - Type IValConv.Get() => typeof(ShortConverter); - Type IValConv.Get() => typeof(UShortConverter); - Type IValConv.Get() => typeof(ByteConverter); - Type IValConv.Get() => typeof(SByteConverter); - Type IValConv.Get() => typeof(FloatConverter); - Type IValConv.Get() => typeof(DoubleConverter); - Type IValConv.Get() => typeof(DecimalConverter); - Type IValConv.Get() => typeof(BooleanConverter); + ValueConverter IValConv.Get() => new CharConverter(); + ValueConverter IValConv.Get() => new LongConverter(); + ValueConverter IValConv.Get() => new ULongConverter(); + ValueConverter IValConv.Get() => new IntPtrConverter(); + ValueConverter IValConv.Get() => new UIntPtrConverter(); + ValueConverter IValConv.Get() => new IntConverter(); + ValueConverter IValConv.Get() => new UIntConverter(); + ValueConverter IValConv.Get() => new ShortConverter(); + ValueConverter IValConv.Get() => new UShortConverter(); + ValueConverter IValConv.Get() => new ByteConverter(); + ValueConverter IValConv.Get() => new SByteConverter(); + ValueConverter IValConv.Get() => new FloatConverter(); + ValueConverter IValConv.Get() => new DoubleConverter(); + ValueConverter IValConv.Get() => new DecimalConverter(); + ValueConverter IValConv.Get() => new BooleanConverter(); } } @@ -147,7 +88,7 @@ namespace IPA.Config.Stores.Converters public static ValueConverter Default => defaultConverter ??= MakeDefault(); - internal static ValueConverter MakeDefault(bool returnSimpleConverters = true) + internal static ValueConverter MakeDefault(bool allowValuesAndCustoms = true) { var t = typeof(T); //Logger.log.Debug($"Converter<{t}>.MakeDefault()"); @@ -155,7 +96,57 @@ namespace IPA.Config.Stores.Converters static ValueConverter MakeInstOf(Type ty) => Activator.CreateInstance(ty) as ValueConverter; - return MakeInstOf(Converter.GetDefaultConverterType(t, returnSimpleConverters)); + if (t.IsEnum) + { + return MakeInstOf(typeof(CaseInsensitiveEnumConverter<>).MakeGenericType(t)); + } + if (t == typeof(string)) + { + //Logger.log.Debug($"gives StringConverter"); + return new StringConverter() as ValueConverter; + } + if (t.IsGenericType) + { + var generic = t.GetGenericTypeDefinition(); + var args = t.GetGenericArguments(); + if (generic == typeof(List<>)) + return MakeInstOf(typeof(ListConverter<>).MakeGenericType(args)); + else if (generic == typeof(IList<>)) + return MakeInstOf(typeof(IListConverter<>).MakeGenericType(args)); + else if (generic == typeof(Dictionary<,>) && args[0] == typeof(string)) + return MakeInstOf(typeof(DictionaryConverter<>).MakeGenericType(args[1])); + else if (generic == typeof(IDictionary<,>) && args[0] == typeof(string)) + return MakeInstOf(typeof(IDictionaryConverter<>).MakeGenericType(args[1])); +#if NET4 + else if (generic == typeof(ISet<>)) + return MakeInstOf(typeof(ISetConverter<>).MakeGenericType(args)); + else if (generic == typeof(IReadOnlyDictionary<,>) && args[0] == typeof(string)) + return MakeInstOf(typeof(IReadOnlyDictionaryConverter<>).MakeGenericType(args[1])); +#endif + } + var iCollBase = t.GetInterfaces() + .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICollection<>)); + if (iCollBase != null && t.GetConstructor(Type.EmptyTypes) != null) + { // if it implements ICollection and has a default constructor + var valueType = iCollBase.GetGenericArguments().First(); + return MakeInstOf(typeof(CollectionConverter<,>).MakeGenericType(valueType, t)); + } + if (!allowValuesAndCustoms) return null; + if (t.IsValueType) + { // we have to do this garbo to make it accept the thing that we know is a value type at instantiation time + if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)) + { // this is a Nullable + //Logger.log.Debug($"gives NullableConverter<{Nullable.GetUnderlyingType(t)}>"); + return MakeInstOf(typeof(NullableConverter<>).MakeGenericType(Nullable.GetUnderlyingType(t))); + } + + //Logger.log.Debug($"gives converter for value type {t}"); + var valConv = Activator.CreateInstance(typeof(Converter.ValConv<>).MakeGenericType(t)) as Converter.IValConv; + return valConv.Get(); + } + + //Logger.log.Debug($"gives CustomObjectConverter<{t}>"); + return MakeInstOf(typeof(CustomObjectConverter<>).MakeGenericType(t)); } } diff --git a/IPA.Loader/Config/Stores/GeneratedStoreImpl/ObjectStructure.cs b/IPA.Loader/Config/Stores/GeneratedStoreImpl/ObjectStructure.cs index 2ddefa3b..9467ee7c 100644 --- a/IPA.Loader/Config/Stores/GeneratedStoreImpl/ObjectStructure.cs +++ b/IPA.Loader/Config/Stores/GeneratedStoreImpl/ObjectStructure.cs @@ -1,5 +1,4 @@ using IPA.Config.Stores.Attributes; -using IPA.Config.Stores.Converters; using IPA.Logging; using IPA.Utilities; using System; @@ -68,11 +67,6 @@ namespace IPA.Config.Stores member.HasConverter = false; var converterAttr = attrs.Select(o => o as UseConverterAttribute).NonNull().FirstOrDefault(); - if (converterAttr == null) - { - var defaultType = Converter.GetDefaultConverterType(member.Type, false); - if (defaultType != null) converterAttr = new UseConverterAttribute(defaultType); - } if (converterAttr != null) { member.Converter = converterAttr.ConverterType;