summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/config/Config.java
blob: 6f5a337c10a21842955c55a24948ade7d736832b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package cuchaz.enigma.config;

import com.bulenkov.darcula.DarculaLaf;
import com.google.common.io.Files;
import com.google.gson.*;

import javax.swing.*;
import javax.swing.plaf.metal.MetalLookAndFeel;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.Charset;

public class Config {
	public static class AlphaColorEntry {
		public Integer rgb;
		public float alpha = 1.0f;

		public AlphaColorEntry(Integer rgb, float alpha) {
			this.rgb = rgb;
			this.alpha = alpha;
		}

		public Color get() {
			if (rgb == null) {
				return new Color(0, 0, 0, 0);
			}

			Color baseColor = new Color(rgb);
			return new Color(baseColor.getRed(), baseColor.getGreen(), baseColor.getBlue(), (int)(255 * alpha));
		}
	}

    public enum LookAndFeel {
		DEFAULT("Default"),
		DARCULA("Darcula");

		private final String name;

		LookAndFeel(String name) {
			this.name = name;
		}

		public String getName() {
			return name;
		}

		public void setGlobalLAF() {
			try {
				switch (this) {
					case DEFAULT:
						UIManager.setLookAndFeel(new MetalLookAndFeel());
						break;
					case DARCULA:
						UIManager.setLookAndFeel(new DarculaLaf());
						break;
				}
			} catch (Exception e){
				throw new Error("Failed to set global look and feel", e);
			}
		}

		public void apply(Config config) {
			switch (this) {
				case DEFAULT:
					//Defaults found here: https://github.com/Sciss/SyntaxPane/blob/122da367ff7a5d31627a70c62a48a9f0f4f85a0a/src/main/resources/de/sciss/syntaxpane/defaultsyntaxkit/config.properties#L139
					config.lineNumbersForeground = 0x333300;
					config.lineNumbersBackground = 0xEEEEFF;
					config.lineNumbersSelected = 0xCCCCEE;
					config.obfuscatedColor = new AlphaColorEntry(0xFFDCDC, 1.0f);
					config.obfuscatedColorOutline = new AlphaColorEntry(0xA05050, 1.0f);
					config.proposedColor = new AlphaColorEntry(0x000000, 0.075f);
					config.proposedColorOutline = new AlphaColorEntry(0x000000, 0.15f);
					config.deobfuscatedColor = new AlphaColorEntry(0xDCFFDC, 1.0f);
					config.deobfuscatedColorOutline = new AlphaColorEntry(0x50A050, 1.0f);
					config.editorBackground = 0xFFFFFF;
					config.highlightColor = 0x3333EE;
					config.caretColor = 0x000000;
					config.selectionHighlightColor = 0x000000;
					config.stringColor = 0xCC6600;
					config.numberColor = 0x999933;
					config.operatorColor = 0x000000;
					config.delimiterColor = 0x000000;
					config.typeColor = 0x000000;
					config.identifierColor = 0x000000;
					config.defaultTextColor = 0x000000;
					break;
				case DARCULA:
					//Based off colors found here: https://github.com/dracula/dracula-theme/
					config.lineNumbersForeground = 0xA4A4A3;
					config.lineNumbersBackground = 0x313335;
					config.lineNumbersSelected = 0x606366;
					config.obfuscatedColor = new AlphaColorEntry(0xFF5555, 0.3f);
					config.obfuscatedColorOutline = new AlphaColorEntry(0xFF5555, 0.5f);
					config.deobfuscatedColor = new AlphaColorEntry(0x50FA7B, 0.3f);
					config.deobfuscatedColorOutline = new AlphaColorEntry(0x50FA7B, 0.5f);
					config.proposedColor = new AlphaColorEntry(0x606366, 0.3f);
					config.proposedColorOutline = new AlphaColorEntry(0x606366, 0.5f);
					config.editorBackground = 0x282A36;
					config.highlightColor = 0xFF79C6;
					config.caretColor = 0xF8F8F2;
					config.selectionHighlightColor = 0xF8F8F2;
					config.stringColor = 0xF1FA8C;
					config.numberColor = 0xBD93F9;
					config.operatorColor = 0xF8F8F2;
					config.delimiterColor = 0xF8F8F2;
					config.typeColor = 0xF8F8F2;
					config.identifierColor = 0xF8F8F2;
					config.defaultTextColor = 0xF8F8F2;
					break;
			}
		}
	}

	private static final File DIR_HOME = new File(System.getProperty("user.home"));
	private static final File ENIGMA_DIR = new File(DIR_HOME, ".enigma");
	private static final File CONFIG_FILE = new File(ENIGMA_DIR, "config.json");
	private static final Config INSTANCE = new Config();

	private final transient Gson gson; // transient to exclude it from being exposed

	public AlphaColorEntry obfuscatedColor;
	public AlphaColorEntry obfuscatedColorOutline;
	public AlphaColorEntry proposedColor;
	public AlphaColorEntry proposedColorOutline;
	public AlphaColorEntry deobfuscatedColor;
	public AlphaColorEntry deobfuscatedColorOutline;

	public Integer editorBackground;
	public Integer highlightColor;
	public Integer caretColor;
	public Integer selectionHighlightColor;

	public Integer stringColor;
	public Integer numberColor;
	public Integer operatorColor;
	public Integer delimiterColor;
	public Integer typeColor;
	public Integer identifierColor;
	public Integer defaultTextColor;

	public Integer lineNumbersBackground;
	public Integer lineNumbersSelected;
	public Integer lineNumbersForeground;

	public LookAndFeel lookAndFeel = LookAndFeel.DEFAULT;

	private Config() {
		gson = new GsonBuilder()
			.registerTypeAdapter(Integer.class, new IntSerializer())
			.registerTypeAdapter(Integer.class, new IntDeserializer())
			.registerTypeAdapter(Config.class, (InstanceCreator<Config>) type -> this)
			.setPrettyPrinting()
			.create();
		try {
			this.loadConfig();
		} catch (IOException ignored) {
			try {
				this.reset();
			} catch (IOException ignored1) {
			}
		}
	}

	public void loadConfig() throws IOException {
		if (!ENIGMA_DIR.exists()) ENIGMA_DIR.mkdirs();
		File configFile = new File(ENIGMA_DIR, "config.json");
		boolean loaded = false;

		if (configFile.exists()) {
			try {
				gson.fromJson(Files.asCharSource(configFile, Charset.defaultCharset()).read(), Config.class);
				loaded = true;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		if (!loaded) {
			this.reset();
			Files.touch(configFile);
		}
		saveConfig();
	}

	public void saveConfig() throws IOException {
		Files.asCharSink(CONFIG_FILE, Charset.defaultCharset()).write(gson.toJson(this));
	}

	public void reset() throws IOException {
		this.lookAndFeel = LookAndFeel.DEFAULT;
		this.lookAndFeel.apply(this);
		this.saveConfig();
	}

	private static class IntSerializer implements JsonSerializer<Integer> {
		@Override
		public JsonElement serialize(Integer src, Type typeOfSrc, JsonSerializationContext context) {
			return new JsonPrimitive("#" + Integer.toHexString(src).toUpperCase());
		}
	}

	private static class IntDeserializer implements JsonDeserializer<Integer> {
		@Override
		public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
			return (int) Long.parseLong(json.getAsString().replace("#", ""), 16);
		}
	}

	public static Config getInstance() {
		return INSTANCE;
	}
}