From ad4cbe9a4a2d8a53a3b9f00a35b291ae97d80e9b Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Sun, 6 Jan 2019 20:31:22 -0600 Subject: [PATCH] Integrated the new loader into the load order --- IPA.Loader/Loader/PluginComponent.cs | 3 +- IPA.Loader/Loader/PluginLoader.cs | 14 +++- IPA.Loader/Loader/PluginManager.cs | 85 ++++--------------------- IPA.Loader/Updating/ModSaber/Updater.cs | 6 +- 4 files changed, 27 insertions(+), 81 deletions(-) diff --git a/IPA.Loader/Loader/PluginComponent.cs b/IPA.Loader/Loader/PluginComponent.cs index 2ff13378..60f45c71 100644 --- a/IPA.Loader/Loader/PluginComponent.cs +++ b/IPA.Loader/Loader/PluginComponent.cs @@ -1,5 +1,6 @@ using IPA.Loader.Composite; using System.Diagnostics.CodeAnalysis; +using System.Linq; using UnityEngine; using UnityEngine.SceneManagement; // ReSharper disable UnusedMember.Local @@ -22,7 +23,7 @@ namespace IPA.Loader { DontDestroyOnLoad(gameObject); - bsPlugins = new CompositeBSPlugin(PluginManager.BSPlugins); + bsPlugins = new CompositeBSPlugin(PluginManager.BSPlugins.Where(p => p != null)); #pragma warning disable 618 ipaPlugins = new CompositeIPAPlugin(PluginManager.Plugins); #pragma warning restore 618 diff --git a/IPA.Loader/Loader/PluginLoader.cs b/IPA.Loader/Loader/PluginLoader.cs index c79f2496..e4d96cc5 100644 --- a/IPA.Loader/Loader/PluginLoader.cs +++ b/IPA.Loader/Loader/PluginLoader.cs @@ -29,6 +29,7 @@ namespace IPA.Loader /// public class PluginMetadata { + //TODO: rework this to load using Mono.Cecil to prevent multiples of each module being loaded into memory // ReSharper disable once UnusedAutoPropertyAccessor.Global /// /// The assembly the plugin was loaded from. @@ -84,7 +85,6 @@ namespace IPA.Loader public class PluginInfo { internal IBeatSaberPlugin Plugin { get; set; } - internal string Filename { get; set; } /// /// Metadata for the plugin. /// @@ -292,13 +292,22 @@ namespace IPA.Loader PluginsMetadata = metadata; } - internal static void LoadPlugins() + internal static List LoadPlugins() { + var list = PluginsMetadata.Select(LoadPlugin).Where(p => p != null).ToList(); + return list; } internal static PluginInfo LoadPlugin(PluginMetadata meta) { + if (meta.PluginType == null) + return new PluginInfo() + { + Metadata = meta, + Plugin = null + }; + var info = new PluginInfo(); try @@ -310,7 +319,6 @@ namespace IPA.Loader var instance = (IBeatSaberPlugin)Activator.CreateInstance(type); info.Metadata = meta; - info.Filename = meta.File.FullName; info.Plugin = instance; { diff --git a/IPA.Loader/Loader/PluginManager.cs b/IPA.Loader/Loader/PluginManager.cs index 6d235e09..0dce9e15 100644 --- a/IPA.Loader/Loader/PluginManager.cs +++ b/IPA.Loader/Loader/PluginManager.cs @@ -7,10 +7,7 @@ using System.Reflection; using System.Runtime.InteropServices; using System.Text; using IPA.Config; -using IPA.Config.ConfigProviders; -using IPA.Logging; using IPA.Old; -using IPA.Updating; using IPA.Utilities; using Mono.Cecil; using UnityEngine; @@ -62,7 +59,7 @@ namespace IPA.Loader /// the plugin info for the requested plugin or null public static PluginInfo GetPlugin(string name) { - return BSMetas.FirstOrDefault(p => p.Plugin.Name == name); + return BSMetas.FirstOrDefault(p => p.Metadata.Name == name); } /// @@ -168,7 +165,7 @@ namespace IPA.Loader #endregion } - var selfPlugin = new PluginInfo + /*var selfPlugin = new PluginInfo { Filename = Path.Combine(BeatSaber.InstallPath, "IPA.exe"), Plugin = SelfPlugin.Instance @@ -184,16 +181,16 @@ namespace IPA.Loader }; selfPlugin.Metadata.File = new FileInfo(Path.Combine(BeatSaber.InstallPath, "IPA.exe")); - _bsPlugins.Add(selfPlugin); + _bsPlugins.Add(selfPlugin);*/ //Load copied plugins string[] copiedPlugins = Directory.GetFiles(cacheDir, "*.dll"); foreach (string s in copiedPlugins) { var result = LoadPluginsFromFile(s); - _bsPlugins.AddRange(result.Item1); _ipaPlugins.AddRange(result.Item2); } + _bsPlugins.AddRange(PluginLoader.LoadPlugins()); Logger.log.Info(exeName); Logger.log.Info($"Running on Unity {Application.unityVersion}"); @@ -203,7 +200,7 @@ namespace IPA.Loader Logger.log.Info("-----------------------------"); foreach (var plugin in _bsPlugins) { - Logger.log.Info($"{plugin.Plugin.Name}: {plugin.Plugin.Version}"); + Logger.log.Info($"{plugin.Metadata.Name}: {plugin.Metadata.Version}"); } Logger.log.Info("-----------------------------"); foreach (var plugin in _ipaPlugins) @@ -215,11 +212,10 @@ namespace IPA.Loader private static Tuple, IEnumerable> LoadPluginsFromFile(string file) { - List bsPlugins = new List(); List ipaPlugins = new List(); if (!File.Exists(file) || !file.EndsWith(".dll", true, null)) - return new Tuple, IEnumerable>(bsPlugins, ipaPlugins); + return new Tuple, IEnumerable>(null, ipaPlugins); T OptionalGetPlugin(Type t) where T : class { @@ -250,74 +246,15 @@ namespace IPA.Loader try { - Assembly assembly = Assembly.LoadFrom(file); foreach (Type t in assembly.GetTypes()) { - IBeatSaberPlugin bsPlugin = OptionalGetPlugin(t); - if (bsPlugin != null) - { - try - { - var init = t.GetMethod("Init", BindingFlags.Instance | BindingFlags.Public); - if (init != null) - { - var initArgs = new List(); - var initParams = init.GetParameters(); - - Logger modLogger = null; - IModPrefs modPrefs = null; - IConfigProvider cfgProvider = null; - - foreach (var param in initParams) - { - var ptype = param.ParameterType; - if (ptype.IsAssignableFrom(typeof(Logger))) { - if (modLogger == null) modLogger = new StandardLogger(bsPlugin.Name); - initArgs.Add(modLogger); - } - else if (ptype.IsAssignableFrom(typeof(IModPrefs))) - { - if (modPrefs == null) modPrefs = new ModPrefs(bsPlugin); - initArgs.Add(modPrefs); - } - else if (ptype.IsAssignableFrom(typeof(IConfigProvider))) - { - if (cfgProvider == null) - { - cfgProvider = new JsonConfigProvider { Filename = Path.Combine("UserData", $"{bsPlugin.Name}") }; - //configProviders.Add(new KeyValuePair>(cfgProvider, new Ref(cfgProvider.LastModified))); - cfgProvider.Load(); - } - initArgs.Add(cfgProvider); - } - else - initArgs.Add(ptype.GetDefault()); - } - - init.Invoke(bsPlugin, initArgs.ToArray()); - } - - bsPlugins.Add(new PluginInfo - { - Plugin = bsPlugin, - Filename = file.Replace("\\.cache", ""), // quick and dirty fix - //ModSaberInfo = bsPlugin.ModInfo - }); - } - catch (AmbiguousMatchException) - { - Logger.loader.Error("Only one Init allowed per plugin"); - } - } - else + + IPlugin ipaPlugin = OptionalGetPlugin(t); + if (ipaPlugin != null) { - IPlugin ipaPlugin = OptionalGetPlugin(t); - if (ipaPlugin != null) - { - ipaPlugins.Add(ipaPlugin); - } + ipaPlugins.Add(ipaPlugin); } } @@ -327,7 +264,7 @@ namespace IPA.Loader Logger.loader.Error($"Could not load {Path.GetFileName(file)}! {e}"); } - return new Tuple, IEnumerable>(bsPlugins, ipaPlugins); + return new Tuple, IEnumerable>(null, ipaPlugins); } internal class AppInfo diff --git a/IPA.Loader/Updating/ModSaber/Updater.cs b/IPA.Loader/Updating/ModSaber/Updater.cs index d29b4597..6a279ecf 100644 --- a/IPA.Loader/Updating/ModSaber/Updater.cs +++ b/IPA.Loader/Updating/ModSaber/Updater.cs @@ -511,7 +511,7 @@ namespace IPA.Updating.ModSaber FileInfo targetFile = new FileInfo(Path.Combine(targetDir, entry.FileName)); Directory.CreateDirectory(targetFile.DirectoryName ?? throw new InvalidOperationException()); - if (LoneFunctions.GetRelativePath(targetFile.FullName, targetDir) == LoneFunctions.GetRelativePath(item.LocalPluginMeta?.Filename, BeatSaber.InstallPath)) + if (LoneFunctions.GetRelativePath(targetFile.FullName, targetDir) == LoneFunctions.GetRelativePath(item.LocalPluginMeta?.Metadata.File.FullName, BeatSaber.InstallPath)) shouldDeleteOldFile = false; // overwriting old file, no need to delete /*if (targetFile.Exists) @@ -530,7 +530,7 @@ namespace IPA.Updating.ModSaber } if (shouldDeleteOldFile && item.LocalPluginMeta != null) - File.AppendAllLines(Path.Combine(targetDir, SpecialDeletionsFile), new[] { LoneFunctions.GetRelativePath(item.LocalPluginMeta.Filename, BeatSaber.InstallPath) }); + File.AppendAllLines(Path.Combine(targetDir, SpecialDeletionsFile), new[] { LoneFunctions.GetRelativePath(item.LocalPluginMeta?.Metadata.File.FullName, BeatSaber.InstallPath) }); } catch (Exception) { // something failed; restore @@ -549,7 +549,7 @@ namespace IPA.Updating.ModSaber if (File.Exists(Path.Combine(BeatSaber.InstallPath, SpecialDeletionsFile))) File.Delete(Path.Combine(BeatSaber.InstallPath, SpecialDeletionsFile)); Process.Start(new ProcessStartInfo { - FileName = item.LocalPluginMeta.Filename, + FileName = item.LocalPluginMeta?.Metadata.File.FullName, Arguments = $"-nw={Process.GetCurrentProcess().Id}", UseShellExecute = false });