    001package cpw.mods.fml.common;
    002
    003import java.io.InputStream;
    004import java.io.InputStreamReader;
    005import java.util.Map;
    006import java.util.logging.Level;
    007
    008import argo.jdom.JdomParser;
    009import argo.jdom.JsonNode;
    010import argo.jdom.JsonRootNode;
    011import argo.saj.InvalidSyntaxException;
    012
    013import com.google.common.base.Throwables;
    014import com.google.common.collect.Maps;
    015
    016public class MetadataCollection
    017{
    018    private static JdomParser parser = new JdomParser();
    019    private Map<String, ModMetadata> metadatas = Maps.newHashMap();
    020    private int metadataVersion = 1;
    021
    022    public static MetadataCollection from(InputStream inputStream, String sourceName)
    023    {
    024        if (inputStream == null)
    025        {
    026            return new MetadataCollection();
    027        }
    028
    029        InputStreamReader reader = new InputStreamReader(inputStream);
    030        try
    031        {
    032            JsonRootNode root = parser.parse(reader);
    033            if (root.hasElements())
    034            {
    035                return parse10ModInfo(root);
    036            }
    037            else
    038            {
    039                return parseModInfo(root);
    040            }
    041        }
    042        catch (InvalidSyntaxException e)
    043        {
    044            FMLLog.log(Level.SEVERE, e, "The mcmod.info file in %s cannot be parsed as valid JSON. It will be ignored", sourceName);
    045            return new MetadataCollection();
    046        }
    047        catch (Exception e)
    048        {
    049            throw Throwables.propagate(e);
    050        }
    051    }
    052
    053    private static MetadataCollection parseModInfo(JsonRootNode root)
    054    {
    055        MetadataCollection mc = new MetadataCollection();
    056        mc.metadataVersion = Integer.parseInt(root.getNumberValue("modinfoversion"));
    057        mc.parseModMetadataList(root.getNode("modlist"));
    058        return mc;
    059    }
    060
    061    private static MetadataCollection parse10ModInfo(JsonRootNode root)
    062    {
    063        MetadataCollection mc = new MetadataCollection();
    064        mc.parseModMetadataList(root);
    065        return mc;
    066    }
    067
    068    private void parseModMetadataList(JsonNode metadataList)
    069    {
    070        for (JsonNode node : metadataList.getElements())
    071        {
    072            ModMetadata mmd = new ModMetadata(node);
    073            metadatas.put(mmd.modId, mmd);
    074        }
    075    }
    076
    077    public ModMetadata getMetadataForId(String modId, Map<String, Object> extraData)
    078    {
    079        if (!metadatas.containsKey(modId))
    080        {
    081            ModMetadata dummy = new ModMetadata();
    082            dummy.modId = modId;
    083            dummy.name = (String) extraData.get("name");
    084            dummy.version = (String) extraData.get("version");
    085            dummy.autogenerated = true;
    086            metadatas.put(modId, dummy);
    087        }
    088        return metadatas.get(modId);
    089    }
    090
    091}