001    /*
002     * The FML Forge Mod Loader suite.
003     * Copyright (C) 2012 cpw
004     *
005     * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or any later version.
007     *
008     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
009     * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
010     *
011     * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51
012     * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
013     */
014    package cpw.mods.fml.common;
015    
016    import java.io.File;
017    import java.net.MalformedURLException;
018    import java.net.URISyntaxException;
019    import java.net.URL;
020    import java.net.URLClassLoader;
021    import java.util.List;
022    import java.util.logging.Level;
023    
024    import com.google.common.collect.ImmutableList;
025    
026    import cpw.mods.fml.common.asm.ASMTransformer;
027    import cpw.mods.fml.common.asm.transformers.AccessTransformer;
028    import cpw.mods.fml.common.modloader.BaseModProxy;
029    import cpw.mods.fml.relauncher.RelaunchClassLoader;
030    
031    /**
032     * A simple delegating class loader used to load mods into the system
033     *
034     *
035     * @author cpw
036     *
037     */
038    public class ModClassLoader extends URLClassLoader
039    {
040        private static final List<String> STANDARD_LIBRARIES = ImmutableList.of("jinput.jar", "lwjgl.jar", "lwjgl_util.jar");
041        private RelaunchClassLoader mainClassLoader;
042    
043        public ModClassLoader(ClassLoader parent) {
044            super(new URL[0], null);
045            this.mainClassLoader = (RelaunchClassLoader)parent;
046        }
047    
048        public void addFile(File modFile) throws MalformedURLException
049        {
050                URL url = modFile.toURI().toURL();
051            mainClassLoader.addURL(url);
052        }
053    
054        @Override
055        public Class<?> loadClass(String name) throws ClassNotFoundException
056        {
057            return mainClassLoader.loadClass(name);
058        }
059    
060        public File[] getParentSources() {
061            List<URL> urls=mainClassLoader.getSources();
062            File[] sources=new File[urls.size()];
063            try
064            {
065                for (int i = 0; i<urls.size(); i++)
066                {
067                    sources[i]=new File(urls.get(i).toURI());
068                }
069                return sources;
070            }
071            catch (URISyntaxException e)
072            {
073                FMLLog.log(Level.SEVERE, "Unable to process our input to locate the minecraft code", e);
074                throw new LoaderException(e);
075            }
076        }
077    
078        public List<String> getDefaultLibraries()
079        {
080            return STANDARD_LIBRARIES;
081        }
082    
083        public Class<? extends BaseModProxy> loadBaseModClass(String modClazzName) throws Exception
084        {
085            AccessTransformer transformer = (AccessTransformer)mainClassLoader.getTransformers().get(0);
086            transformer.ensurePublicAccessFor(modClazzName);
087            return (Class<? extends BaseModProxy>) Class.forName(modClazzName, true, this);
088        }
089    }