From f92275fa50249877492dd63dc20da5e6e2411030 Mon Sep 17 00:00:00 2001 From: huangyuhui Date: Mon, 21 Aug 2017 00:37:53 +0800 Subject: [PATCH] orignal log window --- .../org/jackhuang/hmcl/game/LauncherHelper.kt | 18 ++++-- .../jackhuang/hmcl/setting/VersionSetting.kt | 9 +++ .../kotlin/org/jackhuang/hmcl/ui/LogWindow.kt | 57 +++++++++++++++++++ .../hmcl/ui/VersionSettingsController.kt | 4 ++ .../assets/fxml/version-settings.fxml | 6 +- 5 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/LogWindow.kt diff --git a/HMCL/src/main/kotlin/org/jackhuang/hmcl/game/LauncherHelper.kt b/HMCL/src/main/kotlin/org/jackhuang/hmcl/game/LauncherHelper.kt index bbd47e7da..0ac9f9eaa 100644 --- a/HMCL/src/main/kotlin/org/jackhuang/hmcl/game/LauncherHelper.kt +++ b/HMCL/src/main/kotlin/org/jackhuang/hmcl/game/LauncherHelper.kt @@ -25,11 +25,9 @@ import org.jackhuang.hmcl.launch.ProcessListener import org.jackhuang.hmcl.mod.CurseForgeModpackCompletionTask import org.jackhuang.hmcl.setting.LauncherVisibility import org.jackhuang.hmcl.setting.Settings +import org.jackhuang.hmcl.setting.VersionSetting import org.jackhuang.hmcl.task.* -import org.jackhuang.hmcl.ui.Controllers -import org.jackhuang.hmcl.ui.DialogController -import org.jackhuang.hmcl.ui.LaunchingStepsPane -import org.jackhuang.hmcl.ui.runOnUiThread +import org.jackhuang.hmcl.ui.* import org.jackhuang.hmcl.util.JavaProcess import org.jackhuang.hmcl.util.Log4jLevel import java.util.concurrent.ConcurrentSkipListSet @@ -71,7 +69,7 @@ object LauncherHelper { repository = repository, versionId = profile.selectedVersion, options = setting.toLaunchOptions(profile.gameDir), - listener = HMCLProcessListener(it["account"], setting.launcherVisibility), + listener = HMCLProcessListener(it["account"], setting), account = it["account"] ) }) @@ -110,17 +108,23 @@ object LauncherHelper { * Guarantee that one [JavaProcess], one [HMCLProcessListener]. * Because every time we launched a game, we generates a new [HMCLProcessListener] */ - class HMCLProcessListener(authInfo: AuthInfo?, private val launcherVisibility: LauncherVisibility) : ProcessListener { + class HMCLProcessListener(authInfo: AuthInfo?, private val setting: VersionSetting) : ProcessListener { val forbiddenTokens: List> = if (authInfo == null) emptyList() else listOf( authInfo.authToken to "", authInfo.userId to "", authInfo.username to "" ) + private val launcherVisibility = setting.launcherVisibility private lateinit var process: JavaProcess private var lwjgl = false + private var logWindow: LogWindow? = null override fun setProcess(process: JavaProcess) { this.process = process + + if (setting.showLogs) { + runOnUiThread { logWindow = LogWindow(); logWindow?.show() } + } } override fun onLog(log: String, level: Log4jLevel) { @@ -129,6 +133,8 @@ object LauncherHelper { else System.out.print(log) + runOnUiThread { logWindow?.logLine(log, level) } + if (!lwjgl && log.contains("LWJGL Version: ")) { lwjgl = true when (launcherVisibility) { diff --git a/HMCL/src/main/kotlin/org/jackhuang/hmcl/setting/VersionSetting.kt b/HMCL/src/main/kotlin/org/jackhuang/hmcl/setting/VersionSetting.kt index 0236d92b4..0237b56d1 100644 --- a/HMCL/src/main/kotlin/org/jackhuang/hmcl/setting/VersionSetting.kt +++ b/HMCL/src/main/kotlin/org/jackhuang/hmcl/setting/VersionSetting.kt @@ -114,6 +114,12 @@ class VersionSetting() { val noCommonProperty = ImmediateBooleanProperty(this, "noCommon", false) var noCommon: Boolean by noCommonProperty + /** + * True if show the logs after game launched. + */ + val showLogsProperty = ImmediateBooleanProperty(this, "showLogs", false) + var showLogs: Boolean by showLogsProperty + // Minecraft settings. /** @@ -203,6 +209,7 @@ class VersionSetting() { noJVMArgsProperty.addListener(listener) notCheckGameProperty.addListener(listener) noCommonProperty.addListener(listener) + showLogsProperty.addListener(listener) serverIpProperty.addListener(listener) fullscreenProperty.addListener(listener) widthProperty.addListener(listener) @@ -257,6 +264,7 @@ class VersionSetting() { addProperty("noJVMArgs", src.noJVMArgs) addProperty("notCheckGame", src.notCheckGame) addProperty("noCommon", src.noCommon) + addProperty("showLogs", src.showLogs) addProperty("launcherVisibility", src.launcherVisibility.ordinal) addProperty("gameDirType", src.gameDirType.ordinal) } @@ -287,6 +295,7 @@ class VersionSetting() { noJVMArgs = json["noJVMArgs"]?.asBoolean ?: false notCheckGame = json["notCheckGame"]?.asBoolean ?: false noCommon = json["noCommon"]?.asBoolean ?: false + showLogs = json["showLogs"]?.asBoolean ?: false launcherVisibility = LauncherVisibility.values()[json["launcherVisibility"]?.asInt ?: 1] gameDirType = EnumGameDirectory.values()[json["gameDirType"]?.asInt ?: 0] } diff --git a/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/LogWindow.kt b/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/LogWindow.kt new file mode 100644 index 000000000..db4b21332 --- /dev/null +++ b/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/LogWindow.kt @@ -0,0 +1,57 @@ +/* + * Hello Minecraft! Launcher. + * Copyright (C) 2017 huangyuhui + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see {http://www.gnu.org/licenses/}. + */ +package org.jackhuang.hmcl.ui + +import javafx.concurrent.Worker +import javafx.scene.Scene +import javafx.scene.layout.StackPane +import javafx.scene.web.WebEngine +import javafx.scene.web.WebView +import javafx.stage.Stage +import org.jackhuang.hmcl.util.Log4jLevel +import org.w3c.dom.Document +import org.w3c.dom.Node + +class LogWindow : Stage() { + val contentPane = WebView() + val rootPane = StackPane().apply { + children.setAll(contentPane) + } + val engine: WebEngine + lateinit var body: Node + lateinit var document: Document + + init { + scene = Scene(rootPane, 800.0, 480.0) + engine = contentPane.engine + engine.loadContent("") + engine.loadWorker.stateProperty().addListener { _, _, newValue -> + if (newValue == Worker.State.SUCCEEDED) { + document = engine.document + body = document.getElementsByTagName("body").item(0) + } + } + } + + fun logLine(line: String, level: Log4jLevel) { + body.appendChild(contentPane.engine.document.createElement("div").apply { + setAttribute("style", "color: #${level.color.toString().substring(2)};") + textContent = line + }) + } +} \ No newline at end of file diff --git a/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/VersionSettingsController.kt b/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/VersionSettingsController.kt index d6ede55ea..1dc4b8a8d 100644 --- a/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/VersionSettingsController.kt +++ b/HMCL/src/main/kotlin/org/jackhuang/hmcl/ui/VersionSettingsController.kt @@ -63,6 +63,7 @@ class VersionSettingsController { @FXML lateinit var javaPaneCustom: BorderPane @FXML lateinit var radioCustom: JFXRadioButton @FXML lateinit var btnJavaSelect: JFXButton + @FXML lateinit var chkShowLogs: JFXToggleButton val javaGroup = ToggleGroup() @@ -81,6 +82,7 @@ class VersionSettingsController { chkNoJVMArgs.limitHeight(limitHeight) chkNoCommon.limitHeight(limitHeight) chkNoGameCheck.limitHeight(limitHeight) + chkShowLogs.limitHeight(limitHeight) fun validation(field: JFXTextField) = InvalidationListener { field.validate() } fun validator(nullable: Boolean = false) = NumberValidator(nullable).apply { message = "Must be a number." } @@ -138,6 +140,7 @@ class VersionSettingsController { notCheckGameProperty.unbind() noCommonProperty.unbind() javaDirProperty.unbind() + showLogsProperty.unbind() unbindEnum(cboLauncherVisibility) unbindEnum(cboRunDirectory) } @@ -157,6 +160,7 @@ class VersionSettingsController { bindBoolean(chkFullscreen, version.fullscreenProperty) bindBoolean(chkNoGameCheck, version.notCheckGameProperty) bindBoolean(chkNoCommon, version.noCommonProperty) + bindBoolean(chkShowLogs, version.showLogsProperty) val javaGroupKey = "java_group.listener" @Suppress("UNCHECKED_CAST") diff --git a/HMCL/src/main/resources/assets/fxml/version-settings.fxml b/HMCL/src/main/resources/assets/fxml/version-settings.fxml index b9b900888..c6bb7a4bf 100644 --- a/HMCL/src/main/resources/assets/fxml/version-settings.fxml +++ b/HMCL/src/main/resources/assets/fxml/version-settings.fxml @@ -15,9 +15,7 @@ - + @@ -97,6 +95,8 @@ + +