You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
5.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using IPA.Config;
  6. using IPA.Logging;
  7. using IPA.Utilities;
  8. #if NET3
  9. using Net3_Proxy;
  10. #endif
  11. namespace IPA.Loader
  12. {
  13. /// <summary>
  14. /// The type that handles value injecting into a plugin's Init.
  15. /// </summary>
  16. public static class PluginInitInjector
  17. {
  18. /// <summary>
  19. /// A typed injector for a plugin's Init method. When registered, called for all associated types. If it returns null, the default for the type will be used.
  20. /// </summary>
  21. /// <param name="previous">the previous return value of the function, or <see langword="null"/> if never called for plugin.</param>
  22. /// <param name="param">the <see cref="ParameterInfo"/> of the parameter being injected.</param>
  23. /// <param name="meta">the <see cref="PluginLoader.PluginMetadata"/> for the plugin being loaded.</param>
  24. /// <returns>the value to inject into that parameter.</returns>
  25. public delegate object InjectParameter(object previous, ParameterInfo param, PluginLoader.PluginMetadata meta);
  26. /// <summary>
  27. /// Adds an injector to be used when calling future plugins' Init methods.
  28. /// </summary>
  29. /// <param name="type">the type of the parameter.</param>
  30. /// <param name="injector">the function to call for injection.</param>
  31. public static void AddInjector(Type type, InjectParameter injector)
  32. {
  33. injectors.Add(new TypedInjector(type, injector));
  34. }
  35. private struct TypedInjector : IEquatable<TypedInjector>
  36. {
  37. public Type Type;
  38. public InjectParameter Injector;
  39. public TypedInjector(Type t, InjectParameter i)
  40. { Type = t; Injector = i; }
  41. public object Inject(object prev, ParameterInfo info, PluginLoader.PluginMetadata meta)
  42. => Injector(prev, info, meta);
  43. public bool Equals(TypedInjector other)
  44. => Type == other.Type && Injector == other.Injector;
  45. public override bool Equals(object obj)
  46. => obj is TypedInjector i && Equals(i);
  47. public override int GetHashCode()
  48. => Type.GetHashCode() ^ Injector.GetHashCode();
  49. public static bool operator ==(TypedInjector a, TypedInjector b) => a.Equals(b);
  50. public static bool operator !=(TypedInjector a, TypedInjector b) => !a.Equals(b);
  51. }
  52. private static readonly List<TypedInjector> injectors = new List<TypedInjector>
  53. {
  54. new TypedInjector(typeof(Logger), (prev, param, meta) => prev ?? new StandardLogger(meta.Name)),
  55. new TypedInjector(typeof(PluginLoader.PluginMetadata), (prev, param, meta) => prev ?? meta),
  56. new TypedInjector(typeof(IConfigProvider), (prev, param, meta) =>
  57. {
  58. if (prev != null) return prev;
  59. var cfgProvider = Config.Config.GetProviderFor(meta.Name, param);
  60. cfgProvider.Load();
  61. return cfgProvider;
  62. })
  63. };
  64. private static int? MatchPriority(Type target, Type source)
  65. {
  66. if (target == source) return int.MaxValue;
  67. if (!target.IsAssignableFrom(source)) return null;
  68. if (!target.IsInterface && !source.IsSubclassOf(target)) return int.MinValue;
  69. int value = 0;
  70. while (true)
  71. {
  72. if (source == null) return value;
  73. if (target.IsInterface && source.GetInterfaces().Contains(target))
  74. return value;
  75. else if (target == source)
  76. return value;
  77. else
  78. {
  79. value--; // lower priority
  80. source = source.BaseType;
  81. }
  82. }
  83. }
  84. internal static void Inject(MethodInfo init, PluginLoader.PluginInfo info)
  85. {
  86. var instance = info.Plugin;
  87. var meta = info.Metadata;
  88. var initArgs = new List<object>();
  89. var initParams = init.GetParameters();
  90. var previousValues = new Dictionary<TypedInjector, object>(injectors.Count);
  91. foreach (var param in initParams)
  92. {
  93. var paramType = param.ParameterType;
  94. var value = paramType.GetDefault();
  95. var toUse = injectors.Select(i => (inject: i, priority: MatchPriority(paramType, i.Type))) // check match priority, combine it
  96. .Where(t => t.priority != null) // filter null priorities
  97. .Select(t => (t.inject, priority: t.priority.Value)) // remove nullable
  98. .OrderByDescending(t => t.priority) // sort by value
  99. .Select(t => t.inject); // remove priority value
  100. // this tries injectors in order of closest match by type provided
  101. foreach (var pair in toUse)
  102. {
  103. object prev = null;
  104. if (previousValues.ContainsKey(pair))
  105. prev = previousValues[pair];
  106. var val = pair.Inject(prev, param, meta);
  107. if (previousValues.ContainsKey(pair))
  108. previousValues[pair] = val;
  109. else
  110. previousValues.Add(pair, val);
  111. if (val == null) continue;
  112. value = val;
  113. break;
  114. }
  115. initArgs.Add(value);
  116. }
  117. init.Invoke(instance, initArgs.ToArray());
  118. }
  119. }
  120. }