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(kotlinx("coroutines-core", Versions.Kotlin.coroutines))
api(kotlinx("coroutines-swing",Versions.Kotlin.coroutines))
api(ktor("client-cio", 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.system.exitProcess
internal object MiraiDownloader{
private val tasks = mutableMapOf<String,File>()
@ -21,19 +20,30 @@ internal object MiraiDownloader{
tasks[fromUrl] = to
}
suspend fun downloadIfNeed(){
suspend fun downloadIfNeed(isUI:Boolean){
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(
override val coroutineContext: CoroutineContext = EmptyCoroutineContext,
tasks: Map<String, File>
tasks: Map<String, File>,
val background:Boolean,
val bar:MiraiDownloadProgressBar
):CoroutineScope {
val bar = MiraiDownloaderProgressBar()
fun log(any:Any?){
if(!background && any != null){
println(background)
}
}
var totalDownload = AtomicInteger(0)
var totalSize = AtomicInteger(0)
@ -41,8 +51,7 @@ private class MiraiDownloaderImpl(
private var isDownloadFinish: Job
init {
println("Mirai Downloader")
println("[Mirai国内镜像] 感谢崔Cloud慷慨提供免费的国内储存分发")
bar.ad()
isDownloadFinish = this.async {
tasks.forEach {
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')
}
override fun ad(){
println("Mirai Downloader")
println("[Mirai国内镜像] 感谢崔Cloud慷慨提供免费的国内储存分发")
}
private val barLen = 40
fun update(rate: Float, message: String) {
override fun update(rate: Float, message: String) {
reset()
print("Progress: ")
val len = (rate * barLen).toInt()
@ -111,12 +131,39 @@ class MiraiDownloaderProgressBar(){
print(" | $message")
}
fun complete(){
override fun complete(){
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")
package net.mamoe.mirai.console.wrapper
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*
import java.awt.Frame
import java.awt.Panel
import java.awt.TextArea
import java.awt.Toolkit
import java.io.File
import java.lang.StringBuilder
import java.net.URLClassLoader
import java.util.*
@ -27,16 +29,70 @@ val contentPath by lazy {
}
object WrapperMain {
internal var uiBarOutput = StringBuilder()
private val uilog = StringBuilder()
internal fun uiLog(any: Any?){
if(any!=null) {
uilog.append(any)
}
}
@JvmStatic
fun main(args: Array<String>) {
GlobalScope.launch{
while (true) {
delay(1000*60*5)
System.gc()
}
}
println("You are running Mirai-Console-Wrapper under " + System.getProperty("user.dir"))
gc()
if(args.contains("native") || args.contains("-native")){
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)
if(type!=null){
println("Starting Mirai Console $type, reset by clear /content/")
@ -55,7 +111,6 @@ object WrapperMain {
}
WrapperProperties.content = type
}
println("Starting version check...")
runBlocking {
launch {
@ -67,7 +122,7 @@ object WrapperMain {
}
runBlocking {
MiraiDownloader.downloadIfNeed()
MiraiDownloader.downloadIfNeed(false)
}
println("Version check complete, starting Mirai")
@ -75,6 +130,10 @@ object WrapperMain {
println("Console :" + ConsoleUpdater.getFile()!!)
println("Root :" + System.getProperty("user.dir") + "/")
start(type)
}
private fun start(type: String){
val loader = MiraiClassLoader(
CoreUpdater.getProtocolLib()!!,
ConsoleUpdater.getFile()!!,
@ -94,7 +153,9 @@ object WrapperMain {
}
}
class MiraiClassLoader(
private class MiraiClassLoader(
protocol: File,
console: File,
parent: ClassLoader
@ -104,7 +165,7 @@ class MiraiClassLoader(
), parent)
object WrapperProperties{
private object WrapperProperties{
val contentFile by lazy{
File(contentPath.absolutePath + "/.wrapper.txt").also {
if(!it.exists())it.createNewFile()
@ -117,12 +178,20 @@ object WrapperProperties{
fun determineConsoleType(
type:String
type: String
):String?{
if(type == CONSOLE_PURE || type == CONSOLE_GRAPHICAL || type == CONSOLE_TERMINAL){
return type
}
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")
/* 依次启用功能 */
DefaultCommands()
PluginManager.loadPlugins()
@ -107,14 +106,12 @@ object MiraiConsole {
*/
fun stop() {
PluginManager.disablePlugins()
CommandManager.cancel()
CommandManager.cancel()
try {
bots.forEach {
it.get()?.close()
}
} catch (ignored: Exception) {
}
} catch (ignored: Exception) { }
}
@Suppress("RedundantSuspendModifier") // binary compatibility
@ -169,7 +166,6 @@ object MiraiConsole {
CommandManager.runCommand(sender, command)
}
}
}