A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 line
2.4KB

  1. using System;
  2. using IllusionInjector;
  3. using IllusionPlugin;
  4. namespace TechbloxModdingAPI.Utility
  5. {
  6. /// <summary>
  7. /// Simple plugin interaction operations
  8. /// </summary>
  9. public static class Dependency
  10. {
  11. /// <summary>
  12. /// Find a plugin by name
  13. /// </summary>
  14. /// <returns>The plugin.</returns>
  15. /// <param name="name">The plugin's name.</param>
  16. public static IPlugin GetPlugin(string name)
  17. {
  18. foreach(IPlugin plugin in PluginManager.Plugins)
  19. {
  20. if (plugin.Name == name)
  21. {
  22. return plugin;
  23. }
  24. }
  25. return null;
  26. }
  27. /// <summary>
  28. /// Gets the plugin version.
  29. /// This gives priority to the plugin's Version string but falls back to the Assembly's version
  30. /// </summary>
  31. /// <returns>The plugin's version.</returns>
  32. /// <param name="name">The plugin's name.</param>
  33. public static Version GetPluginVersion(string name)
  34. {
  35. IPlugin plugin = GetPlugin(name);
  36. if (plugin != null) {
  37. try
  38. {
  39. return new Version(plugin.Version);
  40. } catch (Exception e) when (
  41. e is ArgumentException
  42. || e is ArgumentNullException
  43. || e is ArgumentOutOfRangeException
  44. || e is FormatException
  45. || e is OverflowException) {}
  46. return plugin.GetType().Assembly.GetName().Version;
  47. }
  48. return null;
  49. }
  50. // (I'm leaving the auto-generated version)
  51. // <summary>
  52. // Hell the specified name and version.
  53. // </summary>
  54. // <returns>The hell.</returns>
  55. // <param name="name">Name.</param>
  56. // <param name="version">Version.</param>
  57. /// <summary>
  58. /// Detect if you're in dependency hell with respect to the plugin.
  59. /// ie Check if the plugin doesn't exist or is out of date.
  60. /// When version is null, this only checks if the plugin exists.
  61. /// The version is retrieved using GetPluginVersion(string name).
  62. /// </summary>
  63. /// <returns>Are you in dependency hell?</returns>
  64. /// <param name="name">The plugin's name'</param>
  65. /// <param name="version">The target version.</param>
  66. public static bool Hell(string name, Version version = null)
  67. {
  68. Version pluginVersion = GetPluginVersion(name);
  69. if (version == null) {
  70. return pluginVersion == null;
  71. }
  72. return (pluginVersion == null || pluginVersion < version);
  73. }
  74. }
  75. }