Merge remote-tracking branch 'origin/master'

This commit is contained in:
ryoii 2020-03-28 23:12:37 +08:00
commit 4d304ba1ac
4 changed files with 148 additions and 35 deletions

View File

@ -28,6 +28,7 @@ dependencies {
api(kotlin("reflect", Versions.Kotlin.stdlib)) api(kotlin("reflect", Versions.Kotlin.stdlib))
api(kotlinx("coroutines-core", Versions.Kotlin.coroutines)) api(kotlinx("coroutines-core", Versions.Kotlin.coroutines))
api(kotlinx("coroutines-swing",Versions.Kotlin.coroutines))
api(ktor("client-cio", Versions.Kotlin.ktor)) api(ktor("client-cio", Versions.Kotlin.ktor))
api(ktor("client-core", Versions.Kotlin.ktor)) api(ktor("client-core", Versions.Kotlin.ktor))

View File

@ -10,7 +10,6 @@ import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext import kotlin.coroutines.EmptyCoroutineContext
import kotlin.system.exitProcess import kotlin.system.exitProcess
internal object MiraiDownloader{ internal object MiraiDownloader{
private val tasks = mutableMapOf<String,File>() private val tasks = mutableMapOf<String,File>()
@ -21,19 +20,30 @@ internal object MiraiDownloader{
tasks[fromUrl] = to tasks[fromUrl] = to
} }
suspend fun downloadIfNeed(){ suspend fun downloadIfNeed(isUI:Boolean){
if(tasks.isNotEmpty()){ if(tasks.isNotEmpty()){
MiraiDownloaderImpl(EmptyCoroutineContext, tasks).waitUntilFinish() if(!isUI) {
MiraiDownloaderImpl(EmptyCoroutineContext, tasks, false, MiraiDownloaderProgressBarInTerminal()).waitUntilFinish()
}else{
MiraiDownloaderImpl(EmptyCoroutineContext, tasks, false, MiraiDownloaderProgressBarInUI()).waitUntilFinish()
}
} }
} }
} }
//background => any print
private class MiraiDownloaderImpl( private class MiraiDownloaderImpl(
override val coroutineContext: CoroutineContext = EmptyCoroutineContext, override val coroutineContext: CoroutineContext = EmptyCoroutineContext,
tasks: Map<String, File> tasks: Map<String, File>,
val background:Boolean,
val bar:MiraiDownloadProgressBar
):CoroutineScope { ):CoroutineScope {
val bar = MiraiDownloaderProgressBar() fun log(any:Any?){
if(!background && any != null){
println(background)
}
}
var totalDownload = AtomicInteger(0) var totalDownload = AtomicInteger(0)
var totalSize = AtomicInteger(0) var totalSize = AtomicInteger(0)
@ -41,8 +51,7 @@ private class MiraiDownloaderImpl(
private var isDownloadFinish: Job private var isDownloadFinish: Job
init { init {
println("Mirai Downloader") bar.ad()
println("[Mirai国内镜像] 感谢崔Cloud慷慨提供免费的国内储存分发")
isDownloadFinish = this.async { isDownloadFinish = this.async {
tasks.forEach { tasks.forEach {
this.launch { this.launch {
@ -90,15 +99,26 @@ private class MiraiDownloaderImpl(
} }
class MiraiDownloaderProgressBar(){ interface MiraiDownloadProgressBar{
fun reset()
fun update(rate: Float, message: String)
fun complete()
fun ad()
}
private fun reset() { class MiraiDownloaderProgressBarInTerminal(): MiraiDownloadProgressBar{
override fun reset() {
print('\r') print('\r')
} }
override fun ad(){
println("Mirai Downloader")
println("[Mirai国内镜像] 感谢崔Cloud慷慨提供免费的国内储存分发")
}
private val barLen = 40 private val barLen = 40
fun update(rate: Float, message: String) { override fun update(rate: Float, message: String) {
reset() reset()
print("Progress: ") print("Progress: ")
val len = (rate * barLen).toInt() val len = (rate * barLen).toInt()
@ -111,12 +131,39 @@ class MiraiDownloaderProgressBar(){
print(" | $message") print(" | $message")
} }
fun complete(){ override fun complete(){
println() println()
} }
} }
class MiraiDownloaderProgressBarInUI(): MiraiDownloadProgressBar{
override fun reset() {
WrapperMain.uiBarOutput.clear()
}
override fun ad(){
WrapperMain.uiLog("[Mirai国内镜像] 感谢崔Cloud慷慨提供更新服务器")
}
private val barLen = 20
override fun update(rate: Float, message: String) {
reset()
WrapperMain.uiBarOutput.append("Progress: ")
val len = (rate * barLen).toInt()
for (i in 0 until len) {
WrapperMain.uiBarOutput.append("#")
}
for (i in 0 until barLen - len) {
WrapperMain.uiBarOutput.append(" ")
}
WrapperMain.uiBarOutput.append(" | $message")
}
override fun complete() {
TODO("Not yet implemented")
}
}

View File

@ -9,11 +9,13 @@
@file:Suppress("EXPERIMENTAL_API_USAGE") @file:Suppress("EXPERIMENTAL_API_USAGE")
package net.mamoe.mirai.console.wrapper package net.mamoe.mirai.console.wrapper
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.*
import kotlinx.coroutines.delay import java.awt.Frame
import kotlinx.coroutines.launch import java.awt.Panel
import kotlinx.coroutines.runBlocking import java.awt.TextArea
import java.awt.Toolkit
import java.io.File import java.io.File
import java.lang.StringBuilder
import java.net.URLClassLoader import java.net.URLClassLoader
import java.util.* import java.util.*
@ -27,16 +29,70 @@ val contentPath by lazy {
} }
object WrapperMain { object WrapperMain {
internal var uiBarOutput = StringBuilder()
private val uilog = StringBuilder()
internal fun uiLog(any: Any?){
if(any!=null) {
uilog.append(any)
}
}
@JvmStatic @JvmStatic
fun main(args: Array<String>) { fun main(args: Array<String>) {
GlobalScope.launch{ gc()
while (true) { if(args.contains("native") || args.contains("-native")){
delay(1000*60*5)
System.gc()
}
}
println("You are running Mirai-Console-Wrapper under " + System.getProperty("user.dir"))
val f = Frame("Mirai-Console Version Check")
f.isResizable = false
val srcSize= Toolkit.getDefaultToolkit().screenSize
val width = 300
val height = 200
val p = Panel()
val textArea = TextArea()
textArea.isEditable = false
p.add(textArea)
p.isVisible = true
f.setLocation((srcSize.width-width)/2, (srcSize.height-height)/2)
f.setSize(width, height)
f.add(p)
f.isVisible = true
uiLog("正在进行版本检查\n")
var uiOpen = true
GlobalScope.launch {
while (isActive && uiOpen){
delay(16)//60 fps
withContext(Dispatchers.Main){
textArea.text = uilog.toString() + "\n" + uiBarOutput.toString()
}
}
}
runBlocking {
launch {
CoreUpdater.versionCheck()
}
launch {
ConsoleUpdater.versionCheck(CONSOLE_GRAPHICAL)
}
}
uiLog("版本检查完成\n")
runBlocking {
MiraiDownloader.downloadIfNeed(true)
}
start(CONSOLE_GRAPHICAL)
}else{
preStartInNonNative()
}
}
private fun preStartInNonNative(){
println("You are running Mirai-Console-Wrapper under " + System.getProperty("user.dir"))
var type = WrapperProperties.determineConsoleType(WrapperProperties.content) var type = WrapperProperties.determineConsoleType(WrapperProperties.content)
if(type!=null){ if(type!=null){
println("Starting Mirai Console $type, reset by clear /content/") println("Starting Mirai Console $type, reset by clear /content/")
@ -55,7 +111,6 @@ object WrapperMain {
} }
WrapperProperties.content = type WrapperProperties.content = type
} }
println("Starting version check...") println("Starting version check...")
runBlocking { runBlocking {
launch { launch {
@ -67,7 +122,7 @@ object WrapperMain {
} }
runBlocking { runBlocking {
MiraiDownloader.downloadIfNeed() MiraiDownloader.downloadIfNeed(false)
} }
println("Version check complete, starting Mirai") println("Version check complete, starting Mirai")
@ -75,6 +130,10 @@ object WrapperMain {
println("Console :" + ConsoleUpdater.getFile()!!) println("Console :" + ConsoleUpdater.getFile()!!)
println("Root :" + System.getProperty("user.dir") + "/") println("Root :" + System.getProperty("user.dir") + "/")
start(type)
}
private fun start(type: String){
val loader = MiraiClassLoader( val loader = MiraiClassLoader(
CoreUpdater.getProtocolLib()!!, CoreUpdater.getProtocolLib()!!,
ConsoleUpdater.getFile()!!, ConsoleUpdater.getFile()!!,
@ -94,7 +153,9 @@ object WrapperMain {
} }
} }
class MiraiClassLoader(
private class MiraiClassLoader(
protocol: File, protocol: File,
console: File, console: File,
parent: ClassLoader parent: ClassLoader
@ -104,7 +165,7 @@ class MiraiClassLoader(
), parent) ), parent)
object WrapperProperties{ private object WrapperProperties{
val contentFile by lazy{ val contentFile by lazy{
File(contentPath.absolutePath + "/.wrapper.txt").also { File(contentPath.absolutePath + "/.wrapper.txt").also {
if(!it.exists())it.createNewFile() if(!it.exists())it.createNewFile()
@ -117,12 +178,20 @@ object WrapperProperties{
fun determineConsoleType( fun determineConsoleType(
type:String type: String
):String?{ ):String?{
if(type == CONSOLE_PURE || type == CONSOLE_GRAPHICAL || type == CONSOLE_TERMINAL){ if(type == CONSOLE_PURE || type == CONSOLE_GRAPHICAL || type == CONSOLE_TERMINAL){
return type return type
} }
return null return null
} }
}
private fun gc(){
GlobalScope.launch{
while (true) {
delay(1000*60*5)
System.gc()
}
}
} }

View File

@ -90,7 +90,6 @@ object MiraiConsole {
} }
//Security.removeProvider("BC") //Security.removeProvider("BC")
/* 依次启用功能 */ /* 依次启用功能 */
DefaultCommands() DefaultCommands()
PluginManager.loadPlugins() PluginManager.loadPlugins()
@ -107,14 +106,12 @@ object MiraiConsole {
*/ */
fun stop() { fun stop() {
PluginManager.disablePlugins() PluginManager.disablePlugins()
CommandManager.cancel() CommandManager.cancel()
try { try {
bots.forEach { bots.forEach {
it.get()?.close() it.get()?.close()
} }
} catch (ignored: Exception) { } catch (ignored: Exception) { }
}
} }
@Suppress("RedundantSuspendModifier") // binary compatibility @Suppress("RedundantSuspendModifier") // binary compatibility
@ -169,7 +166,6 @@ object MiraiConsole {
CommandManager.runCommand(sender, command) CommandManager.runCommand(sender, command)
} }
} }
} }