001/*
002 * Forge Mod Loader
003 * Copyright (c) 2012-2013 cpw.
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser Public License v2.1
006 * which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
008 * 
009 * Contributors:
010 *     cpw - implementation
011 */
012
013package cpw.mods.fml.common;
014
015import java.io.File;
016import java.net.MalformedURLException;
017import java.net.URISyntaxException;
018import java.net.URL;
019import java.net.URLClassLoader;
020import java.util.List;
021import java.util.logging.Level;
022
023import com.google.common.collect.ImmutableList;
024
025import cpw.mods.fml.common.asm.ASMTransformer;
026import cpw.mods.fml.common.asm.transformers.AccessTransformer;
027import cpw.mods.fml.common.modloader.BaseModProxy;
028import cpw.mods.fml.relauncher.RelaunchClassLoader;
029
030/**
031 * A simple delegating class loader used to load mods into the system
032 *
033 *
034 * @author cpw
035 *
036 */
037public class ModClassLoader extends URLClassLoader
038{
039    private static final List<String> STANDARD_LIBRARIES = ImmutableList.of("jinput.jar", "lwjgl.jar", "lwjgl_util.jar");
040    private RelaunchClassLoader mainClassLoader;
041
042    public ModClassLoader(ClassLoader parent) {
043        super(new URL[0], null);
044        this.mainClassLoader = (RelaunchClassLoader)parent;
045    }
046
047    public void addFile(File modFile) throws MalformedURLException
048    {
049            URL url = modFile.toURI().toURL();
050        mainClassLoader.addURL(url);
051    }
052
053    @Override
054    public Class<?> loadClass(String name) throws ClassNotFoundException
055    {
056        return mainClassLoader.loadClass(name);
057    }
058
059    public File[] getParentSources() {
060        List<URL> urls=mainClassLoader.getSources();
061        File[] sources=new File[urls.size()];
062        try
063        {
064            for (int i = 0; i<urls.size(); i++)
065            {
066                sources[i]=new File(urls.get(i).toURI());
067            }
068            return sources;
069        }
070        catch (URISyntaxException e)
071        {
072            FMLLog.log(Level.SEVERE, e, "Unable to process our input to locate the minecraft code");
073            throw new LoaderException(e);
074        }
075    }
076
077    public List<String> getDefaultLibraries()
078    {
079        return STANDARD_LIBRARIES;
080    }
081
082    public Class<? extends BaseModProxy> loadBaseModClass(String modClazzName) throws Exception
083    {
084        AccessTransformer transformer = (AccessTransformer)mainClassLoader.getTransformers().get(0);
085        transformer.ensurePublicAccessFor(modClazzName);
086        return (Class<? extends BaseModProxy>) Class.forName(modClazzName, true, this);
087    }
088}