From e68274b1a7c54b8755c6723662da0336719754cd Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Tue, 6 Apr 2021 19:49:37 -0500 Subject: [PATCH] Add IAntiMalware to InitInjector --- IPA.Loader/Loader/PluginInitInjector.cs | 31 ++++++++++++------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/IPA.Loader/Loader/PluginInitInjector.cs b/IPA.Loader/Loader/PluginInitInjector.cs index c4d085ab..9272239d 100644 --- a/IPA.Loader/Loader/PluginInitInjector.cs +++ b/IPA.Loader/Loader/PluginInitInjector.cs @@ -1,4 +1,5 @@ -using System; +#nullable enable +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -6,6 +7,7 @@ using IPA.Config; using IPA.Logging; using IPA.Utilities; using System.Linq.Expressions; +using IPA.AntiMalware; #if NET4 using Expression = System.Linq.Expressions.Expression; using ExpressionEx = System.Linq.Expressions.Expression; @@ -57,7 +59,7 @@ namespace IPA.Loader /// 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, PluginMetadata meta); + public delegate object? InjectParameter(object? previous, ParameterInfo param, PluginMetadata meta); /// /// Adds an injector to be used when calling future plugins' Init methods. @@ -77,7 +79,7 @@ namespace IPA.Loader public TypedInjector(Type t, InjectParameter i) { Type = t; Injector = i; } - public object Inject(object prev, ParameterInfo info, PluginMetadata meta) + public object? Inject(object? prev, ParameterInfo info, PluginMetadata meta) => Injector(prev, info, meta); public bool Equals(TypedInjector other) @@ -94,15 +96,12 @@ namespace IPA.Loader public static bool operator !=(TypedInjector a, TypedInjector b) => !a.Equals(b); } - private static readonly List injectors = new List + private static readonly List injectors = new() { new TypedInjector(typeof(Logger), (prev, param, meta) => prev ?? new StandardLogger(meta.Name)), new TypedInjector(typeof(PluginMetadata), (prev, param, meta) => prev ?? meta), - new TypedInjector(typeof(Config.Config), (prev, param, meta) => - { - if (prev != null) return prev; - return Config.Config.GetConfigFor(meta.Name, param); - }) + new TypedInjector(typeof(Config.Config), (prev, param, meta) => prev ?? Config.Config.GetConfigFor(meta.Name, param)), + new TypedInjector(typeof(IAntiMalware), (prev, param, meta) => prev ?? AntiMalwareEngine.Engine) }; private static int? MatchPriority(Type target, Type source) @@ -139,14 +138,14 @@ namespace IPA.Loader Expression.ArrayIndex(arr, Expression.Constant(i)), t)))); } - internal static object[] Inject(ParameterInfo[] initParams, PluginMetadata meta, ref object persist) + internal static object?[] Inject(ParameterInfo[] initParams, PluginMetadata meta, ref object? persist) { - var initArgs = new List(); + var initArgs = new List(); - var previousValues = persist as Dictionary; + var previousValues = persist as Dictionary; if (previousValues == null) { - previousValues = new Dictionary(injectors.Count); + previousValues = new(injectors.Count); persist = previousValues; } @@ -157,15 +156,15 @@ namespace IPA.Loader var value = paramType.GetDefault(); var toUse = injectors.Select(i => (inject: i, priority: MatchPriority(paramType, i.Type))) // check match priority, combine it - .Where(t => t.priority != null) // filter null priorities - .Select(t => (t.inject, priority: t.priority.Value)) // remove nullable + .NonNull(t => t.priority) // filter null priorities + .Select(t => (t.inject, priority: t.priority!.Value)) // remove nullable .OrderByDescending(t => t.priority) // sort by value .Select(t => t.inject); // remove priority value // this tries injectors in order of closest match by type provided foreach (var pair in toUse) { - object prev = null; + object? prev = null; if (previousValues.ContainsKey(pair)) prev = previousValues[pair];