Browse Source

Integrated the new loader into the load order

pull/46/head
Anairkoen Schno 5 years ago
parent
commit
7d7c8a319f
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 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


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

@ -29,6 +29,7 @@ namespace IPA.Loader
/// </summary>
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
/// <summary>
/// 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; }
/// <summary>
/// Metadata for the plugin.
/// </summary>
@ -292,13 +292,22 @@ namespace IPA.Loader
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)
{
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;
{


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

@ -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
/// <returns>the plugin info for the requested plugin or null</returns>
public static PluginInfo GetPlugin(string name)
{
return BSMetas.FirstOrDefault(p => p.Plugin.Name == name);
return BSMetas.FirstOrDefault(p => p.Metadata.Name == name);
}
/// <summary>
@ -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<PluginInfo>, IEnumerable<IPlugin>> LoadPluginsFromFile(string file)
{
List<PluginInfo> bsPlugins = new List<PluginInfo>();
List<IPlugin> ipaPlugins = new List<IPlugin>();
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
{
@ -250,74 +246,15 @@ namespace IPA.Loader
try
{
Assembly assembly = Assembly.LoadFrom(file);
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}");
}
return new Tuple<IEnumerable<PluginInfo>, IEnumerable<IPlugin>>(bsPlugins, ipaPlugins);
return new Tuple<IEnumerable<PluginInfo>, IEnumerable<IPlugin>>(null, ipaPlugins);
}
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));
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
});


Loading…
Cancel
Save