diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/multiplayer/MultiplayerManager.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/multiplayer/MultiplayerManager.java index d8793572d..e3671bb9e 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/multiplayer/MultiplayerManager.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/multiplayer/MultiplayerManager.java @@ -23,7 +23,6 @@ import javafx.application.Platform; import org.jackhuang.hmcl.Metadata; import org.jackhuang.hmcl.event.Event; import org.jackhuang.hmcl.event.EventManager; -import org.jackhuang.hmcl.launch.StreamPump; import org.jackhuang.hmcl.task.FileDownloadTask; import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.ui.FXUtils; @@ -358,8 +357,8 @@ public final class MultiplayerManager { this.type = type; addRelatedThread(Lang.thread(this::waitFor, "CatoExitWaiter", true)); - addRelatedThread(Lang.thread(new StreamPump(process.getInputStream(), this::checkCatoLog), "CatoInputStreamPump", true)); - addRelatedThread(Lang.thread(new StreamPump(process.getErrorStream(), this::checkCatoLog), "CatoErrorStreamPump", true)); + pumpInputStream(this::checkCatoLog); + pumpErrorStream(this::checkCatoLog); writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8)); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java index 71be8d5a0..527c6fcd8 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java @@ -17,8 +17,13 @@ */ package org.jackhuang.hmcl.util.platform; +import org.jackhuang.hmcl.launch.StreamPump; +import org.jackhuang.hmcl.util.Lang; + +import java.io.IOException; import java.util.*; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.function.Consumer; /** * The managed process. @@ -36,6 +41,12 @@ public class ManagedProcess { private final Queue lines = new ConcurrentLinkedQueue<>(); private final List relatedThreads = new ArrayList<>(); + public ManagedProcess(ProcessBuilder processBuilder) throws IOException { + this.process = processBuilder.start(); + this.commands = processBuilder.command(); + this.classpath = null; + } + /** * Constructor. * @@ -119,6 +130,14 @@ public class ManagedProcess { relatedThreads.add(thread); } + public synchronized void pumpInputStream(Consumer onLogLine) { + addRelatedThread(Lang.thread(new StreamPump(process.getInputStream(), onLogLine), "ProcessInputStreamPump", true)); + } + + public synchronized void pumpErrorStream(Consumer onLogLine) { + addRelatedThread(Lang.thread(new StreamPump(process.getErrorStream(), onLogLine), "ProcessErrorStreamPump", true)); + } + /** * True if the managed process is running. */ diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/SystemUtils.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/SystemUtils.java index 3ac9eee91..b9f356f3d 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/SystemUtils.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/SystemUtils.java @@ -18,10 +18,11 @@ package org.jackhuang.hmcl.util.platform; import java.io.IOException; -import java.lang.ProcessBuilder.Redirect; import java.util.Arrays; import java.util.List; +import static org.jackhuang.hmcl.util.Logging.LOG; + public final class SystemUtils { private SystemUtils() {} @@ -30,10 +31,14 @@ public final class SystemUtils { } public static int callExternalProcess(List command) throws IOException, InterruptedException { - Process process = new ProcessBuilder(command) - .redirectOutput(Redirect.INHERIT) - .redirectError(Redirect.INHERIT) - .start(); - return process.waitFor(); + ManagedProcess managedProcess = new ManagedProcess(new ProcessBuilder(command)); + managedProcess.pumpInputStream(SystemUtils::onLogLine); + managedProcess.pumpErrorStream(SystemUtils::onLogLine); + return managedProcess.getProcess().waitFor(); } + + private static void onLogLine(String log) { + LOG.info(log); + } + }