From 2b2280bf3db5cf61f8e696cd67b4bc1ca37cc166 Mon Sep 17 00:00:00 2001 From: asforest Date: Sun, 16 Jul 2023 13:13:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E4=BF=AE=E5=A4=8D=E7=8B=AC?= =?UTF-8?q?=E7=AB=8B=E8=BF=9B=E7=A8=8B=E5=90=AF=E5=8A=A8=E6=97=B6=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=90=8E=E5=8D=A1=E6=AD=BB=E4=B8=8D=E8=83=BD=E9=80=80?= =?UTF-8?q?=E5=87=BA=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/mcpatch/McPatchClient.kt | 47 +++++++++++-------- src/main/kotlin/mcpatch/WorkThread.kt | 4 +- src/main/kotlin/mcpatch/data/GlobalOptions.kt | 2 +- src/main/kotlin/mcpatch/gui/ChangeLogs.kt | 4 +- src/main/kotlin/mcpatch/gui/McPatchWindow.kt | 12 ++--- 5 files changed, 40 insertions(+), 29 deletions(-) diff --git a/src/main/kotlin/mcpatch/McPatchClient.kt b/src/main/kotlin/mcpatch/McPatchClient.kt index 7c2edf4..800f502 100644 --- a/src/main/kotlin/mcpatch/McPatchClient.kt +++ b/src/main/kotlin/mcpatch/McPatchClient.kt @@ -88,9 +88,6 @@ class McPatchClient // 将更新任务单独分进一个线程执行,方便随时打断线程 var ex: Throwable? = null - val workthread = WorkThread(window, options, updateDir, progDir) - workthread.isDaemon = true - workthread.setUncaughtExceptionHandler { _, e -> ex = e } if (!options.quietMode) window?.show() @@ -98,14 +95,14 @@ class McPatchClient window?.titleTextSuffix = "" window?.titleText = Localization[LangNodes.window_title] window?.labelText = Localization[LangNodes.connecting_message] - window?.onWindowClosing?.once { win -> - win.hide() - if (workthread.isAlive) - workthread.interrupt() - } - workthread.start() - workthread.join() + val workthread = WorkThread(window, options, updateDir, progDir) + + try { + workthread.run() + } catch (e: Exception) { + ex = e + } window?.destroy() @@ -118,8 +115,8 @@ class McPatchClient ex !is ClosedByInterruptException) { try { - Log.error(ex!!.javaClass.name) - Log.error(ex!!.stackTraceToString()) + Log.error(ex.javaClass.name) + Log.error(ex.stackTraceToString()) } catch (e: Exception) { println("------------------------") println(e.javaClass.name) @@ -129,8 +126,8 @@ class McPatchClient if (graphicsMode) { val appVersion = "${Environment.Version} (${Environment.GitCommit})" - val className = if (ex!! !is BaseException) ex!!.javaClass.name + "\n" else "" - val errMessage = MiscUtils.stringBreak(className + (ex!!.message ?: ""), 80) + val className = if (ex !is BaseException) ex.javaClass.name + "\n" else "" + val errMessage = MiscUtils.stringBreak(className + (ex.message ?: ""), 80) val title = "发生错误 $appVersion" var content = errMessage + "\n" content += if (!hasStandaloneProgress) "点击\"是\"显示错误详情并停止启动Minecraft," else "点击\"是\"显示错误详情并退出," @@ -141,19 +138,19 @@ class McPatchClient { if (choice) { - DialogUtils.error("错误详情 $appVersion", ex!!.stackTraceToString()) - throw ex!! + DialogUtils.error("错误详情 $appVersion", ex.stackTraceToString()) + throw ex } } else { if (choice) - DialogUtils.error("错误详情 $appVersion", ex!!.stackTraceToString()) - throw ex!! + DialogUtils.error("错误详情 $appVersion", ex.stackTraceToString()) + throw ex } } else { if (options.noThrowing) println("文件更新失败!但因为设置了no-throwing参数,游戏仍会继续运行!\n\n\n") else - throw ex!! + throw ex } } else { Log.info("updating thread interrupted by user") @@ -183,6 +180,18 @@ class McPatchClient throw e } finally { Log.info("RAM: " + MiscUtils.convertBytes(Runtime.getRuntime().usedMemory())) + +// 打印调用堆栈 + for ((thread, frames) in Thread.getAllStackTraces()) { + Log.info("Thread #${thread.id}: ${thread.name}: (daemon: ${thread.isDaemon})") + for (frame in frames) { + Log.info(" $frame") + } + Log.info("") + + if (hasStandaloneProgress && !thread.isDaemon) + thread.interrupt() + } } return false diff --git a/src/main/kotlin/mcpatch/WorkThread.kt b/src/main/kotlin/mcpatch/WorkThread.kt index 47e0ccf..a07d75b 100644 --- a/src/main/kotlin/mcpatch/WorkThread.kt +++ b/src/main/kotlin/mcpatch/WorkThread.kt @@ -29,13 +29,13 @@ class WorkThread( val options: GlobalOptions, val updateDir: File2, val progDir: File2, -): Thread() { +) { var downloadedVersionCount = 0 /** * McPatch工作线程 */ - override fun run() + fun run() { if (!options.quietMode) window?.show() diff --git a/src/main/kotlin/mcpatch/data/GlobalOptions.kt b/src/main/kotlin/mcpatch/data/GlobalOptions.kt index fb90375..da89087 100644 --- a/src/main/kotlin/mcpatch/data/GlobalOptions.kt +++ b/src/main/kotlin/mcpatch/data/GlobalOptions.kt @@ -131,7 +131,7 @@ data class GlobalOptions( concurrentThreads = getOption(map, "concurrent-threads") ?: 4, concurrentBlockSize = getOption(map, "concurrent-block-size") ?: 4194304, versionsFileName = getOption(map, "server-versions-file-name") ?: "versions.txt", - ignoreHttpsCertificate = getOption(map, "ignore-https-certificate") ?: true + ignoreHttpsCertificate = getOption(map, "ignore-https-certificate") ?: false ) } diff --git a/src/main/kotlin/mcpatch/gui/ChangeLogs.kt b/src/main/kotlin/mcpatch/gui/ChangeLogs.kt index eaad9d7..90b9c54 100644 --- a/src/main/kotlin/mcpatch/gui/ChangeLogs.kt +++ b/src/main/kotlin/mcpatch/gui/ChangeLogs.kt @@ -76,17 +76,19 @@ class ChangeLogs changlogs.lineWrap = true closeButton.addActionListener { close() } + threadLock.isDaemon = true threadLock.start() Thread { Thread.sleep(1000) this.window.repaint() - }.start() + }.also { it.isDaemon = true }.start() } fun setAutoClose(time: Int) { autoCloseDelay = time + autoCloseThread.isDaemon = true autoCloseThread.start() } diff --git a/src/main/kotlin/mcpatch/gui/McPatchWindow.kt b/src/main/kotlin/mcpatch/gui/McPatchWindow.kt index 113e845..4ad4c82 100644 --- a/src/main/kotlin/mcpatch/gui/McPatchWindow.kt +++ b/src/main/kotlin/mcpatch/gui/McPatchWindow.kt @@ -15,7 +15,7 @@ class McPatchWindow(width: Int = 300, height: Int = 120) private var label = JLabel("空标签空标签空标签空标签空标签空标签空标签空标签空标签").apply { setBounds(5, 10, 275, 20); horizontalAlignment = JLabel.CENTER; window.contentPane.add(this) } private var progressBar = JProgressBar(0, 1000).apply { setBounds(10, 40, 265, 30); isStringPainted = true; window.contentPane.add(this) } - val onWindowClosing = Event() +// val onWindowClosing = Event() init { window.isUndecorated = false @@ -27,11 +27,11 @@ class McPatchWindow(width: Int = 300, height: Int = 120) window.isResizable = false // window.isAlwaysOnTop = true - window.addWindowListener(object : WindowAdapter() { - override fun windowClosing(e: WindowEvent?) { - onWindowClosing.invoke(this@McPatchWindow) - } - }) +// window.addWindowListener(object : WindowAdapter() { +// override fun windowClosing(e: WindowEvent?) { +// onWindowClosing.invoke(this@McPatchWindow) +// } +// }) } /**