diff options
| author | 2020-06-03 13:39:42 -0400 | |
|---|---|---|
| committer | 2020-06-03 18:39:42 +0100 | |
| commit | 0f47403d0220757fed189b76e2071e25b1025cb8 (patch) | |
| tree | 879bf72c4476f0a5e0d82da99d7ff2b2276bcaca /src/main/java/cuchaz/enigma/EnigmaProfile.java | |
| parent | Fix search dialog hanging for a short time sometimes (#250) (diff) | |
| download | enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.tar.gz enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.tar.xz enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.zip | |
Split GUI code to separate module (#242)
* Split into modules
* Post merge compile fixes
Co-authored-by: modmuss50 <modmuss50@gmail.com>
Diffstat (limited to 'src/main/java/cuchaz/enigma/EnigmaProfile.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/EnigmaProfile.java | 131 |
1 files changed, 0 insertions, 131 deletions
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 @@ | |||
| 1 | package cuchaz.enigma; | ||
| 2 | |||
| 3 | import com.google.common.collect.ImmutableMap; | ||
| 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; | ||
| 11 | import com.google.gson.annotations.SerializedName; | ||
| 12 | import com.google.gson.reflect.TypeToken; | ||
| 13 | import cuchaz.enigma.api.service.EnigmaServiceType; | ||
| 14 | import cuchaz.enigma.translation.mapping.MappingFileNameFormat; | ||
| 15 | import cuchaz.enigma.translation.mapping.MappingSaveParameters; | ||
| 16 | |||
| 17 | import javax.annotation.Nullable; | ||
| 18 | import java.io.BufferedReader; | ||
| 19 | import java.io.IOException; | ||
| 20 | import java.io.InputStreamReader; | ||
| 21 | import java.io.Reader; | ||
| 22 | import java.lang.reflect.Type; | ||
| 23 | import java.nio.charset.StandardCharsets; | ||
| 24 | import java.nio.file.Files; | ||
| 25 | import java.nio.file.Path; | ||
| 26 | import java.util.Collections; | ||
| 27 | import java.util.List; | ||
| 28 | import java.util.Map; | ||
| 29 | import java.util.Optional; | ||
| 30 | |||
| 31 | public final class EnigmaProfile { | ||
| 32 | public static final EnigmaProfile EMPTY = new EnigmaProfile(new ServiceContainer(ImmutableMap.of())); | ||
| 33 | |||
| 34 | private static final MappingSaveParameters DEFAULT_MAPPING_SAVE_PARAMETERS = new MappingSaveParameters(MappingFileNameFormat.BY_DEOBF); | ||
| 35 | private static final Gson GSON = new GsonBuilder() | ||
| 36 | .registerTypeAdapter(ServiceContainer.class, (JsonDeserializer<ServiceContainer>) EnigmaProfile::loadServiceContainer) | ||
| 37 | .create(); | ||
| 38 | private static final Type SERVICE_LIST_TYPE = new TypeToken<List<Service>>() { | ||
| 39 | }.getType(); | ||
| 40 | |||
| 41 | @SerializedName("services") | ||
| 42 | private final ServiceContainer serviceProfiles; | ||
| 43 | |||
| 44 | @SerializedName("mapping_save_parameters") | ||
| 45 | private final MappingSaveParameters mappingSaveParameters = null; | ||
| 46 | |||
| 47 | private EnigmaProfile(ServiceContainer serviceProfiles) { | ||
| 48 | this.serviceProfiles = serviceProfiles; | ||
| 49 | } | ||
| 50 | |||
| 51 | public static EnigmaProfile read(@Nullable Path file) throws IOException { | ||
| 52 | if (file != null) { | ||
| 53 | try (BufferedReader reader = Files.newBufferedReader(file)) { | ||
| 54 | return EnigmaProfile.parse(reader); | ||
| 55 | } | ||
| 56 | } else { | ||
| 57 | try (BufferedReader reader = new BufferedReader(new InputStreamReader(Main.class.getResourceAsStream("/profile.json"), StandardCharsets.UTF_8))) { | ||
| 58 | return EnigmaProfile.parse(reader); | ||
| 59 | } catch (IOException ex) { | ||
| 60 | System.err.println("Failed to load default profile, will use empty profile: " + ex.getMessage()); | ||
| 61 | return EnigmaProfile.EMPTY; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | public static EnigmaProfile parse(Reader reader) { | ||
| 67 | return GSON.fromJson(reader, EnigmaProfile.class); | ||
| 68 | } | ||
| 69 | |||
| 70 | private static ServiceContainer loadServiceContainer(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||
| 71 | if (!json.isJsonObject()) { | ||
| 72 | throw new JsonParseException("services must be an Object!"); | ||
| 73 | } | ||
| 74 | |||
| 75 | JsonObject object = json.getAsJsonObject(); | ||
| 76 | |||
| 77 | ImmutableMap.Builder<String, List<Service>> builder = ImmutableMap.builder(); | ||
| 78 | |||
| 79 | for (Map.Entry<String, JsonElement> entry : object.entrySet()) { | ||
| 80 | JsonElement value = entry.getValue(); | ||
| 81 | if (value.isJsonObject()) { | ||
| 82 | builder.put(entry.getKey(), Collections.singletonList(GSON.fromJson(value, Service.class))); | ||
| 83 | } else if (value.isJsonArray()) { | ||
| 84 | builder.put(entry.getKey(), GSON.fromJson(value, SERVICE_LIST_TYPE)); | ||
| 85 | } else { | ||
| 86 | throw new JsonParseException(String.format("Don't know how to convert %s to a list of service!", value)); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | return new ServiceContainer(builder.build()); | ||
| 91 | } | ||
| 92 | |||
| 93 | public List<Service> getServiceProfiles(EnigmaServiceType<?> serviceType) { | ||
| 94 | return serviceProfiles.get(serviceType.key); | ||
| 95 | } | ||
| 96 | |||
| 97 | public MappingSaveParameters getMappingSaveParameters() { | ||
| 98 | //noinspection ConstantConditions | ||
| 99 | return mappingSaveParameters == null ? EnigmaProfile.DEFAULT_MAPPING_SAVE_PARAMETERS : mappingSaveParameters; | ||
| 100 | } | ||
| 101 | |||
| 102 | public static class Service { | ||
| 103 | private final String id; | ||
| 104 | private final Map<String, String> args; | ||
| 105 | |||
| 106 | Service(String id, Map<String, String> args) { | ||
| 107 | this.id = id; | ||
| 108 | this.args = args; | ||
| 109 | } | ||
| 110 | |||
| 111 | public boolean matches(String id) { | ||
| 112 | return this.id.equals(id); | ||
| 113 | } | ||
| 114 | |||
| 115 | public Optional<String> getArgument(String key) { | ||
| 116 | return args != null ? Optional.ofNullable(args.get(key)) : Optional.empty(); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | static final class ServiceContainer { | ||
| 121 | private final Map<String, List<Service>> services; | ||
| 122 | |||
| 123 | ServiceContainer(Map<String, List<Service>> services) { | ||
| 124 | this.services = services; | ||
| 125 | } | ||
| 126 | |||
| 127 | List<Service> get(String key) { | ||
| 128 | return services.getOrDefault(key, Collections.emptyList()); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | } | ||