Skip to content

Commit

Permalink
Inject mods into current class loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hallowizer committed Nov 12, 2018
1 parent 4584632 commit 98196db
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/tanks/RegistryTank.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
Expand Down Expand Up @@ -56,10 +58,21 @@ public static void loadRegistry(String homedir)
{
try
{
@SuppressWarnings("resource")
ClassLoader loader = new URLClassLoader( new URL[] { new File(tankLine[3]).toURI().toURL() }); // super messy
@SuppressWarnings("unchecked")
Class<? extends Tank> clasz = (Class<? extends Tank>) loader.loadClass(tankLine[4]);
ClassLoader classLoader = RegistryTank.class.getClassLoader();
Class<?> clazz = classLoader.getClass();

if (!clazz.equals(URLClassLoader.class)) // Java 9 jdk.internal.loader.ClassLoaders$AppClassLoader
clazz = clazz.getSuperclass(); // jdk.internal.loader.BuiltinClassLoader

Field field = clazz.getDeclaredField("ucp");
field.setAccessible(true);
Object ucp = field.get(classLoader);

Class<?> URLClassPath = ucp.getClass();
Method method = URLClassPath.getDeclaredMethod("addURL", URL.class);
method.invoke(ucp, new File(tankLine[3]).toURI().toURL());

Class<? extends Tank> clasz = Class.forName(tankLine[4], true, classLoader).asSubclass(Tank.class);
new RegistryTank.TankEntry(Game.registryTank, clasz, tankLine[0], Double.parseDouble(tankLine[1]));
}
catch (Exception e)
Expand Down

0 comments on commit 98196db

Please sign in to comment.