mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-13 14:50:43 +08:00
Plugin Center Ready
This commit is contained in:
parent
cace645e01
commit
b5d631c79a
@ -5,10 +5,16 @@ import com.google.gson.JsonObject
|
|||||||
import com.google.gson.JsonParser
|
import com.google.gson.JsonParser
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import net.mamoe.mirai.console.plugins.PluginManager
|
||||||
import org.jsoup.Connection
|
import org.jsoup.Connection
|
||||||
import org.jsoup.Jsoup
|
import org.jsoup.Jsoup
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileOutputStream
|
||||||
|
import java.net.HttpURLConnection
|
||||||
|
import java.net.URL
|
||||||
|
import kotlin.system.exitProcess
|
||||||
|
|
||||||
object CuiPluginCenter: PluginCenter{
|
internal object CuiPluginCenter: PluginCenter{
|
||||||
|
|
||||||
var plugins:JsonArray? = null
|
var plugins:JsonArray? = null
|
||||||
|
|
||||||
@ -84,6 +90,26 @@ object CuiPluginCenter: PluginCenter{
|
|||||||
plugins = results.get("result").asJsonArray//先不解析
|
plugins = results.get("result").asJsonArray//先不解析
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun <T : Any> T.downloadPlugin(name: String, progressListener: T.(Float) -> Unit):Boolean {
|
||||||
|
val info = findPlugin(name) ?: error("Plugin Not Found")
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
val con = URL("https://pan.jasonczc.cn/?/mirai/plugins/$name/$name-" + info.version + ".mp4").openConnection() as HttpURLConnection
|
||||||
|
val input= con.inputStream
|
||||||
|
val size = con.contentLength
|
||||||
|
var totalDownload = 0F
|
||||||
|
val targetFile = File(PluginManager.pluginsPath , "$name-" + info.version + ".jar")
|
||||||
|
val outputStream = FileOutputStream(targetFile)
|
||||||
|
var len: Int
|
||||||
|
val buff = ByteArray(1024)
|
||||||
|
while (input.read(buff).also { len = it } != -1) {
|
||||||
|
totalDownload+=len
|
||||||
|
outputStream.write(buff, 0, len)
|
||||||
|
progressListener.invoke(this@downloadPlugin,totalDownload/size)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
override val name: String
|
override val name: String
|
||||||
get() = "崔云"
|
get() = "崔云"
|
||||||
|
|
||||||
|
@ -46,6 +46,10 @@ interface PluginCenter {
|
|||||||
suspend fun findPlugin(name:String):PluginInfo?
|
suspend fun findPlugin(name:String):PluginInfo?
|
||||||
|
|
||||||
|
|
||||||
|
suspend fun <T:Any> T.downloadPlugin(name:String, progressListener:T.(Float) -> Unit)
|
||||||
|
|
||||||
|
suspend fun downloadPlugin(name:String, progressListener:PluginCenter.(Float) -> Unit) = downloadPlugin<PluginCenter>(name,progressListener)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 刷新
|
* 刷新
|
||||||
*/
|
*/
|
||||||
|
@ -11,6 +11,7 @@ package net.mamoe.mirai.console.command
|
|||||||
|
|
||||||
import net.mamoe.mirai.Bot
|
import net.mamoe.mirai.Bot
|
||||||
import net.mamoe.mirai.console.MiraiConsole
|
import net.mamoe.mirai.console.MiraiConsole
|
||||||
|
import net.mamoe.mirai.console.center.PluginCenter
|
||||||
import net.mamoe.mirai.console.plugins.PluginManager
|
import net.mamoe.mirai.console.plugins.PluginManager
|
||||||
import net.mamoe.mirai.console.utils.addManager
|
import net.mamoe.mirai.console.utils.addManager
|
||||||
import net.mamoe.mirai.console.utils.checkManager
|
import net.mamoe.mirai.console.utils.checkManager
|
||||||
@ -279,5 +280,58 @@ object DefaultCommands {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registerConsoleCommands {
|
||||||
|
name = "install"
|
||||||
|
description = "Install plugin from PC"
|
||||||
|
usage = "/install [plugin-name] to install plugin or /install [page-num] to show list "
|
||||||
|
onCommand {
|
||||||
|
|
||||||
|
val center = PluginCenter.Default
|
||||||
|
|
||||||
|
suspend fun showPage(num:Int){
|
||||||
|
sendMessage("正在连接" + center.name)
|
||||||
|
val list = PluginCenter.Default.fetchPlugin(num)
|
||||||
|
list.values.forEach {
|
||||||
|
appendMessage("=>" + it.name + " ;作者: " + it.author + " ;介绍: " + it.description)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun installPlugin(name: String){
|
||||||
|
sendMessage("正在连接" + center.name)
|
||||||
|
val plugin = center.findPlugin(name)
|
||||||
|
if(plugin == null){
|
||||||
|
sendMessage("插件未找到, 请注意大小写")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sendMessage("正在安装" + plugin.name)
|
||||||
|
try{
|
||||||
|
center.downloadPlugin(name){}
|
||||||
|
sendMessage("安装" + plugin.name + "成功")
|
||||||
|
}catch (e: Exception){
|
||||||
|
sendMessage("安装" + plugin.name + "失败, " + (e.message?:"未知原因"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(it.isEmpty()){
|
||||||
|
showPage(1)
|
||||||
|
}else{
|
||||||
|
val arg = it[0]
|
||||||
|
|
||||||
|
val id = try{
|
||||||
|
arg.toInt()
|
||||||
|
}catch (e:Exception){
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
||||||
|
if(id > 0){
|
||||||
|
showPage(id)
|
||||||
|
}else{
|
||||||
|
installPlugin(arg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user