/* * Forge Mod Loader * Copyright (c) 2012-2013 cpw. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v2.1 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * * Contributors: * cpw - implementation */ package cpw.mods.fml.relauncher; import java.applet.Applet; import java.io.File; import java.lang.reflect.Method; import java.net.URLClassLoader; import javax.swing.JDialog; import javax.swing.JOptionPane; public class FMLRelauncher { private static FMLRelauncher INSTANCE; public static String logFileNamePattern; private static String side; private RelaunchClassLoader classLoader; private Object newApplet; private Class super Object> appletClass; JDialog popupWindow; public static void handleClientRelaunch(ArgsWrapper wrap) { logFileNamePattern = "ForgeModLoader-client-%g.log"; side = "CLIENT"; instance().relaunchClient(wrap); } public static void handleServerRelaunch(ArgsWrapper wrap) { logFileNamePattern = "ForgeModLoader-server-%g.log"; side = "SERVER"; instance().relaunchServer(wrap); } static FMLRelauncher instance() { if (INSTANCE == null) { INSTANCE = new FMLRelauncher(); } return INSTANCE; } private FMLRelauncher() { URLClassLoader ucl = (URLClassLoader) getClass().getClassLoader(); classLoader = new RelaunchClassLoader(ucl.getURLs()); } private void showWindow(boolean showIt) { if (RelaunchLibraryManager.downloadMonitor != null) { return; } try { if (showIt) { RelaunchLibraryManager.downloadMonitor = new Downloader(); popupWindow = (JDialog) RelaunchLibraryManager.downloadMonitor.makeDialog(); } else { RelaunchLibraryManager.downloadMonitor = new DummyDownloader(); } } catch (Throwable e) { if (RelaunchLibraryManager.downloadMonitor == null) { RelaunchLibraryManager.downloadMonitor = new DummyDownloader(); e.printStackTrace(); } else { RelaunchLibraryManager.downloadMonitor.makeHeadless(); } popupWindow = null; } } private void relaunchClient(ArgsWrapper wrap) { showWindow(true); // Now we re-inject the home into the "new" minecraft under our control Class super Object> client; try { File minecraftHome = computeExistingClientHome(); setupHome(minecraftHome); client = setupNewClientHome(minecraftHome); } finally { if (popupWindow != null) { popupWindow.setVisible(false); popupWindow.dispose(); } } if (RelaunchLibraryManager.downloadMonitor.shouldStopIt()) { System.exit(1); } try { ReflectionHelper.findMethod(client, null, new String[] { "fmlReentry" }, ArgsWrapper.class).invoke(null, wrap); } catch (Exception e) { e.printStackTrace(); // Hmmm } } private Class super Object> setupNewClientHome(File minecraftHome) { Class super Object> client = ReflectionHelper.getClass(classLoader, "net.minecraft.client.Minecraft"); ReflectionHelper.setPrivateValue(client, null, minecraftHome, "field_" + "71463_am" /*Separate that so that MCP's updatenames does not replace it*/, "an", "minecraftDir"); return client; } private void relaunchServer(ArgsWrapper wrap) { showWindow(false); // Now we re-inject the home into the "new" minecraft under our control Class super Object> server; File minecraftHome = new File("."); setupHome(minecraftHome); server = ReflectionHelper.getClass(classLoader, "net.minecraft.server.MinecraftServer"); try { ReflectionHelper.findMethod(server, null, new String[] { "fmlReentry" }, ArgsWrapper.class).invoke(null, wrap); } catch (Exception e) { e.printStackTrace(); } } private void setupHome(File minecraftHome) { FMLInjectionData.build(minecraftHome, classLoader); FMLRelaunchLog.minecraftHome = minecraftHome; FMLRelaunchLog.info("Forge Mod Loader version %s.%s.%s.%s for Minecraft %s loading", FMLInjectionData.major, FMLInjectionData.minor, FMLInjectionData.rev, FMLInjectionData.build, FMLInjectionData.mccversion, FMLInjectionData.mcpversion); FMLRelaunchLog.info("Java is %s, version %s, running on %s:%s:%s, installed at %s", System.getProperty("java.vm.name"), System.getProperty("java.version"), System.getProperty("os.name"), System.getProperty("os.arch"), System.getProperty("os.version"), System.getProperty("java.home")); FMLRelaunchLog.fine("Java classpath at launch is %s", System.getProperty("java.class.path")); FMLRelaunchLog.fine("Java library path at launch is %s", System.getProperty("java.library.path")); try { RelaunchLibraryManager.handleLaunch(minecraftHome, classLoader); } catch (Throwable t) { if (popupWindow != null) { try { String logFile = new File(minecraftHome, "ForgeModLoader-client-0.log").getCanonicalPath(); JOptionPane.showMessageDialog(popupWindow, String.format( "