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.

147 lines
5.9 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. #pragma warning disable CS0618 // Type or member is obsolete
  56. new TypedInjector(typeof(IModPrefs), (prev, param, meta) => prev ?? new ModPrefs(meta)),
  57. #pragma warning restore CS0618 // Type or member is obsolete
  58. new TypedInjector(typeof(PluginLoader.PluginMetadata), (prev, param, meta) => prev ?? meta),
  59. new TypedInjector(typeof(Config.Config), (prev, param, meta) =>
  60. {
  61. if (prev != null) return prev;
  62. return Config.Config.GetConfigFor(meta.Name, param);
  63. })
  64. };
  65. private static int? MatchPriority(Type target, Type source)
  66. {
  67. if (target == source) return int.MaxValue;
  68. if (!target.IsAssignableFrom(source)) return null;
  69. if (!target.IsInterface && !source.IsSubclassOf(target)) return int.MinValue;
  70. int value = 0;
  71. while (true)
  72. {
  73. if (source == null) return value;
  74. if (target.IsInterface && source.GetInterfaces().Contains(target))
  75. return value;
  76. else if (target == source)
  77. return value;
  78. else
  79. {
  80. value--; // lower priority
  81. source = source.BaseType;
  82. }
  83. }
  84. }
  85. internal static void Inject(MethodInfo init, PluginLoader.PluginInfo info)
  86. {
  87. var instance = info.Plugin;
  88. var meta = info.Metadata;
  89. var initArgs = new List<object>();
  90. var initParams = init.GetParameters();
  91. var previousValues = new Dictionary<TypedInjector, object>(injectors.Count);
  92. foreach (var param in initParams)
  93. {
  94. var paramType = param.ParameterType;
  95. var value = paramType.GetDefault();
  96. var toUse = injectors.Select(i => (inject: i, priority: MatchPriority(paramType, i.Type))) // check match priority, combine it
  97. .Where(t => t.priority != null) // filter null priorities
  98. .Select(t => (t.inject, priority: t.priority.Value)) // remove nullable
  99. .OrderByDescending(t => t.priority) // sort by value
  100. .Select(t => t.inject); // remove priority value
  101. // this tries injectors in order of closest match by type provided
  102. foreach (var pair in toUse)
  103. {
  104. object prev = null;
  105. if (previousValues.ContainsKey(pair))
  106. prev = previousValues[pair];
  107. var val = pair.Inject(prev, param, meta);
  108. if (previousValues.ContainsKey(pair))
  109. previousValues[pair] = val;
  110. else
  111. previousValues.Add(pair, val);
  112. if (val == null) continue;
  113. value = val;
  114. break;
  115. }
  116. initArgs.Add(value);
  117. }
  118. init.Invoke(instance, initArgs.ToArray());
  119. }
  120. }
  121. }