2012-03-30 14:11:13 +00:00
|
|
|
/*
|
2016-06-23 03:49:47 +00:00
|
|
|
* Minecraft Forge
|
|
|
|
* Copyright (c) 2016.
|
2013-06-14 21:21:49 +00:00
|
|
|
*
|
2016-06-23 03:49:47 +00:00
|
|
|
* 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 Software Foundation version 2.1
|
|
|
|
* of the License.
|
|
|
|
*
|
|
|
|
* 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 A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* 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 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2012-03-30 14:11:13 +00:00
|
|
|
*/
|
2013-03-08 00:25:48 +00:00
|
|
|
|
2014-09-23 05:01:24 +00:00
|
|
|
package net.minecraftforge.fml.common;
|
2012-03-30 14:11:13 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.net.MalformedURLException;
|
2015-02-03 11:21:27 +00:00
|
|
|
import java.net.URI;
|
2012-04-06 13:59:58 +00:00
|
|
|
import java.net.URISyntaxException;
|
2012-03-30 14:11:13 +00:00
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLClassLoader;
|
2015-02-03 11:21:27 +00:00
|
|
|
import java.util.ArrayList;
|
2015-12-01 18:08:35 +00:00
|
|
|
import java.util.Arrays;
|
2012-07-06 19:59:16 +00:00
|
|
|
import java.util.List;
|
2013-09-25 15:46:11 +00:00
|
|
|
import java.util.Set;
|
2014-09-23 05:01:24 +00:00
|
|
|
|
2013-07-02 22:40:23 +00:00
|
|
|
import net.minecraft.launchwrapper.IClassTransformer;
|
2013-06-14 21:21:49 +00:00
|
|
|
import net.minecraft.launchwrapper.LaunchClassLoader;
|
2014-09-23 05:01:24 +00:00
|
|
|
import net.minecraftforge.fml.common.asm.transformers.ModAPITransformer;
|
|
|
|
import net.minecraftforge.fml.common.discovery.ASMDataTable;
|
2013-06-14 21:21:49 +00:00
|
|
|
|
2015-02-03 11:21:27 +00:00
|
|
|
import org.apache.logging.log4j.Level;
|
|
|
|
|
2012-07-23 19:03:17 +00:00
|
|
|
import com.google.common.collect.ImmutableList;
|
2015-12-01 18:08:35 +00:00
|
|
|
import com.google.common.collect.Lists;
|
2012-07-23 19:03:17 +00:00
|
|
|
|
2012-04-05 20:22:47 +00:00
|
|
|
/**
|
|
|
|
* A simple delegating class loader used to load mods into the system
|
2012-07-06 19:59:16 +00:00
|
|
|
*
|
|
|
|
*
|
2012-04-05 20:22:47 +00:00
|
|
|
* @author cpw
|
|
|
|
*
|
|
|
|
*/
|
2012-04-05 14:07:52 +00:00
|
|
|
public class ModClassLoader extends URLClassLoader
|
|
|
|
{
|
2014-08-02 00:13:53 +00:00
|
|
|
private static final List<String> STANDARD_LIBRARIES = ImmutableList.of("jinput.jar", "lwjgl.jar", "lwjgl_util.jar", "rt.jar");
|
2013-06-14 21:21:49 +00:00
|
|
|
private LaunchClassLoader mainClassLoader;
|
2015-12-01 18:08:35 +00:00
|
|
|
private List<File> sources;
|
2012-03-30 14:11:13 +00:00
|
|
|
|
2012-05-17 19:11:21 +00:00
|
|
|
public ModClassLoader(ClassLoader parent) {
|
|
|
|
super(new URL[0], null);
|
2016-07-19 23:21:52 +00:00
|
|
|
if (parent instanceof LaunchClassLoader)
|
|
|
|
{
|
|
|
|
this.mainClassLoader = (LaunchClassLoader)parent;
|
|
|
|
}
|
2015-12-30 04:15:03 +00:00
|
|
|
this.sources = Lists.newArrayList();
|
2012-05-17 19:11:21 +00:00
|
|
|
}
|
2012-07-06 19:59:16 +00:00
|
|
|
|
2012-04-05 14:07:52 +00:00
|
|
|
public void addFile(File modFile) throws MalformedURLException
|
|
|
|
{
|
2013-06-26 18:52:56 +00:00
|
|
|
URL url = modFile.toURI().toURL();
|
2012-07-06 19:59:16 +00:00
|
|
|
mainClassLoader.addURL(url);
|
2015-12-01 18:08:35 +00:00
|
|
|
this.sources.add(modFile);
|
2012-07-06 19:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Class<?> loadClass(String name) throws ClassNotFoundException
|
|
|
|
{
|
|
|
|
return mainClassLoader.loadClass(name);
|
2012-04-05 14:07:52 +00:00
|
|
|
}
|
2012-07-06 19:59:16 +00:00
|
|
|
|
2012-04-06 18:23:14 +00:00
|
|
|
public File[] getParentSources() {
|
2012-07-06 19:59:16 +00:00
|
|
|
try
|
|
|
|
{
|
2015-02-03 11:21:27 +00:00
|
|
|
List<File> files=new ArrayList<File>();
|
|
|
|
for(URL url : mainClassLoader.getSources())
|
2012-04-06 13:59:58 +00:00
|
|
|
{
|
2015-02-03 11:21:27 +00:00
|
|
|
URI uri = url.toURI();
|
2015-02-14 02:17:14 +00:00
|
|
|
if(uri.getScheme().equals("file"))
|
2015-02-03 11:21:27 +00:00
|
|
|
{
|
|
|
|
files.add(new File(uri));
|
|
|
|
}
|
2012-04-06 13:59:58 +00:00
|
|
|
}
|
2015-02-03 11:21:27 +00:00
|
|
|
return files.toArray(new File[]{});
|
2012-07-06 19:59:16 +00:00
|
|
|
}
|
|
|
|
catch (URISyntaxException e)
|
|
|
|
{
|
2013-12-16 16:47:48 +00:00
|
|
|
FMLLog.log(Level.ERROR, e, "Unable to process our input to locate the minecraft code");
|
2012-07-06 19:59:16 +00:00
|
|
|
throw new LoaderException(e);
|
2012-04-06 13:59:58 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-23 19:03:17 +00:00
|
|
|
|
|
|
|
public List<String> getDefaultLibraries()
|
|
|
|
{
|
|
|
|
return STANDARD_LIBRARIES;
|
|
|
|
}
|
2012-08-26 01:57:25 +00:00
|
|
|
|
2015-02-16 01:30:18 +00:00
|
|
|
public boolean isDefaultLibrary(File file)
|
|
|
|
{
|
2016-08-13 20:28:21 +00:00
|
|
|
String home = System.getProperty("java.home"); // Nullcheck just in case some JVM decides to be stupid
|
|
|
|
if (home != null && file.getAbsolutePath().startsWith(home)) return true;
|
2015-02-16 01:30:18 +00:00
|
|
|
// Should really pull this from the json somehow, but we dont have that at runtime.
|
|
|
|
String name = file.getName();
|
|
|
|
if (!name.endsWith(".jar")) return false;
|
|
|
|
String[] prefixes =
|
|
|
|
{
|
|
|
|
"launchwrapper-",
|
|
|
|
"asm-all-",
|
|
|
|
"akka-actor_2.11-",
|
|
|
|
"config-",
|
|
|
|
"scala-",
|
|
|
|
"jopt-simple-",
|
|
|
|
"lzma-",
|
|
|
|
"realms-",
|
|
|
|
"httpclient-",
|
|
|
|
"httpcore-",
|
|
|
|
"vecmath-",
|
|
|
|
"trove4j-",
|
|
|
|
"icu4j-core-mojang-",
|
|
|
|
"codecjorbis-",
|
|
|
|
"codecwav-",
|
|
|
|
"libraryjavawound-",
|
|
|
|
"librarylwjglopenal-",
|
|
|
|
"soundsystem-",
|
|
|
|
"netty-all-",
|
2015-12-02 06:01:21 +00:00
|
|
|
"guava-",
|
2015-02-16 01:30:18 +00:00
|
|
|
"commons-lang3-",
|
|
|
|
"commons-compress-",
|
|
|
|
"commons-logging-",
|
|
|
|
"commons-io-",
|
|
|
|
"commons-codec-",
|
|
|
|
"jinput-",
|
|
|
|
"jutils-",
|
|
|
|
"gson-",
|
|
|
|
"authlib-",
|
|
|
|
"log4j-api-",
|
|
|
|
"log4j-core-",
|
|
|
|
"lwjgl-",
|
|
|
|
"lwjgl_util-",
|
2016-05-29 00:47:12 +00:00
|
|
|
"twitch-",
|
|
|
|
"jline-",
|
|
|
|
"jna-",
|
|
|
|
"platform-",
|
|
|
|
"oshi-core-",
|
|
|
|
"netty-",
|
|
|
|
"libraryjavasound-",
|
|
|
|
"fastutil-"
|
2015-02-16 01:30:18 +00:00
|
|
|
};
|
|
|
|
for (String s : prefixes)
|
|
|
|
{
|
|
|
|
if (name.startsWith(s)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-25 15:46:11 +00:00
|
|
|
public void clearNegativeCacheFor(Set<String> classList)
|
|
|
|
{
|
|
|
|
mainClassLoader.clearNegativeEntries(classList);
|
|
|
|
}
|
2013-10-04 21:20:05 +00:00
|
|
|
|
|
|
|
public ModAPITransformer addModAPITransformer(ASMDataTable dataTable)
|
|
|
|
{
|
2014-09-23 05:01:24 +00:00
|
|
|
mainClassLoader.registerTransformer("net.minecraftforge.fml.common.asm.transformers.ModAPITransformer");
|
2013-10-04 21:20:05 +00:00
|
|
|
List<IClassTransformer> transformers = mainClassLoader.getTransformers();
|
|
|
|
ModAPITransformer modAPI = (ModAPITransformer) transformers.get(transformers.size()-1);
|
|
|
|
modAPI.initTable(dataTable);
|
|
|
|
return modAPI;
|
|
|
|
}
|
2015-12-01 18:08:35 +00:00
|
|
|
|
|
|
|
List<URL> parentURLs = null;
|
|
|
|
public boolean containsSource(File source)
|
|
|
|
{
|
|
|
|
if (parentURLs == null) {
|
|
|
|
parentURLs = Arrays.asList(mainClassLoader.getURLs());
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return parentURLs.contains(source.toURI().toURL());
|
|
|
|
} catch (MalformedURLException e)
|
|
|
|
{
|
|
|
|
// shouldn't happen
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-03-30 14:11:13 +00:00
|
|
|
}
|