diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/EnigmaProfile.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/EnigmaProfile.java | 61 |
1 files changed, 54 insertions, 7 deletions
diff --git a/src/main/java/cuchaz/enigma/EnigmaProfile.java b/src/main/java/cuchaz/enigma/EnigmaProfile.java index 32f31e3..5a68be1 100644 --- a/src/main/java/cuchaz/enigma/EnigmaProfile.java +++ b/src/main/java/cuchaz/enigma/EnigmaProfile.java | |||
| @@ -2,29 +2,42 @@ package cuchaz.enigma; | |||
| 2 | 2 | ||
| 3 | import com.google.common.collect.ImmutableMap; | 3 | import com.google.common.collect.ImmutableMap; |
| 4 | import com.google.gson.Gson; | 4 | import com.google.gson.Gson; |
| 5 | import com.google.gson.GsonBuilder; | ||
| 6 | import com.google.gson.JsonDeserializationContext; | ||
| 7 | import com.google.gson.JsonDeserializer; | ||
| 8 | import com.google.gson.JsonElement; | ||
| 9 | import com.google.gson.JsonObject; | ||
| 10 | import com.google.gson.JsonParseException; | ||
| 5 | import com.google.gson.annotations.SerializedName; | 11 | import com.google.gson.annotations.SerializedName; |
| 12 | import com.google.gson.reflect.TypeToken; | ||
| 6 | import cuchaz.enigma.api.service.EnigmaServiceType; | 13 | import cuchaz.enigma.api.service.EnigmaServiceType; |
| 7 | import cuchaz.enigma.translation.mapping.MappingFileNameFormat; | 14 | import cuchaz.enigma.translation.mapping.MappingFileNameFormat; |
| 8 | import cuchaz.enigma.translation.mapping.MappingSaveParameters; | 15 | import cuchaz.enigma.translation.mapping.MappingSaveParameters; |
| 9 | 16 | ||
| 10 | import javax.annotation.Nullable; | ||
| 11 | import java.io.Reader; | 17 | import java.io.Reader; |
| 18 | import java.lang.reflect.Type; | ||
| 19 | import java.util.Collections; | ||
| 20 | import java.util.List; | ||
| 12 | import java.util.Map; | 21 | import java.util.Map; |
| 13 | import java.util.Optional; | 22 | import java.util.Optional; |
| 14 | 23 | ||
| 15 | public final class EnigmaProfile { | 24 | public final class EnigmaProfile { |
| 16 | public static final EnigmaProfile EMPTY = new EnigmaProfile(ImmutableMap.of()); | 25 | public static final EnigmaProfile EMPTY = new EnigmaProfile(new ServiceContainer(ImmutableMap.of())); |
| 17 | 26 | ||
| 18 | private static final MappingSaveParameters DEFAULT_MAPPING_SAVE_PARAMETERS = new MappingSaveParameters(MappingFileNameFormat.BY_DEOBF); | 27 | private static final MappingSaveParameters DEFAULT_MAPPING_SAVE_PARAMETERS = new MappingSaveParameters(MappingFileNameFormat.BY_DEOBF); |
| 19 | private static final Gson GSON = new Gson(); | 28 | private static final Gson GSON = new GsonBuilder() |
| 29 | .registerTypeAdapter(ServiceContainer.class, (JsonDeserializer<ServiceContainer>) EnigmaProfile::loadServiceContainer) | ||
| 30 | .create(); | ||
| 31 | private static final Type SERVICE_LIST_TYPE = new TypeToken<List<Service>>() { | ||
| 32 | }.getType(); | ||
| 20 | 33 | ||
| 21 | @SerializedName("services") | 34 | @SerializedName("services") |
| 22 | private final Map<String, Service> serviceProfiles; | 35 | private final ServiceContainer serviceProfiles; |
| 23 | 36 | ||
| 24 | @SerializedName("mapping_save_parameters") | 37 | @SerializedName("mapping_save_parameters") |
| 25 | private final MappingSaveParameters mappingSaveParameters = null; | 38 | private final MappingSaveParameters mappingSaveParameters = null; |
| 26 | 39 | ||
| 27 | private EnigmaProfile(Map<String, Service> serviceProfiles) { | 40 | private EnigmaProfile(ServiceContainer serviceProfiles) { |
| 28 | this.serviceProfiles = serviceProfiles; | 41 | this.serviceProfiles = serviceProfiles; |
| 29 | } | 42 | } |
| 30 | 43 | ||
| @@ -32,8 +45,30 @@ public final class EnigmaProfile { | |||
| 32 | return GSON.fromJson(reader, EnigmaProfile.class); | 45 | return GSON.fromJson(reader, EnigmaProfile.class); |
| 33 | } | 46 | } |
| 34 | 47 | ||
| 35 | @Nullable | 48 | private static ServiceContainer loadServiceContainer(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { |
| 36 | public Service getServiceProfile(EnigmaServiceType<?> serviceType) { | 49 | if (!json.isJsonObject()) { |
| 50 | throw new JsonParseException("services must be an Object!"); | ||
| 51 | } | ||
| 52 | |||
| 53 | JsonObject object = json.getAsJsonObject(); | ||
| 54 | |||
| 55 | ImmutableMap.Builder<String, List<Service>> builder = ImmutableMap.builder(); | ||
| 56 | |||
| 57 | for (Map.Entry<String, JsonElement> entry : object.entrySet()) { | ||
| 58 | JsonElement value = entry.getValue(); | ||
| 59 | if (value.isJsonObject()) { | ||
| 60 | builder.put(entry.getKey(), Collections.singletonList(GSON.fromJson(value, Service.class))); | ||
| 61 | } else if (value.isJsonArray()) { | ||
| 62 | builder.put(entry.getKey(), GSON.fromJson(value, SERVICE_LIST_TYPE)); | ||
| 63 | } else { | ||
| 64 | throw new JsonParseException(String.format("Don't know how to convert %s to a list of service!", value)); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | return new ServiceContainer(builder.build()); | ||
| 69 | } | ||
| 70 | |||
| 71 | public List<Service> getServiceProfiles(EnigmaServiceType<?> serviceType) { | ||
| 37 | return serviceProfiles.get(serviceType.key); | 72 | return serviceProfiles.get(serviceType.key); |
| 38 | } | 73 | } |
| 39 | 74 | ||
| @@ -59,4 +94,16 @@ public final class EnigmaProfile { | |||
| 59 | return args != null ? Optional.ofNullable(args.get(key)) : Optional.empty(); | 94 | return args != null ? Optional.ofNullable(args.get(key)) : Optional.empty(); |
| 60 | } | 95 | } |
| 61 | } | 96 | } |
| 97 | |||
| 98 | static final class ServiceContainer { | ||
| 99 | private final Map<String, List<Service>> services; | ||
| 100 | |||
| 101 | ServiceContainer(Map<String, List<Service>> services) { | ||
| 102 | this.services = services; | ||
| 103 | } | ||
| 104 | |||
| 105 | List<Service> get(String key) { | ||
| 106 | return services.getOrDefault(key, Collections.emptyList()); | ||
| 107 | } | ||
| 108 | } | ||
| 62 | } | 109 | } |