2016-02-27 15:22:35 +08:00
|
|
|
package customskinloader.config;
|
|
|
|
|
|
2016-08-21 11:26:00 +08:00
|
|
|
import java.io.File;
|
2018-09-02 15:57:08 +08:00
|
|
|
import java.lang.reflect.Field;
|
2023-01-11 18:51:44 +08:00
|
|
|
import java.util.*;
|
2016-08-21 11:26:00 +08:00
|
|
|
|
2016-04-03 13:01:06 +08:00
|
|
|
import customskinloader.CustomSkinLoader;
|
2016-08-21 11:26:00 +08:00
|
|
|
import customskinloader.loader.ProfileLoader;
|
2021-05-25 12:55:42 +08:00
|
|
|
import customskinloader.plugin.ICustomSkinLoaderPlugin;
|
2021-04-24 10:33:18 +08:00
|
|
|
import customskinloader.plugin.PluginLoader;
|
2016-11-06 12:00:36 +08:00
|
|
|
import customskinloader.utils.HttpRequestUtil;
|
2016-11-27 13:32:25 +08:00
|
|
|
import customskinloader.utils.HttpTextureUtil;
|
2016-10-18 21:48:07 +08:00
|
|
|
import customskinloader.utils.HttpUtil0;
|
2021-04-24 10:33:18 +08:00
|
|
|
import customskinloader.utils.Version;
|
|
|
|
|
import org.apache.commons.io.FileUtils;
|
2016-04-03 13:01:06 +08:00
|
|
|
|
2018-06-25 00:34:32 +08:00
|
|
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
2016-02-27 15:22:35 +08:00
|
|
|
public class Config {
|
2018-02-12 12:20:28 +08:00
|
|
|
//Program
|
|
|
|
|
public String version;
|
2021-04-24 10:33:18 +08:00
|
|
|
public int buildNumber;
|
2018-02-12 12:20:28 +08:00
|
|
|
public List<SkinSiteProfile> loadlist;
|
2018-09-02 15:57:08 +08:00
|
|
|
|
2018-02-12 12:20:28 +08:00
|
|
|
//Function
|
2022-11-24 21:42:45 +08:00
|
|
|
public boolean enableDynamicSkull = true;
|
|
|
|
|
public boolean enableTransparentSkin = true;
|
2020-08-16 21:07:22 +08:00
|
|
|
public boolean forceIgnoreHttpsCertificate = false;
|
2023-01-11 18:51:44 +08:00
|
|
|
public boolean forceLoadAllTextures = true;
|
2018-09-02 15:57:08 +08:00
|
|
|
public boolean enableCape = true;
|
2023-01-12 00:09:57 +08:00
|
|
|
public int threadPoolSize = 8;
|
2022-11-24 21:42:45 +08:00
|
|
|
/**
|
|
|
|
|
* 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;
|
2018-09-02 15:57:08 +08:00
|
|
|
|
2020-08-16 22:18:25 +08:00
|
|
|
//Profile Cache
|
2022-11-24 21:42:45 +08:00
|
|
|
public int cacheExpiry = 30;
|
2020-08-16 21:07:22 +08:00
|
|
|
public boolean forceUpdateSkull = false;
|
2022-11-24 21:42:45 +08:00
|
|
|
public boolean enableLocalProfileCache = false;
|
2020-08-16 22:18:25 +08:00
|
|
|
|
|
|
|
|
//Network Cache
|
2022-11-24 21:42:45 +08:00
|
|
|
public boolean enableCacheAutoClean = false;
|
2020-08-16 21:07:22 +08:00
|
|
|
public boolean forceDisableCache = false;
|
2018-09-02 15:57:08 +08:00
|
|
|
|
2021-05-25 12:55:42 +08:00
|
|
|
// Used by Gson to create an instance with default value.
|
|
|
|
|
public Config() {
|
|
|
|
|
this(new ArrayList<>());
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 12:20:28 +08:00
|
|
|
//Init config
|
2022-11-24 21:42:45 +08:00
|
|
|
public Config(List<SkinSiteProfile> loadlist) {
|
|
|
|
|
this.version = CustomSkinLoader.CustomSkinLoader_VERSION;
|
|
|
|
|
this.buildNumber = CustomSkinLoader.CustomSkinLoader_BUILD_NUMBER;
|
|
|
|
|
this.loadlist = loadlist;
|
2018-02-12 12:20:28 +08:00
|
|
|
}
|
2018-09-02 15:57:08 +08:00
|
|
|
|
2018-02-12 12:20:28 +08:00
|
|
|
public static Config loadConfig0() {
|
2022-11-24 21:42:45 +08:00
|
|
|
Config config = loadConfig();
|
2018-09-02 15:57:08 +08:00
|
|
|
|
2018-02-12 12:20:28 +08:00
|
|
|
//LoadList null checker
|
2022-11-24 21:42:45 +08:00
|
|
|
if (config.loadlist == null) {
|
|
|
|
|
config.loadlist = new ArrayList<>();
|
|
|
|
|
} else {
|
|
|
|
|
for (int i = 0; i < config.loadlist.size(); i++) {
|
|
|
|
|
if (config.loadlist.get(i) == null)
|
2018-02-12 12:20:28 +08:00
|
|
|
config.loadlist.remove(i--);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-02 15:57:08 +08:00
|
|
|
|
2018-02-12 12:20:28 +08:00
|
|
|
//Init program
|
|
|
|
|
config.loadExtraList();
|
2021-11-28 02:17:38 +08:00
|
|
|
config.updateLoadlist();
|
2018-02-12 12:20:28 +08:00
|
|
|
config.initLocalFolder();
|
2020-06-29 00:57:42 +08:00
|
|
|
config.threadPoolSize = Math.max(config.threadPoolSize, 1);
|
2022-11-24 21:42:45 +08:00
|
|
|
if (config.forceIgnoreHttpsCertificate)
|
2018-02-12 12:20:28 +08:00
|
|
|
HttpUtil0.ignoreHttpsCertificate();
|
2022-11-24 21:42:45 +08:00
|
|
|
if (config.enableCacheAutoClean && !config.enableLocalProfileCache) {
|
|
|
|
|
try {
|
2018-02-12 12:20:28 +08:00
|
|
|
FileUtils.deleteDirectory(HttpRequestUtil.CACHE_DIR);
|
|
|
|
|
FileUtils.deleteDirectory(HttpTextureUtil.getCacheDir());
|
|
|
|
|
CustomSkinLoader.logger.info("Successfully cleaned cache.");
|
2022-11-24 21:42:45 +08:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
CustomSkinLoader.logger.warning("Exception occurs while cleaning cache: " + e);
|
2018-02-12 12:20:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-09-02 15:57:08 +08:00
|
|
|
|
2018-02-12 12:20:28 +08:00
|
|
|
//Check config version
|
2018-09-02 15:57:08 +08:00
|
|
|
Version configVersion = Version.of(config.version);
|
2021-04-24 10:33:18 +08:00
|
|
|
if (CustomSkinLoader.CustomSkinLoader_BUILD_NUMBER == 0 // Custom builds
|
2022-11-24 21:42:45 +08:00
|
|
|
|| 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);
|
2018-09-02 15:57:08 +08:00
|
|
|
config.version = CustomSkinLoader.CustomSkinLoader_VERSION;
|
2021-04-24 10:33:18 +08:00
|
|
|
config.buildNumber = CustomSkinLoader.CustomSkinLoader_BUILD_NUMBER;
|
2018-02-12 12:20:28 +08:00
|
|
|
}
|
2021-05-25 12:55:42 +08:00
|
|
|
writeConfig(config, true);
|
2018-09-02 15:57:08 +08:00
|
|
|
|
|
|
|
|
//Output config
|
|
|
|
|
for (Field field : config.getClass().getDeclaredFields()) {
|
|
|
|
|
try {
|
|
|
|
|
Object value = field.get(config);
|
2021-04-24 10:33:18 +08:00
|
|
|
CustomSkinLoader.logger.info(field.getName() + " : " + value);
|
2022-11-24 21:42:45 +08:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
|
}
|
2018-02-12 12:20:28 +08:00
|
|
|
}
|
2018-09-02 15:57:08 +08:00
|
|
|
|
2018-02-12 12:20:28 +08:00
|
|
|
return config;
|
|
|
|
|
}
|
2016-08-21 11:26:00 +08:00
|
|
|
|
2018-02-12 12:20:28 +08:00
|
|
|
private static Config loadConfig() {
|
2022-11-24 21:42:45 +08:00
|
|
|
CustomSkinLoader.logger.info("Config File: " + CustomSkinLoader.CONFIG_FILE.getAbsolutePath());
|
|
|
|
|
if (!CustomSkinLoader.CONFIG_FILE.exists()) {
|
2018-02-12 12:20:28 +08:00
|
|
|
CustomSkinLoader.logger.info("Config file not found, use default instead.");
|
|
|
|
|
return initConfig();
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
CustomSkinLoader.logger.info("Try to load config.");
|
2022-11-24 21:42:45 +08:00
|
|
|
String json = FileUtils.readFileToString(CustomSkinLoader.CONFIG_FILE, "UTF-8");
|
|
|
|
|
Config config = Objects.requireNonNull(CustomSkinLoader.GSON.fromJson(json, Config.class));
|
2018-02-12 12:20:28 +08:00
|
|
|
CustomSkinLoader.logger.info("Successfully load config.");
|
|
|
|
|
return config;
|
2022-11-24 21:42:45 +08:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
CustomSkinLoader.logger.info("Failed to load config, use default instead.(" + e + ")");
|
2021-11-28 02:17:38 +08:00
|
|
|
createBrokenFile(CustomSkinLoader.CONFIG_FILE);
|
2018-02-12 12:20:28 +08:00
|
|
|
return initConfig();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-02 15:57:08 +08:00
|
|
|
|
2022-11-24 21:42:45 +08:00
|
|
|
private void loadExtraList() {
|
|
|
|
|
File listAddition = new File(CustomSkinLoader.DATA_DIR, "ExtraList");
|
|
|
|
|
if (!listAddition.isDirectory()) {
|
2018-02-12 12:20:28 +08:00
|
|
|
listAddition.mkdirs();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-11-24 21:42:45 +08:00
|
|
|
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"))
|
2018-02-12 12:20:28 +08:00
|
|
|
continue;
|
|
|
|
|
try {
|
2022-11-24 21:42:45 +08:00
|
|
|
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);
|
2018-02-12 12:20:28 +08:00
|
|
|
CustomSkinLoader.logger.info("Successfully load Extra List.");
|
2021-11-28 02:17:38 +08:00
|
|
|
if (ssp.type != null) {
|
|
|
|
|
ProfileLoader.IProfileLoader loader = ProfileLoader.LOADERS.get(ssp.type.toLowerCase());
|
|
|
|
|
if (loader == null) {
|
2022-11-24 21:42:45 +08:00
|
|
|
CustomSkinLoader.logger.info("Extra List will be ignored: Type '" + ssp.type +
|
|
|
|
|
"' is not defined.");
|
2018-02-12 12:20:28 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
2021-11-28 02:17:38 +08:00
|
|
|
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 {
|
2022-11-24 21:42:45 +08:00
|
|
|
CustomSkinLoader.logger.info("Extra List is invalid: Type is not defined.(" +
|
|
|
|
|
file.getName() + ")");
|
2021-11-28 02:17:38 +08:00
|
|
|
createBrokenFile(file);
|
2018-02-12 12:20:28 +08:00
|
|
|
}
|
2022-11-24 21:42:45 +08:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
CustomSkinLoader.logger.info("Failed to load Extra List.(" + e + ")");
|
2021-11-28 02:17:38 +08:00
|
|
|
createBrokenFile(file);
|
2018-02-12 12:20:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
2022-11-24 21:42:45 +08:00
|
|
|
if (adds.size() != 0) {
|
2018-02-12 12:20:28 +08:00
|
|
|
adds.addAll(this.loadlist);
|
2022-11-24 21:42:45 +08:00
|
|
|
this.loadlist = adds;
|
2018-02-12 12:20:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-09-02 15:57:08 +08:00
|
|
|
|
2021-11-28 02:17:38 +08:00
|
|
|
private void updateLoadlist() {
|
|
|
|
|
PluginLoader.PLUGINS.stream()
|
2022-11-24 21:42:45 +08:00
|
|
|
.map(ICustomSkinLoaderPlugin::getDefaultProfiles)
|
|
|
|
|
.filter(Objects::nonNull)
|
|
|
|
|
.flatMap(Collection::stream)
|
|
|
|
|
.forEach(profile -> this.loadlist.stream()
|
|
|
|
|
.filter(ssp -> profile.getName().equals(ssp.name))
|
|
|
|
|
.forEach(profile::updateSkinSiteProfile));
|
2021-11-28 02:17:38 +08:00
|
|
|
}
|
|
|
|
|
|
2022-11-24 21:42:45 +08:00
|
|
|
private void initLocalFolder() {
|
|
|
|
|
for (SkinSiteProfile ssp : this.loadlist) {
|
|
|
|
|
if (ssp.type == null)
|
2021-11-28 02:17:38 +08:00
|
|
|
continue;
|
2022-11-24 21:42:45 +08:00
|
|
|
ProfileLoader.IProfileLoader loader = ProfileLoader.LOADERS.get(ssp.type.toLowerCase());
|
|
|
|
|
if (loader == null)
|
2018-02-12 12:20:28 +08:00
|
|
|
continue;
|
2019-09-18 22:15:58 +08:00
|
|
|
loader.init(ssp);
|
2018-02-12 12:20:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
2016-08-21 11:26:00 +08:00
|
|
|
|
2021-11-28 02:17:38 +08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-24 10:33:18 +08:00
|
|
|
// The config file does not exist or was broken.
|
2018-02-12 12:20:28 +08:00
|
|
|
private static Config initConfig() {
|
2021-05-25 12:55:42 +08:00
|
|
|
List<ICustomSkinLoaderPlugin.IDefaultProfile> profiles = new ArrayList<>();
|
|
|
|
|
for (ICustomSkinLoaderPlugin plugin : PluginLoader.PLUGINS) {
|
|
|
|
|
List<ICustomSkinLoaderPlugin.IDefaultProfile> defaultProfiles = plugin.getDefaultProfiles();
|
|
|
|
|
if (defaultProfiles != null) {
|
|
|
|
|
profiles.addAll(defaultProfiles);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-11 18:51:44 +08:00
|
|
|
profiles.sort(Comparator.comparingInt(ICustomSkinLoaderPlugin.IDefaultProfile::getPriority));
|
2021-05-25 12:55:42 +08:00
|
|
|
|
|
|
|
|
List<SkinSiteProfile> loadlist = new ArrayList<>();
|
|
|
|
|
for (ICustomSkinLoaderPlugin.IDefaultProfile profile : profiles) {
|
2021-04-24 10:33:18 +08:00
|
|
|
SkinSiteProfile ssp = new SkinSiteProfile();
|
2021-05-25 12:55:42 +08:00
|
|
|
ssp.name = profile.getName();
|
|
|
|
|
profile.updateSkinSiteProfile(ssp);
|
2021-04-24 10:33:18 +08:00
|
|
|
loadlist.add(ssp);
|
2021-05-25 12:55:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Config config = new Config(loadlist);
|
|
|
|
|
writeConfig(config, false);
|
2018-02-12 12:20:28 +08:00
|
|
|
return config;
|
|
|
|
|
}
|
2022-11-24 21:42:45 +08:00
|
|
|
|
|
|
|
|
private static void writeConfig(Config config, boolean update) {
|
|
|
|
|
String json = CustomSkinLoader.GSON.toJson(config);
|
|
|
|
|
if (CustomSkinLoader.CONFIG_FILE.exists())
|
2018-02-12 12:20:28 +08:00
|
|
|
CustomSkinLoader.CONFIG_FILE.delete();
|
|
|
|
|
try {
|
|
|
|
|
CustomSkinLoader.CONFIG_FILE.createNewFile();
|
2018-06-25 00:34:32 +08:00
|
|
|
FileUtils.write(CustomSkinLoader.CONFIG_FILE, json, "UTF-8");
|
2022-11-24 21:42:45 +08:00
|
|
|
CustomSkinLoader.logger.info("Successfully " + (update ? "update" : "create") + " config.");
|
2018-02-12 12:20:28 +08:00
|
|
|
} catch (Exception e) {
|
2022-11-24 21:42:45 +08:00
|
|
|
CustomSkinLoader.logger.info("Failed to " + (update ? "update" : "create") + " config.(" + e + ")");
|
2018-02-12 12:20:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
2016-02-27 15:22:35 +08:00
|
|
|
}
|