diff --git a/IPA.Injector/Injector.cs b/IPA.Injector/Injector.cs index 1ea7e058..26542ea9 100644 --- a/IPA.Injector/Injector.cs +++ b/IPA.Injector/Injector.cs @@ -1,4 +1,6 @@ -using IPA.Config; +#nullable enable +using IPA.AntiMalware; +using IPA.Config; using IPA.Injector.Backups; using IPA.Loader; using IPA.Logging; @@ -29,8 +31,8 @@ namespace IPA.Injector // ReSharper disable once UnusedMember.Global internal static class Injector { - private static Task pluginAsyncLoadTask; - private static Task permissionFixTask; + private static Task? pluginAsyncLoadTask; + private static Task? permissionFixTask; //private static string otherNewtonsoftJson = null; // ReSharper disable once UnusedParameter.Global @@ -40,7 +42,7 @@ namespace IPA.Injector // and since this class doesn't have any static fields that // aren't defined in mscorlib, we can control exactly what // gets loaded. - + _ = args; try { if (Environment.GetCommandLineArgs().Contains("--verbose")) @@ -48,16 +50,6 @@ namespace IPA.Injector SetupLibraryLoading(); - /*var otherNewtonsoft = Path.Combine( - Directory.EnumerateDirectories(Environment.CurrentDirectory, "*_Data").First(), - "Managed", - "Newtonsoft.Json.dll"); - if (File.Exists(otherNewtonsoft)) - { // this game ships its own Newtonsoft; force load ours and flag loading theirs - LibLoader.LoadLibrary(new AssemblyName("Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed")); - otherNewtonsoftJson = otherNewtonsoft; - }*/ - EnsureDirectories(); // this is weird, but it prevents Mono from having issues loading the type. @@ -93,6 +85,9 @@ namespace IPA.Injector GameVersionEarly.Load(); + // load the anti-malware engine + _ = AntiMalwareEngine.Engine; + Updates.InstallPendingUpdates(); LibLoader.SetupAssemblyFilenames(true); @@ -110,9 +105,9 @@ namespace IPA.Injector { string path; if (!Directory.Exists(path = Path.Combine(Environment.CurrentDirectory, "UserData"))) - Directory.CreateDirectory(path); + _ = Directory.CreateDirectory(path); if (!Directory.Exists(path = Path.Combine(Environment.CurrentDirectory, "Plugins"))) - Directory.CreateDirectory(path); + _ = Directory.CreateDirectory(path); } private static void SetupLibraryLoading() @@ -184,7 +179,7 @@ namespace IPA.Injector goto endPatchCoreModule; } - MethodDefinition cctor = null; + MethodDefinition? cctor = null; foreach (var m in application.Methods) if (m.IsRuntimeSpecialName && m.Name == ".cctor") cctor = m; @@ -311,9 +306,6 @@ namespace IPA.Injector if (bootstrapped) return; bootstrapped = true; - /*if (otherNewtonsoftJson != null) - Assembly.LoadFrom(otherNewtonsoftJson);*/ - Application.logMessageReceived += delegate (string condition, string stackTrace, LogType type) { @@ -336,12 +328,12 @@ namespace IPA.Injector private static void Bootstrapper_Destroyed() { // wait for plugins to finish loading - pluginAsyncLoadTask.Wait(); - permissionFixTask.Wait(); + pluginAsyncLoadTask?.Wait(); + permissionFixTask?.Wait(); log.Debug("Plugins loaded"); log.Debug(string.Join(", ", PluginLoader.PluginsMetadata.StrJP())); - PluginComponent.Create(); + _ = PluginComponent.Create(); } } } \ No newline at end of file diff --git a/IPA.Loader/AntiMalware/AntiMalwareEngine.cs b/IPA.Loader/AntiMalware/AntiMalwareEngine.cs index c3cedc6a..501aa0d3 100644 --- a/IPA.Loader/AntiMalware/AntiMalwareEngine.cs +++ b/IPA.Loader/AntiMalware/AntiMalwareEngine.cs @@ -1,10 +1,6 @@ #nullable enable +using IPA.Config; using IPA.Logging; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace IPA.AntiMalware { @@ -23,10 +19,13 @@ namespace IPA.AntiMalware private static IAntiMalware InitializeEngine() { IAntiMalware? engine = null; + if (SelfConfig.AntiMalware_.UseIfAvailable_) + { #if !NET35 - engine = WindowsCOMAntiMalware.TryInitialize(); + engine = WindowsCOMAntiMalware.TryInitialize(); #endif - engine ??= WindowsWin32AntiMalware.TryInitialize(); + engine ??= WindowsWin32AntiMalware.TryInitialize(); + } engine ??= new NoopAntiMalware(); Logger.AntiMalware.Debug($"Antimalware engine initialized with {engine.GetType()}"); diff --git a/IPA.Loader/Config/SelfConfig.cs b/IPA.Loader/Config/SelfConfig.cs index 8bd610de..0434961f 100644 --- a/IPA.Loader/Config/SelfConfig.cs +++ b/IPA.Loader/Config/SelfConfig.cs @@ -79,6 +79,9 @@ namespace IPA.Config // Debug.CondenseModLogs internal static SelfConfig CommandLineValues = new(); + // For readability's sake, I want the default values to be visible in source. +#pragma warning disable CA1805 // Do not initialize unnecessarily + // END: section ignore public virtual bool Regenerate { get; set; } = true; @@ -149,7 +152,22 @@ namespace IPA.Config // LINE: ignore [NonNullable] - public virtual Debug_ Debug { get; set; } = new Debug_(); + public virtual Debug_ Debug { get; set; } = new(); + + public class AntiMalware_ + { + public virtual bool UseIfAvailable { get; set; } = true; + // LINE: ignore + public static bool UseIfAvailable_ => Instance?.AntiMalware?.UseIfAvailable ?? true; + + public virtual bool LoadPartialThreatPlugins { get; set; } = false; + // LINE: ignore + public static bool LoadPartialThreatPlugins_ => Instance?.AntiMalware?.LoadPartialThreatPlugins ?? true; + } + + // LINE: ignore + [NonNullable] + public virtual AntiMalware_ AntiMalware { get; set; } = new(); public virtual bool YeetMods { get; set; } = true; // LINE: ignore 2