using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using IPA.Logging;
using IPA.Utilities;
using Newtonsoft.Json;
using Version = SemVer.Version;
namespace IPA.Loader
{
///
/// A type to manage the loading of plugins.
///
public class PluginLoader
{
///
/// The directory to load plugins from.
///
public static string PluginsDirectory => Path.Combine(BeatSaber.InstallPath, "Plugins");
internal static Task LoadTask() => Task.Run(() =>
{
LoadMetadata();
Logger.log.Debug(string.Join(", ", PluginsMetadata));
Resolve();
Logger.log.Debug(string.Join(", ", PluginsMetadata));
ComputeLoadOrder();
});
///
/// A class which describes
///
public class PluginMetadata
{
// ReSharper disable once UnusedAutoPropertyAccessor.Global
///
/// The assembly the plugin was loaded from.
///
public Assembly Assembly { get; internal set; }
///
/// The Type that is the main type for the plugin.
///
public Type PluginType { get; internal set; }
///
/// The human readable name of the plugin.
///
public string Name { get; internal set; }
///
/// The ModSaber ID of the plugin, or null if it doesn't have one.
///
public string Id { get; internal set; }
///
/// The version of the plugin.
///
public Version Version { get; internal set; }
///
/// The file the plugin was loaded from.
///
public FileInfo File { get; internal set; }
// ReSharper disable once UnusedAutoPropertyAccessor.Global
///
/// The features this plugin requests.
///
public string[] Features { get; internal set; }
private PluginManifest manifest;
internal PluginManifest Manifest
{
get => manifest;
set
{
manifest = value;
Name = value.Name;
Version = value.Version;
Id = value.Id;
Features = value.Features;
}
}
///
public override string ToString() => $"{Name}({Id}@{Version})({PluginType?.AssemblyQualifiedName}) from '{File.Name}'";
}
///
/// A container object for all the data relating to a plugin.
///
public class PluginInfo
{
internal IBeatSaberPlugin Plugin { get; set; }
internal string Filename { get; set; }
///
/// Metadata for the plugin.
///
public PluginMetadata Metadata { get; internal set; } = new PluginMetadata();
}
internal static List PluginsMetadata = new List();
internal static void LoadMetadata()
{
string[] plugins = Directory.GetFiles(PluginsDirectory, "*.dll");
try
{
var selfmeta = new PluginMetadata
{
Assembly = Assembly.ReflectionOnlyLoadFrom(Assembly.GetExecutingAssembly()
.Location), // load self as reflection only
File = new FileInfo(Path.Combine(BeatSaber.InstallPath, "IPA.exe")),
PluginType = null
};
string manifest;
using (var manifestReader =
new StreamReader(
selfmeta.Assembly.GetManifestResourceStream(typeof(PluginLoader), "manifest.json") ??
throw new InvalidOperationException()))
manifest = manifestReader.ReadToEnd();
selfmeta.Manifest = JsonConvert.DeserializeObject(manifest);
PluginsMetadata.Add(selfmeta);
}
catch (Exception e)
{
Logger.loader.Critical("Error loading own manifest");
Logger.loader.Critical(e);
}
foreach (var plugin in plugins)
{ // should probably do patching first /shrug
try
{
var metadata = new PluginMetadata();
var assembly = Assembly.ReflectionOnlyLoadFrom(plugin);
metadata.Assembly = assembly;
metadata.File = new FileInfo(plugin);
Type[] types;
try
{
types = assembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
types = e.Types;
}
foreach (var type in types)
{
if (type == null) continue;
var iInterface = type.GetInterface(nameof(IBeatSaberPlugin));
if (iInterface == null) continue;
metadata.PluginType = type;
break;
}
if (metadata.PluginType == null)
{
Logger.loader.Warn($"Could not find plugin type for {Path.GetFileName(plugin)}");
continue;
}
Stream metadataStream;
try
{
metadataStream = assembly.GetManifestResourceStream(metadata.PluginType, "manifest.json");
if (metadataStream == null)
{
Logger.loader.Error($"manifest.json not found in plugin {Path.GetFileName(plugin)}");
continue;
}
}
catch (FileNotFoundException)
{
Logger.loader.Error($"manifest.json not found in plugin {Path.GetFileName(plugin)}");
continue;
}
string manifest;
using (var manifestReader = new StreamReader(metadataStream))
manifest = manifestReader.ReadToEnd();
metadata.Manifest = JsonConvert.DeserializeObject(manifest);
PluginsMetadata.Add(metadata);
}
catch (Exception e)
{
Logger.loader.Error($"Could not load data for plugin {Path.GetFileName(plugin)}");
Logger.loader.Error(e);
}
}
}
internal static void Resolve()
{ // resolves duplicates and conflicts, etc
PluginsMetadata.Sort((a, b) => a.Version.CompareTo(b.Version));
var ids = new HashSet();
var ignore = new HashSet();
var resolved = new List(PluginsMetadata.Count);
foreach (var meta in PluginsMetadata)
{
if (meta.Id != null)
{
if (ids.Contains(meta.Id))
{
Logger.loader.Warn($"Found duplicates of {meta.Id}, using newest");
ignore.Add(meta);
continue; // because of sorted order, hightest order will always be the first one
}
bool processedLater = false;
foreach (var meta2 in PluginsMetadata)
{
if (ignore.Contains(meta2)) continue;
if (meta == meta2)
{
processedLater = true;
continue;
}
if (!meta2.Manifest.Conflicts.ContainsKey(meta.Id)) continue;
var range = meta2.Manifest.Conflicts[meta.Id];
if (!range.IsSatisfied(meta.Version)) continue;
Logger.loader.Warn($"{meta.Id}@{meta.Version} conflicts with {meta2.Name}");
if (processedLater)
{
Logger.loader.Warn($"Ignoring {meta2.Name}");
ignore.Add(meta2);
}
else
{
Logger.loader.Warn($"Ignoring {meta.Name}");
ignore.Add(meta);
break;
}
}
}
if (ignore.Contains(meta)) continue;
if (meta.Id != null) ids.Add(meta.Id);
resolved.Add(meta);
}
PluginsMetadata = resolved;
}
internal static void ComputeLoadOrder()
{
PluginsMetadata.Sort((a, b) =>
{
if (a.Id == b.Id) return 0;
if (a.Id != null)
{
if (b.Manifest.Dependencies.ContainsKey(a.Id) || b.Manifest.LoadAfter.Contains(a.Id)) return -1;
if (b.Manifest.LoadBefore.Contains(a.Id)) return 1;
}
if (b.Id != null)
{
if (a.Manifest.Dependencies.ContainsKey(b.Id) || a.Manifest.LoadAfter.Contains(b.Id)) return 1;
if (a.Manifest.LoadBefore.Contains(b.Id)) return -1;
}
return 0;
});
Logger.log.Debug(string.Join(", ", PluginsMetadata));
var metadata = new List();
var pluginsToLoad = new Dictionary();
foreach (var meta in PluginsMetadata)
{
bool load = true;
foreach (var dep in meta.Manifest.Dependencies)
{
if (pluginsToLoad.ContainsKey(dep.Key) && dep.Value.IsSatisfied(pluginsToLoad[dep.Key])) continue;
load = false;
Logger.loader.Warn($"{meta.Name} is missing dependency {dep.Key}@{dep.Value}");
}
if (load)
{
metadata.Add(meta);
if (meta.Id != null)
pluginsToLoad.Add(meta.Id, meta.Version);
}
}
PluginsMetadata = metadata;
}
internal static void LoadPlugins()
{
}
}
}