From 5546c93f95b87967e716ddde46a0ed225bad37bd Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Sun, 22 Dec 2019 16:09:31 -0600 Subject: [PATCH] Minor changes to initialization sequence --- .../Loader/Composite/CompositeBSPlugin.cs | 63 +++++++++---------- IPA.Loader/Loader/PluginComponent.cs | 27 +++++--- IPA.Loader/Loader/PluginLoader.cs | 5 +- IPA.Loader/Loader/PluginManager.cs | 17 +++-- IPA.Loader/PluginInterfaces/IPA/IPlugin.cs | 4 +- IPA.Loader/Updating/BeatMods/ApiEndpoint.cs | 2 +- IPA.Loader/Updating/BeatMods/Updater.cs | 2 +- Net3-Proxy/CompilerServices.cs | 14 +++++ Net3-Proxy/Net3-Proxy.csproj | 1 + 9 files changed, 77 insertions(+), 58 deletions(-) create mode 100644 Net3-Proxy/CompilerServices.cs diff --git a/IPA.Loader/Loader/Composite/CompositeBSPlugin.cs b/IPA.Loader/Loader/Composite/CompositeBSPlugin.cs index fc25db70..40399252 100644 --- a/IPA.Loader/Loader/Composite/CompositeBSPlugin.cs +++ b/IPA.Loader/Loader/Composite/CompositeBSPlugin.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Runtime.CompilerServices; using UnityEngine.SceneManagement; using Logger = IPA.Logging.Logger; @@ -11,59 +12,57 @@ namespace IPA.Loader.Composite private delegate void CompositeCall(PluginLoader.PluginInfo plugin); - public CompositeBSPlugin(IEnumerable plugins) { + public CompositeBSPlugin(IEnumerable plugins) + { this.plugins = plugins; } - - public void OnApplicationQuit() { - Invoke(plugin => plugin.Plugin.OnApplicationQuit()); - } - - public void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode) { - Invoke(plugin => plugin.Plugin.OnSceneLoaded(scene, sceneMode)); - } - - public void OnSceneUnloaded(Scene scene) { - Invoke(plugin => plugin.Plugin.OnSceneUnloaded(scene)); - } - - public void OnActiveSceneChanged(Scene prevScene, Scene nextScene) { - Invoke(plugin => plugin.Plugin.OnActiveSceneChanged(prevScene, nextScene)); - } - - private void Invoke(CompositeCall callback) { - foreach (var plugin in plugins) { - try { + private void Invoke(CompositeCall callback, [CallerMemberName] string method = "") + { + foreach (var plugin in plugins) + { + try + { if (plugin.Plugin != null) callback(plugin); } - catch (Exception ex) { - Logger.log.Error($"{plugin.Metadata.Name}: {ex}"); + catch (Exception ex) + { + Logger.log.Error($"{plugin.Metadata.Name} {method}: {ex}"); } } } + + public void OnEnable() + => Invoke(plugin => plugin.Plugin.OnEnable()); + + public void OnApplicationQuit() + => Invoke(plugin => plugin.Plugin.OnApplicationQuit()); + + public void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode) + => Invoke(plugin => plugin.Plugin.OnSceneLoaded(scene, sceneMode)); + + public void OnSceneUnloaded(Scene scene) + => Invoke(plugin => plugin.Plugin.OnSceneUnloaded(scene)); + + public void OnActiveSceneChanged(Scene prevScene, Scene nextScene) + => Invoke(plugin => plugin.Plugin.OnActiveSceneChanged(prevScene, nextScene)); public void OnUpdate() - { - Invoke(plugin => { + => Invoke(plugin => { if (plugin.Plugin is IEnhancedPlugin saberPlugin) saberPlugin.OnUpdate(); }); - } public void OnFixedUpdate() - { - Invoke(plugin => { + => Invoke(plugin => { if (plugin.Plugin is IEnhancedPlugin saberPlugin) saberPlugin.OnFixedUpdate(); }); - } - public void OnLateUpdate() { - Invoke(plugin => { + public void OnLateUpdate() + => Invoke(plugin => { if (plugin.Plugin is IEnhancedPlugin saberPlugin) saberPlugin.OnLateUpdate(); }); - } } } \ No newline at end of file diff --git a/IPA.Loader/Loader/PluginComponent.cs b/IPA.Loader/Loader/PluginComponent.cs index ea66eccd..76eccfd2 100644 --- a/IPA.Loader/Loader/PluginComponent.cs +++ b/IPA.Loader/Loader/PluginComponent.cs @@ -13,6 +13,7 @@ namespace IPA.Loader private CompositeBSPlugin bsPlugins; private CompositeIPAPlugin ipaPlugins; private bool quitting; + private static bool initialized = false; internal static PluginComponent Create() { @@ -23,22 +24,28 @@ namespace IPA.Loader { DontDestroyOnLoad(gameObject); - PluginManager.Load(); + if (!initialized) + { + PluginManager.Load(); - bsPlugins = new CompositeBSPlugin(PluginManager.BSMetas); + bsPlugins = new CompositeBSPlugin(PluginManager.BSMetas); #pragma warning disable 618 - ipaPlugins = new CompositeIPAPlugin(PluginManager.Plugins); + ipaPlugins = new CompositeIPAPlugin(PluginManager.Plugins); #pragma warning restore 618 -#if NET4 - gameObject.AddComponent(); +#if BeatSaber + gameObject.AddComponent(); #endif - ipaPlugins.OnApplicationStart(); - - SceneManager.activeSceneChanged += OnActiveSceneChanged; - SceneManager.sceneLoaded += OnSceneLoaded; - SceneManager.sceneUnloaded += OnSceneUnloaded; + bsPlugins.OnEnable(); + ipaPlugins.OnApplicationStart(); + + SceneManager.activeSceneChanged += OnActiveSceneChanged; + SceneManager.sceneLoaded += OnSceneLoaded; + SceneManager.sceneUnloaded += OnSceneUnloaded; + + initialized = true; + } } void Update() diff --git a/IPA.Loader/Loader/PluginLoader.cs b/IPA.Loader/Loader/PluginLoader.cs index 91555a92..d60f3bf4 100644 --- a/IPA.Loader/Loader/PluginLoader.cs +++ b/IPA.Loader/Loader/PluginLoader.cs @@ -360,7 +360,6 @@ namespace IPA.Loader } // keep track of these for the updater; it should still be able to update mods not loaded - // TODO: add ignore reason // the thing -> the reason internal static Dictionary ignoredPlugins = new Dictionary(); @@ -744,7 +743,7 @@ namespace IPA.Loader Logger.loader.Critical($"Feature errored in {nameof(Feature.AfterInit)}: {e}"); } - try // TODO: move this out to after all plugins have been inited + /*try // TODO: move this out to after all plugins have been inited { instance.OnEnable(); } @@ -753,7 +752,7 @@ namespace IPA.Loader Logger.loader.Error($"Error occurred trying to enable {meta.Name}"); Logger.loader.Error(e); return null; // is enable failure a full load failure? - } + }*/ } catch (AmbiguousMatchException) { diff --git a/IPA.Loader/Loader/PluginManager.cs b/IPA.Loader/Loader/PluginManager.cs index dfa8c94a..75fd68ce 100644 --- a/IPA.Loader/Loader/PluginManager.cs +++ b/IPA.Loader/Loader/PluginManager.cs @@ -352,9 +352,8 @@ namespace IPA.Loader { // fix type references if (@ref.FullName == "IllusionPlugin.IPlugin") @ref.Namespace = "IPA.Old"; //@ref.Name = ""; if (@ref.FullName == "IllusionPlugin.IEnhancedPlugin") @ref.Namespace = "IPA.Old"; //@ref.Name = ""; - if (@ref.FullName == "IllusionPlugin.IBeatSaberPlugin") @ref.Namespace = "IPA"; //@ref.Name = ""; - if (@ref.FullName == "IllusionPlugin.IEnhancedBeatSaberPlugin") @ref.Namespace = "IPA"; //@ref.Name = ""; - if (@ref.FullName == "IllusionPlugin.BeatSaber.ModsaberModInfo") @ref.Namespace = "IPA"; //@ref.Name = ""; + if (@ref.FullName == "IllusionPlugin.IBeatSaberPlugin") { @ref.Namespace = "IPA"; @ref.Name = nameof(IPlugin); } + if (@ref.FullName == "IllusionPlugin.IEnhancedBeatSaberPlugin") { @ref.Namespace = "IPA"; @ref.Name = nameof(IEnhancedPlugin); } if (@ref.FullName == "IllusionPlugin.IniFile") @ref.Namespace = "IPA.Config"; //@ref.Name = ""; if (@ref.FullName == "IllusionPlugin.IModPrefs") @ref.Namespace = "IPA.Config"; //@ref.Name = ""; if (@ref.FullName == "IllusionPlugin.ModPrefs") @ref.Namespace = "IPA.Config"; //@ref.Name = ""; @@ -371,7 +370,6 @@ namespace IPA.Loader if (@ref.FullName == "IllusionInjector.Updating.Backup.BackupUnit") @ref.Namespace = "IPA.Updating.Backup"; //@ref.Name = ""; if (@ref.Namespace == "IllusionInjector.Utilities") @ref.Namespace = "IPA.Utilities"; //@ref.Name = ""; if (@ref.Namespace == "IllusionInjector.Logging.Printers") @ref.Namespace = "IPA.Logging.Printers"; //@ref.Name = ""; - if (@ref.Namespace == "IllusionInjector.Updating.ModsaberML") @ref.Namespace = "IPA.Updating.ModSaber"; //@ref.Name = ""; } module.Write(pluginCopy); @@ -383,9 +381,10 @@ namespace IPA.Loader foreach (string s in copiedPlugins) { var result = LoadPluginsFromFile(s); - _ipaPlugins.AddRange(result.Item2); + if (result == null) continue; + _ipaPlugins.AddRange(result.NonNull()); } - + Logger.log.Info(exeName); Logger.log.Info($"Running on Unity {Application.unityVersion}"); Logger.log.Info($"Game version {BeatSaber.GameVersion}"); @@ -404,12 +403,12 @@ namespace IPA.Loader Logger.log.Info("-----------------------------"); } - private static Tuple, IEnumerable> LoadPluginsFromFile(string file) + private static IEnumerable LoadPluginsFromFile(string file) { var ipaPlugins = new List(); if (!File.Exists(file) || !file.EndsWith(".dll", true, null)) - return new Tuple, IEnumerable>(null, ipaPlugins); + return ipaPlugins; T OptionalGetPlugin(Type t) where T : class { @@ -456,7 +455,7 @@ namespace IPA.Loader Logger.loader.Error(e); } - return new Tuple, IEnumerable>(null, ipaPlugins); + return ipaPlugins; } internal static class AppInfo diff --git a/IPA.Loader/PluginInterfaces/IPA/IPlugin.cs b/IPA.Loader/PluginInterfaces/IPA/IPlugin.cs index 325d667f..f6c3083a 100644 --- a/IPA.Loader/PluginInterfaces/IPA/IPlugin.cs +++ b/IPA.Loader/PluginInterfaces/IPA/IPlugin.cs @@ -4,8 +4,8 @@ namespace IPA.Old { /// - /// Interface for generic Illusion unity plugins. Every class that implements this will be loaded if the DLL is placed at - /// data/Managed/Plugins. + /// Interface for generic Illusion unity plugins. Every class that implements this will be loaded if the DLL is placed in + /// Plugins. /// [Obsolete("When building plugins for Beat Saber, use IBeatSaberPlugin")] public interface IPlugin diff --git a/IPA.Loader/Updating/BeatMods/ApiEndpoint.cs b/IPA.Loader/Updating/BeatMods/ApiEndpoint.cs index 8062dbd8..e8a5a690 100644 --- a/IPA.Loader/Updating/BeatMods/ApiEndpoint.cs +++ b/IPA.Loader/Updating/BeatMods/ApiEndpoint.cs @@ -10,7 +10,7 @@ using Version = SemVer.Version; namespace IPA.Updating.BeatMods { -#if NET4 +#if BeatSaber class ApiEndpoint { public const string BeatModBase = "https://beatmods.com"; diff --git a/IPA.Loader/Updating/BeatMods/Updater.cs b/IPA.Loader/Updating/BeatMods/Updater.cs index ad6bf761..d09f3082 100644 --- a/IPA.Loader/Updating/BeatMods/Updater.cs +++ b/IPA.Loader/Updating/BeatMods/Updater.cs @@ -30,7 +30,7 @@ namespace IPA.Updating.BeatMods internal const string SpecialDeletionsFile = "$$delete"; } -#if NET4 +#if BeatSaber [SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")] internal partial class Updater : MonoBehaviour { diff --git a/Net3-Proxy/CompilerServices.cs b/Net3-Proxy/CompilerServices.cs new file mode 100644 index 00000000..789ab55f --- /dev/null +++ b/Net3-Proxy/CompilerServices.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace System.Runtime.CompilerServices +{ + + [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)] + public sealed class CallerMemberNameAttribute : Attribute + { + } + +} diff --git a/Net3-Proxy/Net3-Proxy.csproj b/Net3-Proxy/Net3-Proxy.csproj index f8753e7a..ee06d43e 100644 --- a/Net3-Proxy/Net3-Proxy.csproj +++ b/Net3-Proxy/Net3-Proxy.csproj @@ -40,6 +40,7 @@ +