aboutsummaryrefslogtreecommitdiff
path: root/target/src/main/java/moe/nea/modernjava
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-11-01 14:02:59 +0100
committernea <nea@nea.moe>2023-11-01 14:02:59 +0100
commitd5253dc5c3ae7b2cc1fcb96780e43b929a449eb4 (patch)
tree478973510c9d7eda999b8321a07f34691ba56e8b /target/src/main/java/moe/nea/modernjava
downloadModernJavaLauncher-d5253dc5c3ae7b2cc1fcb96780e43b929a449eb4.tar.gz
ModernJavaLauncher-d5253dc5c3ae7b2cc1fcb96780e43b929a449eb4.tar.bz2
ModernJavaLauncher-d5253dc5c3ae7b2cc1fcb96780e43b929a449eb4.zip
Initial commit
Diffstat (limited to 'target/src/main/java/moe/nea/modernjava')
-rw-r--r--target/src/main/java/moe/nea/modernjava/target/Pack200Retransformer.java46
-rw-r--r--target/src/main/java/moe/nea/modernjava/target/RelaunchEntryPoint.java56
2 files changed, 102 insertions, 0 deletions
diff --git a/target/src/main/java/moe/nea/modernjava/target/Pack200Retransformer.java b/target/src/main/java/moe/nea/modernjava/target/Pack200Retransformer.java
new file mode 100644
index 0000000..385e7ec
--- /dev/null
+++ b/target/src/main/java/moe/nea/modernjava/target/Pack200Retransformer.java
@@ -0,0 +1,46 @@
+package moe.nea.modernjava.target;
+
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassVisitor;
+import org.objectweb.asm.ClassWriter;
+import org.objectweb.asm.commons.ClassRemapper;
+import org.objectweb.asm.commons.Remapper;
+
+import java.lang.instrument.ClassFileTransformer;
+import java.lang.instrument.IllegalClassFormatException;
+import java.lang.instrument.Instrumentation;
+import java.security.ProtectionDomain;
+import java.util.Arrays;
+import java.util.List;
+
+public class Pack200Retransformer implements ClassFileTransformer {
+ // relocate("dev.architectury.pack200.java", "java.util.jar")
+ List<String> classes = Arrays.asList("AdaptiveCoding", "Attribute", "BandStructure", "ClassReader", "ClassWriter", "Code", "Coding", "CodingChooser", "CodingMethod", "ConstantPool", "Constants", "Driver", "DriverResource", "DriverResource_ja", "DriverResource_zh_CN", "FixedList", "Fixups", "Histogram", "Instruction", "NativeUnpack", "Pack200", "Pack200Adapter", "Pack200Plugin", "Package", "PackageReader", "PackageWriter", "PackerImpl", "PopulationCoding", "PropMap", "TLGlobals", "UnpackerImpl", "Utils");
+ String architecturyPackage = "dev/architectury/pack200/java/";
+ String javaPackage = "java/util/jar/";
+
+ public static void premain(String agentArgs, Instrumentation inst) {
+ inst.addTransformer(new Pack200Retransformer());
+ }
+
+ @Override
+ public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
+ ClassReader reader = new ClassReader(classfileBuffer);
+ ClassWriter writer = new ClassWriter(reader, 0);
+ Remapper remapper = new Remapper() {
+ @Override
+ public String map(String internalName) {
+ if (internalName.startsWith(javaPackage)) {
+ for (String aClass : classes) {
+ if (internalName.equals(javaPackage + aClass) || (internalName.startsWith(javaPackage + aClass + "$")))
+ return internalName.replace(javaPackage, architecturyPackage);
+ }
+ }
+ return internalName;
+ }
+ };
+ ClassVisitor visitor = new ClassRemapper(writer, remapper);
+ reader.accept(visitor, 0);
+ return writer.toByteArray();
+ }
+}
diff --git a/target/src/main/java/moe/nea/modernjava/target/RelaunchEntryPoint.java b/target/src/main/java/moe/nea/modernjava/target/RelaunchEntryPoint.java
new file mode 100644
index 0000000..f30de3f
--- /dev/null
+++ b/target/src/main/java/moe/nea/modernjava/target/RelaunchEntryPoint.java
@@ -0,0 +1,56 @@
+package moe.nea.modernjava.target;
+
+import java.io.File;
+import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Arrays;
+
+public class RelaunchEntryPoint {
+ public static void log(String text) {
+ System.out.println("[MODERNJAVA] " + text);
+
+ }
+
+ public static void main(String[] args) {
+ log("This is modern java relaunch talking!");
+ try {
+ setupLaunch(args);
+ } catch (Throwable t) {
+ log("Encountered exception during relaunching");
+ t.printStackTrace();
+ }
+ }
+
+ private static void setupLaunch(String[] args) throws Exception {
+ log("Original SQLException: " + Thread.currentThread().getContextClassLoader().loadClass("java.sql.SQLException"));
+ log("Setting up fake launch class loader");
+ ClassLoader urlClassLoader = new URLClassLoader(retrieveClassPath(), Thread.currentThread().getContextClassLoader());
+ log("Created fake url class loader");
+ Thread.currentThread().setContextClassLoader(urlClassLoader);
+ log("Handing off to Launch");
+ Class<?> launchClass = urlClassLoader.loadClass("net.minecraft.launchwrapper.Launch");
+ log("LaunchClass: " + launchClass);
+ log("LaunchClass ClassLoader: " + launchClass.getClassLoader());
+
+ log("LaunchClass ClassLoader SQLException: " + urlClassLoader.loadClass("java.sql.SQLException"));
+
+
+ Method main = launchClass.getMethod("main", String[].class);
+ main.invoke(null, new Object[]{args});
+ }
+
+ private static URL[] retrieveClassPath() throws MalformedURLException {
+ String property = System.getProperty("modernjava.relaunchclasspath");
+ String[] split = property.split(File.pathSeparator);
+ URL[] urls = new URL[split.length];
+ for (int i = 0; i < split.length; i++) {
+ urls[i] = new File(split[i]).toURI().toURL();
+ }
+ log("Retrieved classpath: " + Arrays.toString(urls));
+ return urls;
+ }
+
+
+}