using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using IPA.Config;
using IPA.Logging;
using IPA.Utilities;
namespace IPA.Loader
{
///
/// The type that handles value injecting into a plugin's Init.
///
public static class PluginInitInjector
{
///
/// 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.
///
/// the previous return value of the function, or if never called for plugin.
/// the of the parameter being injected.
/// the for the plugin being loaded.
/// the value to inject into that parameter.
public delegate object InjectParameter(object previous, ParameterInfo param, PluginLoader.PluginMetadata meta);
///
/// Adds an injector to be used when calling future plugins' Init methods.
///
/// the type of the parameter.
/// the function to call for injection.
public static void AddInjector(Type type, InjectParameter injector)
{
injectors.Add(new Tuple(type, injector));
}
private static readonly List> injectors = new List>
{
new Tuple(typeof(Logger), (prev, param, meta) => prev ?? new StandardLogger(meta.Name)),
#pragma warning disable CS0618 // Type or member is obsolete
new Tuple(typeof(IModPrefs), (prev, param, meta) => prev ?? new ModPrefs(meta)),
#pragma warning restore CS0618 // Type or member is obsolete
new Tuple(typeof(IConfigProvider), (prev, param, meta) =>
{
if (prev != null) return prev;
var cfgProvider = Config.Config.GetProviderFor(meta.Name, param);
cfgProvider.Load();
return cfgProvider;
})
};
internal static void Inject(MethodInfo init, PluginLoader.PluginInfo info)
{
var instance = info.Plugin;
var meta = info.Metadata;
var initArgs = new List