Search in sources :

Example 1 with ASMModParser

use of net.minecraftforge.fml.common.discovery.asm.ASMModParser in project MinecraftForge by MinecraftForge.

the class DirectoryDiscoverer method exploreFileSystem.

public void exploreFileSystem(String path, File modDir, List<ModContainer> harvestedMods, ModCandidate candidate, @Nullable MetadataCollection mc) {
    if (path.length() == 0) {
        File metadata = new File(modDir, "mcmod.info");
        try {
            FileInputStream fis = new FileInputStream(metadata);
            try {
                mc = MetadataCollection.from(fis, modDir.getName());
            } finally {
                IOUtils.closeQuietly(fis);
            }
            FMLLog.fine("Found an mcmod.info file in directory %s", modDir.getName());
        } catch (Exception e) {
            mc = MetadataCollection.from(null, "");
            FMLLog.fine("No mcmod.info file found in directory %s", modDir.getName());
        }
    }
    File[] content = modDir.listFiles(new ClassFilter());
    // Always sort our content
    Arrays.sort(content);
    for (File file : content) {
        if (file.isDirectory()) {
            FMLLog.finer("Recursing into package %s", path + file.getName());
            exploreFileSystem(path + file.getName() + "/", file, harvestedMods, candidate, mc);
            continue;
        }
        Matcher match = classFile.matcher(file.getName());
        if (match.matches()) {
            ASMModParser modParser = null;
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                modParser = new ASMModParser(fis);
                candidate.addClassEntry(path + file.getName());
            } catch (LoaderException e) {
                FMLLog.log(Level.ERROR, e, "There was a problem reading the file %s - probably this is a corrupt file", file.getPath());
                throw e;
            } catch (Exception e) {
                throw Throwables.propagate(e);
            } finally {
                IOUtils.closeQuietly(fis);
            }
            modParser.validate();
            modParser.sendToTable(table, candidate);
            ModContainer container = ModContainerFactory.instance().build(modParser, candidate.getModContainer(), candidate);
            if (container != null) {
                harvestedMods.add(container);
                container.bindMetadata(mc);
            }
        }
    }
}
Also used : LoaderException(net.minecraftforge.fml.common.LoaderException) ModContainer(net.minecraftforge.fml.common.ModContainer) Matcher(java.util.regex.Matcher) ASMModParser(net.minecraftforge.fml.common.discovery.asm.ASMModParser) File(java.io.File) FileInputStream(java.io.FileInputStream) LoaderException(net.minecraftforge.fml.common.LoaderException)

Example 2 with ASMModParser

use of net.minecraftforge.fml.common.discovery.asm.ASMModParser in project MinecraftForge by MinecraftForge.

the class JarDiscoverer method discover.

@Override
public List<ModContainer> discover(ModCandidate candidate, ASMDataTable table) {
    List<ModContainer> foundMods = Lists.newArrayList();
    FMLLog.fine("Examining file %s for potential mods", candidate.getModContainer().getName());
    JarFile jar = null;
    try {
        jar = new JarFile(candidate.getModContainer());
        ZipEntry modInfo = jar.getEntry("mcmod.info");
        MetadataCollection mc = null;
        if (modInfo != null) {
            FMLLog.finer("Located mcmod.info file in file %s", candidate.getModContainer().getName());
            InputStream inputStream = jar.getInputStream(modInfo);
            try {
                mc = MetadataCollection.from(inputStream, candidate.getModContainer().getName());
            } finally {
                IOUtils.closeQuietly(inputStream);
            }
        } else {
            FMLLog.fine("The mod container %s appears to be missing an mcmod.info file", candidate.getModContainer().getName());
            mc = MetadataCollection.from(null, "");
        }
        for (ZipEntry ze : Collections.list(jar.entries())) {
            if (ze.getName() != null && ze.getName().startsWith("__MACOSX")) {
                continue;
            }
            Matcher match = classFile.matcher(ze.getName());
            if (match.matches()) {
                ASMModParser modParser;
                try {
                    InputStream inputStream = jar.getInputStream(ze);
                    try {
                        modParser = new ASMModParser(inputStream);
                    } finally {
                        IOUtils.closeQuietly(inputStream);
                    }
                    candidate.addClassEntry(ze.getName());
                } catch (LoaderException e) {
                    FMLLog.log(Level.ERROR, e, "There was a problem reading the entry %s in the jar %s - probably a corrupt zip", ze.getName(), candidate.getModContainer().getPath());
                    jar.close();
                    throw e;
                }
                modParser.validate();
                modParser.sendToTable(table, candidate);
                ModContainer container = ModContainerFactory.instance().build(modParser, candidate.getModContainer(), candidate);
                if (container != null) {
                    table.addContainer(container);
                    foundMods.add(container);
                    container.bindMetadata(mc);
                    container.setClassVersion(modParser.getClassVersion());
                }
            }
        }
    } catch (Exception e) {
        FMLLog.log(Level.WARN, e, "Zip file %s failed to read properly, it will be ignored", candidate.getModContainer().getName());
    } finally {
        Java6Utils.closeZipQuietly(jar);
    }
    return foundMods;
}
Also used : ModContainer(net.minecraftforge.fml.common.ModContainer) LoaderException(net.minecraftforge.fml.common.LoaderException) Matcher(java.util.regex.Matcher) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ASMModParser(net.minecraftforge.fml.common.discovery.asm.ASMModParser) MetadataCollection(net.minecraftforge.fml.common.MetadataCollection) JarFile(java.util.jar.JarFile) LoaderException(net.minecraftforge.fml.common.LoaderException)

Aggregations

Matcher (java.util.regex.Matcher)2 LoaderException (net.minecraftforge.fml.common.LoaderException)2 ModContainer (net.minecraftforge.fml.common.ModContainer)2 ASMModParser (net.minecraftforge.fml.common.discovery.asm.ASMModParser)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 JarFile (java.util.jar.JarFile)1 ZipEntry (java.util.zip.ZipEntry)1 MetadataCollection (net.minecraftforge.fml.common.MetadataCollection)1