private static void UpdateDependencyBases() { DependencyBases.Clear(); DependencyBaseLatestImplementations.Clear(); foreach (var @base in DI.GetAllTypes().Where(t => t.IsAbstract || t.IsInterface)) { if (@base.IsInterface || @base.BaseType == typeof(object)) { // Interface implements an interface that implements IDependencyBase foreach (var type1 in @base.GetInterfaces(false).Where(i => i.FullName() != typeof(IDependencyBase).FullName())) { if (type1.GetInterfaces().Any(i => i.FullName() == typeof(IDependencyBase).FullName())) { if (!DependencyBases.ContainsKey(type1.FullName())) { DependencyBases.Add(type1.FullName(), new HashSet <string>()); } DependencyBases[type1.FullName()].Add(@base.FullName()); break; } } } else { if (@base.BaseType.GetInterfaces(false).Any(i => i.FullName() == typeof(IDependencyBase).FullName())) { if (!DependencyBases.ContainsKey(@base.BaseType.FullName())) { DependencyBases.Add(@base.BaseType.FullName(), new HashSet <string>()); } DependencyBases[@base.BaseType.FullName()].Add(@base.FullName()); } } } var types = DI.GetAllTypes().Where(t => t.IsClass && !t.IsAbstract).ToList(); foreach (var pair in DependencyBases) { foreach (var type in pair.Value) { var tuple = VersionUtils.GetLastImplementation(Version, types.Where(t => ReflectionUtils.ImplementsOrImplementsEquivalent(t, type, false))); if (tuple != null && tuple.Value.Type != null) { if (!DependencyBaseLatestImplementations.ContainsKey(type)) { DependencyBaseLatestImplementations.Add(type, tuple.Value.Type); } else { DependencyBaseLatestImplementations[type] = tuple.Value.Type; } } } } }
internal static object?GetImplementation(Type baseType, Type wrapperType, params object[] args) { var types = DI.GetAllTypes() .Where(t => t.IsClass && !t.IsAbstract) .Where(t => ReflectionUtils.ImplementsOrImplementsEquivalent(t, baseType)); return(GetImplementationRecursive(baseType, wrapperType, args, types)); }
internal static object?GetImplementation(Type baseType, params object[] args) { var types = DI.GetAllTypes() .Where(t => t.IsClass && !t.IsAbstract) .Where(t => ReflectionUtils.ImplementsOrImplementsEquivalent(t, baseType)); var tuple = VersionUtils.GetLastImplementation(Version, types); return(tuple != null?Activator.CreateInstance(tuple?.Type, args) : null); }
private static void UpdateDependencies() { var interfaces = DI.GetAllTypes() .Where(t => t.IsAbstract || t.IsInterface) .Where(t => t.GetInterfaces(false).Any(i => i.FullName == typeof(IDependency).FullName)); foreach (var @interface in interfaces) { var types = DI.GetAllTypes() .Where(t => t.IsClass && !t.IsAbstract) .Where(t => ReflectionUtils.ImplementsOrImplementsEquivalent(t, @interface)); var tuple = VersionUtils.GetLastImplementation(Version, types); if (tuple != null && tuple.Value.Type != null) { LatestImplementations.Add(@interface, tuple.Value.Type); } } }
internal static IEnumerable <TBase> GetBaseImplementations <TBase, TWrapper>(params object[] args) where TBase : class, IDependencyBase where TWrapper : TBase, IWrapper { var baseTypes = TaleWorlds.Core.Extensions.DistinctBy(DI.GetAllTypes() .Where(t => t.IsAbstract || t.IsInterface) .Where(t => ReflectionUtils.ImplementsOrImplementsEquivalent(t, typeof(TBase), false)), t => t.FullName); foreach (var baseType in baseTypes) { if (baseType == typeof(TWrapper)) { continue; } var implementation = GetImplementation(baseType, typeof(TWrapper), args); if (implementation is TBase @base) { yield return(@base); } } }
private static void UpdateDependencies() { LatestImplementations.Clear(); var bases = DI.GetAllTypes() .Where(t => t.IsAbstract || t.IsInterface) .Where(t => t.GetInterfaces(false).Any(i => i.FullName() == typeof(IDependency).FullName()) && t.GetInterfaces(true).All(i => FullName(i) != typeof(IWrapper).FullName())) .Select(i => i.FullName()) .Distinct(); foreach (var @base in bases) { var types = DI.GetAllTypes() .Where(t => t.IsClass && !t.IsAbstract) .Where(t => ReflectionUtils.ImplementsOrImplementsEquivalent(t, @base)); var tuple = VersionUtils.GetLastImplementation(Version, types); if (tuple != null && tuple.Value.Type != null) { LatestImplementations.Add(@base, tuple.Value.Type); } } }
internal static IEnumerable <TBase> GetBaseImplementations <TBase>(params object[] args) where TBase : class, IDependencyBase { var baseTypes = TaleWorlds.Core.Extensions.DistinctBy(DI.GetAllTypes() .Where(t => t.IsAbstract || t.IsInterface) .Where(t => ReflectionUtils.ImplementsOrImplementsEquivalent(t, typeof(TBase), false)), t => t.FullName); foreach (var baseType in baseTypes) { var wrapper = baseType.Assembly.GetTypes() .Where(t => t.IsClass && !t.IsAbstract) .FirstOrDefault(t => typeof(IWrapper).IsAssignableFrom(t) && baseType.IsAssignableFrom(t)); if (wrapper == null) { continue; } var implementation = GetImplementation(baseType, wrapper, Version, args); if (implementation is TBase @base) { yield return(@base); } } }