Commit e42d7ff7 authored by jiahua.liu's avatar jiahua.liu

add usage

parent 6cf563c8
...@@ -156,6 +156,8 @@ interface Command { ...@@ -156,6 +156,8 @@ interface Command {
val name: String val name: String
val alias: List<String> val alias: List<String>
val description: String val description: String
val usage: String
suspend fun onCommand(sender: CommandSender, args: List<String>): Boolean suspend fun onCommand(sender: CommandSender, args: List<String>): Boolean
fun register() fun register()
} }
...@@ -163,7 +165,8 @@ interface Command { ...@@ -163,7 +165,8 @@ interface Command {
abstract class BlockingCommand( abstract class BlockingCommand(
override val name: String, override val name: String,
override val alias: List<String> = listOf(), override val alias: List<String> = listOf(),
override val description: String = "" override val description: String = "",
override val usage: String = ""
) : Command { ) : Command {
/** /**
* 最高优先级监听器 * 最高优先级监听器
...@@ -186,6 +189,7 @@ class AnonymousCommand internal constructor( ...@@ -186,6 +189,7 @@ class AnonymousCommand internal constructor(
override val name: String, override val name: String,
override val alias: List<String>, override val alias: List<String>,
override val description: String, override val description: String,
override val usage: String = "",
val onCommand: suspend CommandSender.(args: List<String>) -> Boolean val onCommand: suspend CommandSender.(args: List<String>) -> Boolean
) : Command { ) : Command {
override suspend fun onCommand(sender: CommandSender, args: List<String>): Boolean { override suspend fun onCommand(sender: CommandSender, args: List<String>): Boolean {
...@@ -201,6 +205,7 @@ class CommandBuilder internal constructor() { ...@@ -201,6 +205,7 @@ class CommandBuilder internal constructor() {
var name: String? = null var name: String? = null
var alias: List<String>? = null var alias: List<String>? = null
var description: String = "" var description: String = ""
var usage: String = "use /help for help"
var onCommand: (suspend CommandSender.(args: List<String>) -> Boolean)? = null var onCommand: (suspend CommandSender.(args: List<String>) -> Boolean)? = null
fun onCommand(commandProcess: suspend CommandSender.(args: List<String>) -> Boolean) { fun onCommand(commandProcess: suspend CommandSender.(args: List<String>) -> Boolean) {
...@@ -218,6 +223,7 @@ class CommandBuilder internal constructor() { ...@@ -218,6 +223,7 @@ class CommandBuilder internal constructor() {
name!!, name!!,
alias!!, alias!!,
description, description,
usage,
onCommand!! onCommand!!
).also { it.register() } ).also { it.register() }
} }
......
...@@ -31,6 +31,9 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope { ...@@ -31,6 +31,9 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
private val supervisorJob = SupervisorJob() private val supervisorJob = SupervisorJob()
final override val coroutineContext: CoroutineContext = coroutineContext + supervisorJob final override val coroutineContext: CoroutineContext = coroutineContext + supervisorJob
/**
* 插件被分配的data folder, 如果插件改名了 data folder 也会变 请注意
*/
val dataFolder: File by lazy { val dataFolder: File by lazy {
File(PluginManager.pluginsPath + pluginDescription.name).also { it.mkdir() } File(PluginManager.pluginsPath + pluginDescription.name).also { it.mkdir() }
} }
...@@ -68,12 +71,14 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope { ...@@ -68,12 +71,14 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
this.onEnable() this.onEnable()
} }
/**
* 加载一个data folder中的Config
* 这个config是read-write的
*/
fun loadConfig(fileName: String): Config { fun loadConfig(fileName: String): Config {
return Config.load(File(fileName)) return Config.load(dataFolder.absolutePath + fileName)
} }
@JvmOverloads @JvmOverloads
internal fun disable(throwable: CancellationException? = null) { internal fun disable(throwable: CancellationException? = null) {
this.coroutineContext[Job]!!.cancelChildren(throwable) this.coroutineContext[Job]!!.cancelChildren(throwable)
...@@ -87,7 +92,7 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope { ...@@ -87,7 +92,7 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
this.onLoad() this.onLoad()
} }
fun getPluginManager() = PluginManager val pluginManager = PluginManager
val logger: MiraiLogger by lazy { val logger: MiraiLogger by lazy {
SimpleLogger("Plugin ${pluginDescription.name}") { _, message, e -> SimpleLogger("Plugin ${pluginDescription.name}") { _, message, e ->
...@@ -99,15 +104,31 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope { ...@@ -99,15 +104,31 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
} }
} }
/**
* 加载一个插件jar, resources中的东西
*/
fun getResources(fileName: String): InputStream? { fun getResources(fileName: String): InputStream? {
return PluginManager.getFileInJarByName( return try {
this.javaClass.classLoader.getResourceAsStream(fileName)
} catch (e: Exception) {
PluginManager.getFileInJarByName(
this.pluginDescription.name, this.pluginDescription.name,
fileName fileName
) )
} }
}
/**
* 加载一个插件jar, resources中的Config
* 这个Config是read-only的
*/
fun getResourcesConfig(fileName: String): Config {
if (fileName.contains(".")) {
error("Unknown Config Type")
}
return Config.load(getResources(fileName) ?: error("Config Not Found"), fileName.split(".")[1])
}
//fun getResourcesConfig()
} }
class PluginDescription( class PluginDescription(
...@@ -370,7 +391,7 @@ object PluginManager { ...@@ -370,7 +391,7 @@ object PluginManager {
/** /**
* 根据插件名字找Jar resources中的文件 * 根据插件名字找Jar中的文件
* null => 没找到 * null => 没找到
*/ */
fun getFileInJarByName(pluginName: String, toFind: String): InputStream? { fun getFileInJarByName(pluginName: String, toFind: String): InputStream? {
......
...@@ -134,7 +134,7 @@ class ImageSenderMain : PluginBase() { ...@@ -134,7 +134,7 @@ class ImageSenderMain : PluginBase() {
logger.info("loading local image data") logger.info("loading local image data")
try { try {
images = Config.load(getResources(fileName = "data.yml")!!, "yml") images = getResourcesConfig("data.yml")
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
logger.info("无法加载本地图片") logger.info("无法加载本地图片")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment