PluginLoader.kt 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2019-2021 Mamoe Technologies and contributors.
  3. *
  4. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  5. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
  6. *
  7. * https://github.com/mamoe/mirai/blob/dev/LICENSE
  8. */
  9. @file:Suppress("unused", "INAPPLICABLE_JVM_NAME", "NOTHING_TO_INLINE")
  10. package net.mamoe.mirai.console.plugin.loader
  11. import net.mamoe.mirai.console.extensions.PluginLoaderProvider
  12. import net.mamoe.mirai.console.plugin.Plugin
  13. import net.mamoe.mirai.console.plugin.PluginManager
  14. import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.enablePlugin
  15. import net.mamoe.mirai.console.plugin.description.PluginDescription
  16. import net.mamoe.mirai.console.plugin.jvm.JvmPluginLoader
  17. /**
  18. * 插件加载器.
  19. *
  20. * 插件加载器只实现寻找插件列表, 加载插件, 启用插件, 关闭插件这四个功能.
  21. *
  22. * 一个插件要在何时被加载,依赖如何处理,[PluginLoader] 都无需关心.
  23. *
  24. * 有关插件的依赖和已加载的插件列表由 [PluginManager] 维护.
  25. *
  26. * ## 内建加载器
  27. * - [JvmPluginLoader] Jar 插件加载器
  28. *
  29. * ## 扩展加载器
  30. * 插件被允许扩展一个加载器.
  31. *
  32. * ### 实现扩展加载器
  33. * 直接实现接口 [PluginLoader] 或 [FilePluginLoader], 并注册 [PluginLoaderProvider]
  34. *
  35. * @see JvmPluginLoader Jar 插件加载器
  36. * @see PluginLoaderProvider 扩展
  37. */
  38. public interface PluginLoader<P : Plugin, D : PluginDescription> {
  39. /**
  40. * 扫描并返回可以被加载的插件的列表.
  41. *
  42. * 这些插件都应处于还未被加载的状态.
  43. *
  44. * 在 Console 启动时, [PluginManager] 会获取所有 [PluginDescription], 分析依赖关系, 确认插件加载顺序.
  45. *
  46. * **实现细节:** 此函数*只应该*在 Console 启动时被调用一次. 但取决于前端实现不同, 或由于被一些插件需要, 此函数也可能会被多次调用.
  47. */
  48. public fun listPlugins(): List<P>
  49. /**
  50. * 获取此插件的描述.
  51. *
  52. * **实现细节**: 此函数只允许抛出 [PluginLoadException] 作为正常失败原因, 其他任意异常都属于意外错误.
  53. *
  54. * 若在 Console 启动并加载所有插件的过程中, 本函数抛出异常, 则会放弃此插件的加载, 并影响依赖它的其他插件.
  55. *
  56. * @throws PluginLoadException 在加载插件遇到意料之中的错误时抛出 (如无法读取插件信息等).
  57. *
  58. * @see PluginDescription 插件描述
  59. */
  60. @Throws(PluginLoadException::class)
  61. public fun getPluginDescription(plugin: P): D
  62. /**
  63. * 主动加载一个插件 (实例), 但不 [启用][enablePlugin] 它. 返回加载成功的主类实例
  64. *
  65. * **实现注意**: Console 不会把一个已经启用了的插件再次调用 [load] 或 [enablePlugin], 但不排除意外情况. 实现本函数时应在这种情况时立即抛出异常 [IllegalStateException].
  66. *
  67. * **实现细节**: 此函数只允许抛出 [PluginLoadException] 作为正常失败原因, 其他任意异常都属于意外错误.
  68. * 当异常发生时, 插件将会直接被放弃加载, 并影响依赖它的其他插件.
  69. *
  70. * @throws PluginLoadException 在加载插件遇到意料之中的错误时抛出 (如找不到主类等).
  71. * @throws IllegalStateException 在插件已经被加载时抛出. 这属于意料之外的情况.
  72. */
  73. @Throws(IllegalStateException::class, PluginLoadException::class)
  74. public fun load(plugin: P)
  75. /**
  76. * 主动启用这个插件.
  77. *
  78. * **实现注意**: Console 不会把一个已经启用了的插件再次调用 [load] 或 [enablePlugin], 但不排除意外情况. 实现本函数时应在这种情况时立即抛出异常 [IllegalStateException].
  79. *
  80. * **实现细节**: 此函数可抛出 [PluginLoadException] 作为正常失败原因, 其他任意异常都属于意外错误.
  81. * 当异常发生时, 插件将会直接被放弃加载, 并影响依赖它的其他插件.
  82. *
  83. * @throws PluginLoadException 在加载插件遇到意料之中的错误时抛出 (如找不到主类等).
  84. * @throws IllegalStateException 在插件已经被加载时抛出. 这属于意料之外的情况.
  85. *
  86. * @see PluginManager.enablePlugin
  87. */
  88. @Throws(IllegalStateException::class, PluginLoadException::class)
  89. public fun enable(plugin: P)
  90. /**
  91. * 主动禁用这个插件.
  92. *
  93. * **实现细节**: 此函数可抛出 [PluginLoadException] 作为正常失败原因, 其他任意异常都属于意外错误.
  94. * 当异常发生时, 插件将会直接被放弃加载, 并影响依赖它的其他插件.
  95. *
  96. * @throws PluginLoadException 在加载插件遇到意料之中的错误时抛出 (如找不到主类等).
  97. *
  98. * @see PluginManager.disablePlugin
  99. */
  100. @Throws(IllegalStateException::class, PluginLoadException::class)
  101. public fun disable(plugin: P)
  102. }