From fca4441229808891f460d5fbc4affd51e8896aa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Thu, 9 Jan 2014 01:22:34 +0100 Subject: Replace old launcher part with a shiny new one. No more garbage on the command line. --- depends/launcher/org/multimc/EntryPoint.java | 135 ++++++++++++++ depends/launcher/org/multimc/IconLoader.java | 132 ++++++++++++++ depends/launcher/org/multimc/Launcher.java | 22 +++ .../launcher/org/multimc/NotFoundException.java | 21 +++ depends/launcher/org/multimc/ParamBucket.java | 86 +++++++++ depends/launcher/org/multimc/ParseException.java | 22 +++ depends/launcher/org/multimc/Utils.java | 125 +++++++++++++ .../launcher/org/multimc/legacy/LegacyFrame.java | 112 ++++++++++++ .../org/multimc/legacy/LegacyLauncher.java | 178 +++++++++++++++++++ .../org/multimc/onesix/OneSixLauncher.java | 196 +++++++++++++++++++++ 10 files changed, 1029 insertions(+) create mode 100644 depends/launcher/org/multimc/EntryPoint.java create mode 100644 depends/launcher/org/multimc/IconLoader.java create mode 100644 depends/launcher/org/multimc/Launcher.java create mode 100644 depends/launcher/org/multimc/NotFoundException.java create mode 100644 depends/launcher/org/multimc/ParamBucket.java create mode 100644 depends/launcher/org/multimc/ParseException.java create mode 100644 depends/launcher/org/multimc/Utils.java create mode 100644 depends/launcher/org/multimc/legacy/LegacyFrame.java create mode 100644 depends/launcher/org/multimc/legacy/LegacyLauncher.java create mode 100644 depends/launcher/org/multimc/onesix/OneSixLauncher.java (limited to 'depends/launcher/org') diff --git a/depends/launcher/org/multimc/EntryPoint.java b/depends/launcher/org/multimc/EntryPoint.java new file mode 100644 index 00000000..c42e34e7 --- /dev/null +++ b/depends/launcher/org/multimc/EntryPoint.java @@ -0,0 +1,135 @@ +package org.multimc;/* + * Copyright 2012-2014 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.multimc.legacy.LegacyLauncher; +import org.multimc.onesix.OneSixLauncher; +import org.simplericity.macify.eawt.Application; +import org.simplericity.macify.eawt.DefaultApplication; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.*; + +public class EntryPoint +{ + private enum Action + { + Proceed, + Launch + } + + public static void main(String[] args) + { + // Set the OSX application icon first, if we are on OSX. + Application application = new DefaultApplication(); + if(application.isMac()) + { + try + { + BufferedImage image = ImageIO.read(new File("icon.png")); + application.setApplicationIconImage(image); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + EntryPoint listener = new EntryPoint(); + int retCode = listener.listen(); + if (retCode != 0) + { + System.out.println("Exiting with " + retCode); + System.exit(retCode); + } + } + + private Action parseLine(String inData) throws ParseException + { + String[] pair = inData.split(" ", 2); + if(pair.length != 2) + throw new ParseException(); + + String command = pair[0]; + String param = pair[1]; + + if(command.equals("launch")) + { + if(param.equals("legacy")) + { + m_launcher = new LegacyLauncher(); + System.out.println("Using legacy launcher."); + System.out.println(); + return Action.Launch; + } + if(param.equals("onesix")) + { + m_launcher = new OneSixLauncher(); + System.out.println("Using onesix launcher."); + System.out.println(); + return Action.Launch; + } + else + throw new ParseException(); + } + + m_params.add(command, param); + //System.out.println(command + " : " + param); + return Action.Proceed; + } + + public int listen() + { + BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in)); + boolean isListening = true; + // Main loop + while (isListening) + { + String inData=""; + try + { + // Read from the pipe one line at a time + inData = buffer.readLine(); + if (inData != null) + { + if(parseLine(inData) == Action.Launch) + { + isListening = false; + } + } + } + catch (IOException e) + { + e.printStackTrace(); + return 1; + } + catch (ParseException e) + { + e.printStackTrace(); + return 1; + } + } + if(m_launcher != null) + { + return m_launcher.launch(m_params); + } + System.err.println("No valid launcher implementation specified."); + return 1; + } + + private ParamBucket m_params = new ParamBucket(); + private org.multimc.Launcher m_launcher; +} diff --git a/depends/launcher/org/multimc/IconLoader.java b/depends/launcher/org/multimc/IconLoader.java new file mode 100644 index 00000000..f1638f3a --- /dev/null +++ b/depends/launcher/org/multimc/IconLoader.java @@ -0,0 +1,132 @@ +package org.multimc; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; + +/***************************************************************************** + * A convenience class for loading icons from images. + * + * Icons loaded from this class are formatted to fit within the required + * dimension (16x16, 32x32, or 128x128). If the source image is larger than the + * target dimension, it is shrunk down to the minimum size that will fit. If it + * is smaller, then it is only scaled up if the new scale can be a per-pixel + * linear scale (i.e., x2, x3, x4, etc). In both cases, the image's width/height + * ratio is kept the same as the source image. + * + * @author Chris Molini + *****************************************************************************/ +public class IconLoader +{ + /************************************************************************* + * Loads an icon in ByteBuffer form. + * + * @param filepath + * The location of the Image to use as an icon. + * + * @return An array of ByteBuffers containing the pixel data for the icon in + * various sizes (as recommended by the OS). + *************************************************************************/ + public static ByteBuffer[] load(String filepath) + { + BufferedImage image; + try { + image = ImageIO.read ( new File( filepath ) ); + } catch ( IOException e ) { + e.printStackTrace(); + return new ByteBuffer[0]; + } + ByteBuffer[] buffers; + buffers = new ByteBuffer[1]; + buffers[0] = loadInstance(image, 128); + return buffers; + } + + /************************************************************************* + * Copies the supplied image into a square icon at the indicated size. + * + * @param image + * The image to place onto the icon. + * @param dimension + * The desired size of the icon. + * + * @return A ByteBuffer of pixel data at the indicated size. + *************************************************************************/ + private static ByteBuffer loadInstance(BufferedImage image, int dimension) + { + BufferedImage scaledIcon = new BufferedImage(dimension, dimension, + BufferedImage.TYPE_INT_ARGB_PRE); + Graphics2D g = scaledIcon.createGraphics(); + double ratio = getIconRatio(image, scaledIcon); + double width = image.getWidth() * ratio; + double height = image.getHeight() * ratio; + g.drawImage(image, (int) ((scaledIcon.getWidth() - width) / 2), + (int) ((scaledIcon.getHeight() - height) / 2), (int) (width), + (int) (height), null); + g.dispose(); + + return convertToByteBuffer(scaledIcon); + } + + /************************************************************************* + * Gets the width/height ratio of the icon. This is meant to simplify + * scaling the icon to a new dimension. + * + * @param src + * The base image that will be placed onto the icon. + * @param icon + * The icon that will have the image placed on it. + * + * @return The amount to scale the source image to fit it onto the icon + * appropriately. + *************************************************************************/ + private static double getIconRatio(BufferedImage src, BufferedImage icon) + { + double ratio = 1; + if (src.getWidth() > icon.getWidth()) + ratio = (double) (icon.getWidth()) / src.getWidth(); + else + ratio = (int) (icon.getWidth() / src.getWidth()); + if (src.getHeight() > icon.getHeight()) + { + double r2 = (double) (icon.getHeight()) / src.getHeight(); + if (r2 < ratio) + ratio = r2; + } + else + { + double r2 = (int) (icon.getHeight() / src.getHeight()); + if (r2 < ratio) + ratio = r2; + } + return ratio; + } + + /************************************************************************* + * Converts a BufferedImage into a ByteBuffer of pixel data. + * + * @param image + * The image to convert. + * + * @return A ByteBuffer that contains the pixel data of the supplied image. + *************************************************************************/ + public static ByteBuffer convertToByteBuffer(BufferedImage image) + { + byte[] buffer = new byte[image.getWidth() * image.getHeight() * 4]; + int counter = 0; + for (int i = 0; i < image.getHeight(); i++) + for (int j = 0; j < image.getWidth(); j++) + { + int colorSpace = image.getRGB(j, i); + buffer[counter + 0] = (byte) ((colorSpace << 8) >> 24); + buffer[counter + 1] = (byte) ((colorSpace << 16) >> 24); + buffer[counter + 2] = (byte) ((colorSpace << 24) >> 24); + buffer[counter + 3] = (byte) (colorSpace >> 24); + counter += 4; + } + return ByteBuffer.wrap(buffer); + } +} \ No newline at end of file diff --git a/depends/launcher/org/multimc/Launcher.java b/depends/launcher/org/multimc/Launcher.java new file mode 100644 index 00000000..1aa2b21f --- /dev/null +++ b/depends/launcher/org/multimc/Launcher.java @@ -0,0 +1,22 @@ +/* + * Copyright 2012-2014 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.multimc; + +public interface Launcher +{ + abstract int launch(ParamBucket params); +} diff --git a/depends/launcher/org/multimc/NotFoundException.java b/depends/launcher/org/multimc/NotFoundException.java new file mode 100644 index 00000000..fe154a2f --- /dev/null +++ b/depends/launcher/org/multimc/NotFoundException.java @@ -0,0 +1,21 @@ +/* + * Copyright 2012-2014 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.multimc; + +public class NotFoundException extends Exception +{ +} diff --git a/depends/launcher/org/multimc/ParamBucket.java b/depends/launcher/org/multimc/ParamBucket.java new file mode 100644 index 00000000..2e197d9f --- /dev/null +++ b/depends/launcher/org/multimc/ParamBucket.java @@ -0,0 +1,86 @@ +/* + * Copyright 2012-2014 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.multimc; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class ParamBucket +{ + public void add(String key, String value) + { + List coll = null; + if(!m_params.containsKey(key)) + { + coll = new ArrayList(); + m_params.put(key, coll); + } + else + { + coll = m_params.get(key); + } + coll.add(value); + } + + public List all(String key) throws NotFoundException + { + if(!m_params.containsKey(key)) + throw new NotFoundException(); + return m_params.get(key); + } + + public List allSafe(String key, List def) + { + if(!m_params.containsKey(key) || m_params.get(key).size() < 1) + { + return def; + } + return m_params.get(key); + } + + public List allSafe(String key) + { + return allSafe(key, new ArrayList()); + } + + public String first(String key) throws NotFoundException + { + List list = all(key); + if(list.size() < 1) + { + throw new NotFoundException(); + } + return list.get(0); + } + + public String firstSafe(String key, String def) + { + if(!m_params.containsKey(key) || m_params.get(key).size() < 1) + { + return def; + } + return m_params.get(key).get(0); + } + + public String firstSafe(String key) + { + return firstSafe(key, ""); + } + + private HashMap> m_params = new HashMap>(); +} diff --git a/depends/launcher/org/multimc/ParseException.java b/depends/launcher/org/multimc/ParseException.java new file mode 100644 index 00000000..d9e8e53e --- /dev/null +++ b/depends/launcher/org/multimc/ParseException.java @@ -0,0 +1,22 @@ +/* + * Copyright 2012-2014 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.multimc; + +public class ParseException extends java.lang.Exception +{ + +} diff --git a/depends/launcher/org/multimc/Utils.java b/depends/launcher/org/multimc/Utils.java new file mode 100644 index 00000000..ba90c07f --- /dev/null +++ b/depends/launcher/org/multimc/Utils.java @@ -0,0 +1,125 @@ +/* + * Copyright 2012-2014 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.multimc; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Arrays; +import java.util.List; + +public class Utils +{ + /** + * Adds the specified library to the classpath + * + * @param s the path to add + * @throws Exception + */ + public static void addToClassPath(String s) throws Exception + { + File f = new File(s); + URL u = f.toURI().toURL(); + URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); + Class urlClass = URLClassLoader.class; + Method method = urlClass.getDeclaredMethod("addURL", new Class[]{URL.class}); + method.setAccessible(true); + method.invoke(urlClassLoader, new Object[]{u}); + } + + /** + * Adds many libraries to the classpath + * + * @param jars the paths to add + */ + public static boolean addToClassPath(List jars) + { + boolean pure = true; + // initialize the class path + for (String jar : jars) + { + try + { + Utils.addToClassPath(jar); + } catch (Exception e) + { + System.err.println("Unable to load: " + jar); + e.printStackTrace(System.err); + pure = false; + } + } + return pure; + } + + /** + * Adds the specified path to the java library path + * + * @param pathToAdd the path to add + * @throws Exception + */ + @Deprecated public static void addLibraryPath(String pathToAdd) throws Exception + { + final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths"); + usrPathsField.setAccessible(true); + + //get array of paths + final String[] paths = (String[]) usrPathsField.get(null); + + //check if the path to add is already present + for (String path : paths) + { + if (path.equals(pathToAdd)) + { + return; + } + } + + //add the new path + final String[] newPaths = Arrays.copyOf(paths, paths.length + 1); + newPaths[newPaths.length - 1] = pathToAdd; + usrPathsField.set(null, newPaths); + } + + /** + * Finds a field that looks like a Minecraft base folder in a supplied class + * + * @param mc the class to scan + */ + public static Field getMCPathField(Class mc) + { + Field[] fields = mc.getDeclaredFields(); + + for (Field f : fields) + { + if (f.getType() != File.class) + { + // Has to be File + continue; + } + if (f.getModifiers() != (Modifier.PRIVATE + Modifier.STATIC)) + { + // And Private Static. + continue; + } + return f; + } + return null; + } +} diff --git a/depends/launcher/org/multimc/legacy/LegacyFrame.java b/depends/launcher/org/multimc/legacy/LegacyFrame.java new file mode 100644 index 00000000..c3c0cafc --- /dev/null +++ b/depends/launcher/org/multimc/legacy/LegacyFrame.java @@ -0,0 +1,112 @@ +package org.multimc.legacy;/* + * Copyright 2012-2014 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import net.minecraft.Launcher; + +import javax.imageio.ImageIO; +import java.applet.Applet; +import java.awt.*; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; + +public class LegacyFrame extends Frame implements WindowListener +{ + private Launcher appletWrap = null; + public LegacyFrame(String title) + { + super ( title ); + BufferedImage image; + try { + image = ImageIO.read ( new File ( "icon.png" ) ); + setIconImage ( image ); + } catch ( IOException e ) { + e.printStackTrace(); + } + this.addWindowListener ( this ); + } + + public void start ( Applet mcApplet, String user, String session, Dimension winSize, boolean maximize ) + { + try { + appletWrap = new Launcher( mcApplet, new URL ( "http://www.minecraft.net/game" ) ); + } catch ( MalformedURLException ignored ) {} + appletWrap.setParameter ( "username", user ); + appletWrap.setParameter ( "sessionid", session ); + appletWrap.setParameter ( "stand-alone", "true" ); // Show the quit button. + appletWrap.setParameter ( "demo", "false" ); + appletWrap.setParameter("fullscreen", "false"); + mcApplet.setStub(appletWrap); + this.add ( appletWrap ); + appletWrap.setPreferredSize ( winSize ); + this.pack(); + this.setLocationRelativeTo ( null ); + this.setResizable ( true ); + if ( maximize ) { + this.setExtendedState ( MAXIMIZED_BOTH ); + } + validate(); + appletWrap.init(); + appletWrap.start(); + setVisible ( true ); + } + + @Override + public void windowActivated ( WindowEvent e ) {} + + @Override + public void windowClosed ( WindowEvent e ) {} + + @Override + public void windowClosing ( WindowEvent e ) + { + new Thread() { + public void run() { + try { + Thread.sleep ( 30000L ); + } catch ( InterruptedException localInterruptedException ) { + localInterruptedException.printStackTrace(); + } + System.out.println ( "FORCING EXIT!" ); + System.exit ( 0 ); + } + } + .start(); + + if ( appletWrap != null ) { + appletWrap.stop(); + appletWrap.destroy(); + } + // old minecraft versions can hang without this >_< + System.exit ( 0 ); + } + + @Override + public void windowDeactivated ( WindowEvent e ) {} + + @Override + public void windowDeiconified ( WindowEvent e ) {} + + @Override + public void windowIconified ( WindowEvent e ) {} + + @Override + public void windowOpened ( WindowEvent e ) {} +} diff --git a/depends/launcher/org/multimc/legacy/LegacyLauncher.java b/depends/launcher/org/multimc/legacy/LegacyLauncher.java new file mode 100644 index 00000000..6a0a3014 --- /dev/null +++ b/depends/launcher/org/multimc/legacy/LegacyLauncher.java @@ -0,0 +1,178 @@ +package org.multimc.legacy;/* + * Copyright 2012-2014 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.multimc.Launcher; +import org.multimc.NotFoundException; +import org.multimc.ParamBucket; +import org.multimc.Utils; + +import java.applet.Applet; +import java.awt.*; +import java.io.File; +import java.lang.reflect.Field; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; + +public class LegacyLauncher implements Launcher +{ + @Override + public int launch(ParamBucket params) + { + String userName, sessionId, windowTitle, windowParams, lwjgl; + String mainClass = "net.minecraft.client.Minecraft"; + try + { + userName = params.first("userName"); + sessionId = params.first("sessionId"); + windowTitle = params.first("windowTitle"); + windowParams = params.first("windowParams"); + lwjgl = params.first("lwjgl"); + } catch (NotFoundException e) + { + System.err.println("Not enough arguments."); + return -1; + } + + String cwd = System.getProperty("user.dir"); + Dimension winSize = new Dimension(854, 480); + boolean maximize = false; + + String[] dimStrings = windowParams.split("x"); + + if (windowParams.equalsIgnoreCase("max")) + { + maximize = true; + } + else if (dimStrings.length == 2) + { + try + { + winSize = new Dimension(Integer.parseInt(dimStrings[0]), Integer.parseInt(dimStrings[1])); + } catch (NumberFormatException ignored) {} + } + + File binDir = new File(cwd, "bin"); + File lwjglDir; + if (lwjgl.equalsIgnoreCase("Mojang")) + { + lwjglDir = binDir; + } + else + { + lwjglDir = new File(lwjgl); + } + + URL[] classpath; + { + try + { + classpath = new URL[] + { + new File(binDir, "minecraft.jar").toURI().toURL(), + new File(lwjglDir, "lwjgl.jar").toURI().toURL(), + new File(lwjglDir, "lwjgl_util.jar").toURI().toURL(), + new File(lwjglDir, "jinput.jar").toURI().toURL(), + }; + } catch (MalformedURLException e) + { + System.err.println("Class path entry is badly formed:"); + e.printStackTrace(System.err); + return -1; + } + } + + String nativesDir = new File(lwjglDir, "natives").toString(); + + System.setProperty("org.lwjgl.librarypath", nativesDir); + System.setProperty("net.java.games.input.librarypath", nativesDir); + + // print the pretty things + { + System.out.println("Main Class:"); + System.out.println(mainClass); + System.out.println(); + + System.out.println("Class Path:"); + for (URL s : classpath) + { + System.out.println(s); + } + System.out.println(); + + System.out.println("Native Path:"); + System.out.println(nativesDir); + System.out.println(); + } + + URLClassLoader cl = new URLClassLoader(classpath, LegacyLauncher.class.getClassLoader()); + + // Get the Minecraft Class and set the base folder + Class mc; + try + { + mc = cl.loadClass(mainClass); + + Field f = Utils.getMCPathField(mc); + + if (f == null) + { + System.err.println("Could not find Minecraft path field. Launch failed."); + return -1; + } + + f.setAccessible(true); + f.set(null, new File(cwd)); + } catch (Exception e) + { + System.err.println("Could not set base folder. Failed to find/access Minecraft main class:"); + e.printStackTrace(System.err); + return -1; + } + + System.setProperty("minecraft.applet.TargetDirectory", cwd); + + String[] mcArgs = new String[2]; + mcArgs[0] = userName; + mcArgs[1] = sessionId; + + System.out.println("Launching with applet wrapper..."); + try + { + Class MCAppletClass = cl.loadClass("net.minecraft.client.MinecraftApplet"); + Applet mcappl = (Applet) MCAppletClass.newInstance(); + LegacyFrame mcWindow = new LegacyFrame(windowTitle); + mcWindow.start(mcappl, userName, sessionId, winSize, maximize); + } catch (Exception e) + { + System.err.println("Applet wrapper failed:"); + e.printStackTrace(System.err); + System.err.println(); + System.out.println("Falling back to compatibility mode."); + try + { + mc.getMethod("main", String[].class).invoke(null, (Object) mcArgs); + } catch (Exception e1) + { + System.err.println("Failed to invoke the Minecraft main class:"); + e1.printStackTrace(System.err); + return -1; + } + } + + return 0; + } +} diff --git a/depends/launcher/org/multimc/onesix/OneSixLauncher.java b/depends/launcher/org/multimc/onesix/OneSixLauncher.java new file mode 100644 index 00000000..2232eeba --- /dev/null +++ b/depends/launcher/org/multimc/onesix/OneSixLauncher.java @@ -0,0 +1,196 @@ +/* Copyright 2012-2014 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.multimc.onesix; + +import org.multimc.*; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.List; + +public class OneSixLauncher implements Launcher +{ + @Override + public int launch(ParamBucket params) + { + // get and process the launch script params + List libraries; + List mcparams; + List mods; + String mainClass; + String natives; + final String windowTitle; + String windowParams; + try + { + libraries = params.all("cp"); + mcparams = params.all("param"); + mainClass = params.first("mainClass"); + mods = params.allSafe("mods", new ArrayList()); + natives = params.first("natives"); + windowTitle = params.first("windowTitle"); + // windowParams = params.first("windowParams"); + } catch (NotFoundException e) + { + System.err.println("Not enough arguments."); + e.printStackTrace(System.err); + return -1; + } + + List allJars = new ArrayList(); + allJars.addAll(mods); + allJars.addAll(libraries); + + if(!Utils.addToClassPath(allJars)) + { + System.err.println("Halting launch due to previous errors."); + return -1; + } + + final ClassLoader cl = ClassLoader.getSystemClassLoader(); + + // print the pretty things + { + System.out.println("Main Class:"); + System.out.println(mainClass); + System.out.println(); + + System.out.println("Libraries:"); + for (String s : libraries) + { + System.out.println(s); + } + System.out.println(); + + if(mods.size() > 0) + { + System.out.println("Class Path Mods:"); + for (String s : mods) + { + System.out.println(s); + } + System.out.println(); + } + + System.out.println("Params:"); + System.out.println(mcparams.toString()); + System.out.println(); + } + + // set up the natives path(s). + System.setProperty("java.library.path", natives ); + Field fieldSysPath; + try + { + fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths"); + fieldSysPath.setAccessible( true ); + fieldSysPath.set( null, null ); + } catch (Exception e) + { + System.err.println("Failed to set the native library path:"); + e.printStackTrace(System.err); + return -1; + } + + // Get the Minecraft Class. + Class mc; + try + { + mc = cl.loadClass(mainClass); + } catch (ClassNotFoundException e) + { + System.err.println("Failed to find Minecraft main class:"); + e.printStackTrace(System.err); + return -1; + } + + // get the main method. + Method meth; + try + { + meth = mc.getMethod("main", String[].class); + } catch (NoSuchMethodException e) + { + System.err.println("Failed to acquire the main method:"); + e.printStackTrace(System.err); + return -1; + } + + // FIXME: works only on linux, we need a better solution +/* + final java.nio.ByteBuffer[] icons = IconLoader.load("icon.png"); + new Thread() { + public void run() { + ClassLoader cl = ClassLoader.getSystemClassLoader(); + try + { + Class Display; + Method isCreated; + Method setTitle; + Method setIcon; + + Display = cl.loadClass("org.lwjgl.opengl.Display"); + isCreated = Display.getMethod("isCreated"); + setTitle = Display.getMethod("setTitle", String.class); + setIcon = Display.getMethod("setIcon", java.nio.ByteBuffer[].class); + + // set the window title? Maybe? + while(!(Boolean) isCreated.invoke(null)) + { + try + { + Thread.sleep(150); + } catch (InterruptedException ignored) {} + } + // Give it a bit more time ;) + Thread.sleep(150); + // set the title + setTitle.invoke(null,windowTitle); + // only set icon when there's actually something to set... + if(icons.length > 0) + { + setIcon.invoke(null,(Object)icons); + } + } + catch (Exception e) + { + System.err.println("Couldn't set window icon or title."); + e.printStackTrace(System.err); + } + } + } + .start(); +*/ + // start Minecraft + String[] paramsArray = mcparams.toArray(new String[mcparams.size()]); // init params accordingly + try + { + meth.invoke(null, (Object) paramsArray); // static method doesn't have an instance + } catch (Exception e) + { + System.err.println("Failed to start Minecraft:"); + e.printStackTrace(System.err); + return -1; + } + return 0; + } +} -- cgit v1.2.3 From 555cbe00ced6097202f148f200811acc6b1eeb4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Tue, 14 Jan 2014 01:13:35 +0100 Subject: Do not use the java checker during instance update --- depends/launcher/org/multimc/Utils.java | 31 ++++++++++++++++++++++ .../org/multimc/onesix/OneSixLauncher.java | 17 ++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) (limited to 'depends/launcher/org') diff --git a/depends/launcher/org/multimc/Utils.java b/depends/launcher/org/multimc/Utils.java index ba90c07f..c4d55b6e 100644 --- a/depends/launcher/org/multimc/Utils.java +++ b/depends/launcher/org/multimc/Utils.java @@ -27,6 +27,37 @@ import java.util.List; public class Utils { + /** + * Combine two parts of a path. + * @param path1 + * @param path2 + * @return the paths, combined + */ + public static String combine (String path1, String path2) + { + File file1 = new File(path1); + File file2 = new File(file1, path2); + return file2.getPath(); + } + + /** + * Join a list of strings into a string using a separator! + * @param strings the string list to join + * @param separator the glue + * @return the result. + */ + public static String join (List strings, String separator) + { + StringBuilder sb = new StringBuilder(); + String sep = ""; + for(String s: strings) + { + sb.append(sep).append(s); + sep = separator; + } + return sb.toString(); + } + /** * Adds the specified library to the classpath * diff --git a/depends/launcher/org/multimc/onesix/OneSixLauncher.java b/depends/launcher/org/multimc/onesix/OneSixLauncher.java index 2232eeba..f25a3245 100644 --- a/depends/launcher/org/multimc/onesix/OneSixLauncher.java +++ b/depends/launcher/org/multimc/onesix/OneSixLauncher.java @@ -66,7 +66,11 @@ public class OneSixLauncher implements Launcher return -1; } - final ClassLoader cl = ClassLoader.getSystemClassLoader(); + String property = System.getProperty("os.arch"); + List allNativePaths = new ArrayList(); + boolean is_64 = property.equalsIgnoreCase("x86_64") || property.equalsIgnoreCase("amd64"); + allNativePaths.add(natives); + allNativePaths.add(Utils.combine(natives, is_64 ? "64" : "32")); // print the pretty things { @@ -74,6 +78,13 @@ public class OneSixLauncher implements Launcher System.out.println(mainClass); System.out.println(); + System.out.println("Native paths:"); + for (String s : allNativePaths) + { + System.out.println(s); + } + System.out.println(); + System.out.println("Libraries:"); for (String s : libraries) { @@ -96,8 +107,10 @@ public class OneSixLauncher implements Launcher System.out.println(); } + final ClassLoader cl = ClassLoader.getSystemClassLoader(); + // set up the natives path(s). - System.setProperty("java.library.path", natives ); + System.setProperty("java.library.path", Utils.join(allNativePaths, String.valueOf(File.pathSeparatorChar)) ); Field fieldSysPath; try { -- cgit v1.2.3 From 188d0d58865f5e134b5803bda2cd631a61cf2915 Mon Sep 17 00:00:00 2001 From: Orochimarufan Date: Fri, 17 Jan 2014 22:55:10 +0100 Subject: Improve Console window output. -> Log Pre- and Post-Launch command happenings -> Enable the java part to specify the level TODO: fix logging with mc 1.7's log4j logging infrastructure Signed-off-by: Orochimarufan --- depends/launcher/org/multimc/EntryPoint.java | 8 +++--- depends/launcher/org/multimc/Utils.java | 23 +++++++++++++++++ .../org/multimc/legacy/LegacyLauncher.java | 28 ++++++++++---------- .../org/multimc/onesix/OneSixLauncher.java | 30 +++++++++++----------- 4 files changed, 56 insertions(+), 33 deletions(-) (limited to 'depends/launcher/org') diff --git a/depends/launcher/org/multimc/EntryPoint.java b/depends/launcher/org/multimc/EntryPoint.java index c42e34e7..83f232f1 100644 --- a/depends/launcher/org/multimc/EntryPoint.java +++ b/depends/launcher/org/multimc/EntryPoint.java @@ -71,15 +71,15 @@ public class EntryPoint if(param.equals("legacy")) { m_launcher = new LegacyLauncher(); - System.out.println("Using legacy launcher."); - System.out.println(); + Utils.log("Using legacy launcher."); + Utils.log(); return Action.Launch; } if(param.equals("onesix")) { m_launcher = new OneSixLauncher(); - System.out.println("Using onesix launcher."); - System.out.println(); + Utils.log("Using onesix launcher."); + Utils.log(); return Action.Launch; } else diff --git a/depends/launcher/org/multimc/Utils.java b/depends/launcher/org/multimc/Utils.java index c4d55b6e..df0ef861 100644 --- a/depends/launcher/org/multimc/Utils.java +++ b/depends/launcher/org/multimc/Utils.java @@ -153,4 +153,27 @@ public class Utils } return null; } + + /** + * Log to the MultiMC console + * + * @param message A String containing the message + * @param level A String containing the level name. See MinecraftProcess::getLevel() + */ + public static void log(String message, String level) + { + // Kinda dirty + String tag = "!![" + level + "]!"; + System.out.println(tag + message.replace("\n", "\n" + tag)); + } + + public static void log(String message) + { + log(message, "MultiMC"); + } + + public static void log() + { + System.out.println(); + } } diff --git a/depends/launcher/org/multimc/legacy/LegacyLauncher.java b/depends/launcher/org/multimc/legacy/LegacyLauncher.java index 6a0a3014..1ca37c4a 100644 --- a/depends/launcher/org/multimc/legacy/LegacyLauncher.java +++ b/depends/launcher/org/multimc/legacy/LegacyLauncher.java @@ -102,20 +102,20 @@ public class LegacyLauncher implements Launcher // print the pretty things { - System.out.println("Main Class:"); - System.out.println(mainClass); - System.out.println(); + Utils.log("Main Class:"); + Utils.log(" " + mainClass); + Utils.log(); - System.out.println("Class Path:"); + Utils.log("Class Path:"); for (URL s : classpath) { - System.out.println(s); + Utils.log(" " + s); } - System.out.println(); + Utils.log(); - System.out.println("Native Path:"); - System.out.println(nativesDir); - System.out.println(); + Utils.log("Native Path:"); + Utils.log(" " + nativesDir); + Utils.log(); } URLClassLoader cl = new URLClassLoader(classpath, LegacyLauncher.class.getClassLoader()); @@ -149,7 +149,7 @@ public class LegacyLauncher implements Launcher mcArgs[0] = userName; mcArgs[1] = sessionId; - System.out.println("Launching with applet wrapper..."); + Utils.log("Launching with applet wrapper..."); try { Class MCAppletClass = cl.loadClass("net.minecraft.client.MinecraftApplet"); @@ -158,16 +158,16 @@ public class LegacyLauncher implements Launcher mcWindow.start(mcappl, userName, sessionId, winSize, maximize); } catch (Exception e) { - System.err.println("Applet wrapper failed:"); + Utils.log("Applet wrapper failed:", "Error"); e.printStackTrace(System.err); - System.err.println(); - System.out.println("Falling back to compatibility mode."); + Utils.log(); + Utils.log("Falling back to compatibility mode."); try { mc.getMethod("main", String[].class).invoke(null, (Object) mcArgs); } catch (Exception e1) { - System.err.println("Failed to invoke the Minecraft main class:"); + Utils.log("Failed to invoke the Minecraft main class:", "Fatal"); e1.printStackTrace(System.err); return -1; } diff --git a/depends/launcher/org/multimc/onesix/OneSixLauncher.java b/depends/launcher/org/multimc/onesix/OneSixLauncher.java index f25a3245..d6c80b67 100644 --- a/depends/launcher/org/multimc/onesix/OneSixLauncher.java +++ b/depends/launcher/org/multimc/onesix/OneSixLauncher.java @@ -74,37 +74,37 @@ public class OneSixLauncher implements Launcher // print the pretty things { - System.out.println("Main Class:"); - System.out.println(mainClass); - System.out.println(); + Utils.log("Main Class:"); + Utils.log(" " + mainClass); + Utils.log(); - System.out.println("Native paths:"); + Utils.log("Native paths:"); for (String s : allNativePaths) { - System.out.println(s); + Utils.log(" " + s); } - System.out.println(); + Utils.log(); - System.out.println("Libraries:"); + Utils.log("Libraries:"); for (String s : libraries) { - System.out.println(s); + Utils.log(" " + s); } - System.out.println(); + Utils.log(); if(mods.size() > 0) { - System.out.println("Class Path Mods:"); + Utils.log("Class Path Mods:"); for (String s : mods) { - System.out.println(s); + Utils.log(" " + s); } - System.out.println(); + Utils.log(); } - System.out.println("Params:"); - System.out.println(mcparams.toString()); - System.out.println(); + Utils.log("Params:"); + Utils.log(" " + mcparams.toString()); + Utils.log(); } final ClassLoader cl = ClassLoader.getSystemClassLoader(); -- cgit v1.2.3 From ecc80bd763111b0e368aa80366bd8382cd814ee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sat, 18 Jan 2014 03:32:31 +0100 Subject: Change the native extraction/loading logic. --- depends/launcher/org/multimc/onesix/OneSixLauncher.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'depends/launcher/org') diff --git a/depends/launcher/org/multimc/onesix/OneSixLauncher.java b/depends/launcher/org/multimc/onesix/OneSixLauncher.java index d6c80b67..c1676c94 100644 --- a/depends/launcher/org/multimc/onesix/OneSixLauncher.java +++ b/depends/launcher/org/multimc/onesix/OneSixLauncher.java @@ -70,7 +70,7 @@ public class OneSixLauncher implements Launcher List allNativePaths = new ArrayList(); boolean is_64 = property.equalsIgnoreCase("x86_64") || property.equalsIgnoreCase("amd64"); allNativePaths.add(natives); - allNativePaths.add(Utils.combine(natives, is_64 ? "64" : "32")); + allNativePaths.add(natives + "/" + (is_64 ? "64" : "32")); // print the pretty things { @@ -110,7 +110,8 @@ public class OneSixLauncher implements Launcher final ClassLoader cl = ClassLoader.getSystemClassLoader(); // set up the natives path(s). - System.setProperty("java.library.path", Utils.join(allNativePaths, String.valueOf(File.pathSeparatorChar)) ); + String libpath = Utils.join(allNativePaths, String.valueOf(File.pathSeparatorChar)); + System.setProperty("java.library.path", libpath); Field fieldSysPath; try { -- cgit v1.2.3