Search in sources :

Example 1 with MetadataCollection

use of net.minecraftforge.fml.common.MetadataCollection in project SpongeForge by SpongePowered.

the class SpongeModPluginContainer method bindMetadata.

@Override
@SuppressWarnings("unchecked")
public void bindMetadata(MetadataCollection mc) {
    this.metadata = mc.getMetadataForId(this.id, this.descriptor);
    if (isNullOrEmpty(this.metadata.name)) {
        this.metadata.name = this.id;
    }
    if (this.metadata.version == null) {
        this.metadata.version = "";
    }
    if (this.metadata.autogenerated) {
        if (!this.invalid) {
            SpongeImpl.getLogger().warn("Plugin '{}' seems to be missing a valid mcmod.info metadata file. This is not a problem when testing " + "plugins, however it is recommended to include one in public plugins.\n" + "Please see https://docs.spongepowered.org/master/en/plugin/plugin-meta.html for details.", this.id);
        }
        // Version is set in the dummy automatically (see getMetadataForId)
        this.metadata.description = getDescriptorValue("description");
        this.metadata.url = getDescriptorValue("url");
        Collection<String> authors = (Collection<String>) this.descriptor.get("authors");
        if (authors != null) {
            this.metadata.authorList = new ArrayList<>(authors);
        }
        Object deps = this.descriptor.get("dependencies");
        if (deps != null) {
            Iterable<Map<String, Object>> depDescriptors = (Iterable<Map<String, Object>>) this.descriptor.get("dependencies");
            if (depDescriptors != null) {
                Set<ArtifactVersion> requirements = this.metadata.requiredMods;
                List<ArtifactVersion> dependencies = this.metadata.dependencies;
                for (Map<String, Object> depDescriptor : depDescriptors) {
                    String dep = checkNotNull((String) depDescriptor.get("id"), "dependency id");
                    if (this.id.equals(dep)) {
                        this.invalid = true;
                        SpongeImpl.getLogger().error("Plugin '{}' cannot have a dependency on itself. This is redundant and should be " + "removed.", this.id);
                        continue;
                    }
                    String depVersion = (String) depDescriptor.get("version");
                    ArtifactVersion dependency;
                    if (isNullOrEmpty(depVersion)) {
                        dependency = new DefaultArtifactVersion(dep, true);
                    } else {
                        dependency = new DefaultArtifactVersion(dep, VersionParser.parseRange(depVersion));
                    }
                    Boolean optional = (Boolean) depDescriptor.get("optional");
                    if (optional == null || !optional) {
                        requirements.add(dependency);
                    }
                    // TODO: Load order
                    dependencies.add(dependency);
                }
            }
        }
    } else {
        // Check dependencies
        Iterator<ArtifactVersion> itr = this.metadata.requiredMods.iterator();
        while (itr.hasNext()) {
            if (this.id.equals(itr.next().getLabel())) {
                SpongeImpl.getLogger().warn("Plugin '{}' requires itself to be loaded. This is redundant and can be removed from the " + "dependencies.", this.id);
                itr.remove();
            }
        }
        if (!this.metadata.dependants.isEmpty()) {
            SpongeImpl.getLogger().error("Invalid dependency with load order AFTER on plugin '{}'. This is currently not supported for Sponge " + "plugins. Requested dependencies: {}", this.id, this.metadata.dependants);
            this.invalid = true;
        }
        this.metadata.dependants = ImmutableList.of();
    }
}
Also used : DefaultArtifactVersion(net.minecraftforge.fml.common.versioning.DefaultArtifactVersion) ArtifactVersion(net.minecraftforge.fml.common.versioning.ArtifactVersion) DefaultArtifactVersion(net.minecraftforge.fml.common.versioning.DefaultArtifactVersion) Collection(java.util.Collection) MetadataCollection(net.minecraftforge.fml.common.MetadataCollection) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with MetadataCollection

use of net.minecraftforge.fml.common.MetadataCollection in project SpongeForge by SpongePowered.

the class SpongeModMetadata method load.

public static void load() {
    if (metadata != null) {
        return;
    }
    try {
        String source = SpongeCoremod.modFile.toURI().toString();
        boolean isJar = source.endsWith(".jar");
        Enumeration<URL> urls = SpongeModMetadata.class.getClassLoader().getResources(McModInfo.STANDARD_FILENAME);
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            if (isJar) {
                if (!"jar".equals(url.getProtocol())) {
                    continue;
                }
                if (!url.getPath().startsWith(source)) {
                    // mcmod.info from different mod
                    continue;
                }
            } else if (!"file".equals(url.getProtocol())) {
                continue;
            }
            // Attempt to parse file
            try (InputStream in = url.openStream()) {
                metadata = MetadataCollection.from(in, url.toString());
                ModMetadata meta = getSpongeForgeMetadata();
                if (!meta.autogenerated) {
                    SpongeImpl.getLogger().info("Found mcmod.info at {}", url);
                    return;
                }
            // Continue searching (in development environment we may have the wrong mcmod.info file)
            }
        }
    } catch (IOException e) {
        SpongeImpl.getLogger().warn("Failed to load metadata", e);
    }
    if (metadata == null) {
        metadata = new MetadataCollection();
    }
}
Also used : ModMetadata(net.minecraftforge.fml.common.ModMetadata) InputStream(java.io.InputStream) MetadataCollection(net.minecraftforge.fml.common.MetadataCollection) IOException(java.io.IOException) URL(java.net.URL)

Example 3 with MetadataCollection

use of net.minecraftforge.fml.common.MetadataCollection 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)

Example 4 with MetadataCollection

use of net.minecraftforge.fml.common.MetadataCollection in project AppleCore by squeek502.

the class AppleCore method preInit.

@EventHandler
public void preInit(FMLPreInitializationEvent event) {
    // too lazy to figure out a real solution for this (@Mod enforces mcmod.info filename)
    // this will at least allow the metadata to populate the mod listing, though
    InputStream is = MetadataCollection.class.getResourceAsStream("/applecore.info");
    MetadataCollection metadataCollection = MetadataCollection.from(is, ModInfo.MODID);
    Loader.instance().activeModContainer().bindMetadata(metadataCollection);
    // force initialization of the singletons
    AppleCoreAccessorMutatorImpl.values();
    AppleCoreDispatcherImpl.values();
    AppleCoreRegistryImpl.values();
    FMLInterModComms.sendRuntimeMessage(ModInfo.MODID, "VersionChecker", "addVersionCheck", "http://www.ryanliptak.com/minecraft/versionchecker/squeek502/AppleCore");
}
Also used : InputStream(java.io.InputStream) MetadataCollection(net.minecraftforge.fml.common.MetadataCollection) EventHandler(net.minecraftforge.fml.common.Mod.EventHandler)

Aggregations

MetadataCollection (net.minecraftforge.fml.common.MetadataCollection)4 InputStream (java.io.InputStream)3 IOException (java.io.IOException)1 URL (java.net.URL)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JarFile (java.util.jar.JarFile)1 Matcher (java.util.regex.Matcher)1 ZipEntry (java.util.zip.ZipEntry)1 LoaderException (net.minecraftforge.fml.common.LoaderException)1 EventHandler (net.minecraftforge.fml.common.Mod.EventHandler)1 ModContainer (net.minecraftforge.fml.common.ModContainer)1 ModMetadata (net.minecraftforge.fml.common.ModMetadata)1 ASMModParser (net.minecraftforge.fml.common.discovery.asm.ASMModParser)1 ArtifactVersion (net.minecraftforge.fml.common.versioning.ArtifactVersion)1 DefaultArtifactVersion (net.minecraftforge.fml.common.versioning.DefaultArtifactVersion)1