using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using IPA.Config; using IPA.Old; using IPA.Utilities; using Mono.Cecil; using UnityEngine; using Logger = IPA.Logging.Logger; using static IPA.Loader.PluginLoader; namespace IPA.Loader { /// /// The manager class for all plugins. /// public static class PluginManager { #pragma warning disable CS0618 // Type or member is obsolete (IPlugin) /// /// An of new Beat Saber plugins /// internal static IEnumerable BSPlugins => (_bsPlugins ?? throw new InvalidOperationException()).Select(p => p.Plugin); private static List _bsPlugins; internal static IEnumerable BSMetas => _bsPlugins; /// /// Gets info about the plugin with the specified name. /// /// the name of the plugin to get (must be an exact match) /// the plugin info for the requested plugin or null public static PluginInfo GetPlugin(string name) { return BSMetas.FirstOrDefault(p => p.Metadata.Name == name); } /// /// Gets info about the plugin with the specified ModSaber name. /// /// the ModSaber name of the plugin to get (must be an exact match) /// the plugin info for the requested plugin or null [Obsolete("Old name. Use GetPluginFromId instead.")] public static PluginInfo GetPluginFromModSaberName(string name) => GetPluginFromId(name); /// /// Gets info about the plugin with the specified ID. /// /// the ModSaber name of the plugin to get (must be an exact match) /// the plugin info for the requested plugin or null public static PluginInfo GetPluginFromId(string name) { return BSMetas.FirstOrDefault(p => p.Metadata.Id == name); } /// /// Gets a disabled plugin's metadata by its name. /// /// the name of the disabled plugin to get /// the metadata for the corresponding plugin public static PluginMetadata GetDisabledPlugin(string name) => DisabledPlugins.FirstOrDefault(p => p.Name == name); /// /// Gets a disabled plugin's metadata by its ID. /// /// the ID of the disabled plugin to get /// the metadata for the corresponding plugin public static PluginMetadata GetDisabledPluginFromId(string name) => DisabledPlugins.FirstOrDefault(p => p.Id == name); /// /// Disables a plugin. /// /// the plugin to disable /// whether or not it needs a restart to enable public static bool DisablePlugin(PluginInfo plugin) { if (plugin == null) return false; DisabledConfig.Ref.Value.DisabledModIds.Add(plugin.Metadata.Id ?? plugin.Metadata.Name); DisabledConfig.Provider.Store(DisabledConfig.Ref.Value); if (plugin.Plugin is IDisablablePlugin disable) { try { disable.OnDisable(); } catch (Exception e) { Logger.loader.Error($"Error occurred trying to disable {plugin.Metadata.Name}"); Logger.loader.Error(e); } runtimeDisabled.Add(plugin); _bsPlugins.Remove(plugin); return false; } return true; } /// /// Disables a plugin. /// /// the ID, or name if the ID is null, of the plugin to disable /// whether a restart is needed to activate public static bool DisablePlugin(string pluginId) => DisablePlugin(GetPluginFromId(pluginId) ?? GetPlugin(pluginId)); /// /// Enables a plugin that had been previously disabled. /// /// the plugin to enable /// whether a restart is needed to activate public static bool EnablePlugin(PluginMetadata plugin) { if (plugin == null) return false; DisabledConfig.Ref.Value.DisabledModIds.Remove(plugin.Id ?? plugin.Name); DisabledConfig.Provider.Store(DisabledConfig.Ref.Value); var needsRestart = true; var runtimeInfo = runtimeDisabled.FirstOrDefault(p => p.Metadata == plugin); if (runtimeInfo != null && runtimeInfo.Plugin is IDisablablePlugin disable) { runtimeDisabled.Remove(runtimeInfo); try { disable.OnEnable(); } catch (Exception e) { Logger.loader.Error($"Error occurred trying to enable {plugin.Name}"); Logger.loader.Error(e); } needsRestart = false; } else { PluginLoader.DisabledPlugins.Remove(plugin); runtimeInfo = InitPlugin(plugin); } _bsPlugins.Add(runtimeInfo); return needsRestart; } /// /// Enables a plugin that had been previously disabled. /// /// the ID, or name if the ID is null, of the plugin to enable /// whether a restart is needed to activate public static bool EnablePlugin(string pluginId) => EnablePlugin(GetDisabledPluginFromId(pluginId) ?? GetDisabledPlugin(pluginId)); private static readonly List runtimeDisabled = new List(); /// /// Gets a list of disabled BSIPA plugins. /// public static IEnumerable DisabledPlugins => PluginLoader.DisabledPlugins.Concat(runtimeDisabled.Select(p => p.Metadata)); /// /// Gets a list of all BSIPA plugins. /// public static IEnumerable AllPlugins => BSMetas; /// /// An of old IPA plugins. /// [Obsolete("I mean, IPlugin shouldn't be used, so why should this? Not renaming to extend support for old plugins.")] public static IEnumerable Plugins => _ipaPlugins; private static List _ipaPlugins; internal static IConfigProvider SelfConfigProvider { get; set; } internal static void Load() { string pluginDir = BeatSaber.PluginsPath; var gameVer = BeatSaber.GameVersion; var lastVerS = SelfConfig.SelfConfigRef.Value.LastGameVersion; var lastVer = lastVerS != null ? new SemVer.Version(lastVerS) : null; if (lastVer != null && gameVer != lastVer) { var oldPluginsName = Path.Combine(BeatSaber.InstallPath, $"{lastVer} Plugins"); var newPluginsName = Path.Combine(BeatSaber.InstallPath, $"{gameVer} Plugins"); ReleaseAll(); if (Directory.Exists(oldPluginsName)) Directory.Delete(oldPluginsName, true); Directory.Move(pluginDir, oldPluginsName); if (Directory.Exists(newPluginsName)) Directory.Move(newPluginsName, pluginDir); else Directory.CreateDirectory(pluginDir); LoadTask().Wait(); } SelfConfig.SelfConfigRef.Value.LastGameVersion = gameVer.ToString(); SelfConfig.LoaderConfig.Store(SelfConfig.SelfConfigRef.Value); LoadPlugins(); } private static void LoadPlugins() { string pluginDirectory = BeatSaber.PluginsPath; // Process.GetCurrentProcess().MainModule crashes the game and Assembly.GetEntryAssembly() is NULL, // so we need to resort to P/Invoke string exeName = Path.GetFileNameWithoutExtension(AppInfo.StartupPath); _bsPlugins = new List(); _ipaPlugins = new List(); if (!Directory.Exists(pluginDirectory)) return; string cacheDir = Path.Combine(pluginDirectory, ".cache"); if (!Directory.Exists(cacheDir)) { Directory.CreateDirectory(cacheDir); } else { foreach (string plugin in Directory.GetFiles(cacheDir, "*")) { File.Delete(plugin); } } // initialize BSIPA plugins first _bsPlugins.AddRange(PluginLoader.LoadPlugins()); //Copy plugins to .cache string[] originalPlugins = Directory.GetFiles(pluginDirectory, "*.dll"); foreach (string s in originalPlugins) { if (PluginsMetadata.Select(m => m.File.FullName).Contains(s)) continue; string pluginCopy = Path.Combine(cacheDir, Path.GetFileName(s)); #region Fix assemblies for refactor var module = ModuleDefinition.ReadModule(Path.Combine(pluginDirectory, s)); foreach (var @ref in module.AssemblyReferences) { // fix assembly references if (@ref.Name == "IllusionPlugin" || @ref.Name == "IllusionInjector") { @ref.Name = "IPA.Loader"; } } foreach (var @ref in module.GetTypeReferences()) { // 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.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 = ""; if (@ref.FullName == "IllusionPlugin.Utils.ReflectionUtil") @ref.Namespace = "IPA.Utilities"; //@ref.Name = ""; if (@ref.FullName == "IllusionPlugin.Logging.Logger") @ref.Namespace = "IPA.Logging"; //@ref.Name = ""; if (@ref.FullName == "IllusionPlugin.Logging.LogPrinter") @ref.Namespace = "IPA.Logging"; //@ref.Name = ""; if (@ref.FullName == "IllusionInjector.PluginManager") @ref.Namespace = "IPA.Loader"; //@ref.Name = ""; if (@ref.FullName == "IllusionInjector.PluginComponent") @ref.Namespace = "IPA.Loader"; //@ref.Name = ""; if (@ref.FullName == "IllusionInjector.CompositeBSPlugin") @ref.Namespace = "IPA.Loader.Composite"; //@ref.Name = ""; if (@ref.FullName == "IllusionInjector.CompositeIPAPlugin") @ref.Namespace = "IPA.Loader.Composite"; //@ref.Name = ""; if (@ref.FullName == "IllusionInjector.Logging.UnityLogInterceptor") @ref.Namespace = "IPA.Logging"; //@ref.Name = ""; if (@ref.FullName == "IllusionInjector.Logging.StandardLogger") @ref.Namespace = "IPA.Logging"; //@ref.Name = ""; if (@ref.FullName == "IllusionInjector.Updating.SelfPlugin") @ref.Namespace = "IPA.Updating"; //@ref.Name = ""; 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); #endregion } //Load copied plugins string[] copiedPlugins = Directory.GetFiles(cacheDir, "*.dll"); foreach (string s in copiedPlugins) { var result = LoadPluginsFromFile(s); _ipaPlugins.AddRange(result.Item2); } Logger.log.Info(exeName); Logger.log.Info($"Running on Unity {Application.unityVersion}"); Logger.log.Info($"Game version {BeatSaber.GameVersion}"); Logger.log.Info("-----------------------------"); Logger.log.Info($"Loading plugins from {Utils.GetRelativePath(pluginDirectory, Environment.CurrentDirectory)} and found {_bsPlugins.Count + _ipaPlugins.Count}"); Logger.log.Info("-----------------------------"); foreach (var plugin in _bsPlugins) { Logger.log.Info($"{plugin.Metadata.Name} ({plugin.Metadata.Id}): {plugin.Metadata.Version}"); } Logger.log.Info("-----------------------------"); foreach (var plugin in _ipaPlugins) { Logger.log.Info($"{plugin.Name}: {plugin.Version}"); } Logger.log.Info("-----------------------------"); } private static Tuple, IEnumerable> LoadPluginsFromFile(string file) { List ipaPlugins = new List(); if (!File.Exists(file) || !file.EndsWith(".dll", true, null)) return new Tuple, IEnumerable>(null, ipaPlugins); T OptionalGetPlugin(Type t) where T : class { // use typeof() to allow for easier renaming (in an ideal world this compiles to a string, but ¯\_(ツ)_/¯) if (t.GetInterface(typeof(T).Name) != null) { try { T pluginInstance = Activator.CreateInstance(t) as T; return pluginInstance; } catch (Exception e) { Logger.loader.Error($"Could not load plugin {t.FullName} in {Path.GetFileName(file)}! {e}"); } } return null; } try { Assembly assembly = Assembly.LoadFrom(file); foreach (Type t in assembly.GetTypes()) { IPlugin ipaPlugin = OptionalGetPlugin(t); if (ipaPlugin != null) { ipaPlugins.Add(ipaPlugin); } } } catch (ReflectionTypeLoadException e) { Logger.loader.Error($"Could not load the following types from {Path.GetFileName(file)}:"); Logger.loader.Error($" {string.Join("\n ", e.LoaderExceptions?.Select(e1 => e1?.Message) ?? new string[0])}"); } catch (Exception e) { Logger.loader.Error($"Could not load {Path.GetFileName(file)}!"); Logger.loader.Error(e); } return new Tuple, IEnumerable>(null, ipaPlugins); } internal static class AppInfo { [DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = false)] private static extern int GetModuleFileName(HandleRef hModule, StringBuilder buffer, int length); private static HandleRef NullHandleRef = new HandleRef(null, IntPtr.Zero); public static string StartupPath { get { StringBuilder stringBuilder = new StringBuilder(260); GetModuleFileName(NullHandleRef, stringBuilder, stringBuilder.Capacity); return stringBuilder.ToString(); } } } #pragma warning restore CS0618 // Type or member is obsolete (IPlugin) } }