Browse Source

Integrated the new loader into the load order

pull/1/head
Anairkoen Schno 5 years ago
parent
commit
ad4cbe9a4a
4 changed files with 27 additions and 81 deletions
  1. +2
    -1
      IPA.Loader/Loader/PluginComponent.cs
  2. +11
    -3
      IPA.Loader/Loader/PluginLoader.cs
  3. +11
    -74
      IPA.Loader/Loader/PluginManager.cs
  4. +3
    -3
      IPA.Loader/Updating/ModSaber/Updater.cs

+ 2
- 1
IPA.Loader/Loader/PluginComponent.cs View File

@ -1,5 +1,6 @@
using IPA.Loader.Composite; using IPA.Loader.Composite;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
// ReSharper disable UnusedMember.Local // ReSharper disable UnusedMember.Local
@ -22,7 +23,7 @@ namespace IPA.Loader
{ {
DontDestroyOnLoad(gameObject); DontDestroyOnLoad(gameObject);
bsPlugins = new CompositeBSPlugin(PluginManager.BSPlugins);
bsPlugins = new CompositeBSPlugin(PluginManager.BSPlugins.Where(p => p != null));
#pragma warning disable 618 #pragma warning disable 618
ipaPlugins = new CompositeIPAPlugin(PluginManager.Plugins); ipaPlugins = new CompositeIPAPlugin(PluginManager.Plugins);
#pragma warning restore 618 #pragma warning restore 618


+ 11
- 3
IPA.Loader/Loader/PluginLoader.cs View File

@ -29,6 +29,7 @@ namespace IPA.Loader
/// </summary> /// </summary>
public class PluginMetadata 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 // ReSharper disable once UnusedAutoPropertyAccessor.Global
/// <summary> /// <summary>
/// The assembly the plugin was loaded from. /// The assembly the plugin was loaded from.
@ -84,7 +85,6 @@ namespace IPA.Loader
public class PluginInfo public class PluginInfo
{ {
internal IBeatSaberPlugin Plugin { get; set; } internal IBeatSaberPlugin Plugin { get; set; }
internal string Filename { get; set; }
/// <summary> /// <summary>
/// Metadata for the plugin. /// Metadata for the plugin.
/// </summary> /// </summary>
@ -292,13 +292,22 @@ namespace IPA.Loader
PluginsMetadata = metadata; PluginsMetadata = metadata;
} }
internal static void LoadPlugins()
internal static List<PluginInfo> LoadPlugins()
{ {
var list = PluginsMetadata.Select(LoadPlugin).Where(p => p != null).ToList();
return list;
} }
internal static PluginInfo LoadPlugin(PluginMetadata meta) internal static PluginInfo LoadPlugin(PluginMetadata meta)
{ {
if (meta.PluginType == null)
return new PluginInfo()
{
Metadata = meta,
Plugin = null
};
var info = new PluginInfo(); var info = new PluginInfo();
try try
@ -310,7 +319,6 @@ namespace IPA.Loader
var instance = (IBeatSaberPlugin)Activator.CreateInstance(type); var instance = (IBeatSaberPlugin)Activator.CreateInstance(type);
info.Metadata = meta; info.Metadata = meta;
info.Filename = meta.File.FullName;
info.Plugin = instance; info.Plugin = instance;
{ {


+ 11
- 74
IPA.Loader/Loader/PluginManager.cs View File

@ -7,10 +7,7 @@ using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using IPA.Config; using IPA.Config;
using IPA.Config.ConfigProviders;
using IPA.Logging;
using IPA.Old; using IPA.Old;
using IPA.Updating;
using IPA.Utilities; using IPA.Utilities;
using Mono.Cecil; using Mono.Cecil;
using UnityEngine; using UnityEngine;
@ -62,7 +59,7 @@ namespace IPA.Loader
/// <returns>the plugin info for the requested plugin or null</returns> /// <returns>the plugin info for the requested plugin or null</returns>
public static PluginInfo GetPlugin(string name) public static PluginInfo GetPlugin(string name)
{ {
return BSMetas.FirstOrDefault(p => p.Plugin.Name == name);
return BSMetas.FirstOrDefault(p => p.Metadata.Name == name);
} }
/// <summary> /// <summary>
@ -168,7 +165,7 @@ namespace IPA.Loader
#endregion #endregion
} }
var selfPlugin = new PluginInfo
/*var selfPlugin = new PluginInfo
{ {
Filename = Path.Combine(BeatSaber.InstallPath, "IPA.exe"), Filename = Path.Combine(BeatSaber.InstallPath, "IPA.exe"),
Plugin = SelfPlugin.Instance Plugin = SelfPlugin.Instance
@ -184,16 +181,16 @@ namespace IPA.Loader
}; };
selfPlugin.Metadata.File = new FileInfo(Path.Combine(BeatSaber.InstallPath, "IPA.exe")); selfPlugin.Metadata.File = new FileInfo(Path.Combine(BeatSaber.InstallPath, "IPA.exe"));
_bsPlugins.Add(selfPlugin);
_bsPlugins.Add(selfPlugin);*/
//Load copied plugins //Load copied plugins
string[] copiedPlugins = Directory.GetFiles(cacheDir, "*.dll"); string[] copiedPlugins = Directory.GetFiles(cacheDir, "*.dll");
foreach (string s in copiedPlugins) foreach (string s in copiedPlugins)
{ {
var result = LoadPluginsFromFile(s); var result = LoadPluginsFromFile(s);
_bsPlugins.AddRange(result.Item1);
_ipaPlugins.AddRange(result.Item2); _ipaPlugins.AddRange(result.Item2);
} }
_bsPlugins.AddRange(PluginLoader.LoadPlugins());
Logger.log.Info(exeName); Logger.log.Info(exeName);
Logger.log.Info($"Running on Unity {Application.unityVersion}"); Logger.log.Info($"Running on Unity {Application.unityVersion}");
@ -203,7 +200,7 @@ namespace IPA.Loader
Logger.log.Info("-----------------------------"); Logger.log.Info("-----------------------------");
foreach (var plugin in _bsPlugins) 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("-----------------------------"); Logger.log.Info("-----------------------------");
foreach (var plugin in _ipaPlugins) foreach (var plugin in _ipaPlugins)
@ -215,11 +212,10 @@ namespace IPA.Loader
private static Tuple<IEnumerable<PluginInfo>, IEnumerable<IPlugin>> LoadPluginsFromFile(string file) private static Tuple<IEnumerable<PluginInfo>, IEnumerable<IPlugin>> LoadPluginsFromFile(string file)
{ {
List<PluginInfo> bsPlugins = new List<PluginInfo>();
List<IPlugin> ipaPlugins = new List<IPlugin>(); List<IPlugin> ipaPlugins = new List<IPlugin>();
if (!File.Exists(file) || !file.EndsWith(".dll", true, null)) if (!File.Exists(file) || !file.EndsWith(".dll", true, null))
return new Tuple<IEnumerable<PluginInfo>, IEnumerable<IPlugin>>(bsPlugins, ipaPlugins);
return new Tuple<IEnumerable<PluginInfo>, IEnumerable<IPlugin>>(null, ipaPlugins);
T OptionalGetPlugin<T>(Type t) where T : class T OptionalGetPlugin<T>(Type t) where T : class
{ {
@ -250,74 +246,15 @@ namespace IPA.Loader
try try
{ {
Assembly assembly = Assembly.LoadFrom(file); Assembly assembly = Assembly.LoadFrom(file);
foreach (Type t in assembly.GetTypes()) foreach (Type t in assembly.GetTypes())
{ {
IBeatSaberPlugin bsPlugin = OptionalGetPlugin<IBeatSaberPlugin>(t);
if (bsPlugin != null)
{
try
{
var init = t.GetMethod("Init", BindingFlags.Instance | BindingFlags.Public);
if (init != null)
{
var initArgs = new List<object>();
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<IConfigProvider, Ref<DateTime>>(cfgProvider, new Ref<DateTime>(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<IPlugin>(t);
if (ipaPlugin != null)
{ {
IPlugin ipaPlugin = OptionalGetPlugin<IPlugin>(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}"); Logger.loader.Error($"Could not load {Path.GetFileName(file)}! {e}");
} }
return new Tuple<IEnumerable<PluginInfo>, IEnumerable<IPlugin>>(bsPlugins, ipaPlugins);
return new Tuple<IEnumerable<PluginInfo>, IEnumerable<IPlugin>>(null, ipaPlugins);
} }
internal class AppInfo internal class AppInfo


+ 3
- 3
IPA.Loader/Updating/ModSaber/Updater.cs View File

@ -511,7 +511,7 @@ namespace IPA.Updating.ModSaber
FileInfo targetFile = new FileInfo(Path.Combine(targetDir, entry.FileName)); FileInfo targetFile = new FileInfo(Path.Combine(targetDir, entry.FileName));
Directory.CreateDirectory(targetFile.DirectoryName ?? throw new InvalidOperationException()); 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 shouldDeleteOldFile = false; // overwriting old file, no need to delete
/*if (targetFile.Exists) /*if (targetFile.Exists)
@ -530,7 +530,7 @@ namespace IPA.Updating.ModSaber
} }
if (shouldDeleteOldFile && item.LocalPluginMeta != null) 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) catch (Exception)
{ // something failed; restore { // 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)); if (File.Exists(Path.Combine(BeatSaber.InstallPath, SpecialDeletionsFile))) File.Delete(Path.Combine(BeatSaber.InstallPath, SpecialDeletionsFile));
Process.Start(new ProcessStartInfo Process.Start(new ProcessStartInfo
{ {
FileName = item.LocalPluginMeta.Filename,
FileName = item.LocalPluginMeta?.Metadata.File.FullName,
Arguments = $"-nw={Process.GetCurrentProcess().Id}", Arguments = $"-nw={Process.GetCurrentProcess().Id}",
UseShellExecute = false UseShellExecute = false
}); });


Loading…
Cancel
Save