summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/EnigmaProfile.java
diff options
context:
space:
mode:
authorGravatar Runemoro2020-06-03 13:39:42 -0400
committerGravatar GitHub2020-06-03 18:39:42 +0100
commit0f47403d0220757fed189b76e2071e25b1025cb8 (patch)
tree879bf72c4476f0a5e0d82da99d7ff2b2276bcaca /src/main/java/cuchaz/enigma/EnigmaProfile.java
parentFix search dialog hanging for a short time sometimes (#250) (diff)
downloadenigma-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.java131
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 @@
1package cuchaz.enigma;
2
3import com.google.common.collect.ImmutableMap;
4import com.google.gson.Gson;
5import com.google.gson.GsonBuilder;
6import com.google.gson.JsonDeserializationContext;
7import com.google.gson.JsonDeserializer;
8import com.google.gson.JsonElement;
9import com.google.gson.JsonObject;
10import com.google.gson.JsonParseException;
11import com.google.gson.annotations.SerializedName;
12import com.google.gson.reflect.TypeToken;
13import cuchaz.enigma.api.service.EnigmaServiceType;
14import cuchaz.enigma.translation.mapping.MappingFileNameFormat;
15import cuchaz.enigma.translation.mapping.MappingSaveParameters;
16
17import javax.annotation.Nullable;
18import java.io.BufferedReader;
19import java.io.IOException;
20import java.io.InputStreamReader;
21import java.io.Reader;
22import java.lang.reflect.Type;
23import java.nio.charset.StandardCharsets;
24import java.nio.file.Files;
25import java.nio.file.Path;
26import java.util.Collections;
27import java.util.List;
28import java.util.Map;
29import java.util.Optional;
30
31public 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}