| 1 | package net.minecraft.client.resources.data;
|
| 2 |
|
| 3 | import com.google.gson.Gson;
|
| 4 | import com.google.gson.GsonBuilder;
|
| 5 | import com.google.gson.JsonElement;
|
| 6 | import com.google.gson.JsonObject;
|
| 7 | import net.minecraft.util.ChatStyle;
|
| 8 | import net.minecraft.util.EnumTypeAdapterFactory;
|
| 9 | import net.minecraft.util.IChatComponent;
|
| 10 | import net.minecraft.util.IRegistry;
|
| 11 | import net.minecraft.util.RegistrySimple;
|
| 12 |
|
| 13 | public class IMetadataSerializer
|
| 14 | {
|
| 15 | private final IRegistry < String, IMetadataSerializer.Registration <? extends IMetadataSection >> metadataSectionSerializerRegistry = new RegistrySimple();
|
| 16 | private final GsonBuilder gsonBuilder = new GsonBuilder();
|
| 17 |
|
| 18 | /**
|
| 19 | * Cached Gson instance. Set to null when more sections are registered, and then re-created from the builder.
|
| 20 | */
|
| 21 | private Gson gson;
|
| 22 |
|
| 23 | public IMetadataSerializer()
|
| 24 | {
|
| 25 | this.gsonBuilder.registerTypeHierarchyAdapter(IChatComponent.class, new IChatComponent.Serializer());
|
| 26 | this.gsonBuilder.registerTypeHierarchyAdapter(ChatStyle.class, new ChatStyle.Serializer());
|
| 27 | this.gsonBuilder.registerTypeAdapterFactory(new EnumTypeAdapterFactory());
|
| 28 | }
|
| 29 |
|
| 30 | public <T extends IMetadataSection> void registerMetadataSectionType(IMetadataSectionSerializer<T> metadataSectionSerializer, Class<T> clazz)
|
| 31 | {
|
| 32 | this.metadataSectionSerializerRegistry.putObject(metadataSectionSerializer.getSectionName(), new IMetadataSerializer.Registration(metadataSectionSerializer, clazz));
|
| 33 | this.gsonBuilder.registerTypeAdapter(clazz, metadataSectionSerializer);
|
| 34 | this.gson = null;
|
| 35 | }
|
| 36 |
|
| 37 | public <T extends IMetadataSection> T parseMetadataSection(String sectionName, JsonObject json)
|
| 38 | {
|
| 39 | if (sectionName == null)
|
| 40 | {
|
| 41 | throw new IllegalArgumentException("Metadata section name cannot be null");
|
| 42 | }
|
| 43 | else if (!json.has(sectionName))
|
| 44 | {
|
| 45 | return (T)null;
|
| 46 | }
|
| 47 | else if (!json.get(sectionName).isJsonObject())
|
| 48 | {
|
| 49 | throw new IllegalArgumentException("Invalid metadata for \'" + sectionName + "\' - expected object, found " + json.get(sectionName));
|
| 50 | }
|
| 51 | else
|
| 52 | {
|
| 53 | IMetadataSerializer.Registration<?> registration = (IMetadataSerializer.Registration)this.metadataSectionSerializerRegistry.getObject(sectionName);
|
| 54 |
|
| 55 | if (registration == null)
|
| 56 | {
|
| 57 | throw new IllegalArgumentException("Don\'t know how to handle metadata section \'" + sectionName + "\'");
|
| 58 | }
|
| 59 | else
|
| 60 | {
|
| 61 | return (T)((IMetadataSection)this.getGson().fromJson((JsonElement)json.getAsJsonObject(sectionName), registration.clazz));
|
| 62 | }
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | /**
|
| 67 | * Returns a Gson instance with type adapters registered for metadata sections.
|
| 68 | */
|
| 69 | private Gson getGson()
|
| 70 | {
|
| 71 | if (this.gson == null)
|
| 72 | {
|
| 73 | this.gson = this.gsonBuilder.create();
|
| 74 | }
|
| 75 |
|
| 76 | return this.gson;
|
| 77 | }
|
| 78 |
|
| 79 | class Registration<T extends IMetadataSection>
|
| 80 | {
|
| 81 | final IMetadataSectionSerializer<T> section;
|
| 82 | final Class<T> clazz;
|
| 83 |
|
| 84 | private Registration(IMetadataSectionSerializer<T> metadataSectionSerializer, Class<T> clazzToRegister)
|
| 85 | {
|
| 86 | this.section = metadataSectionSerializer;
|
| 87 | this.clazz = clazzToRegister;
|
| 88 | }
|
| 89 | }
|
| 90 | }
|
| 91 |
|