package cpw.mods.fml.relauncher; import java.awt.HeadlessException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import java.math.BigInteger; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.net.URLConnection; import java.nio.MappedByteBuffer; import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; import java.nio.channels.FileChannel.MapMode; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.jar.Attributes; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import javax.swing.ProgressMonitorInputStream; import cpw.mods.fml.common.discovery.ModCandidate; public class RelaunchLibraryManager { private static String[] rootPlugins = { "cpw.mods.fml.relauncher.FMLCorePlugin" , "net.minecraftforge.classloading.FMLForgePlugin" }; private static final String HEXES = "0123456789abcdef"; private static List loadedLibraries = new ArrayList(); public static void handleLaunch(File mcDir, RelaunchClassLoader actualClassLoader) { List loadPlugins = new ArrayList(); List libraries = new ArrayList(); for (String s : rootPlugins) { try { IFMLLoadingPlugin plugin = (IFMLLoadingPlugin) Class.forName(s, true, actualClassLoader).newInstance(); loadPlugins.add(plugin); for (String libName : plugin.getLibraryRequestClass()) { libraries.add((ILibrarySet) Class.forName(libName, true, actualClassLoader).newInstance()); } } catch (Exception e) { // HMMM } } if (loadPlugins.isEmpty()) { throw new RuntimeException("A fatal error has occured - no valid fml load plugin was found - this is a completely corrupt FML installation."); } // Now that we have the root plugins loaded - lets see what else might be around discoverCoreMods(mcDir, actualClassLoader, loadPlugins, libraries); List caughtErrors = new ArrayList(); try { File libDir; try { libDir = setupLibDir(mcDir); } catch (Exception e) { caughtErrors.add(e); return; } for (ILibrarySet lib : libraries) { for (int i=0; i"); FMLLog.severe("The following is the errors that caused the setup to fail. " + "They may help you diagnose and resolve the issue"); for (Throwable t : caughtErrors) { FMLLog.severe(t.getMessage()); } FMLLog.severe("<<< ==== >>>"); FMLLog.severe("The following is diagnostic information for developers to review."); for (Throwable t : caughtErrors) { FMLLog.log(Level.SEVERE, t, "Error details"); } throw new RuntimeException("A fatal error occured and FML cannot continue"); } } for (IFMLLoadingPlugin plug : loadPlugins) { if (plug.getASMTransformerClass()!=null) { for (String xformClass : plug.getASMTransformerClass()) { actualClassLoader.registerTransformer(xformClass); } } } try { Class loaderClazz = Class.forName("cpw.mods.fml.common.Loader", true, actualClassLoader); Method m = loaderClazz.getMethod("injectData", Object[].class); m.invoke(null, (Object)FMLVersionData.data()); } catch (Exception e) { // Load in the Loader, make sure he's ready to roll - this will initialize most of the rest of minecraft here throw new RuntimeException(e); } } private static void discoverCoreMods(File mcDir, RelaunchClassLoader classLoader, List loadPlugins, List libraries) { File coreMods = setupCoreModDir(mcDir); FilenameFilter ff = new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(".jar"); } }; File[] coreModList = coreMods.listFiles(ff); Arrays.sort(coreModList); for (File coreMod : coreModList) { FMLLog.fine("Found a candidate coremod %s", coreMod.getName()); JarFile jar; Attributes mfAttributes; try { jar = new JarFile(coreMod); mfAttributes = jar.getManifest().getMainAttributes(); } catch (IOException ioe) { FMLLog.log(Level.SEVERE, ioe, "Unable to read the coremod jar file %s - ignoring", coreMod.getName()); continue; } String fmlCorePlugin = mfAttributes.getValue("FMLCorePlugin"); if (fmlCorePlugin == null) { FMLLog.severe("The coremod %s does not contain a valid jar manifest- it will be ignored", coreMod.getName()); continue; } // String className = fmlCorePlugin.replace('.', '/').concat(".class"); // JarEntry ent = jar.getJarEntry(className); // if (ent ==null) // { // FMLLog.severe("The coremod %s specified %s as it's loading class but it does not include it - it will be ignored", coreMod.getName(), fmlCorePlugin); // continue; // } // try // { // Class coreModClass = Class.forName(fmlCorePlugin, false, classLoader); // FMLLog.severe("The coremods %s specified a class %s that is already present in the classpath - it will be ignored", coreMod.getName(), fmlCorePlugin); // continue; // } // catch (ClassNotFoundException cnfe) // { // // didn't find it, good // } try { classLoader.addURL(coreMod.toURI().toURL()); } catch (MalformedURLException e) { FMLLog.log(Level.SEVERE, e, "Unable to convert file into a URL. weird"); continue; } try { Class coreModClass = Class.forName(fmlCorePlugin, true, classLoader); IFMLLoadingPlugin plugin = (IFMLLoadingPlugin) coreModClass.newInstance(); loadPlugins.add(plugin); if (plugin.getLibraryRequestClass()!=null) { for (String libName : plugin.getLibraryRequestClass()) { libraries.add((ILibrarySet) Class.forName(libName, true, classLoader).newInstance()); } } FMLLog.fine("Loaded coremod %s", coreMod.getName()); } catch (ClassNotFoundException cnfe) { FMLLog.log(Level.SEVERE, cnfe, "Coremod %s: Unable to class load the plugin %s", coreMod.getName(), fmlCorePlugin); } catch (ClassCastException cce) { FMLLog.log(Level.SEVERE, cce, "Coremod %s: The plugin %s is not an implementor of IFMLLoadingPlugin", coreMod.getName(), fmlCorePlugin); } catch (InstantiationException ie) { FMLLog.log(Level.SEVERE, ie, "Coremod %s: The plugin class %s was not instantiable", coreMod.getName(), fmlCorePlugin); } catch (IllegalAccessException iae) { FMLLog.log(Level.SEVERE, iae, "Coremod %s: The plugin class %s was not accessible", coreMod.getName(), fmlCorePlugin); } } } /** * @param mcDir * @return */ private static File setupLibDir(File mcDir) { File libDir = new File(mcDir,"lib"); try { libDir = libDir.getCanonicalFile(); } catch (IOException e) { throw new RuntimeException(String.format("Unable to canonicalize the lib dir at %s", mcDir.getName()),e); } if (!libDir.exists()) { libDir.mkdir(); } else if (libDir.exists() && !libDir.isDirectory()) { throw new RuntimeException(String.format("Found a lib file in %s that's not a directory", mcDir.getName())); } return libDir; } /** * @param mcDir * @return */ private static File setupCoreModDir(File mcDir) { File coreModDir = new File(mcDir,"coremods"); try { coreModDir = coreModDir.getCanonicalFile(); } catch (IOException e) { throw new RuntimeException(String.format("Unable to canonicalize the coremod dir at %s", mcDir.getName()),e); } if (!coreModDir.exists()) { coreModDir.mkdir(); } else if (coreModDir.exists() && !coreModDir.isDirectory()) { throw new RuntimeException(String.format("Found a coremod file in %s that's not a directory", mcDir.getName())); } return coreModDir; } private static String generateChecksum(File file) { try { FileInputStream fis = new FileInputStream(file); FileChannel chan = fis.getChannel(); MessageDigest digest = MessageDigest.getInstance("SHA-1"); MappedByteBuffer mappedFile = chan.map(MapMode.READ_ONLY, 0, file.length()); digest.update(mappedFile); chan.close(); fis.close(); byte[] chksum = digest.digest(); final StringBuilder hex = new StringBuilder( 2 * chksum.length ); for ( final byte b : chksum ) { hex.append(HEXES.charAt((b & 0xF0) >> 4)) .append(HEXES.charAt((b & 0x0F))); } return hex.toString(); } catch (Exception e) { return null; } } private static void downloadFile(File libFile, String rootUrl) { try { URL libDownload = new URL(String.format(rootUrl,libFile.getName())); String infoString = String.format("Downloading file %s", libDownload.toString()); FMLLog.info(infoString); InputStream urlStream = libDownload.openStream(); urlStream = FMLRelauncher.instance().wrapStream(urlStream, infoString); ReadableByteChannel urlChannel = Channels.newChannel(urlStream); FileOutputStream libFileStream = new FileOutputStream(libFile); FileChannel libFileChannel = libFileStream.getChannel(); libFileChannel.transferFrom(urlChannel, 0, 1<<24); libFileChannel.close(); libFileStream.close(); urlChannel.close(); urlStream.close(); FMLLog.info("Download complete"); } catch (Exception e) { FMLLog.severe("There was a problem downloading the file %s automatically. Perhaps you" + "have an environment without internet access. You will need to download " + "the file manually\n", libFile.getName()); libFile.delete(); throw new RuntimeException("A download error occured", e); } } public static List getLibraries() { return loadedLibraries; } }