summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/config/Config.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/cuchaz/enigma/config/Config.java')
-rw-r--r--src/main/java/cuchaz/enigma/config/Config.java261
1 files changed, 0 insertions, 261 deletions
diff --git a/src/main/java/cuchaz/enigma/config/Config.java b/src/main/java/cuchaz/enigma/config/Config.java
deleted file mode 100644
index e116fce..0000000
--- a/src/main/java/cuchaz/enigma/config/Config.java
+++ /dev/null
@@ -1,261 +0,0 @@
1package cuchaz.enigma.config;
2
3import com.bulenkov.darcula.DarculaLaf;
4import com.google.common.io.Files;
5import com.google.gson.*;
6import cuchaz.enigma.source.DecompilerService;
7import cuchaz.enigma.source.Decompilers;
8
9import cuchaz.enigma.utils.I18n;
10
11import javax.swing.*;
12import javax.swing.plaf.metal.MetalLookAndFeel;
13import java.awt.*;
14import java.awt.image.BufferedImage;
15import java.io.File;
16import java.io.IOException;
17import java.lang.reflect.Type;
18import java.nio.charset.Charset;
19
20public class Config {
21 public static class AlphaColorEntry {
22 public Integer rgb;
23 public float alpha = 1.0f;
24
25 public AlphaColorEntry(Integer rgb, float alpha) {
26 this.rgb = rgb;
27 this.alpha = alpha;
28 }
29
30 public Color get() {
31 if (rgb == null) {
32 return new Color(0, 0, 0, 0);
33 }
34
35 Color baseColor = new Color(rgb);
36 return new Color(baseColor.getRed(), baseColor.getGreen(), baseColor.getBlue(), (int)(255 * alpha));
37 }
38 }
39
40 public enum LookAndFeel {
41 DEFAULT("Default"),
42 DARCULA("Darcula"),
43 SYSTEM("System"),
44 NONE("None (JVM default)");
45
46 // the "JVM default" look and feel, get it at the beginning and store it so we can set it later
47 private static javax.swing.LookAndFeel NONE_LAF = UIManager.getLookAndFeel();
48 private final String name;
49
50 LookAndFeel(String name) {
51 this.name = name;
52 }
53
54 public String getName() {
55 return name;
56 }
57
58 public void setGlobalLAF() {
59 try {
60 switch (this) {
61 case NONE:
62 UIManager.setLookAndFeel(NONE_LAF);
63 break;
64 case DEFAULT:
65 UIManager.setLookAndFeel(new MetalLookAndFeel());
66 break;
67 case DARCULA:
68 UIManager.setLookAndFeel(new DarculaLaf());
69 break;
70 case SYSTEM:
71 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
72 }
73 } catch (Exception e){
74 throw new Error("Failed to set global look and feel", e);
75 }
76 }
77
78 public static boolean isDarkLaf() {
79 // a bit of a hack because swing doesn't give any API for that, and we need colors that aren't defined in look and feel
80 JPanel panel = new JPanel();
81 panel.setSize(new Dimension(10, 10));
82 panel.doLayout();
83
84 BufferedImage image = new BufferedImage(panel.getSize().width, panel.getSize().height, BufferedImage.TYPE_INT_RGB);
85 panel.printAll(image.getGraphics());
86
87 Color c = new Color(image.getRGB(0, 0));
88
89 // convert the color we got to grayscale
90 int b = (int) (0.3 * c.getRed() + 0.59 * c.getGreen() + 0.11 * c.getBlue());
91 return b < 85;
92 }
93
94 public void apply(Config config) {
95 boolean isDark = this == LookAndFeel.DARCULA || isDarkLaf();
96 if (!isDark) {//Defaults found here: https://github.com/Sciss/SyntaxPane/blob/122da367ff7a5d31627a70c62a48a9f0f4f85a0a/src/main/resources/de/sciss/syntaxpane/defaultsyntaxkit/config.properties#L139
97 config.lineNumbersForeground = 0x333300;
98 config.lineNumbersBackground = 0xEEEEFF;
99 config.lineNumbersSelected = 0xCCCCEE;
100 config.obfuscatedColor = new AlphaColorEntry(0xFFDCDC, 1.0f);
101 config.obfuscatedColorOutline = new AlphaColorEntry(0xA05050, 1.0f);
102 config.proposedColor = new AlphaColorEntry(0x000000, 0.075f);
103 config.proposedColorOutline = new AlphaColorEntry(0x000000, 0.15f);
104 config.deobfuscatedColor = new AlphaColorEntry(0xDCFFDC, 1.0f);
105 config.deobfuscatedColorOutline = new AlphaColorEntry(0x50A050, 1.0f);
106 config.editorBackground = 0xFFFFFF;
107 config.highlightColor = 0x3333EE;
108 config.caretColor = 0x000000;
109 config.selectionHighlightColor = 0x000000;
110 config.stringColor = 0xCC6600;
111 config.numberColor = 0x999933;
112 config.operatorColor = 0x000000;
113 config.delimiterColor = 0x000000;
114 config.typeColor = 0x000000;
115 config.identifierColor = 0x000000;
116 config.defaultTextColor = 0x000000;
117 } else {//Based off colors found here: https://github.com/dracula/dracula-theme/
118 config.lineNumbersForeground = 0xA4A4A3;
119 config.lineNumbersBackground = 0x313335;
120 config.lineNumbersSelected = 0x606366;
121 config.obfuscatedColor = new AlphaColorEntry(0xFF5555, 0.3f);
122 config.obfuscatedColorOutline = new AlphaColorEntry(0xFF5555, 0.5f);
123 config.deobfuscatedColor = new AlphaColorEntry(0x50FA7B, 0.3f);
124 config.deobfuscatedColorOutline = new AlphaColorEntry(0x50FA7B, 0.5f);
125 config.proposedColor = new AlphaColorEntry(0x606366, 0.3f);
126 config.proposedColorOutline = new AlphaColorEntry(0x606366, 0.5f);
127 config.editorBackground = 0x282A36;
128 config.highlightColor = 0xFF79C6;
129 config.caretColor = 0xF8F8F2;
130 config.selectionHighlightColor = 0xF8F8F2;
131 config.stringColor = 0xF1FA8C;
132 config.numberColor = 0xBD93F9;
133 config.operatorColor = 0xF8F8F2;
134 config.delimiterColor = 0xF8F8F2;
135 config.typeColor = 0xF8F8F2;
136 config.identifierColor = 0xF8F8F2;
137 config.defaultTextColor = 0xF8F8F2;
138 }
139 }
140 }
141
142 public enum Decompiler {
143 PROCYON("Procyon", Decompilers.PROCYON),
144 CFR("CFR", Decompilers.CFR);
145
146 public final DecompilerService service;
147 public final String name;
148
149 Decompiler(String name, DecompilerService service) {
150 this.name = name;
151 this.service = service;
152 }
153 }
154
155 private static final File DIR_HOME = new File(System.getProperty("user.home"));
156 private static final File ENIGMA_DIR = new File(DIR_HOME, ".enigma");
157 private static final File CONFIG_FILE = new File(ENIGMA_DIR, "config.json");
158 private static final Config INSTANCE = new Config();
159
160 private final transient Gson gson; // transient to exclude it from being exposed
161
162 public AlphaColorEntry obfuscatedColor;
163 public AlphaColorEntry obfuscatedColorOutline;
164 public AlphaColorEntry proposedColor;
165 public AlphaColorEntry proposedColorOutline;
166 public AlphaColorEntry deobfuscatedColor;
167 public AlphaColorEntry deobfuscatedColorOutline;
168
169 public Integer editorBackground;
170 public Integer highlightColor;
171 public Integer caretColor;
172 public Integer selectionHighlightColor;
173
174 public Integer stringColor;
175 public Integer numberColor;
176 public Integer operatorColor;
177 public Integer delimiterColor;
178 public Integer typeColor;
179 public Integer identifierColor;
180 public Integer defaultTextColor;
181
182 public Integer lineNumbersBackground;
183 public Integer lineNumbersSelected;
184 public Integer lineNumbersForeground;
185
186 public String language = I18n.DEFAULT_LANGUAGE;
187
188 public LookAndFeel lookAndFeel = LookAndFeel.DEFAULT;
189
190 public float scaleFactor = 1.0f;
191
192 public Decompiler decompiler = Decompiler.PROCYON;
193
194 private Config() {
195 gson = new GsonBuilder()
196 .registerTypeAdapter(Integer.class, new IntSerializer())
197 .registerTypeAdapter(Integer.class, new IntDeserializer())
198 .registerTypeAdapter(Config.class, (InstanceCreator<Config>) type -> this)
199 .setPrettyPrinting()
200 .create();
201 try {
202 this.loadConfig();
203 } catch (IOException ignored) {
204 try {
205 this.reset();
206 } catch (IOException ignored1) {
207 }
208 }
209 }
210
211 public void loadConfig() throws IOException {
212 if (!ENIGMA_DIR.exists()) ENIGMA_DIR.mkdirs();
213 File configFile = new File(ENIGMA_DIR, "config.json");
214 boolean loaded = false;
215
216 if (configFile.exists()) {
217 try {
218 gson.fromJson(Files.asCharSource(configFile, Charset.defaultCharset()).read(), Config.class);
219 loaded = true;
220 } catch (Exception e) {
221 e.printStackTrace();
222 }
223 }
224
225 if (!loaded) {
226 this.reset();
227 Files.touch(configFile);
228 }
229 saveConfig();
230 }
231
232 public void saveConfig() throws IOException {
233 Files.asCharSink(CONFIG_FILE, Charset.defaultCharset()).write(gson.toJson(this));
234 }
235
236 public void reset() throws IOException {
237 this.lookAndFeel = LookAndFeel.DEFAULT;
238 this.lookAndFeel.apply(this);
239 this.decompiler = Decompiler.PROCYON;
240 this.language = I18n.DEFAULT_LANGUAGE;
241 this.saveConfig();
242 }
243
244 private static class IntSerializer implements JsonSerializer<Integer> {
245 @Override
246 public JsonElement serialize(Integer src, Type typeOfSrc, JsonSerializationContext context) {
247 return new JsonPrimitive("#" + Integer.toHexString(src).toUpperCase());
248 }
249 }
250
251 private static class IntDeserializer implements JsonDeserializer<Integer> {
252 @Override
253 public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
254 return (int) Long.parseLong(json.getAsString().replace("#", ""), 16);
255 }
256 }
257
258 public static Config getInstance() {
259 return INSTANCE;
260 }
261}