Fixed outputing crash advice twice
This commit is contained in:
@@ -17,12 +17,11 @@
|
||||
*/
|
||||
package org.jackhuang.hmcl.util.sys;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import org.jackhuang.hmcl.api.IProcess;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import org.jackhuang.hmcl.api.HMCLog;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -30,10 +29,9 @@ import org.jackhuang.hmcl.api.HMCLog;
|
||||
*/
|
||||
public class JavaProcess implements IProcess {
|
||||
|
||||
private final CountDownLatch latch = new CountDownLatch(2);
|
||||
private final List<String> commands;
|
||||
private final Process process;
|
||||
private final Vector<String> stdOutLines = new Vector<>();
|
||||
private final List<String> stdOutLines = Collections.synchronizedList(new ArrayList<>());
|
||||
|
||||
public JavaProcess(List<String> commands, Process process) {
|
||||
this.commands = commands;
|
||||
@@ -75,19 +73,6 @@ public class JavaProcess implements IProcess {
|
||||
return false;
|
||||
}
|
||||
|
||||
CountDownLatch getLatch() {
|
||||
return latch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void waitForCommandLineCompletion() {
|
||||
try {
|
||||
latch.await();
|
||||
} catch (InterruptedException ignore) {
|
||||
HMCLog.warn("Thread has been interrupted.", ignore);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getExitCode() {
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Hello Minecraft! Launcher.
|
||||
* Copyright (C) 2013 huangyuhui <huanghongxun2008@126.com>
|
||||
*
|
||||
* 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.util.sys;
|
||||
|
||||
import java.util.EventObject;
|
||||
|
||||
/**
|
||||
* For ProcessThread.
|
||||
* @author huang
|
||||
*/
|
||||
public class PrintlnEvent extends EventObject {
|
||||
|
||||
String line;
|
||||
boolean error;
|
||||
|
||||
public PrintlnEvent(Object source, String line, boolean isError) {
|
||||
super(source);
|
||||
this.line = line;
|
||||
this.error = isError;
|
||||
}
|
||||
|
||||
public String getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
public boolean isError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,6 +19,7 @@ package org.jackhuang.hmcl.util.sys;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import org.jackhuang.hmcl.api.HMCLApi;
|
||||
import org.jackhuang.hmcl.api.event.process.JVMLaunchFailedEvent;
|
||||
import org.jackhuang.hmcl.api.event.process.JavaProcessExitedAbnormallyEvent;
|
||||
@@ -39,6 +40,7 @@ public class ProcessMonitor {
|
||||
|
||||
public static final HashSet<ProcessMonitor> MONITORS = new HashSet<>();
|
||||
|
||||
private final CountDownLatch latch = new CountDownLatch(2);
|
||||
ProcessThread inputThread, errorThread;
|
||||
private final IProcess p;
|
||||
|
||||
@@ -64,13 +66,14 @@ public class ProcessMonitor {
|
||||
public void setTag(Object tag) {
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
public void registerPrintlnEvent(Consumer<SimpleEvent<String>> c) {
|
||||
|
||||
public void registerPrintlnEvent(Consumer<PrintlnEvent> c) {
|
||||
inputThread.printlnEvent.register(c);
|
||||
errorThread.printlnEvent.register(c);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
hasFired = false;
|
||||
MONITORS.add(this);
|
||||
HMCLApi.EVENT_BUS.fireChannel(new JavaProcessStartingEvent(this, p));
|
||||
inputThread.start();
|
||||
@@ -78,21 +81,34 @@ public class ProcessMonitor {
|
||||
}
|
||||
|
||||
private void threadStopped(SimpleEvent<IProcess> event) {
|
||||
latch.countDown();
|
||||
ProcessThread t = (ProcessThread) event.getSource();
|
||||
HMCLog.log("Process exit code: " + p.getExitCode());
|
||||
if (p.getExitCode() != 0 || StrUtils.containsOne(t.getLines(),
|
||||
Arrays.asList("Unable to launch"),
|
||||
x -> Level.guessLevel(x, Level.INFO).lessOrEqual(Level.ERROR)))
|
||||
HMCLApi.EVENT_BUS.fireChannel(new JavaProcessExitedAbnormallyEvent(ProcessMonitor.this, p));
|
||||
synchronized (this) {
|
||||
if (!hasFired) {
|
||||
hasFired = true;
|
||||
HMCLApi.EVENT_BUS.fireChannel(new JavaProcessExitedAbnormallyEvent(ProcessMonitor.this, p));
|
||||
}
|
||||
}
|
||||
if (p.getExitCode() != 0 && StrUtils.containsOne(t.getLines(),
|
||||
Arrays.asList("Could not create the Java Virtual Machine.",
|
||||
"Error occurred during initialization of VM",
|
||||
"A fatal exception has occurred. Program will exit.",
|
||||
"Unable to launch"),
|
||||
x -> Level.guessLevel(x, Level.INFO).lessOrEqual(Level.ERROR)))
|
||||
HMCLApi.EVENT_BUS.fireChannel(new JVMLaunchFailedEvent(ProcessMonitor.this, p));
|
||||
synchronized (this) {
|
||||
if (!hasFired) {
|
||||
hasFired = true;
|
||||
HMCLApi.EVENT_BUS.fireChannel(new JVMLaunchFailedEvent(ProcessMonitor.this, p));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean hasFired = false;
|
||||
|
||||
private void processThreadStopped(ProcessThread t1) {
|
||||
MONITORS.remove(this);
|
||||
errorThread.interrupt();
|
||||
@@ -106,4 +122,12 @@ public class ProcessMonitor {
|
||||
monitor.errorThread.interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
public void waitForCommandLineCompletion() {
|
||||
try {
|
||||
latch.await();
|
||||
} catch (InterruptedException ignore) {
|
||||
HMCLog.warn("Thread has been interrupted.", ignore);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,8 +21,8 @@ import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
import org.jackhuang.hmcl.api.HMCLog;
|
||||
import org.jackhuang.hmcl.api.event.EventHandler;
|
||||
import org.jackhuang.hmcl.api.event.SimpleEvent;
|
||||
@@ -36,24 +36,27 @@ import org.jackhuang.hmcl.api.IProcess;
|
||||
*/
|
||||
public class ProcessThread extends Thread {
|
||||
|
||||
Vector<String> lines = new Vector<>();
|
||||
List<String> lines = new ArrayList<>();
|
||||
ProcessMonitor monitor;
|
||||
IProcess p;
|
||||
boolean readError;
|
||||
public final EventHandler<SimpleEvent<String>> printlnEvent = new EventHandler<>();
|
||||
public final EventHandler<PrintlnEvent> printlnEvent = new EventHandler<>();
|
||||
public final EventHandler<SimpleEvent<IProcess>> stopEvent = new EventHandler<>();
|
||||
|
||||
public ProcessThread(ProcessMonitor monitor, boolean readError) {
|
||||
this.monitor = monitor;
|
||||
this.readError = readError;
|
||||
p = monitor.getProcess();
|
||||
setDaemon(readError);
|
||||
}
|
||||
|
||||
public IProcess getProcess() {
|
||||
return monitor.getProcess();
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only get stdout or stderr output according to readError().
|
||||
* Invoke this method only if the process thread has stopped.
|
||||
*/
|
||||
public List<String> getLines() {
|
||||
return lines;
|
||||
@@ -63,32 +66,28 @@ public class ProcessThread extends Thread {
|
||||
public void run() {
|
||||
setName("ProcessMonitor");
|
||||
BufferedReader br = null;
|
||||
IProcess p = monitor.getProcess();
|
||||
try {
|
||||
InputStream in = readError ? p.getRawProcess().getErrorStream() : p.getRawProcess().getInputStream();
|
||||
br = new BufferedReader(new InputStreamReader(in, Charsets.toCharset()));
|
||||
|
||||
String line;
|
||||
while (p.isRunning())
|
||||
while ((line = br.readLine()) != null) {
|
||||
printlnEvent.fire(new SimpleEvent<>(monitor, line));
|
||||
System.out.println("MC: " + line);
|
||||
lines.add(line);
|
||||
p.getStdOutLines().add(line);
|
||||
}
|
||||
while ((line = br.readLine()) != null) {
|
||||
printlnEvent.fire(new SimpleEvent<>(monitor, line));
|
||||
System.out.println("MC: " + line);
|
||||
lines.add(line);
|
||||
p.getStdOutLines().add(line);
|
||||
}
|
||||
while ((line = br.readLine()) != null)
|
||||
println(line);
|
||||
while ((line = br.readLine()) != null)
|
||||
println(line);
|
||||
} catch (IOException e) {
|
||||
HMCLog.err("An error occured when reading process stdout/stderr.", e);
|
||||
} finally {
|
||||
IOUtils.closeQuietly(br);
|
||||
}
|
||||
if (p instanceof JavaProcess)
|
||||
((JavaProcess) p).getLatch().countDown();
|
||||
stopEvent.fire(new SimpleEvent<>(this, p));
|
||||
}
|
||||
|
||||
protected void println(String line) {
|
||||
printlnEvent.fire(new PrintlnEvent(monitor, line, readError));
|
||||
(readError ? System.err : System.out).println("MC: " + line);
|
||||
lines.add(line);
|
||||
p.getStdOutLines().add(line);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user