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(); Resolve(); 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"); Assembly.ReflectionOnlyLoadFrom(Assembly.GetExecutingAssembly().Location); // load self as reflection only 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.log.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.log.Error($"manifest.json not found in plugin {Path.GetFileName(plugin)}"); continue; } } catch (FileNotFoundException) { Logger.log.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.log.Error($"Could not load data for plugin {Path.GetFileName(plugin)}"); Logger.log.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.log.Warn($"Found duplicates of {meta.Id}, using newest"); 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)) { var range = meta2.Manifest.Conflicts[meta.Id]; if (range.IsSatisfied(meta.Version)) { //TODO: actually choose the one most depended on Logger.log.Warn($"{meta.Id}@{meta.Version} conflicts with {meta2.Name}"); if (processedLater) { Logger.log.Warn($"Ignoring {meta2.Name}"); ignore.Add(meta2); } else { Logger.log.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; }); } internal static void LoadPlugins() { } } }