    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 */
    014package cpw.mods.fml.common;
    015
    016import java.io.File;
    017import java.net.MalformedURLException;
    018import java.net.URISyntaxException;
    019import java.net.URL;
    020import java.net.URLClassLoader;
    021import java.util.List;
    022import java.util.logging.Level;
    023
    024import com.google.common.collect.ImmutableList;
    025
    026import cpw.mods.fml.common.asm.ASMTransformer;
    027import cpw.mods.fml.common.asm.transformers.AccessTransformer;
    028import cpw.mods.fml.common.modloader.BaseModProxy;
    029import 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 */
    038public 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, e, "Unable to process our input to locate the minecraft code");
    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}