diff options
Diffstat (limited to 'src/main/java/moe/nea/modernjava/launch/util')
| -rw-r--r-- | src/main/java/moe/nea/modernjava/launch/util/ClassLoaderManipulations.java | 31 | ||||
| -rw-r--r-- | src/main/java/moe/nea/modernjava/launch/util/ReflectionUtils.java | 33 | 
2 files changed, 64 insertions, 0 deletions
| diff --git a/src/main/java/moe/nea/modernjava/launch/util/ClassLoaderManipulations.java b/src/main/java/moe/nea/modernjava/launch/util/ClassLoaderManipulations.java new file mode 100644 index 0000000..09f2368 --- /dev/null +++ b/src/main/java/moe/nea/modernjava/launch/util/ClassLoaderManipulations.java @@ -0,0 +1,31 @@ +package moe.nea.modernjava.launch.util; + +import net.minecraft.launchwrapper.Launch; + +import java.io.File; +import java.lang.reflect.Method; +import java.net.MalformedURLException; +import java.net.URL; + +public class ClassLoaderManipulations { + +    public static void addToParentClassLoader(File file) { +        try { +            addToParentClassLoader(file.toURI().toURL()); +        } catch (MalformedURLException e) { +            throw new RuntimeException(e); +        } +    } + +    public static void addToParentClassLoader(URL file) { +        try { +            Launch.classLoader.addURL(file); +            ClassLoader parentClassLoader = Launch.classLoader.getClass().getClassLoader(); +            Method addUrl = parentClassLoader.getClass().getDeclaredMethod("addURL", URL.class); +            addUrl.setAccessible(true); +            addUrl.invoke(parentClassLoader, file); +        } catch (Exception e) { +            throw new RuntimeException(e); +        } +    } +} diff --git a/src/main/java/moe/nea/modernjava/launch/util/ReflectionUtils.java b/src/main/java/moe/nea/modernjava/launch/util/ReflectionUtils.java new file mode 100644 index 0000000..abc2364 --- /dev/null +++ b/src/main/java/moe/nea/modernjava/launch/util/ReflectionUtils.java @@ -0,0 +1,33 @@ +package moe.nea.modernjava.launch.util; + +import sun.misc.Unsafe; + +import java.lang.reflect.Field; + +public class ReflectionUtils { +    private static Unsafe unsafe; + +    static { +        try { +            final Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe"); +            unsafeField.setAccessible(true); +            unsafe = (Unsafe) unsafeField.get(null); +        } catch (Exception ex) { +            ex.printStackTrace(); +        } +    } + +    public static void makeFieldWritable(Field f) { +        String s = "Doing nothing. We will use unsafe to set the value instead, if possible"; +    } + +    public static void doFieldWrite(Field field, Object object) throws IllegalAccessException { +        if (unsafe == null) { +            field.set(null, object); +        } else { +            Object o = unsafe.staticFieldBase(field); +            long l = unsafe.staticFieldOffset(field); +            unsafe.putObject(o, l, object); +        } +    } +} | 
