Use classloader for ResourceContainer

This commit is contained in:
Him188 2020-08-20 14:30:12 +08:00
parent d240ba7b83
commit dc6bb61352
4 changed files with 21 additions and 8 deletions

View File

@ -39,8 +39,8 @@ internal abstract class JvmPluginInternal(
) : JvmPlugin, ) : JvmPlugin,
CoroutineScope { CoroutineScope {
private val resourceContainerDelegate by lazy { this::class.asResourceContainer() } private val resourceContainerDelegate by lazy { this::class.java.classLoader.asResourceContainer() }
override fun getResourceAsStream(name: String): InputStream = resourceContainerDelegate.getResourceAsStream(name) override fun getResourceAsStream(name: String): InputStream? = resourceContainerDelegate.getResourceAsStream(name)
// region JvmPlugin // region JvmPlugin
/** /**

View File

@ -24,6 +24,9 @@ import kotlin.reflect.KClass
/** /**
* Java, Kotlin 或其他 JVM 平台插件 * Java, Kotlin 或其他 JVM 平台插件
* *
* ### ResourceContainer
* 实现为 [ClassLoader.getResourceAsStream]
*
* @see AbstractJvmPlugin 默认实现 * @see AbstractJvmPlugin 默认实现
* *
* @see JavaPlugin Java 插件 * @see JavaPlugin Java 插件
@ -46,20 +49,28 @@ public interface JvmPlugin : Plugin, CoroutineScope,
get() = JarPluginLoader get() = JarPluginLoader
/** /**
* 获取一个 [Setting] 实例 * [JarPluginLoader.settingStorage] 获取一个 [Setting] 实例
*/ */
@JvmDefault @JvmDefault
public fun <T : Setting> loadSetting(clazz: Class<T>): T = loader.settingStorage.load(this, clazz) public fun <T : Setting> loadSetting(clazz: Class<T>): T = loader.settingStorage.load(this, clazz)
// TODO: 2020/7/11 document onLoad, onEnable, onDisable /**
* 在插件被加载时调用. 只会被调用一次.
*/
@JvmDefault @JvmDefault
public fun onLoad() { public fun onLoad() {
} }
/**
* 在插件被启用时调用, 可能会被调用多次
*/
@JvmDefault @JvmDefault
public fun onEnable() { public fun onEnable() {
} }
/**
* 在插件被关闭时调用, 可能会被调用多次
*/
@JvmDefault @JvmDefault
public fun onDisable() { public fun onDisable() {
} }

View File

@ -20,7 +20,7 @@ import kotlin.reflect.KClass
/** /**
* 资源容器. * 资源容器.
* *
* 资源容器可能使用 [Class.getResourceAsStream], 也可能使用其他方式, 取决于实现方式. * 资源容器可能使用 [Class.getResourceAsStream], [ClassLoader.getResourceAsStream], 也可能使用其他方式, 取决于实现方式.
* *
* @see JvmPlugin [JvmPlugin] 通过 [ClassLoader.getResourceAsStream] 实现 [ResourceContainer], 使用 [ResourceContainer.asResourceContainer] * @see JvmPlugin [JvmPlugin] 通过 [ClassLoader.getResourceAsStream] 实现 [ResourceContainer], 使用 [ResourceContainer.asResourceContainer]
*/ */
@ -46,13 +46,15 @@ public interface ResourceContainer {
public companion object { public companion object {
/** /**
* 使用 [Class.getResourceAsStream] 读取资源文件 * 使用 [Class.getResourceAsStream] 读取资源文件
*
* @see ClassLoader.asResourceContainer
*/ */
@JvmStatic @JvmStatic
@JvmName("create") @JvmName("create")
public fun KClass<*>.asResourceContainer(): ResourceContainer = this.java.asResourceContainer() public fun KClass<*>.asResourceContainer(): ResourceContainer = this.java.asResourceContainer()
/** /**
* 使用 [Class.getResourceAsStream] 读取资源文件 * 使用 [ClassLoader.getResourceAsStream] 读取资源文件
*/ */
@JvmStatic @JvmStatic
@JvmName("create") @JvmName("create")

View File

@ -18,7 +18,7 @@
object Versions { object Versions {
const val core = "1.2.1" const val core = "1.2.1"
const val console = "1.0-M2" const val console = "1.0-M2-1"
const val consoleGraphical = "0.0.7" const val consoleGraphical = "0.0.7"
const val consoleTerminal = "0.1.0" const val consoleTerminal = "0.1.0"
const val consolePure = console const val consolePure = console
@ -36,5 +36,5 @@ object Versions {
const val bintray = "1.8.5" const val bintray = "1.8.5"
const val blockingBridge = "1.0.2" const val blockingBridge = "1.0.3"
} }