Search in sources :

Example 1 with IClassTransformer

use of net.minecraft.launchwrapper.IClassTransformer in project LogisticsPipes by RS485.

the class DevEnvHelper method handleSpecialClassTransformer.

public static void handleSpecialClassTransformer() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, IOException, CantLoadMCPMappingException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
    if (!isDevelopmentEnvironment() || !new File(".mcpMappings").exists()) {
        return;
    }
    DevEnvHelper.m = new MappingLoader_MCP("1.7.10", Side.UNIVERSAL, new File(".mcpMappings")).getForwardCSV();
    final Field ucpF = URLClassLoader.class.getDeclaredField("ucp");
    ucpF.setAccessible(true);
    Object ucp = ucpF.get(Launch.classLoader);
    final Field pathF = ucp.getClass().getDeclaredField("path");
    final Field loadersF = ucp.getClass().getDeclaredField("loaders");
    final Method addURL = ucp.getClass().getMethod("addURL", URL.class);
    pathF.setAccessible(true);
    loadersF.setAccessible(true);
    addURL.setAccessible(true);
    ArrayList<File> modFileList = new ArrayList<>();
    File modsFolder = new File("mods");
    if (modsFolder.exists()) {
        File[] modses = modsFolder.listFiles();
        if (modses != null) {
            Collections.addAll(modFileList, modses);
        }
    }
    for (File f : modFileList) {
        if (!f.isFile()) {
            continue;
        }
        String path = f.getAbsolutePath();
        if (path.endsWith("LP_DEOBF.jar")) {
            URL toMove = f.toURI().toURL();
            addURL.invoke(ucp, toMove);
            Enumeration<URL> tmp = Launch.classLoader.findResources("notToBeFound");
            while (tmp.hasMoreElements()) {
                tmp.nextElement();
            }
            //ucp = ucpF.get(Launch.classLoader);
            @SuppressWarnings("unchecked") List<Object> pathes = (List<Object>) pathF.get(ucp);
            @SuppressWarnings("unchecked") List<Object> loaders = (List<Object>) loadersF.get(ucp);
            Object toMoveLoaders = null;
            Class<?> loaderClass = Class.forName("sun.misc.URLClassPath$Loader");
            for (Object loader : loaders) {
                Field baseF = loaderClass.getDeclaredField("base");
                baseF.setAccessible(true);
                URL base = (URL) baseF.get(loader);
                if (base.toExternalForm().contains(toMove.toExternalForm())) {
                    toMoveLoaders = loader;
                    break;
                }
            }
            Object toMovepathes = pathes.get(loaders.indexOf(toMoveLoaders));
            for (int i = loaders.indexOf(toMoveLoaders); i > 0; i--) {
                loaders.set(i, loaders.get(i - 1));
                pathes.set(i, pathes.get(i - 1));
            }
            loaders.set(0, toMoveLoaders);
            pathes.set(0, toMovepathes);
        }
    }
    Field fTransformers = LaunchClassLoader.class.getDeclaredField("transformers");
    fTransformers.setAccessible(true);
    @SuppressWarnings("unchecked") List<IClassTransformer> transformers = (List<IClassTransformer>) fTransformers.get(Launch.classLoader);
    transformers.add(DevEnvHelper.transformer);
    for (int i = transformers.size() - 1; i > 0; i--) {
        // Move everything one up
        transformers.set(i, transformers.get(i - 1));
    }
    // So that our injector can be first
    transformers.set(0, DevEnvHelper.transformer);
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) URL(java.net.URL) Field(java.lang.reflect.Field) IClassTransformer(net.minecraft.launchwrapper.IClassTransformer) List(java.util.List) ArrayList(java.util.ArrayList) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 2 with IClassTransformer

use of net.minecraft.launchwrapper.IClassTransformer in project MinecraftForge by MinecraftForge.

the class ModClassLoader method addModAPITransformer.

public ModAPITransformer addModAPITransformer(ASMDataTable dataTable) {
    mainClassLoader.registerTransformer("net.minecraftforge.fml.common.asm.transformers.ModAPITransformer");
    List<IClassTransformer> transformers = mainClassLoader.getTransformers();
    ModAPITransformer modAPI = (ModAPITransformer) transformers.get(transformers.size() - 1);
    modAPI.initTable(dataTable);
    return modAPI;
}
Also used : ModAPITransformer(net.minecraftforge.fml.common.asm.transformers.ModAPITransformer) IClassTransformer(net.minecraft.launchwrapper.IClassTransformer)

Example 3 with IClassTransformer

use of net.minecraft.launchwrapper.IClassTransformer in project LogisticsPipes by RS485.

the class DevEnvHelper method detectCoreModInDevEnv.

@SuppressWarnings("unchecked")
public static void detectCoreModInDevEnv() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, NoSuchFieldException, IOException {
    if (!isDevelopmentEnvironment()) {
        return;
    }
    Method handleCascadingTweak = CoreModManager.class.getDeclaredMethod("handleCascadingTweak", File.class, JarFile.class, String.class, LaunchClassLoader.class, Integer.class);
    handleCascadingTweak.setAccessible(true);
    Method loadCoreMod = CoreModManager.class.getDeclaredMethod("loadCoreMod", LaunchClassLoader.class, String.class, File.class);
    loadCoreMod.setAccessible(true);
    Method setupCoreModDir = CoreModManager.class.getDeclaredMethod("setupCoreModDir", File.class);
    setupCoreModDir.setAccessible(true);
    Field loadedCoremods = CoreModManager.class.getDeclaredField("loadedCoremods");
    loadedCoremods.setAccessible(true);
    Field reparsedCoremods = CoreModManager.class.getDeclaredField("reparsedCoremods");
    reparsedCoremods.setAccessible(true);
    Field mcDir = CoreModManager.class.getDeclaredField("mcDir");
    mcDir.setAccessible(true);
    Field transformers = LaunchClassLoader.class.getDeclaredField("transformers");
    transformers.setAccessible(true);
    LaunchClassLoader classLoader = Launch.classLoader;
    FMLRelaunchLog.fine("Discovering coremods");
    File coreMods = (File) setupCoreModDir.invoke(null, mcDir.get(null));
    FilenameFilter ff = (dir, name) -> name.endsWith(".jar");
    File[] FMLcoreModListArray = coreMods.listFiles(ff);
    File versionedModDir = new File(coreMods, "1.7.10");
    if (versionedModDir.isDirectory()) {
        File[] versionedCoreMods = versionedModDir.listFiles(ff);
        FMLcoreModListArray = ObjectArrays.concat(FMLcoreModListArray, versionedCoreMods, File.class);
    }
    List<String> FMLcoreModList = new ArrayList<>();
    for (File f : FMLcoreModListArray) {
        FMLcoreModList.add(f.getName());
    }
    List<File> coreModList = new ArrayList<>();
    for (URL path : classLoader.getURLs()) {
        File file = new File(URLDecoder.decode(path.getFile()));
        if (!FMLcoreModList.contains(file.getName()) && file.getName().endsWith(".jar")) {
            coreModList.add(file);
        }
    }
    coreModList = Arrays.asList(FileListHelper.sortFileList(coreModList.toArray(new File[coreModList.size()])));
    for (File coreMod : coreModList) {
        FMLRelaunchLog.fine("Examining for coremod candidacy %s", coreMod.getName());
        JarFile jar = null;
        Attributes mfAttributes;
        try {
            jar = new JarFile(coreMod);
            if (jar.getManifest() == null) {
                // Not a coremod and no access transformer list
                continue;
            }
            ModAccessTransformer.addJar(jar);
            mfAttributes = jar.getManifest().getMainAttributes();
        } catch (IOException ioe) {
            FMLRelaunchLog.log(Level.ERROR, ioe, "Unable to read the jar file %s - ignoring", coreMod.getName());
            continue;
        } finally {
            if (jar != null) {
                try {
                    jar.close();
                } catch (IOException e) {
                // Noise
                }
            }
        }
        //AccessTransformer //For NEI
        if (mfAttributes.getValue("AccessTransformer") != null) {
            String cfg = mfAttributes.getValue("AccessTransformer");
            ((List<IClassTransformer>) transformers.get(classLoader)).add(new AccessTransformer(cfg) {
            });
        }
        //FMLAT //For newer NEI
        if (mfAttributes.getValue("FMLAT") != null) {
            String cfg = "META-INF/" + mfAttributes.getValue("FMLAT");
            ((List<IClassTransformer>) transformers.get(classLoader)).add(new AccessTransformer(cfg) {
            });
        }
        String cascadedTweaker = mfAttributes.getValue("TweakClass");
        if (cascadedTweaker != null) {
            FMLRelaunchLog.info("Loading tweaker %s from %s", cascadedTweaker, coreMod.getName());
            Integer sortOrder = Ints.tryParse(Strings.nullToEmpty(mfAttributes.getValue("TweakOrder")));
            sortOrder = (sortOrder == null ? Integer.valueOf(0) : sortOrder);
            handleCascadingTweak.invoke(null, coreMod, jar, cascadedTweaker, classLoader, sortOrder);
            ((List<String>) loadedCoremods.get(null)).add(coreMod.getName());
            continue;
        }
        String fmlCorePlugin = mfAttributes.getValue("FMLCorePlugin");
        if (fmlCorePlugin == null) {
            // Not a coremod
            FMLRelaunchLog.fine("Not found coremod data in %s", coreMod.getName());
            continue;
        }
        //classLoader.addURL(coreMod.toURI().toURL());
        if (!mfAttributes.containsKey(DevEnvHelper.COREMODCONTAINSFMLMOD)) {
            FMLRelaunchLog.finer("Adding %s to the list of known coremods, it will not be examined again", coreMod.getName());
            ((List<String>) loadedCoremods.get(null)).add(coreMod.getName());
        } else {
            FMLRelaunchLog.finer("Found FMLCorePluginContainsFMLMod marker in %s, it will be examined later for regular @Mod instances", coreMod.getName());
        //((List<String>)reparsedCoremods.get(null)).add(coreMod.getName());
        }
        //} catch(MalformedURLException e) {
        //	FMLRelaunchLog.log(Level.ERROR, e, "Unable to convert file into a URL. weird");
        //	continue;
        //}
        loadCoreMod.invoke(null, classLoader, fmlCorePlugin, coreMod);
    }
    try {
        URL resource = Resources.getResource("CoFH_at.cfg");
        if (resource != null) {
            AccessTransformer acc = new AccessTransformer("CoFH_at.cfg") {
            };
            insertTransformer(acc);
        }
    } catch (Throwable t) {
    }
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) Arrays(java.util.Arrays) URLDecoder(java.net.URLDecoder) Enumeration(java.util.Enumeration) SneakyThrows(lombok.SneakyThrows) Side(logisticspipes.asm.DevEnvHelper.MinecraftNameSet.Side) URL(java.net.URL) Scanner(java.util.Scanner) Level(org.apache.logging.log4j.Level) JarFile(java.util.jar.JarFile) Type(org.objectweb.asm.Type) MultiANewArrayInsnNode(org.objectweb.asm.tree.MultiANewArrayInsnNode) InnerClassNode(org.objectweb.asm.tree.InnerClassNode) URLClassLoader(java.net.URLClassLoader) Map(java.util.Map) Method(java.lang.reflect.Method) AccessTransformer(cpw.mods.fml.common.asm.transformers.AccessTransformer) Set(java.util.Set) Reader(java.io.Reader) Attributes(java.util.jar.Attributes) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) ClassReader(org.objectweb.asm.ClassReader) FrameNode(org.objectweb.asm.tree.FrameNode) FieldNode(org.objectweb.asm.tree.FieldNode) ClassNode(org.objectweb.asm.tree.ClassNode) ClassWriter(org.objectweb.asm.ClassWriter) AnnotationNode(org.objectweb.asm.tree.AnnotationNode) CantLoadMCPMappingException(logisticspipes.asm.DevEnvHelper.MappingLoader_MCP.CantLoadMCPMappingException) FilenameFilter(java.io.FilenameFilter) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) HashMap(java.util.HashMap) FileListHelper(cpw.mods.fml.relauncher.FileListHelper) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) MethodNode(org.objectweb.asm.tree.MethodNode) LocalVariableNode(org.objectweb.asm.tree.LocalVariableNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) IClassTransformer(net.minecraft.launchwrapper.IClassTransformer) LPConstants(logisticspipes.LPConstants) LaunchClassLoader(net.minecraft.launchwrapper.LaunchClassLoader) Opcodes(org.objectweb.asm.Opcodes) ObjectArrays(com.google.common.collect.ObjectArrays) Resources(com.google.common.io.Resources) IOException(java.io.IOException) Field(java.lang.reflect.Field) InputStreamReader(java.io.InputStreamReader) Ints(com.google.common.primitives.Ints) File(java.io.File) ModAccessTransformer(cpw.mods.fml.common.asm.transformers.ModAccessTransformer) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) CoreModManager(cpw.mods.fml.relauncher.CoreModManager) Launch(net.minecraft.launchwrapper.Launch) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Collections(java.util.Collections) InputStream(java.io.InputStream) FMLRelaunchLog(cpw.mods.fml.relauncher.FMLRelaunchLog) LaunchClassLoader(net.minecraft.launchwrapper.LaunchClassLoader) ArrayList(java.util.ArrayList) Attributes(java.util.jar.Attributes) Method(java.lang.reflect.Method) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) URL(java.net.URL) Field(java.lang.reflect.Field) FilenameFilter(java.io.FilenameFilter) List(java.util.List) ArrayList(java.util.ArrayList) JarFile(java.util.jar.JarFile) File(java.io.File) AccessTransformer(cpw.mods.fml.common.asm.transformers.AccessTransformer) ModAccessTransformer(cpw.mods.fml.common.asm.transformers.ModAccessTransformer)

Example 4 with IClassTransformer

use of net.minecraft.launchwrapper.IClassTransformer in project LogisticsPipes by RS485.

the class DevEnvHelper method insertTransformer.

@SneakyThrows
private static void insertTransformer(IClassTransformer transformer) {
    Field fTransformers = LaunchClassLoader.class.getDeclaredField("transformers");
    fTransformers.setAccessible(true);
    @SuppressWarnings("unchecked") List<IClassTransformer> transformerList = (List<IClassTransformer>) fTransformers.get(Launch.classLoader);
    transformerList.add(transformer);
}
Also used : Field(java.lang.reflect.Field) IClassTransformer(net.minecraft.launchwrapper.IClassTransformer) List(java.util.List) ArrayList(java.util.ArrayList) SneakyThrows(lombok.SneakyThrows)

Aggregations

IClassTransformer (net.minecraft.launchwrapper.IClassTransformer)4 Field (java.lang.reflect.Field)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 File (java.io.File)2 Method (java.lang.reflect.Method)2 URL (java.net.URL)2 JarFile (java.util.jar.JarFile)2 SneakyThrows (lombok.SneakyThrows)2 Strings (com.google.common.base.Strings)1 ObjectArrays (com.google.common.collect.ObjectArrays)1 Resources (com.google.common.io.Resources)1 Ints (com.google.common.primitives.Ints)1 AccessTransformer (cpw.mods.fml.common.asm.transformers.AccessTransformer)1 ModAccessTransformer (cpw.mods.fml.common.asm.transformers.ModAccessTransformer)1 CoreModManager (cpw.mods.fml.relauncher.CoreModManager)1 FMLRelaunchLog (cpw.mods.fml.relauncher.FMLRelaunchLog)1 FileListHelper (cpw.mods.fml.relauncher.FileListHelper)1 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1