Wrapper: update native window

This commit is contained in:
PeratX 2020-03-29 18:38:28 +08:00
parent eb1a15de76
commit f6c508883f
2 changed files with 40 additions and 47 deletions

View File

@ -161,7 +161,6 @@ class MiraiDownloaderProgressBarInUI(): MiraiDownloadProgressBar{
} }
override fun complete() { override fun complete() {
TODO("Not yet implemented")
} }
} }

View File

@ -7,17 +7,16 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@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.* import kotlinx.coroutines.*
import java.awt.Frame
import java.awt.Panel
import java.awt.TextArea 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.*
import javax.swing.JFrame
import javax.swing.JPanel
val contentPath by lazy { val contentPath by lazy {
@ -31,8 +30,8 @@ val contentPath by lazy {
object WrapperMain { object WrapperMain {
internal var uiBarOutput = StringBuilder() internal var uiBarOutput = StringBuilder()
private val uilog = StringBuilder() private val uilog = StringBuilder()
internal fun uiLog(any: Any?){ internal fun uiLog(any: Any?) {
if(any!=null) { if (any != null) {
uilog.append(any) uilog.append(any)
} }
} }
@ -40,33 +39,27 @@ object WrapperMain {
@JvmStatic @JvmStatic
fun main(args: Array<String>) { fun main(args: Array<String>) {
gc() gc()
if(args.contains("native") || args.contains("-native")){ if (args.contains("native") || args.contains("-native")) {
val f = JFrame("Mirai-Console Version Check")
val f = Frame("Mirai-Console Version Check") f.setSize(500, 200)
f.setLocationRelativeTo(null)
f.isResizable = false f.isResizable = false
val srcSize= Toolkit.getDefaultToolkit().screenSize
val width = 300 val p = JPanel()
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.add(p)
val textArea = TextArea()
p.add(textArea)
textArea.isEditable = false
f.isVisible = true f.isVisible = true
uiLog("正在进行版本检查\n") uiLog("正在进行版本检查\n")
var uiOpen = true var uiOpen = true
GlobalScope.launch { GlobalScope.launch {
while (isActive && uiOpen){ while (isActive && uiOpen) {
delay(16)//60 fps delay(16)//60 fps
withContext(Dispatchers.Main){ withContext(Dispatchers.Main) {
textArea.text = uilog.toString() + "\n" + uiBarOutput.toString() textArea.text = uilog.toString() + "\n" + uiBarOutput.toString()
} }
} }
@ -85,27 +78,27 @@ object WrapperMain {
} }
start(CONSOLE_GRAPHICAL) start(CONSOLE_GRAPHICAL)
}else{ } else {
preStartInNonNative() preStartInNonNative()
} }
} }
private fun preStartInNonNative(){ private fun preStartInNonNative() {
println("You are running Mirai-Console-Wrapper under " + System.getProperty("user.dir")) 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/")
}else{ } else {
println("Please select Console Type") println("Please select Console Type")
println("请选择 Console 版本") println("请选择 Console 版本")
println("=> Pure : pure console") println("=> Pure : pure console")
println("=> Graphical : graphical UI except unix") println("=> Graphical : graphical UI except unix")
println("=> Terminal : [Not Supported Yet] console in unix") println("=> Terminal : [Not Supported Yet] console in unix")
val scanner = Scanner(System.`in`) val scanner = Scanner(System.`in`)
while (type == null){ while (type == null) {
var input = scanner.next() var input = scanner.next()
input = input.toUpperCase()[0] + input.toLowerCase().substring(1) input = input.toUpperCase()[0] + input.toLowerCase().substring(1)
println("Selecting $input") println("Selecting $input")
type = WrapperProperties.determineConsoleType(input) type = WrapperProperties.determineConsoleType(input)
} }
@ -133,7 +126,7 @@ object WrapperMain {
start(type) start(type)
} }
private fun start(type: String){ private fun start(type: String) {
val loader = MiraiClassLoader( val loader = MiraiClassLoader(
CoreUpdater.getProtocolLib()!!, CoreUpdater.getProtocolLib()!!,
ConsoleUpdater.getFile()!!, ConsoleUpdater.getFile()!!,
@ -149,7 +142,7 @@ object WrapperMain {
).getMethod("load", String::class.java, String::class.java) ).getMethod("load", String::class.java, String::class.java)
.invoke(null, CoreUpdater.getCurrentVersion(), ConsoleUpdater.getCurrentVersion()) .invoke(null, CoreUpdater.getCurrentVersion(), ConsoleUpdater.getCurrentVersion())
} }
CONSOLE_GRAPHICAL -> { CONSOLE_GRAPHICAL -> {
loader.loadClass( loader.loadClass(
"net.mamoe.mirai.console.graphical.MiraiConsoleGraphicalLoader" "net.mamoe.mirai.console.graphical.MiraiConsoleGraphicalLoader"
).getMethod("load", String::class.java, String::class.java) ).getMethod("load", String::class.java, String::class.java)
@ -160,21 +153,22 @@ object WrapperMain {
} }
private class MiraiClassLoader( private class MiraiClassLoader(
protocol: File, protocol: File,
console: File, console: File,
parent: ClassLoader? parent: ClassLoader?
): URLClassLoader(arrayOf( ) : URLClassLoader(
protocol.toURI().toURL(), arrayOf(
console.toURI().toURL() protocol.toURI().toURL(),
), parent) console.toURI().toURL()
), parent
)
private 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()
} }
} }
@ -185,19 +179,19 @@ private 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(){ private fun gc() {
GlobalScope.launch{ GlobalScope.launch {
while (true) { while (true) {
delay(1000*60*5) delay(1000 * 60 * 5)
System.gc() System.gc()
} }
} }
} }