|
| 1 | +/* |
| 2 | + * This file is part of JavaDowngrader - https://github.com/RaphiMC/JavaDowngrader |
| 3 | + * Copyright (C) 2023 RK_01/RaphiMC and contributors |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; either |
| 8 | + * version 3 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package net.raphimc.javadowngrader.bootstrap; |
| 19 | + |
| 20 | +import net.lenni0451.classtransform.TransformerManager; |
| 21 | +import net.lenni0451.classtransform.additionalclassprovider.LazyFileClassProvider; |
| 22 | +import net.lenni0451.classtransform.utils.loader.InjectionClassLoader; |
| 23 | +import net.lenni0451.classtransform.utils.tree.BasicClassProvider; |
| 24 | +import net.lenni0451.reflect.ClassLoaders; |
| 25 | +import net.lenni0451.reflect.Methods; |
| 26 | +import net.raphimc.javadowngrader.impl.classtransform.JavaDowngraderTransformer; |
| 27 | + |
| 28 | +import java.io.File; |
| 29 | +import java.net.URL; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.jar.JarFile; |
| 33 | + |
| 34 | +public class Main { |
| 35 | + |
| 36 | + public static void main(String[] args) throws ClassNotFoundException { |
| 37 | + if (args.length < 1) { |
| 38 | + final String jarPath = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath(); |
| 39 | + final String jarName = jarPath.substring(jarPath.lastIndexOf('/') + 1); |
| 40 | + System.out.println("Usage: java -jar " + jarName + " <jar> [args]"); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + final File jarFile = new File(args[0]); |
| 45 | + if (!jarFile.isFile()) { |
| 46 | + System.err.println("File not found: " + jarFile.getAbsolutePath()); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + URL jarUrl; |
| 51 | + String mainClass; |
| 52 | + try { |
| 53 | + jarUrl = jarFile.toURI().toURL(); |
| 54 | + |
| 55 | + try (final JarFile jar = new JarFile(jarFile)) { |
| 56 | + mainClass = jar.getManifest().getMainAttributes().getValue("Main-Class"); |
| 57 | + } |
| 58 | + if (mainClass == null) throw new RuntimeException("No main class found"); |
| 59 | + } catch (Throwable e) { |
| 60 | + throw new RuntimeException("Invalid jar file", e); |
| 61 | + } |
| 62 | + |
| 63 | + final URL[] systemClassPath = ClassLoaders.getSystemClassPath(); |
| 64 | + final URL[] urls = Arrays.copyOf(systemClassPath, systemClassPath.length + 1); |
| 65 | + urls[urls.length - 1] = jarUrl; |
| 66 | + args = Arrays.copyOfRange(args, 1, args.length); |
| 67 | + |
| 68 | + final TransformerManager transformerManager = new TransformerManager(new LazyFileClassProvider(Collections.singletonList(jarFile), new BasicClassProvider())); |
| 69 | + transformerManager.addBytecodeTransformer(new JavaDowngraderTransformer(transformerManager)); |
| 70 | + final InjectionClassLoader injectionClassLoader = new InjectionClassLoader(transformerManager, urls); |
| 71 | + Thread.currentThread().setContextClassLoader(injectionClassLoader); |
| 72 | + Methods.invoke(null, Methods.getDeclaredMethod(injectionClassLoader.loadClass(mainClass), "main", String[].class), (Object) args); |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments