    001/*
    002 * Forge Mod Loader
    003 * Copyright (c) 2012-2013 cpw.
    004 * All rights reserved. This program and the accompanying materials
    005 * are made available under the terms of the GNU Lesser Public License v2.1
    006 * which accompanies this distribution, and is available at
    007 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
    008 *
    009 * Contributors:
    010 *     cpw - implementation
    011 */
    012
    013package cpw.mods.fml.common;
    014
    015import java.io.InputStream;
    016import java.io.InputStreamReader;
    017import java.util.Map;
    018import java.util.logging.Level;
    019
    020import argo.jdom.JdomParser;
    021import argo.jdom.JsonNode;
    022import argo.jdom.JsonRootNode;
    023import argo.saj.InvalidSyntaxException;
    024
    025import com.google.common.base.Throwables;
    026import com.google.common.collect.Maps;
    027
    028public class MetadataCollection
    029{
    030    private static JdomParser parser = new JdomParser();
    031    private Map<String, ModMetadata> metadatas = Maps.newHashMap();
    032    private int metadataVersion = 1;
    033
    034    public static MetadataCollection from(InputStream inputStream, String sourceName)
    035    {
    036        if (inputStream == null)
    037        {
    038            return new MetadataCollection();
    039        }
    040
    041        InputStreamReader reader = new InputStreamReader(inputStream);
    042        try
    043        {
    044            JsonRootNode root = parser.parse(reader);
    045            if (root.hasElements())
    046            {
    047                return parse10ModInfo(root);
    048            }
    049            else
    050            {
    051                return parseModInfo(root);
    052            }
    053        }
    054        catch (InvalidSyntaxException e)
    055        {
    056            FMLLog.log(Level.SEVERE, e, "The mcmod.info file in %s cannot be parsed as valid JSON. It will be ignored", sourceName);
    057            return new MetadataCollection();
    058        }
    059        catch (Exception e)
    060        {
    061            throw Throwables.propagate(e);
    062        }
    063    }
    064
    065    private static MetadataCollection parseModInfo(JsonRootNode root)
    066    {
    067        MetadataCollection mc = new MetadataCollection();
    068        mc.metadataVersion = Integer.parseInt(root.getNumberValue("modinfoversion"));
    069        mc.parseModMetadataList(root.getNode("modlist"));
    070        return mc;
    071    }
    072
    073    private static MetadataCollection parse10ModInfo(JsonRootNode root)
    074    {
    075        MetadataCollection mc = new MetadataCollection();
    076        mc.parseModMetadataList(root);
    077        return mc;
    078    }
    079
    080    private void parseModMetadataList(JsonNode metadataList)
    081    {
    082        for (JsonNode node : metadataList.getElements())
    083        {
    084            ModMetadata mmd = new ModMetadata(node);
    085            metadatas.put(mmd.modId, mmd);
    086        }
    087    }
    088
    089    public ModMetadata getMetadataForId(String modId, Map<String, Object> extraData)
    090    {
    091        if (!metadatas.containsKey(modId))
    092        {
    093            ModMetadata dummy = new ModMetadata();
    094            dummy.modId = modId;
    095            dummy.name = (String) extraData.get("name");
    096            dummy.version = (String) extraData.get("version");
    097            dummy.autogenerated = true;
    098            metadatas.put(modId, dummy);
    099        }
    100        return metadatas.get(modId);
    101    }
    102
    103}