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,
CoroutineScope {
private val resourceContainerDelegate by lazy { this::class.asResourceContainer() }
override fun getResourceAsStream(name: String): InputStream = resourceContainerDelegate.getResourceAsStream(name)
private val resourceContainerDelegate by lazy { this::class.java.classLoader.asResourceContainer() }
override fun getResourceAsStream(name: String): InputStream? = resourceContainerDelegate.getResourceAsStream(name)
// region JvmPlugin
/**

View File

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

View File

@ -18,7 +18,7 @@
object Versions {
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 consoleTerminal = "0.1.0"
const val consolePure = console
@ -36,5 +36,5 @@ object Versions {
const val bintray = "1.8.5"
const val blockingBridge = "1.0.2"
const val blockingBridge = "1.0.3"
}