EnabledModsList.java
6.51 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package com.mumfrey.liteloader.core;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/**
* Serialisable (via GSON) object which stores list of enabled/disabled mods for
* each profile.
*
* @author Adam Mummery-Smith
*/
public final class EnabledModsList
{
@SuppressWarnings("unused")
private static final transient long serialVersionUID = -6449451105617763769L;
/**
* Gson object for serialisation/deserialisation
*/
private static transient Gson gson = new GsonBuilder().setPrettyPrinting().create();
/**
* This is the node which gets serialised
*/
private TreeMap<String, TreeMap<String, Boolean>> mods;
/**
* By default, when we discover a mod which is NOT in the list for the
* current profile, we will ENABLE the mod and add it to the list. However,
* when we receive a list of mods on the command line, we instead want to
* <b>disable</b> any additional unlisted mods, we also don't want to save
* the mods list because the command line is supposed to be an override
* rather than a new mask. These two values provide this behaviour.
*/
private transient Boolean defaultEnabledValue = Boolean.TRUE;
private transient boolean allowSave = true;
/**
* JSON file containing the list of enabled/disabled mods by profile
*/
private transient File enabledModsFile = null;
private EnabledModsList()
{
// Private because we are always instanced by the static createFrom() method below
}
/**
* Check whether a particular mod is enabled
*
* @param profileName
* @param identifier
*/
public boolean isEnabled(String profileName, String identifier)
{
Map<String, Boolean> profile = this.getProfile(profileName);
identifier = identifier.toLowerCase().trim();
if (!profile.containsKey(identifier))
{
profile.put(identifier, this.defaultEnabledValue);
}
return profile.get(identifier);
}
/**
* Set the enablement state of a mod in the specified profile
*
* @param profileName
* @param identifier
* @param enabled
*/
public void setEnabled(String profileName, String identifier, boolean enabled)
{
Map<String, Boolean> profile = this.getProfile(profileName);
profile.put(identifier.toLowerCase().trim(), Boolean.valueOf(enabled));
this.allowSave = true;
}
/**
* Reads the mods list passed in on the command line
*
* @param profileName
* @param modNameFilter
*/
public void processModsList(String profileName, List<String> modNameFilter)
{
Map<String, Boolean> profile = this.getProfile(profileName);
try
{
if (modNameFilter != null)
{
for (String modName : profile.keySet())
{
profile.put(modName, Boolean.FALSE);
}
this.defaultEnabledValue = Boolean.FALSE;
this.allowSave = false;
for (String filterEntry : modNameFilter)
{
profile.put(filterEntry.toLowerCase().trim(), Boolean.TRUE);
}
}
}
catch (Exception ex)
{
this.defaultEnabledValue = Boolean.TRUE;
this.allowSave = true;
}
}
/**
* Internal method which returns the map for the specified profile
*
* @param profileName
*/
private Map<String, Boolean> getProfile(String profileName)
{
if (profileName == null) profileName = "default";
if (this.mods == null) this.mods = new TreeMap<String, TreeMap<String,Boolean>>();
if (!this.mods.containsKey(profileName))
{
this.mods.put(profileName, new TreeMap<String, Boolean>());
}
return this.mods.get(profileName);
}
/**
* Factory method which tries to deserialise the enablement list from the
* file or if failing creates and returns a new instance.
*
* @param file JSON file to create the EnabledModsList from
* @return a new EnabledModsList instance
*/
public static EnabledModsList createFrom(File file)
{
if (file.exists())
{
FileReader reader = null;
try
{
reader = new FileReader(file);
EnabledModsList instance = gson.fromJson(reader, EnabledModsList.class);
instance.setEnabledModsFile(file);
return instance;
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if (reader != null)
{
reader.close();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
EnabledModsList instance = new EnabledModsList();
instance.setEnabledModsFile(file);
return instance;
}
/**
* Save the enablement list to the specified file
*
* @param file
*/
public void saveTo(File file)
{
if (!this.allowSave) return;
FileWriter writer = null;
try
{
writer = new FileWriter(file);
gson.toJson(this, writer);
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if (writer != null)
{
writer.close();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
/**
* Save to the file we were loaded from
*/
public void save()
{
if (this.enabledModsFile != null)
{
this.saveTo(this.enabledModsFile);
}
}
/**
* Get whether saving this list is allowed
*/
public boolean saveAllowed()
{
return this.allowSave;
}
public File getEnabledModsFile()
{
return this.enabledModsFile;
}
public void setEnabledModsFile(File enabledModsFile)
{
this.enabledModsFile = enabledModsFile;
}
}