From b3dc1873be5fdb3cdf25cc196bbac3a9ee589859 Mon Sep 17 00:00:00 2001 From: TheShark34 Date: Sun, 13 Dec 2015 23:08:30 +0100 Subject: [PATCH] Now entirely working --- .../internal/InternalLauncher.java | 32 +++- .../minecraft/MinecraftLauncher.java | 26 +++- .../openlauncherlib/util/CrashReporter.java | 45 ++++-- .../util/ramselector/RamSelector.java | 145 ++++++++++++------ 4 files changed, 182 insertions(+), 66 deletions(-) diff --git a/src/main/java/fr/theshark34/openlauncherlib/internal/InternalLauncher.java b/src/main/java/fr/theshark34/openlauncherlib/internal/InternalLauncher.java index 3731b19..b585a00 100644 --- a/src/main/java/fr/theshark34/openlauncherlib/internal/InternalLauncher.java +++ b/src/main/java/fr/theshark34/openlauncherlib/internal/InternalLauncher.java @@ -21,7 +21,9 @@ import fr.theshark34.openlauncherlib.LaunchException; import fr.theshark34.openlauncherlib.util.LogUtil; import java.io.File; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.Date; @@ -99,11 +101,9 @@ public Object launch() throws LaunchException } Class theClass; - Object initClass; try { theClass = ClassLoader.getSystemClassLoader().loadClass(profile.getTargetClass()); - initClass = initializer.init(theClass); } catch (Exception e) { @@ -137,6 +137,20 @@ public Object launch() throws LaunchException throw e instanceof UnknownMethodException ? (UnknownMethodException) e : new UnknownMethodException(profile.getTargetMethod(), e); } + + Object initClass = null; + if (!Modifier.isStatic(method.getModifiers())) + try + { + initClass = initializer.init(theClass); + } + catch (Throwable t) + { + throw new LaunchException("Can't initialize the main class", t); + } + + method.setAccessible(true); + long totalTime = System.currentTimeMillis() - start; int seconds = (int) (totalTime / 1000) % 60; int minutes = (int) ((totalTime / (1000 * 60)) % 60); @@ -150,9 +164,19 @@ public Object launch() throws LaunchException { return method.invoke(initClass, profile.getParameters()); } - catch (Exception e) + catch (InvocationTargetException e) + { + Throwable thrown = e.getTargetException(); + if (thrown instanceof ExceptionInInitializerError) + { + ExceptionInInitializerError initError = (ExceptionInInitializerError) thrown; + thrown = initError.getException(); + } + throw new LaunchException("Invoked method returned an exception", thrown); + } + catch (IllegalAccessException e) { - throw new LaunchException("Invoked method returned an exception", e); + throw new LaunchException("This is not supposed to happen", e); } } diff --git a/src/main/java/fr/theshark34/openlauncherlib/minecraft/MinecraftLauncher.java b/src/main/java/fr/theshark34/openlauncherlib/minecraft/MinecraftLauncher.java index b1086bd..b12f204 100644 --- a/src/main/java/fr/theshark34/openlauncherlib/minecraft/MinecraftLauncher.java +++ b/src/main/java/fr/theshark34/openlauncherlib/minecraft/MinecraftLauncher.java @@ -26,6 +26,8 @@ import fr.theshark34.openlauncherlib.util.LogUtil; import fr.theshark34.openlauncherlib.util.explorer.Explorer; import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -65,12 +67,21 @@ public static InternalLaunchProfile createInternalProfile(GameInfos infos, GameF List arguments = infos.getGameVersion().getGameType().getLaunchArgs(infos, folder, authInfos); + if(infos.getGameTweaks() != null) + for (GameTweak tweak : infos.getGameTweaks()) + { + arguments.add("--tweakClass"); + arguments.add(tweak.getTweakClass(infos)); + } + String mainClass = infos.getGameTweaks() == null || infos.getGameTweaks().length == 0 ? infos.getGameVersion().getGameType().getMainClass(infos) : GameTweak.LAUNCHWRAPPER_MAIN_CLASS; String[] args = arguments.toArray(new String[arguments.size()]); InternalLaunchProfile profile = new InternalLaunchProfile(mainClass, args); profile.setClasspath(libs); + System.setProperty("fml.ignoreInvalidMinecraftCertificates", "true"); + LogUtil.info("nat"); try { @@ -99,7 +110,7 @@ public static InternalLaunchProfile createInternalProfile(GameInfos infos, GameF */ public static ExternalLaunchProfile createExternalProfile(GameInfos infos, GameFolder folder, AuthInfos authInfos) throws LaunchException { - LogUtil.info("mc-int", infos.getGameVersion().getName()); + LogUtil.info("mc-ext", infos.getGameVersion().getName()); LogUtil.info("mc-check", infos.getGameDir().getAbsolutePath()); checkFolder(folder, infos.getGameDir()); @@ -110,10 +121,19 @@ public static ExternalLaunchProfile createExternalProfile(GameInfos infos, GameF constructor.add(Explorer.dir(infos.getGameDir()).sub(folder.getLibsFolder()).allRecursive().files().match("^(.*\\.((jar)$))*$").get()); constructor.add(Explorer.dir(infos.getGameDir()).get(folder.getMainJar())); - String mainClass = infos.getGameVersion().getGameType().getMainClass(infos); + String mainClass = infos.getGameTweaks() == null || infos.getGameTweaks().length == 0 ? infos.getGameVersion().getGameType().getMainClass(infos) : GameTweak.LAUNCHWRAPPER_MAIN_CLASS; String classpath = constructor.make(); List args = infos.getGameVersion().getGameType().getLaunchArgs(infos, folder, authInfos); - List vmArgs = Collections.singletonList("-Djava.library.path=" + Explorer.dir(infos.getGameDir()).sub(folder.getNativesFolder()).get().getAbsolutePath()); + List vmArgs = new ArrayList(); + vmArgs.add("-Djava.library.path=" + Explorer.dir(infos.getGameDir()).sub(folder.getNativesFolder()).get().getAbsolutePath()); + vmArgs.add("-Dfml.ignoreInvalidMinecraftCertificates=true"); + + if(infos.getGameTweaks() != null) + for (GameTweak tweak : infos.getGameTweaks()) + { + args.add("--tweakClass"); + args.add(tweak.getTweakClass(infos)); + } ExternalLaunchProfile profile = new ExternalLaunchProfile(mainClass, classpath, vmArgs, args, true, infos.getServerName(), infos.getGameDir()); diff --git a/src/main/java/fr/theshark34/openlauncherlib/util/CrashReporter.java b/src/main/java/fr/theshark34/openlauncherlib/util/CrashReporter.java index 24d1cb2..8f13aff 100644 --- a/src/main/java/fr/theshark34/openlauncherlib/util/CrashReporter.java +++ b/src/main/java/fr/theshark34/openlauncherlib/util/CrashReporter.java @@ -52,10 +52,12 @@ public class CrashReporter /** * Basic constructor * + * @param name The project name * @param dir The directory to write the crashes */ - public CrashReporter(File dir) + public CrashReporter(String name, File dir) { + this.name = name; this.dir = dir; } @@ -113,28 +115,43 @@ public File writeError(Exception e) throws IOException FileWriter fw = new FileWriter(file); fw.write(makeCrashReport(name, e)); - fw.write(e.toString()); fw.close(); return file; } + /** + * Return the crash directory + * @return The crash dir + */ public File getDir() { return dir; } + /** + * Set the directory where are the crashes + * @param dir The crash dir + */ public void setDir(File dir) { this.dir = dir; } + /** + * Return the reporter name + * @return The name + */ public String getName() { return name; } + /** + * Set the reporter name + * @param name The new name + */ public void setName(String name) { this.name = name; @@ -153,17 +170,27 @@ public static String makeCrashReport(String projectName, Exception e) DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = new Date(); - String report = "# " + projectName + " Crash Report\n" + - "#\n" + - "# At : " + dateFormat.format(date) + "\n" + - "#\n" + - "# Exception : " + e.getClass().getSimpleName() + "\n"; + String report = "# " + projectName + " Crash Report\n\r" + + "#\n\r" + + "# At : " + dateFormat.format(date) + "\n\r" + + "#\n\r" + + "# Exception : " + e.getClass().getSimpleName() + "\n\r"; - report += "\n# " + e.toString(); + report += "\n\r# " + e.toString(); StackTraceElement[] stackTrace = e.getStackTrace(); for (StackTraceElement element : stackTrace) - report += "\n# " + element; + report += "\n\r# " + element; + + Throwable cause = e.getCause(); + if (cause != null) + { + report += "\n\r# Caused by: " + cause.toString(); + + StackTraceElement[] causeStackTrace = cause.getStackTrace(); + for (StackTraceElement element : causeStackTrace) + report += "\n\r# " + element; + } return report; } diff --git a/src/main/java/fr/theshark34/openlauncherlib/util/ramselector/RamSelector.java b/src/main/java/fr/theshark34/openlauncherlib/util/ramselector/RamSelector.java index 3ea0af9..50cba7a 100644 --- a/src/main/java/fr/theshark34/openlauncherlib/util/ramselector/RamSelector.java +++ b/src/main/java/fr/theshark34/openlauncherlib/util/ramselector/RamSelector.java @@ -21,8 +21,10 @@ import fr.theshark34.openlauncherlib.util.CrashReporter; import fr.theshark34.openlauncherlib.util.LogUtil; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.Constructor; import javax.swing.JFrame; @@ -37,7 +39,13 @@ * @author TheShark34 * @version 3.0.0-BETA */ -public class RamSelector { +public class RamSelector +{ + + /** + * The RAM ! + */ + public static final String[] RAM_ARRAY = new String[]{"1Go", "2Go", "3Go", "4Go", "5Go", "6Go", "7Go", "8Go", "9Go", "10Go"}; /** * The file where to save the ram @@ -54,18 +62,13 @@ public class RamSelector { */ private AbstractOptionFrame frame; - /** - * The RAM ! - */ - public static final String[] RAM_ARRAY = new String[] {"1Go", "2Go", "3Go", "4Go", "5Go", "6Go", "7Go", "8Go", "9Go", "10Go"}; - /** * The RAM Selector with a file to save the RAM * - * @param file - * The file where to save the RAM + * @param file The file where to save the RAM */ - public RamSelector(File file) { + public RamSelector(File file) + { this.file = file; } @@ -73,33 +76,37 @@ public RamSelector(File file) { * Display the selector * * @return The displayed frame, an instance of the given - * frame class (by default OptionFrame) + * frame class (by default OptionFrame) * * @see #setFrameClass(Class) * @see #getFrameClass() */ - public JFrame display() { - if(frame == null) - try { + public JFrame display() + { + if (frame == null) + try + { Constructor[] contructors = frameClass.getDeclaredConstructors(); Constructor constructor = null; - for(Constructor c : contructors) - if(c.getParameterTypes().length == 1 && c.getParameterTypes()[0] == RamSelector.class) + for (Constructor c : contructors) + if (c.getParameterTypes().length == 1 && c.getParameterTypes()[0] == RamSelector.class) constructor = c; - if(constructor == null) + if (constructor == null) throw new IllegalStateException("Can't load the OptionFrame class, it needs to have a constructor with just a RamSelector as argument."); frame = (AbstractOptionFrame) constructor.newInstance(this); - } catch (Exception e) { + frame.setSelectedIndex(readRam()); + } + catch (Exception e) + { System.err.println("[OpenLauncherLib] Can't display the Ram Selector !"); System.err.println(CrashReporter.makeCrashReport("OpenLauncherLib Ram Selector", e)); return null; } - frame.setSelectedIndex(readRam()); frame.setVisible(true); return frame; @@ -110,11 +117,12 @@ public JFrame display() { * * @return An array of two strings containing the arguments */ - public String[] getRamArguments() { - int maxRam = Integer.parseInt(frame == null ? RAM_ARRAY[readRam()] : RAM_ARRAY[frame.getSelectedIndex()].replace("Go", "")) * 1024; + public String[] getRamArguments() + { + int maxRam = Integer.parseInt(frame == null ? RAM_ARRAY[readRam()].replace("Go", "") : RAM_ARRAY[frame.getSelectedIndex()].replace("Go", "")) * 1024; int minRam = maxRam - 512; - return new String[] {"-Xms" + minRam + "M", "-Xmx" + maxRam + "M"}; + return new String[]{"-Xms" + minRam + "M", "-Xmx" + maxRam + "M"}; } /** @@ -122,23 +130,32 @@ public String[] getRamArguments() { * * @return An int, of the selected index of RAM_ARRAY */ - private int readRam() { + private int readRam() + { BufferedReader br = null; - try { + try + { br = new BufferedReader(new FileReader(file)); String ramText = br.readLine(); - if(ramText != null) + if (ramText != null) return Integer.parseInt(ramText); else LogUtil.err("warn", "ram-empty"); - } catch (IOException e) { + } + catch (IOException e) + { System.err.println("[OpenLauncherLib] WARNING: Can't read ram : " + e); - } finally { - if(br != null) - try { + } + finally + { + if (br != null) + try + { br.close(); - } catch (IOException e) { + } + catch (IOException e) + { System.err.println("[OpenLauncherLib] WARNING: Can't close the file : " + e); } } @@ -149,49 +166,77 @@ private int readRam() { /** * Save the RAM */ - public void save() { - - } - - /** - * Set the file where to save the ram - * - * @param file - * The new file where the ram is saved - * @see #getFile() - */ - public void setFile(File file) { - this.file = file; + public void save() + { + BufferedWriter bw = null; + try + { + bw = new BufferedWriter(new FileWriter(file)); + bw.write(String.valueOf(frame.getSelectedIndex())); + } + catch (IOException e) + { + System.err.println("[OpenLauncherLib] WARNING: Can't save ram : " + e); + } + finally + { + if (bw != null) + try + { + bw.close(); + } + catch (IOException e) + { + System.err.println("[OpenLauncherLib] WARNING: Can't close the file : " + e); + } + } } /** * Return the file where to save the ram * * @return The file where the ram is saved + * * @see #setFile(File) */ - public File getFile() { + public File getFile() + { return file; } /** - * Set the class of the selector Frame (need to be a JFrame) + * Set the file where to save the ram * - * @param frameClass - * The new class of the selector - * @see #getFrameClass() + * @param file The new file where the ram is saved + * + * @see #getFile() */ - public void setFrameClass(Class frameClass) { - this.frameClass = frameClass; + public void setFile(File file) + { + this.file = file; } /** * Return the class of the selector Frame (? extends JFrame) * * @return The selector frame class + * * @see #setFrameClass(Class) */ - public Class getFrameClass() { + public Class getFrameClass() + { return frameClass; } + + /** + * Set the class of the selector Frame (need to be a JFrame) + * + * @param frameClass The new class of the selector + * + * @see #getFrameClass() + */ + public void setFrameClass(Class frameClass) + { + this.frameClass = frameClass; + } }