From 0f47403d0220757fed189b76e2071e25b1025cb8 Mon Sep 17 00:00:00 2001 From: Runemoro Date: Wed, 3 Jun 2020 13:39:42 -0400 Subject: Split GUI code to separate module (#242) * Split into modules * Post merge compile fixes Co-authored-by: modmuss50 --- src/main/java/cuchaz/enigma/EnigmaProfile.java | 131 ------------------------- 1 file changed, 131 deletions(-) delete mode 100644 src/main/java/cuchaz/enigma/EnigmaProfile.java (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 deleted file mode 100644 index 09b90f5..0000000 --- a/src/main/java/cuchaz/enigma/EnigmaProfile.java +++ /dev/null @@ -1,131 +0,0 @@ -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.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.Reader; -import java.lang.reflect.Type; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -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(new ServiceContainer(ImmutableMap.of())); - - private static final MappingSaveParameters DEFAULT_MAPPING_SAVE_PARAMETERS = new MappingSaveParameters(MappingFileNameFormat.BY_DEOBF); - 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 ServiceContainer serviceProfiles; - - @SerializedName("mapping_save_parameters") - private final MappingSaveParameters mappingSaveParameters = null; - - private EnigmaProfile(ServiceContainer serviceProfiles) { - this.serviceProfiles = serviceProfiles; - } - - public static EnigmaProfile read(@Nullable Path file) throws IOException { - if (file != null) { - try (BufferedReader reader = Files.newBufferedReader(file)) { - return EnigmaProfile.parse(reader); - } - } else { - try (BufferedReader reader = new BufferedReader(new InputStreamReader(Main.class.getResourceAsStream("/profile.json"), StandardCharsets.UTF_8))) { - return EnigmaProfile.parse(reader); - } catch (IOException ex) { - System.err.println("Failed to load default profile, will use empty profile: " + ex.getMessage()); - return EnigmaProfile.EMPTY; - } - } - } - - public static EnigmaProfile parse(Reader reader) { - return GSON.fromJson(reader, EnigmaProfile.class); - } - - 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); - } - - public MappingSaveParameters getMappingSaveParameters() { - //noinspection ConstantConditions - return mappingSaveParameters == null ? EnigmaProfile.DEFAULT_MAPPING_SAVE_PARAMETERS : mappingSaveParameters; - } - - public static class Service { - private final String id; - private final Map args; - - Service(String id, Map args) { - this.id = id; - this.args = args; - } - - public boolean matches(String id) { - return this.id.equals(id); - } - - public Optional getArgument(String key) { - 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