Files
MCCustomSkinLoader/Common/source/customskinloader/CustomSkinLoader.java
T

177 lines
6.7 KiB
Java
Raw Normal View History

2016-02-27 15:22:35 +08:00
package customskinloader;
import java.io.File;
2016-02-27 21:43:46 +08:00
import java.util.Map;
2016-02-27 15:22:35 +08:00
2016-04-04 13:13:18 +08:00
import com.google.common.collect.Maps;
2016-02-27 15:22:35 +08:00
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
2016-07-31 22:00:46 +08:00
import com.mojang.authlib.GameProfile;
2016-04-03 01:14:33 +08:00
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type;
2016-02-27 15:22:35 +08:00
import customskinloader.config.Config;
import customskinloader.config.SkinSiteProfile;
2016-08-09 17:23:27 +08:00
import customskinloader.loader.*;
2016-09-10 21:00:53 +08:00
import customskinloader.profile.DynamicSkullManager;
2016-07-31 22:00:46 +08:00
import customskinloader.profile.ModelManager0;
import customskinloader.profile.ProfileCache;
import customskinloader.profile.UserProfile;
2016-04-30 22:12:26 +08:00
import customskinloader.utils.MinecraftUtil;
2016-02-27 15:22:35 +08:00
/**
* Custom skin loader mod for Minecraft.
2018-02-11 12:44:41 +08:00
* @author (C) Jeremy Lam [JLChnToZ] 2013 & Alexander Xia [xfl03] 2014-2018
2016-08-20 19:36:44 +08:00
* @version @MOD_FULL_VERSION@
2016-02-27 15:22:35 +08:00
*/
2016-06-06 13:48:59 +08:00
public class CustomSkinLoader {
2016-08-20 19:36:44 +08:00
public static final String CustomSkinLoader_VERSION="@MOD_VERSION@";
public static final String CustomSkinLoader_FULL_VERSION="@MOD_FULL_VERSION@";
2016-09-24 19:08:01 +08:00
2016-12-25 17:05:30 +08:00
public static final File DATA_DIR=new File(MinecraftUtil.getMinecraftDataDir(),"CustomSkinLoader"),
2016-06-06 13:53:45 +08:00
LOG_FILE=new File(DATA_DIR,"CustomSkinLoader.log"),
CONFIG_FILE=new File(DATA_DIR,"CustomSkinLoader.json");
2016-06-06 13:48:59 +08:00
public static final SkinSiteProfile[] DEFAULT_LOAD_LIST={
2016-07-31 22:00:46 +08:00
SkinSiteProfile.createMojangAPI("Mojang"),
2017-08-13 20:48:18 +08:00
SkinSiteProfile.createCustomSkinAPI("LittleSkin","https://mcskin.littleservice.cn/"),
SkinSiteProfile.createCustomSkinAPI("BlessingSkin","http://skin.prinzeugen.net/"),
//OneSkin has been removed temporarily
//SkinSiteProfile.createCustomSkinAPI("OneSkin","http://fleey.cn/skin/skin_user/skin_json.php/"),
2016-06-06 13:48:59 +08:00
//Minecrack could not load skin correctly
2016-09-24 19:08:01 +08:00
//SkinSiteProfile.creatLegacy("Minecrack","http://minecrack.fr.nf/mc/skinsminecrackd/{USERNAME}.png","http://minecrack.fr.nf/mc/cloaksminecrackd/{USERNAME}.png",null),
2016-07-31 22:00:46 +08:00
SkinSiteProfile.createUniSkinAPI("SkinMe","http://www.skinme.cc/uniskin/"),
2016-09-24 19:08:01 +08:00
SkinSiteProfile.createLegacy("LocalSkin","LocalSkin/skins/{USERNAME}.png","LocalSkin/capes/{USERNAME}.png","LocalSkin/elytras/{USERNAME}.png")};
2016-06-06 13:48:59 +08:00
2016-07-31 22:00:46 +08:00
public static final Gson GSON=new GsonBuilder().setPrettyPrinting().create();
2016-06-06 13:48:59 +08:00
public static final Logger logger=initLogger();
2016-08-21 11:26:00 +08:00
public static final Config config=Config.loadConfig0();
2016-06-06 13:48:59 +08:00
2016-07-31 22:00:46 +08:00
private static final ProfileCache profileCache=new ProfileCache();
2016-09-10 21:00:53 +08:00
private static final DynamicSkullManager dynamicSkullManager=new DynamicSkullManager();
2016-06-06 13:48:59 +08:00
2016-07-31 22:00:46 +08:00
//For User Skin
public static Map loadProfile(GameProfile gameProfile){
String username=gameProfile.getName();
2018-02-03 18:40:04 +08:00
String credential=MinecraftUtil.getCredential(gameProfile);
//Fix: http://hopper.minecraft.net/crashes/minecraft/MCX-2773713
if(username==null){
2016-06-30 23:40:10 +08:00
logger.warning("Could not load profile: username is null.");
return Maps.newHashMap();
}
2016-06-06 13:48:59 +08:00
String tempName=Thread.currentThread().getName();
Thread.currentThread().setName(username);//Change Thread Name
2016-07-31 22:00:46 +08:00
UserProfile profile=null;
2018-02-03 18:40:04 +08:00
if(profileCache.isReady(credential)){
2016-07-31 22:00:46 +08:00
logger.info("Cached profile will be used.");
2018-02-03 18:40:04 +08:00
profile=profileCache.getProfile(credential);
2016-07-31 22:00:46 +08:00
if(profile==null){
2018-02-03 18:40:04 +08:00
logger.warning("(!Cached Profile is empty!) Expiry:"+profileCache.getExpiry(credential));
if(profileCache.isExpired(credential))//force load
2016-07-31 22:00:46 +08:00
profile=loadProfile0(gameProfile);
}
else
2018-02-03 18:40:04 +08:00
logger.info(profile.toString(profileCache.getExpiry(credential)));
2016-07-31 22:00:46 +08:00
}else{
2018-02-03 18:40:04 +08:00
profileCache.setLoading(credential, true);
2016-07-31 22:00:46 +08:00
profile=loadProfile0(gameProfile);
}
2016-06-06 13:48:59 +08:00
Thread.currentThread().setName(tempName);
return ModelManager0.fromUserProfile(profile);
}
2016-07-31 22:00:46 +08:00
//Core
public static UserProfile loadProfile0(GameProfile gameProfile){
String username=gameProfile.getName();
2018-02-03 18:40:04 +08:00
String credential=MinecraftUtil.getCredential(gameProfile);
profileCache.setLoading(credential, true);
2016-06-06 13:48:59 +08:00
logger.info("Loading "+username+"'s profile.");
2016-11-27 13:32:25 +08:00
if(config.loadlist==null||config.loadlist.isEmpty()){
logger.info("LoadList is Empty.");
return null;
}
2017-07-12 20:00:06 +08:00
UserProfile profile0=new UserProfile();
2016-11-27 13:32:25 +08:00
for(int i=0;i<config.loadlist.size();i++){
SkinSiteProfile ssp=config.loadlist.get(i);
logger.info((i+1)+"/"+config.loadlist.size()+" Try to load profile from '"+ssp.name+"'.");
2016-08-21 11:26:00 +08:00
ProfileLoader.IProfileLoader loader=ProfileLoader.LOADERS.get(ssp.type.toLowerCase());
2016-06-06 13:48:59 +08:00
if(loader==null){
logger.info("Type '"+ssp.type+"' is not defined.");
continue;
}
UserProfile profile=null;
try{
2016-09-10 21:00:53 +08:00
profile=loader.loadProfile(ssp, gameProfile);
2016-06-06 13:48:59 +08:00
}catch(Exception e){
2016-07-31 22:00:46 +08:00
logger.warning("Exception occurs while loading.");
logger.warning(e);
2018-02-11 12:44:41 +08:00
if(e.getCause()!=null) {
logger.warning("Caused By:");
logger.warning(e.getCause());
}
2016-06-06 13:48:59 +08:00
}
if(profile==null)
continue;
2017-07-12 20:00:06 +08:00
if(!config.forceLoadAllTextures){
profile0=profile;
break;
}
profile0.mix(profile);
if(profile0.isFull())
break;
}
if(!profile0.isEmpty()){
2016-06-06 13:48:59 +08:00
logger.info(username+"'s profile loaded.");
2018-02-03 18:40:04 +08:00
profileCache.updateCache(credential, profile0);
profileCache.setLoading(credential, false);
logger.info(profile0.toString(profileCache.getExpiry(credential)));
2017-07-12 20:00:06 +08:00
return profile0;
2016-06-06 13:48:59 +08:00
}
2016-07-31 22:00:46 +08:00
logger.info(username+"'s profile not found in load list.");
2017-07-12 20:00:06 +08:00
2016-07-31 22:00:46 +08:00
if(config.enableLocalProfileCache){
2018-02-03 18:40:04 +08:00
UserProfile profile=profileCache.getLocalProfile(credential);
2016-07-31 22:00:46 +08:00
if(profile==null)
logger.info(username+"'s LocalProfile not found.");
else{
2018-02-03 18:40:04 +08:00
profileCache.updateCache(credential, profile, false);
profileCache.setLoading(credential, false);
2016-07-31 22:00:46 +08:00
logger.info(username+"'s LocalProfile will be used.");
2018-02-03 18:40:04 +08:00
logger.info(profile.toString(profileCache.getExpiry(credential)));
2016-07-31 22:00:46 +08:00
return profile;
}
}
2018-02-03 18:40:04 +08:00
profileCache.setLoading(credential, false);
2016-07-31 22:00:46 +08:00
return null;
2016-06-06 13:48:59 +08:00
}
//For Skull
2016-07-31 22:00:46 +08:00
public static Map<Type, MinecraftProfileTexture> loadProfileFromCache(final GameProfile gameProfile) {
String username=gameProfile.getName();
2018-02-03 18:40:04 +08:00
String credential=MinecraftUtil.getCredential(gameProfile);
if(credential==null)
2016-09-10 21:00:53 +08:00
return dynamicSkullManager.getTexture(gameProfile);
2018-02-03 18:40:04 +08:00
if(config.enableUpdateSkull?profileCache.isReady(credential):profileCache.isExist(credential)){
UserProfile profile=profileCache.getProfile(credential);
2016-06-06 13:48:59 +08:00
return ModelManager0.fromUserProfile(profile);
}
2016-07-31 22:00:46 +08:00
//profileCache.setLoading(username, true);
2016-06-06 13:48:59 +08:00
Thread loadThread=new Thread(){
public void run(){
2016-07-31 22:00:46 +08:00
loadProfile0(gameProfile);//Load in thread
2016-06-06 13:48:59 +08:00
}
};
loadThread.setName(username+"'s skull");
loadThread.start();
return Maps.newHashMap();
}
private static Logger initLogger() {
Logger logger=new Logger(LOG_FILE);
2016-08-20 19:36:44 +08:00
logger.info("CustomSkinLoader "+CustomSkinLoader_FULL_VERSION);
2016-06-06 13:48:59 +08:00
logger.info("DataDir: "+DATA_DIR.getAbsolutePath());
2016-09-24 20:47:08 +08:00
logger.info("Minecraft: "+MinecraftUtil.getMinecraftMainVersion()+"("+MinecraftUtil.getMinecraftVersionText()+")");
2016-06-06 13:48:59 +08:00
return logger;
}
2016-02-27 15:22:35 +08:00
}