From c57154a970954175c981570b7ed03fa7fe2d5e52 Mon Sep 17 00:00:00 2001 From: NGnius Date: Tue, 18 Feb 2020 14:02:27 -0500 Subject: [PATCH] Add basic plugin version checking functionality --- GamecraftModdingAPI/Utility/Dependency.cs | 79 +++++++++++++++++++++-- 1 file changed, 74 insertions(+), 5 deletions(-) diff --git a/GamecraftModdingAPI/Utility/Dependency.cs b/GamecraftModdingAPI/Utility/Dependency.cs index 854dff3..c7dd64d 100644 --- a/GamecraftModdingAPI/Utility/Dependency.cs +++ b/GamecraftModdingAPI/Utility/Dependency.cs @@ -1,10 +1,79 @@ using System; -namespace GamecraftModdingAPI + +using IllusionInjector; +using IllusionPlugin; + +namespace GamecraftModdingAPI.Utility { - public class Dependency + /// + /// Simple plugin interaction operations + /// + public static class Dependency { - public Dependency() - { - } + /// + /// Find a plugin by name + /// + /// The plugin. + /// The plugin's name. + public static IPlugin GetPlugin(string name) + { + foreach(IPlugin plugin in PluginManager.Plugins) + { + if (plugin.Name == name) + { + return plugin; + } + } + return null; + } + + /// + /// Gets the plugin version. + /// This gives priority to the plugin's Version string but falls back to the Assembly's version + /// + /// The plugin's version. + /// The plugin's name. + public static Version GetPluginVersion(string name) + { + IPlugin plugin = GetPlugin(name); + if (plugin != null) { + try + { + return new Version(plugin.Version); + } catch (Exception e) when ( + e is ArgumentException + || e is ArgumentNullException + || e is ArgumentOutOfRangeException + || e is FormatException + || e is OverflowException) {} + return plugin.GetType().Assembly.GetName().Version; + } + return null; + } + + // (I'm leaving the auto-generated version) + // + // Hell the specified name and version. + // + // The hell. + // Name. + // Version. + /// + /// Detect if you're in dependency hell with respect to the plugin. + /// ie Check if the plugin doesn't exist or is out of date. + /// When version is null, this only checks if the plugin exists. + /// The version is retrieved using GetPluginVersion(string name). + /// + /// Are you in dependency hell? + /// The plugin's name' + /// The target version. + public static bool Hell(string name, Version version = null) + { + Version pluginVersion = GetPluginVersion(name); + if (version == null) { + return pluginVersion == null; + } + return (pluginVersion == null || pluginVersion < version); + } } }