2013-03-08 00:25:48 +00:00
|
|
|
/*
|
2016-06-23 03:49:47 +00:00
|
|
|
* Minecraft Forge
|
|
|
|
* Copyright (c) 2016.
|
2013-11-10 16:49:06 +00:00
|
|
|
*
|
2016-06-23 03:49:47 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation version 2.1
|
|
|
|
* of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2013-03-08 00:25:48 +00:00
|
|
|
*/
|
|
|
|
|
2014-09-23 05:01:24 +00:00
|
|
|
package net.minecraftforge.fml.common;
|
2012-07-22 14:26:38 +00:00
|
|
|
|
2014-01-19 15:49:28 +00:00
|
|
|
import java.io.IOException;
|
2012-07-22 14:26:38 +00:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.util.Map;
|
2014-09-23 05:01:24 +00:00
|
|
|
|
|
|
|
import net.minecraftforge.fml.common.versioning.ArtifactVersion;
|
|
|
|
import net.minecraftforge.fml.common.versioning.VersionParser;
|
|
|
|
|
2013-12-16 16:47:48 +00:00
|
|
|
import org.apache.logging.log4j.Level;
|
2012-07-22 14:26:38 +00:00
|
|
|
|
|
|
|
import com.google.common.base.Throwables;
|
|
|
|
import com.google.common.collect.Maps;
|
2013-12-03 04:46:42 +00:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonParseException;
|
|
|
|
import com.google.gson.JsonParser;
|
2014-01-19 15:49:28 +00:00
|
|
|
import com.google.gson.TypeAdapter;
|
|
|
|
import com.google.gson.stream.JsonReader;
|
|
|
|
import com.google.gson.stream.JsonWriter;
|
2012-07-22 14:26:38 +00:00
|
|
|
|
|
|
|
public class MetadataCollection
|
|
|
|
{
|
2013-12-03 04:46:42 +00:00
|
|
|
private String modListVersion;
|
|
|
|
private ModMetadata[] modList;
|
2012-07-22 14:26:38 +00:00
|
|
|
private Map<String, ModMetadata> metadatas = Maps.newHashMap();
|
|
|
|
|
2012-08-23 20:43:28 +00:00
|
|
|
public static MetadataCollection from(InputStream inputStream, String sourceName)
|
2012-07-22 14:26:38 +00:00
|
|
|
{
|
2012-07-23 19:03:17 +00:00
|
|
|
if (inputStream == null)
|
|
|
|
{
|
|
|
|
return new MetadataCollection();
|
|
|
|
}
|
2012-08-23 20:43:28 +00:00
|
|
|
|
2012-07-22 14:26:38 +00:00
|
|
|
InputStreamReader reader = new InputStreamReader(inputStream);
|
|
|
|
try
|
|
|
|
{
|
2013-12-03 04:46:42 +00:00
|
|
|
MetadataCollection collection;
|
2014-01-19 15:49:28 +00:00
|
|
|
Gson gson = new GsonBuilder().registerTypeAdapter(ArtifactVersion.class, new ArtifactVersionAdapter()).create();
|
2013-12-03 04:46:42 +00:00
|
|
|
JsonParser parser = new JsonParser();
|
|
|
|
JsonElement rootElement = parser.parse(reader);
|
|
|
|
if (rootElement.isJsonArray())
|
2012-07-22 14:26:38 +00:00
|
|
|
{
|
2013-12-03 04:46:42 +00:00
|
|
|
collection = new MetadataCollection();
|
|
|
|
JsonArray jsonList = rootElement.getAsJsonArray();
|
|
|
|
collection.modList = new ModMetadata[jsonList.size()];
|
|
|
|
int i = 0;
|
|
|
|
for (JsonElement mod : jsonList)
|
|
|
|
{
|
|
|
|
collection.modList[i++]=gson.fromJson(mod, ModMetadata.class);
|
|
|
|
}
|
2012-07-22 14:26:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-03 04:46:42 +00:00
|
|
|
collection = gson.fromJson(rootElement, MetadataCollection.class);
|
2012-07-22 14:26:38 +00:00
|
|
|
}
|
2013-12-03 04:46:42 +00:00
|
|
|
collection.parseModMetadataList();
|
|
|
|
return collection;
|
2012-07-22 14:26:38 +00:00
|
|
|
}
|
2013-12-03 04:46:42 +00:00
|
|
|
catch (JsonParseException e)
|
2012-08-23 20:43:28 +00:00
|
|
|
{
|
2013-12-16 16:47:48 +00:00
|
|
|
FMLLog.log(Level.ERROR, e, "The mcmod.info file in %s cannot be parsed as valid JSON. It will be ignored", sourceName);
|
2012-08-23 21:27:53 +00:00
|
|
|
return new MetadataCollection();
|
2012-08-23 20:43:28 +00:00
|
|
|
}
|
2012-07-22 14:26:38 +00:00
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
throw Throwables.propagate(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-03 04:46:42 +00:00
|
|
|
private void parseModMetadataList()
|
2012-07-22 14:26:38 +00:00
|
|
|
{
|
2013-12-03 04:46:42 +00:00
|
|
|
for (ModMetadata modMetadata : modList)
|
2012-07-22 14:26:38 +00:00
|
|
|
{
|
2013-12-03 04:46:42 +00:00
|
|
|
metadatas.put(modMetadata.modId, modMetadata);
|
2012-07-22 14:26:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-23 19:03:17 +00:00
|
|
|
public ModMetadata getMetadataForId(String modId, Map<String, Object> extraData)
|
2012-07-22 14:26:38 +00:00
|
|
|
{
|
2012-07-23 19:03:17 +00:00
|
|
|
if (!metadatas.containsKey(modId))
|
|
|
|
{
|
|
|
|
ModMetadata dummy = new ModMetadata();
|
|
|
|
dummy.modId = modId;
|
|
|
|
dummy.name = (String) extraData.get("name");
|
|
|
|
dummy.version = (String) extraData.get("version");
|
|
|
|
dummy.autogenerated = true;
|
|
|
|
metadatas.put(modId, dummy);
|
|
|
|
}
|
2012-07-22 14:26:38 +00:00
|
|
|
return metadatas.get(modId);
|
|
|
|
}
|
|
|
|
|
2014-01-19 15:49:28 +00:00
|
|
|
public static class ArtifactVersionAdapter extends TypeAdapter<ArtifactVersion>
|
|
|
|
{
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(JsonWriter out, ArtifactVersion value) throws IOException
|
|
|
|
{
|
|
|
|
// no op - we never write these out
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ArtifactVersion read(JsonReader in) throws IOException
|
|
|
|
{
|
|
|
|
return VersionParser.parseVersionReference(in.nextString());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-07-22 14:26:38 +00:00
|
|
|
}
|