-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathConfig.java
More file actions
257 lines (234 loc) · 10.3 KB
/
Copy pathConfig.java
File metadata and controls
257 lines (234 loc) · 10.3 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
253
254
255
256
257
package customskinloader.config;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import customskinloader.CustomSkinLoader;
import customskinloader.loader.ProfileLoader;
import customskinloader.plugin.ICustomSkinLoaderPlugin;
import customskinloader.plugin.PluginLoader;
import customskinloader.utils.HttpRequestUtil;
import customskinloader.utils.HttpTextureUtil;
import customskinloader.utils.Version;
import org.apache.commons.io.FileUtils;
@SuppressWarnings("ResultOfMethodCallIgnored")
public class Config {
//Program
public String version;
public int buildNumber;
public List<SkinSiteProfile> loadlist;
//Function
public boolean enableDynamicSkull = true;
public boolean enableTransparentSkin = true;
public boolean forceLoadAllTextures = true;
public boolean enableCape = true;
/**
* Can logger write message to standard output(System.out).
* Because standard output won't write to latest.log after Forge 1.17,
* printing to standard output is meaningless,
* which makes this switch default value is <code>false</code>.
* @since 14.15
*/
public boolean enableLogStdOut = false;
//Profile Cache
public int cacheExpiry = 30;
public boolean forceUpdateSkull = false;
public boolean enableLocalProfileCache = false;
//Network Cache
public boolean enableCacheAutoClean = false;
public boolean forceDisableCache = false;
// Used by Gson to create an instance with default value.
public Config() {
this(new ArrayList<>());
}
//Init config
public Config(List<SkinSiteProfile> loadlist) {
this.version = CustomSkinLoader.CustomSkinLoader_VERSION;
this.buildNumber = CustomSkinLoader.CustomSkinLoader_BUILD_NUMBER;
this.loadlist = loadlist;
}
public static Config loadConfig0() {
Config config = loadConfig();
//LoadList null checker
if (config.loadlist == null) {
config.loadlist = new ArrayList<>();
} else {
for (int i = 0; i < config.loadlist.size(); i++) {
if (config.loadlist.get(i) == null)
config.loadlist.remove(i--);
}
}
//Init program
config.loadExtraList();
config.updateLoadlist();
config.initLocalFolder();
if (config.enableCacheAutoClean && !config.enableLocalProfileCache) {
try {
FileUtils.deleteDirectory(HttpRequestUtil.CACHE_DIR);
FileUtils.deleteDirectory(HttpTextureUtil.getCacheDir());
CustomSkinLoader.logger.info("Successfully cleaned cache.");
} catch (Exception e) {
CustomSkinLoader.logger.warning("Exception occurs while cleaning cache: " + e);
}
}
//Check config version
Version configVersion = Version.of(config.version);
if (CustomSkinLoader.CustomSkinLoader_BUILD_NUMBER == 0 // Custom builds
|| configVersion.compareTo(CustomSkinLoader.CustomSkinLoader_VERSION) < 0
|| config.buildNumber < CustomSkinLoader.CustomSkinLoader_BUILD_NUMBER) {
CustomSkinLoader.logger.info("Config File is out of date: " + config.version +
", build number: " + config.buildNumber);
config.version = CustomSkinLoader.CustomSkinLoader_VERSION;
config.buildNumber = CustomSkinLoader.CustomSkinLoader_BUILD_NUMBER;
}
writeConfig(config, true);
//Output config
for (Field field : config.getClass().getDeclaredFields()) {
try {
Object value = field.get(config);
CustomSkinLoader.logger.info(field.getName() + " : " + value);
} catch (Exception ignored) {
}
}
return config;
}
private static Config loadConfig() {
CustomSkinLoader.logger.info("Config File: " + CustomSkinLoader.CONFIG_FILE.getAbsolutePath());
if (!CustomSkinLoader.CONFIG_FILE.exists()) {
CustomSkinLoader.logger.info("Config file not found, use default instead.");
return initConfig();
}
try {
CustomSkinLoader.logger.info("Try to load config.");
String json = FileUtils.readFileToString(CustomSkinLoader.CONFIG_FILE, "UTF-8");
Config config = Objects.requireNonNull(CustomSkinLoader.GSON.fromJson(json, Config.class));
CustomSkinLoader.logger.info("Successfully load config.");
return config;
} catch (Exception e) {
CustomSkinLoader.logger.info("Failed to load config, use default instead.(" + e + ")");
createBrokenFile(CustomSkinLoader.CONFIG_FILE);
return initConfig();
}
}
private void loadExtraList() {
File listAddition = new File(CustomSkinLoader.DATA_DIR, "ExtraList");
if (!listAddition.isDirectory()) {
listAddition.mkdirs();
return;
}
List<SkinSiteProfile> adds = new ArrayList<>();
File[] files = listAddition.listFiles();
for (File file : files != null ? files : new File[0]) {
if (!file.getName().toLowerCase().endsWith(".json") && !file.getName().toLowerCase().endsWith(".txt"))
continue;
try {
CustomSkinLoader.logger.info("Try to load Extra List.(" + file.getName() + ")");
String json = FileUtils.readFileToString(file, "UTF-8");
SkinSiteProfile ssp = CustomSkinLoader.GSON.fromJson(json, SkinSiteProfile.class);
CustomSkinLoader.logger.info("Successfully load Extra List.");
if (ssp.type != null) {
ProfileLoader.IProfileLoader loader = ProfileLoader.LOADERS.get(ssp.type.toLowerCase());
if (loader == null) {
CustomSkinLoader.logger.info("Extra List will be ignored: Type '" + ssp.type +
"' is not defined.");
continue;
}
boolean duplicate = false;
for (SkinSiteProfile ssp0 : this.loadlist) {
if (!ssp0.type.equalsIgnoreCase(ssp.type))
continue;
if (loader.compare(ssp0, ssp)) {
duplicate = true;
break;
}
}
if (!duplicate) {
adds.add(ssp);
CustomSkinLoader.logger.info("Successfully apply Extra List.(" + ssp.name + ")");
} else {
CustomSkinLoader.logger.info("Extra List will be ignored: Duplicate.(" + ssp.name + ")");
}
file.delete();
} else {
CustomSkinLoader.logger.info("Extra List is invalid: Type is not defined.(" +
file.getName() + ")");
createBrokenFile(file);
}
} catch (Exception e) {
CustomSkinLoader.logger.info("Failed to load Extra List.(" + e + ")");
createBrokenFile(file);
}
}
if (adds.size() != 0) {
adds.addAll(this.loadlist);
this.loadlist = adds;
}
}
private void updateLoadlist() {
PluginLoader.PLUGINS.stream()
.map(ICustomSkinLoaderPlugin::getDefaultProfiles)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.forEach(profile -> this.loadlist.stream()
.filter(ssp -> profile.getName().equals(ssp.name))
.forEach(profile::updateSkinSiteProfile));
}
private void initLocalFolder() {
for (SkinSiteProfile ssp : this.loadlist) {
if (ssp.type == null)
continue;
ProfileLoader.IProfileLoader loader = ProfileLoader.LOADERS.get(ssp.type.toLowerCase());
if (loader == null)
continue;
loader.init(ssp);
}
}
private static void createBrokenFile(File file) {
try {
File brokenFile = new File(file.getParentFile(), file.getName() + ".broken");
if (brokenFile.exists()) {
brokenFile.delete();
}
file.renameTo(brokenFile);
} catch (Exception e) {
CustomSkinLoader.logger.warning("Failed to create broken file. (" + file.getName() + ")");
CustomSkinLoader.logger.warning(e);
}
}
// The config file does not exist or was broken.
private static Config initConfig() {
List<ICustomSkinLoaderPlugin.IDefaultProfile> profiles = new ArrayList<>();
for (ICustomSkinLoaderPlugin plugin : PluginLoader.PLUGINS) {
List<ICustomSkinLoaderPlugin.IDefaultProfile> defaultProfiles = plugin.getDefaultProfiles();
if (defaultProfiles != null) {
profiles.addAll(defaultProfiles);
}
}
profiles.sort(Comparator.comparingInt(ICustomSkinLoaderPlugin.IDefaultProfile::getPriority));
List<SkinSiteProfile> loadlist = new ArrayList<>();
for (ICustomSkinLoaderPlugin.IDefaultProfile profile : profiles) {
SkinSiteProfile ssp = new SkinSiteProfile();
ssp.name = profile.getName();
profile.updateSkinSiteProfile(ssp);
loadlist.add(ssp);
}
Config config = new Config(loadlist);
writeConfig(config, false);
return config;
}
private static void writeConfig(Config config, boolean update) {
String json = CustomSkinLoader.GSON.toJson(config);
if (CustomSkinLoader.CONFIG_FILE.exists())
CustomSkinLoader.CONFIG_FILE.delete();
try {
CustomSkinLoader.CONFIG_FILE.createNewFile();
FileUtils.write(CustomSkinLoader.CONFIG_FILE, json, "UTF-8");
CustomSkinLoader.logger.info("Successfully " + (update ? "update" : "create") + " config.");
} catch (Exception e) {
CustomSkinLoader.logger.info("Failed to " + (update ? "update" : "create") + " config.(" + e + ")");
}
}
}