Search in sources :

Example 1 with AccessTransformer

use of cpw.mods.fml.common.asm.transformers.AccessTransformer 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)

Aggregations

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 File (java.io.File)1 FileReader (java.io.FileReader)1 FilenameFilter (java.io.FilenameFilter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1