Backport #267 and #269 to master, closes #262

This commit is contained in:
dgelessus 2016-08-25 19:00:35 +02:00
parent c4b6237512
commit 9ccbde7ce6
7 changed files with 135 additions and 188 deletions

3
.gitignore vendored
View file

@ -15,3 +15,6 @@
!/README.md
!/STYLE.md
!/TESTING.md
# OS X things
.DS_Store

View file

@ -512,7 +512,7 @@ public class App {
Properties props = new Properties();
props.load(new FileInputStream(f));
props.setProperty("java_version", Utils.getJavaVersion());
props.setProperty("java_version", Utils.getLauncherJavaVersion());
props.setProperty("location", App.settings.getBaseDir().toString());
props.setProperty("executable", new File(Update.class.getProtectionDomain().getCodeSource().getLocation()
.getPath()).getAbsolutePath());

View file

@ -18,19 +18,20 @@
package com.atlauncher;
import java.io.CharArrayWriter;
import java.io.PrintWriter;
public final class ExceptionStrainer implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
e.printStackTrace();
if (e.getMessage() != null && !e.getMessage().isEmpty()) {
LogManager.error(e.getMessage());
}
for (StackTraceElement element : e.getStackTrace()) {
if (element != null) {
LogManager.error(element.toString());
}
CharArrayWriter writer = new CharArrayWriter();
try {
e.printStackTrace(new PrintWriter(writer));
LogManager.error(writer.toString());
} finally {
writer.close();
}
}
}

View file

@ -54,6 +54,7 @@ import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.CharArrayWriter;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
@ -66,6 +67,7 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Proxy;
@ -2611,21 +2613,17 @@ public class Settings {
/**
* Logs a stack trace to the console window
*
* @param exception The exception to show in the console
* @param t The throwable to show in the console
*/
public void logStackTrace(Exception exception) {
exception.printStackTrace();
if (exception.getMessage() != null) {
LogManager.error(exception.getMessage());
}
if (exception.getStackTrace() != null) {
for (StackTraceElement element : exception.getStackTrace()) {
if (element != null) {
LogManager.error(element.toString());
}
}
public void logStackTrace(Throwable t) {
t.printStackTrace();
CharArrayWriter writer = new CharArrayWriter();
try {
t.printStackTrace(new PrintWriter(writer));
LogManager.error(writer.toString());
} finally {
writer.close();
}
}
@ -2633,11 +2631,11 @@ public class Settings {
* Logs a stack trace to the console window with a custom message before it
*
* @param message A message regarding the stack trace to show before it providing more insight
* @param exception The exception to show in the console
* @param t The throwable to show in the console
*/
public void logStackTrace(String message, Exception exception) {
public void logStackTrace(String message, Throwable t) {
LogManager.error(message);
logStackTrace(exception);
logStackTrace(t);
}
public void showKillMinecraft(Process minecraft) {

View file

@ -128,13 +128,13 @@ public class LegacyMCLauncher {
arguments.add("-Xmx" + App.settings.getMaximumMemory() + "M");
}
if (App.settings.getPermGen() < instance.getPermGen() && (Utils.getMaximumRam() / 8) < instance.getPermGen()) {
if (Utils.isJava8()) {
if (Utils.useMetaspace()) {
arguments.add("-XX:MetaspaceSize=" + instance.getPermGen() + "M");
} else {
arguments.add("-XX:PermSize=" + instance.getPermGen() + "M");
}
} else {
if (Utils.isJava8()) {
if (Utils.useMetaspace()) {
arguments.add("-XX:MetaspaceSize=" + App.settings.getPermGen() + "M");
} else {
arguments.add("-XX:PermSize=" + App.settings.getPermGen() + "M");

View file

@ -131,13 +131,13 @@ public class MCLauncher {
arguments.add("-Xmx" + App.settings.getMaximumMemory() + "M");
}
if (App.settings.getPermGen() < instance.getPermGen() && (Utils.getMaximumRam() / 8) < instance.getPermGen()) {
if (Utils.isJava8() || Utils.isJava9()) {
if (Utils.useMetaspace()) {
arguments.add("-XX:MetaspaceSize=" + instance.getPermGen() + "M");
} else {
arguments.add("-XX:PermSize=" + instance.getPermGen() + "M");
}
} else {
if (Utils.isJava8() || Utils.isJava9()) {
if (Utils.useMetaspace()) {
arguments.add("-XX:MetaspaceSize=" + App.settings.getPermGen() + "M");
} else {
arguments.add("-XX:PermSize=" + App.settings.getPermGen() + "M");

View file

@ -357,16 +357,7 @@ public class Utils {
public static String getJavaHome() {
return System.getProperty("java.home");
}
/**
* Gets the java version.
*
* @return the java version
*/
public static String getJavaVersion() {
return System.getProperty("java.runtime.version");
}
/**
* Checks if is windows.
*
@ -1427,186 +1418,140 @@ public class Utils {
}
};
}
/**
* Gets the actual java version.
* Get the Java version that the launcher runs on.
*
* @return the actual java version
* @return the Java version that the launcher runs on
*/
public static String getActualJavaVersion() {
public static String getLauncherJavaVersion() {
return System.getProperty("java.version");
}
/**
* Get the Java version used to run Minecraft.
*
* @return the Java version used to run Minecraft
*/
public static String getMinecraftJavaVersion() {
if (App.settings.isUsingCustomJavaPath()) {
File folder = new File(App.settings.getJavaPath(), "bin/");
List<String> arguments = new ArrayList<String>();
arguments.add(folder + File.separator + "java" + (Utils.isWindows() ? ".exe" : ""));
arguments.add("-version");
ProcessBuilder processBuilder = new ProcessBuilder(arguments);
String javaCommand = folder + File.separator + "java" + (isWindows() ? ".exe" : "");
ProcessBuilder processBuilder = new ProcessBuilder(javaCommand, "-version");
processBuilder.directory(folder);
processBuilder.redirectErrorStream(true);
String version = "Unknown";
BufferedReader br = null;
try {
Process process = processBuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line = null;
Pattern p = Pattern.compile("build ([0-9.-_a-zA-Z]+)");
while ((line = br.readLine()) != null) {
// Extract version information
Matcher m = p.matcher(line);
if (m.find()) {
version = m.group(1);
break;
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
try {
String line = null;
Pattern p = Pattern.compile("java version \"([^\"]*)\"");
while ((line = br.readLine()) != null) {
// Extract version information
Matcher m = p.matcher(line);
if (m.find()) {
version = m.group(1);
break;
}
}
} finally {
br.close();
}
} catch (IOException e) {
App.settings.logStackTrace(e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
App.settings.logStackTrace("Cannot close process input stream reader", e);
}
}
}
return "Launcher: " + System.getProperty("java.version") + ", Minecraft: " + version;
LogManager.warn("Cannot get Java version from the ouput of \"" + javaCommand + "\" -version");
return version;
} else {
return "Launcher: " + System.getProperty("java.version") + ", Minecraft: " + System.getProperty("java" +
".version");
return getLauncherJavaVersion();
}
}
/**
* Checks if the user is using Java 7 or above
* Parse a Java version string and get the major version number. For example "1.8.0_91" is parsed to 8.
*
* @param version the version string to parse
* @return the parsed major version number
*/
public static int parseJavaVersionNumber(String version) {
Matcher m = Pattern.compile("(?:1\\.)?([0-9]+).*").matcher(version);
return m.find() ? Integer.parseInt(m.group(1)) : -1;
}
/**
* Get the major Java version that the launcher runs on.
*
* @return the major Java version that the launcher runs on
*/
public static int getLauncherJavaVersionNumber() {
return parseJavaVersionNumber(getLauncherJavaVersion());
}
/**
* Get the major Java version used to run Minecraft.
*
* @return the major Java version used to run Minecraft
*/
public static int getMinecraftJavaVersionNumber() {
return parseJavaVersionNumber(getMinecraftJavaVersion());
}
/**
* Get the Java versions used by the Launcher and Minecraft as a string.
*
* @return the Java versions used by the Launcher and Minecraft as a string
*/
public static String getActualJavaVersion() {
return String.format("Launcher: Java %d (%s), Minecraft: Java %d (%s)",
getLauncherJavaVersionNumber(), getLauncherJavaVersion(),
getMinecraftJavaVersionNumber(), getMinecraftJavaVersion()
);
}
/**
* Checks if the user is using Java 7 or above.
*
* @return true if the user is using Java 7 or above else false
*/
public static boolean isJava7OrAbove(boolean checkCustomPath) {
if (App.settings.isUsingCustomJavaPath() && checkCustomPath) {
File folder = new File(App.settings.getJavaPath(), "bin/");
List<String> arguments = new ArrayList<String>();
arguments.add(folder + File.separator + "java" + (Utils.isWindows() ? ".exe" : ""));
arguments.add("-version");
ProcessBuilder processBuilder = new ProcessBuilder(arguments);
processBuilder.directory(folder);
processBuilder.redirectErrorStream(true);
BufferedReader br = null;
int version = -1;
try {
Process process = processBuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
if (line.contains("build 1.")) {
int buildIndex = line.indexOf("build 1.") + 8;
version = Integer.parseInt(line.substring(buildIndex, buildIndex + 1));
break;
}
}
if (version == -1) {
LogManager.warn("Cannot get java version number from the ouput of java -version");
} else {
return version >= 7;
}
} catch (NumberFormatException e) {
App.settings.logStackTrace("Cannot get number from the ouput of java -version", e);
} catch (IOException e) {
App.settings.logStackTrace(e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
App.settings.logStackTrace("Cannot close input stream reader", e);
}
}
}
return true; // Can't determine version, so assume true.
} else {
return Integer.parseInt(System.getProperty("java.version").substring(2, 3)) >= 7;
}
int version = checkCustomPath ? getMinecraftJavaVersionNumber() : getLauncherJavaVersionNumber();
return version >= 7 || version == -1;
}
/**
* Checks if is java8.
* Checks if the user is using exactly Java 8.
*
* @return true, if is java8
* @return true if the user is using exactly Java 8
*/
public static boolean isJava8() {
if (App.settings.isUsingCustomJavaPath()) {
File folder = new File(App.settings.getJavaPath(), "bin/");
List<String> arguments = new ArrayList<String>();
arguments.add(folder + File.separator + "java" + (Utils.isWindows() ? ".exe" : ""));
arguments.add("-version");
ProcessBuilder processBuilder = new ProcessBuilder(arguments);
processBuilder.directory(folder);
processBuilder.redirectErrorStream(true);
BufferedReader br = null;
try {
Process process = processBuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line = br.readLine(); // Read first line only
return line.contains("\"1.8");
} catch (IOException e) {
App.settings.logStackTrace(e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
App.settings.logStackTrace("Cannot close input stream reader", e);
}
}
}
return false; // Can't determine version, so fall back to not being Java 8
} else {
return System.getProperty("java.version").substring(0, 3).equalsIgnoreCase("1.8");
}
return getMinecraftJavaVersionNumber() == 8;
}
/**
* Checks if is java9.
* Checks if the user is using exactly Java 9.
*
* @return true, if is java9
* @return true if the user is using exactly Java 9
*/
public static boolean isJava9() {
if (App.settings.isUsingCustomJavaPath()) {
File folder = new File(App.settings.getJavaPath(), "bin/");
List<String> arguments = new ArrayList<String>();
arguments.add(folder + File.separator + "java" + (Utils.isWindows() ? ".exe" : ""));
arguments.add("-version");
ProcessBuilder processBuilder = new ProcessBuilder(arguments);
processBuilder.directory(folder);
processBuilder.redirectErrorStream(true);
BufferedReader br = null;
try {
Process process = processBuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line = br.readLine(); // Read first line only
return line.contains("\"1.9");
} catch (IOException e) {
App.settings.logStackTrace(e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
App.settings.logStackTrace("Cannot close input stream reader", e);
}
}
}
return false; // Can't determine version, so fall back to not being Java 8
} else {
return System.getProperty("java.version").substring(0, 3).equalsIgnoreCase("1.9");
}
return getMinecraftJavaVersionNumber() == 9;
}
/**
* Checks whether Metaspace should be used instead of PermGen. This is the case for Java 8 and above.
*
* @return whether Metaspace should be used instead of PermGen
*/
public static boolean useMetaspace() {
return getMinecraftJavaVersionNumber() >= 8;
}
/**