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

203 lines
9.1 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;
2020-08-16 21:55:06 +08:00
import java.util.concurrent.*;
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.
2020-03-24 11:46:11 +08:00
* @author (C) Jeremy Lam [JLChnToZ] 2013-2014 & Alexander Xia [xfl03] 2014-2020
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 {
2018-02-12 12:20:28 +08:00
public static final String CustomSkinLoader_VERSION="@MOD_VERSION@";
public static final String CustomSkinLoader_FULL_VERSION="@MOD_FULL_VERSION@";
public static final File DATA_DIR=new File(MinecraftUtil.getMinecraftDataDir(),"CustomSkinLoader"),
LOG_FILE=new File(DATA_DIR,"CustomSkinLoader.log"),
CONFIG_FILE=new File(DATA_DIR,"CustomSkinLoader.json");
public static final SkinSiteProfile[] DEFAULT_LOAD_LIST={
SkinSiteProfile.createMojangAPI("Mojang"),
2020-06-23 21:16:20 +08:00
SkinSiteProfile.createCustomSkinAPI("LittleSkin","https://littlesk.in/csl/"),
2018-02-12 12:20:28 +08:00
SkinSiteProfile.createCustomSkinAPI("BlessingSkin","http://skin.prinzeugen.net/"),
//OneSkin has been removed temporarily
//SkinSiteProfile.createCustomSkinAPI("OneSkin","http://fleey.cn/skin/skin_user/skin_json.php/"),
//Minecrack could not load skin correctly
//SkinSiteProfile.creatLegacy("Minecrack","http://minecrack.fr.nf/mc/skinsminecrackd/{USERNAME}.png","http://minecrack.fr.nf/mc/cloaksminecrackd/{USERNAME}.png",null),
2018-07-20 23:23:37 +08:00
SkinSiteProfile.createElyByAPI("ElyBy"),
2018-02-12 12:20:28 +08:00
SkinSiteProfile.createUniSkinAPI("SkinMe","http://www.skinme.cc/uniskin/"),
2019-05-10 11:20:39 +03:00
SkinSiteProfile.createLegacy("LocalSkin","LocalSkin/skins/{USERNAME}.png","LocalSkin/capes/{USERNAME}.png","LocalSkin/elytras/{USERNAME}.png"),
SkinSiteProfile.createGlitchlessAPI("GlitchlessGames","https://games.glitchless.ru/api/minecraft/users/profiles/textures/?nickname=")};
2018-02-12 12:20:28 +08:00
public static final Gson GSON=new GsonBuilder().setPrettyPrinting().create();
public static final Logger logger=initLogger();
public static final Config config=Config.loadConfig0();
private static final ProfileCache profileCache=new ProfileCache();
private static final DynamicSkullManager dynamicSkullManager=new DynamicSkullManager();
2020-03-24 11:46:11 +08:00
2020-08-16 21:55:06 +08:00
//Correct thread name in thread pool
private static final ThreadFactory defaultFactory = Executors.defaultThreadFactory();
private static final ThreadFactory customFactory = r -> {
Thread t = defaultFactory.newThread(r);
if(r instanceof Thread) {
t.setName(((Thread) r).getName());
}
return t;
};
//Thread pool will discard oldest task when queue reaches 333 tasks
private static final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
config.threadPoolSize, config.threadPoolSize, 1L, TimeUnit.MINUTES,
new LinkedBlockingQueue<>(333), customFactory, new ThreadPoolExecutor.DiscardOldestPolicy()
);
2018-02-12 12:20:28 +08:00
//For User Skin
2018-06-25 00:34:32 +08:00
public static Map<Type, MinecraftProfileTexture> loadProfile(GameProfile gameProfile){
2018-02-12 12:20:28 +08:00
String username=gameProfile.getName();
String credential=MinecraftUtil.getCredential(gameProfile);
//Fix: http://hopper.minecraft.net/crashes/minecraft/MCX-2773713
if(username==null){
logger.warning("Could not load profile: username is null.");
return Maps.newHashMap();
}
String tempName=Thread.currentThread().getName();
Thread.currentThread().setName(username);//Change Thread Name
2018-06-25 00:34:32 +08:00
UserProfile profile;
2018-02-12 12:20:28 +08:00
if(profileCache.isReady(credential)){
logger.info("Cached profile will be used.");
profile=profileCache.getProfile(credential);
if(profile==null){
logger.warning("(!Cached Profile is empty!) Expiry:"+profileCache.getExpiry(credential));
if(profileCache.isExpired(credential))//force load
profile=loadProfile0(gameProfile);
}
else
logger.info(profile.toString(profileCache.getExpiry(credential)));
}else{
profileCache.setLoading(credential, true);
profile=loadProfile0(gameProfile);
}
Thread.currentThread().setName(tempName);
return ModelManager0.fromUserProfile(profile);
}
//Core
public static UserProfile loadProfile0(GameProfile gameProfile){
String username=gameProfile.getName();
String credential=MinecraftUtil.getCredential(gameProfile);
profileCache.setLoading(credential, true);
logger.info("Loading "+username+"'s profile.");
if(config.loadlist==null||config.loadlist.isEmpty()){
logger.info("LoadList is Empty.");
return null;
}
UserProfile profile0=new UserProfile();
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+"'.");
ProfileLoader.IProfileLoader loader=ProfileLoader.LOADERS.get(ssp.type.toLowerCase());
if(loader==null){
logger.info("Type '"+ssp.type+"' is not defined.");
continue;
}
UserProfile profile=null;
try{
profile=loader.loadProfile(ssp, gameProfile);
}catch(Exception e){
logger.warning("Exception occurs while loading.");
logger.warning(e);
if(e.getCause()!=null) {
logger.warning("Caused By:");
logger.warning(e.getCause());
}
}
if(profile==null)
continue;
if(!config.forceLoadAllTextures){
profile0=profile;
break;
}
profile0.mix(profile);
if(profile0.isFull())
break;
}
if(!profile0.isEmpty()){
logger.info(username+"'s profile loaded.");
2018-11-26 15:16:07 +08:00
if(!config.enableCape)
profile0.capeUrl = null;
2018-02-12 12:20:28 +08:00
profileCache.updateCache(credential, profile0);
profileCache.setLoading(credential, false);
logger.info(profile0.toString(profileCache.getExpiry(credential)));
return profile0;
}
logger.info(username+"'s profile not found in load list.");
if(config.enableLocalProfileCache){
UserProfile profile=profileCache.getLocalProfile(credential);
if(profile==null)
logger.info(username+"'s LocalProfile not found.");
else{
profileCache.updateCache(credential, profile, false);
profileCache.setLoading(credential, false);
logger.info(username+"'s LocalProfile will be used.");
logger.info(profile.toString(profileCache.getExpiry(credential)));
return profile;
}
}
profileCache.setLoading(credential, false);
return null;
}
//For Skull
public static Map<Type, MinecraftProfileTexture> loadProfileFromCache(final GameProfile gameProfile) {
String username=gameProfile.getName();
String credential=MinecraftUtil.getCredential(gameProfile);
2018-06-24 23:07:51 +08:00
if(username == null || credential == null)
2018-02-12 12:20:28 +08:00
return dynamicSkullManager.getTexture(gameProfile);
2020-08-16 21:07:22 +08:00
if(config.forceUpdateSkull ?profileCache.isReady(credential):profileCache.isExist(credential)){
2018-02-12 12:20:28 +08:00
UserProfile profile=profileCache.getProfile(credential);
return ModelManager0.fromUserProfile(profile);
}
if (!profileCache.isLoading(credential)) {
profileCache.setLoading(credential, true);
Runnable loadThread = () -> {
String tempName = Thread.currentThread().getName();
Thread.currentThread().setName(username + "'s skull");
loadProfile0(gameProfile);//Load in thread
Thread.currentThread().setName(tempName);
};
2020-08-16 21:07:22 +08:00
if (config.forceUpdateSkull) {
new Thread(loadThread).start();
} else {
threadPool.execute(loadThread);
}
}
2018-02-12 12:20:28 +08:00
return Maps.newHashMap();
}
private static Logger initLogger() {
Logger logger=new Logger(LOG_FILE);
logger.info("CustomSkinLoader "+CustomSkinLoader_FULL_VERSION);
logger.info("DataDir: "+DATA_DIR.getAbsolutePath());
logger.info("Minecraft: "+MinecraftUtil.getMinecraftMainVersion()+"("+MinecraftUtil.getMinecraftVersionText()+")");
return logger;
}
2016-02-27 15:22:35 +08:00
}