Files
MCCustomSkinLoader/Common/source/customskinloader/config/Config.java
T

195 lines
6.5 KiB
Java
Raw Normal View History

2016-02-27 15:22:35 +08:00
package customskinloader.config;
2016-08-21 11:26:00 +08:00
import java.io.File;
import java.util.ArrayList;
2016-11-27 13:32:25 +08:00
import java.util.Arrays;
import java.util.List;
2016-08-21 11:26:00 +08:00
import org.apache.commons.io.Charsets;
2016-11-27 13:32:25 +08:00
import org.apache.commons.io.FileUtils;
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;
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;
2016-04-03 13:01:06 +08:00
2016-02-27 15:22:35 +08:00
public class Config {
2016-10-18 21:48:07 +08:00
//Program
2016-04-03 13:01:06 +08:00
public String version;
2016-11-27 13:32:25 +08:00
public List<SkinSiteProfile> loadlist;
2016-10-18 21:48:07 +08:00
//Function
2016-09-10 21:00:53 +08:00
public boolean enableDynamicSkull=true;
2016-08-28 19:08:59 +08:00
public boolean enableTransparentSkin=true;
2016-10-18 21:48:07 +08:00
public boolean ignoreHttpsCertificate=false;
2017-07-12 20:00:06 +08:00
public boolean forceLoadAllTextures=false;
2016-10-18 21:48:07 +08:00
//Cache
2016-07-31 22:00:46 +08:00
public int cacheExpiry=10;
public boolean enableUpdateSkull=false;
public boolean enableLocalProfileCache=false;
2016-10-18 21:48:07 +08:00
public boolean enableCacheAutoClean=false;
2016-02-27 15:22:35 +08:00
2016-04-03 13:01:06 +08:00
//Init config
2016-02-27 15:22:35 +08:00
public Config(SkinSiteProfile[] loadlist){
2016-04-03 13:01:06 +08:00
this.version=CustomSkinLoader.CustomSkinLoader_VERSION;
2016-11-27 13:32:25 +08:00
this.loadlist=Arrays.asList(loadlist);
2016-02-27 15:22:35 +08:00
}
2016-08-21 11:26:00 +08:00
public static Config loadConfig0() {
Config config=loadConfig();
2016-10-18 21:48:07 +08:00
2016-11-27 13:32:25 +08:00
//LoadList null checker
if(config.loadlist==null){
config.loadlist=new ArrayList<SkinSiteProfile>();
}else{
for(int i=0;i<config.loadlist.size();i++){
if(config.loadlist.get(i)==null)
config.loadlist.remove(i--);
}
}
2016-10-18 21:48:07 +08:00
//Init program
2016-08-21 11:26:00 +08:00
config.loadExtraList();
2016-09-24 20:47:08 +08:00
config.initLocalFolder();
2016-10-18 21:48:07 +08:00
if(config.ignoreHttpsCertificate)
HttpUtil0.ignoreHttpsCertificate();
2016-11-27 13:32:25 +08:00
if(config.enableCacheAutoClean && !config.enableLocalProfileCache){
2017-07-09 21:40:04 +08:00
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.toString());
}
2016-11-27 13:32:25 +08:00
}
2016-10-18 21:48:07 +08:00
//Output config
2016-12-29 16:44:16 +08:00
CustomSkinLoader.logger.info("EnableDynamicSkull:"+config.enableDynamicSkull+
2016-08-21 11:26:00 +08:00
", EnableTranSkin:"+config.enableTransparentSkin+
2016-10-18 21:48:07 +08:00
", IgnoreHttpsCertificate:"+config.ignoreHttpsCertificate+
2016-08-21 11:26:00 +08:00
", CacheExpiry:"+config.cacheExpiry+
2016-10-18 21:48:07 +08:00
", EnableUpdateSkull:"+config.enableUpdateSkull+
", EnableLocalProfileCache:"+config.enableLocalProfileCache+
", EnableCacheAutoClean:"+config.enableCacheAutoClean+
2016-11-27 13:32:25 +08:00
", LoadList:"+(config.loadlist==null?0:config.loadlist.size()));
2016-10-18 21:48:07 +08:00
//Check config version
2016-09-10 21:00:53 +08:00
float floatVersion=0f;
2016-09-24 20:47:08 +08:00
float configVersion=0f;
2016-09-10 21:00:53 +08:00
try{
floatVersion=Float.parseFloat(CustomSkinLoader.CustomSkinLoader_VERSION);
2016-09-24 20:47:08 +08:00
configVersion=Float.parseFloat(config.version);
2017-07-12 20:00:06 +08:00
if(configVersion==15.1f)//To avoid some bug
configVersion=14.6f;
2016-09-10 21:00:53 +08:00
}catch (Exception e){
CustomSkinLoader.logger.warning("Exception occurs while parsing version: "+e.toString());
}
2016-09-25 10:31:59 +08:00
if(config.version==null || configVersion==0f || floatVersion > configVersion){
2016-08-21 11:26:00 +08:00
CustomSkinLoader.logger.info("Config File is out of date: "+config.version);
config.version=CustomSkinLoader.CustomSkinLoader_VERSION;
writeConfig(config,true);
}
2016-10-18 21:48:07 +08:00
2016-08-21 11:26:00 +08:00
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.");
2016-11-27 13:32:25 +08:00
String json=FileUtils.readFileToString(CustomSkinLoader.CONFIG_FILE,Charsets.UTF_8);
2016-08-21 11:26:00 +08:00
Config config=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.toString()+")");
File brokenFile=new File(CustomSkinLoader.DATA_DIR,"BROKEN-CustomSkinLoader.json");
if(brokenFile.exists())
brokenFile.delete();
CustomSkinLoader.CONFIG_FILE.renameTo(brokenFile);
return initConfig();
}
}
private void loadExtraList(){
File listAddition=new File(CustomSkinLoader.DATA_DIR,"ExtraList");
if(!listAddition.isDirectory()){
listAddition.mkdirs();
return;
}
2016-11-27 13:32:25 +08:00
List<SkinSiteProfile> adds=new ArrayList<SkinSiteProfile>();
2016-08-21 11:26:00 +08:00
File[] files=listAddition.listFiles();
for(File file:files){
2016-08-28 19:08:59 +08:00
if(!file.getName().toLowerCase().endsWith(".json")&&!file.getName().toLowerCase().endsWith(".txt"))
2016-08-21 11:26:00 +08:00
continue;
try {
CustomSkinLoader.logger.info("Try to load Extra List.("+file.getName()+")");
2016-11-27 13:32:25 +08:00
String json=FileUtils.readFileToString(file,Charsets.UTF_8);
2016-08-21 11:26:00 +08:00
SkinSiteProfile ssp=CustomSkinLoader.GSON.fromJson(json, SkinSiteProfile.class);
CustomSkinLoader.logger.info("Successfully load Extra List.");
file.delete();
ProfileLoader.IProfileLoader loader=ProfileLoader.LOADERS.get(ssp.type.toLowerCase());
if(loader==null){
CustomSkinLoader.logger.info("Extra List will be ignore: 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 ignore: Duplicate.("+ssp.name+")");
}
}catch (Exception e) {
CustomSkinLoader.logger.info("Failed to load Extra List.("+e.toString()+")");
}
}
if(adds.size()!=0){
2016-11-27 13:32:25 +08:00
adds.addAll(this.loadlist);
this.loadlist=adds;
2016-08-21 11:26:00 +08:00
writeConfig(this,true);
}
}
2016-09-24 20:47:08 +08:00
private void initLocalFolder(){
for(SkinSiteProfile ssp:this.loadlist){
ProfileLoader.IProfileLoader loader=ProfileLoader.LOADERS.get(ssp.type.toLowerCase());
if(loader==null)
continue;
loader.initLocalFolder(ssp);
}
}
2016-08-21 11:26:00 +08:00
private static Config initConfig() {
Config config=new Config(CustomSkinLoader.DEFAULT_LOAD_LIST);
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();
2016-11-27 13:32:25 +08:00
FileUtils.write(CustomSkinLoader.CONFIG_FILE, json, Charsets.UTF_8);
2016-08-21 11:26:00 +08:00
CustomSkinLoader.logger.info("Successfully "+(update?"update":"create")+" config.");
} catch (Exception e) {
CustomSkinLoader.logger.info("Failed to "+(update?"update":"create")+" config.("+e.toString()+")");
}
}
2016-02-27 15:22:35 +08:00
}