From ad59e46740ef636b95667615e3881fcee6fbbcb9 Mon Sep 17 00:00:00 2001 From: liach Date: Fri, 8 Nov 2019 16:35:19 -0600 Subject: Allow multiple services for enigma (#168) * Allow multiple services for enigma Signed-off-by: liach * Delete bad dummy Signed-off-by: liach --- src/main/java/cuchaz/enigma/EnigmaProfile.java | 61 +++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 7 deletions(-) (limited to 'src/main/java/cuchaz/enigma/EnigmaProfile.java') 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; import com.google.common.collect.ImmutableMap; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; import com.google.gson.annotations.SerializedName; +import com.google.gson.reflect.TypeToken; import cuchaz.enigma.api.service.EnigmaServiceType; import cuchaz.enigma.translation.mapping.MappingFileNameFormat; import cuchaz.enigma.translation.mapping.MappingSaveParameters; -import javax.annotation.Nullable; import java.io.Reader; +import java.lang.reflect.Type; +import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.Optional; public final class EnigmaProfile { - public static final EnigmaProfile EMPTY = new EnigmaProfile(ImmutableMap.of()); + public static final EnigmaProfile EMPTY = new EnigmaProfile(new ServiceContainer(ImmutableMap.of())); private static final MappingSaveParameters DEFAULT_MAPPING_SAVE_PARAMETERS = new MappingSaveParameters(MappingFileNameFormat.BY_DEOBF); - private static final Gson GSON = new Gson(); + private static final Gson GSON = new GsonBuilder() + .registerTypeAdapter(ServiceContainer.class, (JsonDeserializer) EnigmaProfile::loadServiceContainer) + .create(); + private static final Type SERVICE_LIST_TYPE = new TypeToken>() { + }.getType(); @SerializedName("services") - private final Map serviceProfiles; + private final ServiceContainer serviceProfiles; @SerializedName("mapping_save_parameters") private final MappingSaveParameters mappingSaveParameters = null; - private EnigmaProfile(Map serviceProfiles) { + private EnigmaProfile(ServiceContainer serviceProfiles) { this.serviceProfiles = serviceProfiles; } @@ -32,8 +45,30 @@ public final class EnigmaProfile { return GSON.fromJson(reader, EnigmaProfile.class); } - @Nullable - public Service getServiceProfile(EnigmaServiceType serviceType) { + private static ServiceContainer loadServiceContainer(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + if (!json.isJsonObject()) { + throw new JsonParseException("services must be an Object!"); + } + + JsonObject object = json.getAsJsonObject(); + + ImmutableMap.Builder> builder = ImmutableMap.builder(); + + for (Map.Entry entry : object.entrySet()) { + JsonElement value = entry.getValue(); + if (value.isJsonObject()) { + builder.put(entry.getKey(), Collections.singletonList(GSON.fromJson(value, Service.class))); + } else if (value.isJsonArray()) { + builder.put(entry.getKey(), GSON.fromJson(value, SERVICE_LIST_TYPE)); + } else { + throw new JsonParseException(String.format("Don't know how to convert %s to a list of service!", value)); + } + } + + return new ServiceContainer(builder.build()); + } + + public List getServiceProfiles(EnigmaServiceType serviceType) { return serviceProfiles.get(serviceType.key); } @@ -59,4 +94,16 @@ public final class EnigmaProfile { return args != null ? Optional.ofNullable(args.get(key)) : Optional.empty(); } } + + static final class ServiceContainer { + private final Map> services; + + ServiceContainer(Map> services) { + this.services = services; + } + + List get(String key) { + return services.getOrDefault(key, Collections.emptyList()); + } + } } -- cgit v1.2.3