mirror of
https://github.com/xfl03/MCCustomSkinLoader.git
synced 2026-11-25 19:14:22 +00:00
Revision 14
Local skin supported Local profile cache supported Better profile cache Swifter official profile loading Better skull loading Fix legacy loading bug Fix bug while using ClassTransformer
This commit is contained in:
1 parent
b34a5b3343
commit
5c12463c94
30 files changed
+766
-751
No files matched your search
@@ -0,0 +1,78 @@
|
||||
package customskinloader.profile;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
|
||||
import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type;
|
||||
|
||||
/**
|
||||
* Model Manager for 1.8 and higher.
|
||||
* A manager to check if model is available.
|
||||
* It is the only class in package 'customskinloader' which has differences in different Minecraft version.
|
||||
* @since 13.1
|
||||
*/
|
||||
public class ModelManager0 {
|
||||
private static ArrayList<String> models=null;
|
||||
|
||||
/**
|
||||
* Check if model is available.
|
||||
* @param model - default/slim
|
||||
* @since 13.1
|
||||
*/
|
||||
public static boolean checkModel(String model){
|
||||
if(models==null)
|
||||
refreshModels();
|
||||
return models.contains(model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse hashMapProfile to UserProfile
|
||||
* @param profile - hashMapProfile (HashMap<String,MinecraftProfileTexture>)
|
||||
* @return profile - UserProfile instance
|
||||
* @since 13.1
|
||||
*/
|
||||
public static UserProfile toUserProfile(Map profile){
|
||||
UserProfile userProfile=new UserProfile();
|
||||
if(profile==null)
|
||||
return userProfile;
|
||||
MinecraftProfileTexture skin=(MinecraftProfileTexture)profile.get(Type.SKIN);
|
||||
userProfile.skinUrl= skin==null?null:skin.getUrl();//Avoid NullPointerException
|
||||
userProfile.model= skin==null?null:skin.getMetadata("model");
|
||||
if(userProfile.model==null||userProfile.model.equals(""))
|
||||
userProfile.model="default";
|
||||
MinecraftProfileTexture cape=(MinecraftProfileTexture)profile.get(Type.CAPE);
|
||||
userProfile.capeUrl= cape==null?null:cape.getUrl();
|
||||
return userProfile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse UserProfile to hashMapProfile
|
||||
* @param profile - UserProfile instance
|
||||
* @return profile - hashMapProfile (HashMap<String,MinecraftProfileTexture>)
|
||||
* @since 13.1
|
||||
*/
|
||||
public static Map fromUserProfile(UserProfile profile){
|
||||
Map map=Maps.newHashMap();
|
||||
if(profile==null)
|
||||
return map;
|
||||
if(profile.skinUrl!=null){
|
||||
Map metadata=null;
|
||||
if(profile.model!=null&&profile.model.equals("slim")){
|
||||
metadata = Maps.newHashMap();
|
||||
metadata.put("model", "slim");
|
||||
}
|
||||
map.put(Type.SKIN, new MinecraftProfileTexture(profile.skinUrl,metadata));
|
||||
}
|
||||
if(profile.capeUrl!=null)
|
||||
map.put(Type.CAPE, new MinecraftProfileTexture(profile.capeUrl,null));
|
||||
return map;
|
||||
}
|
||||
|
||||
private static void refreshModels(){
|
||||
models=new ArrayList<String>();
|
||||
models.add("default");
|
||||
models.add("slim");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package customskinloader.profile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import customskinloader.CustomSkinLoader;
|
||||
import customskinloader.config.Config;
|
||||
import customskinloader.utils.TimeUtil;
|
||||
|
||||
public class ProfileCache {
|
||||
public static File PROFILE_CACHE_DIR=new File(CustomSkinLoader.DATA_DIR,"ProfileCache");
|
||||
|
||||
private HashMap<String,CachedProfile> cachedProfiles=new HashMap<String,CachedProfile>();
|
||||
private HashMap<String,UserProfile> localProfiles=new HashMap<String,UserProfile>();
|
||||
|
||||
public ProfileCache(){
|
||||
if(!PROFILE_CACHE_DIR.exists())
|
||||
PROFILE_CACHE_DIR.mkdir();
|
||||
}
|
||||
|
||||
public boolean isExist(String username){
|
||||
return cachedProfiles.containsKey(username.toLowerCase());
|
||||
}
|
||||
public boolean isReady(String username){
|
||||
CachedProfile cp=cachedProfiles.get(username.toLowerCase());
|
||||
return cp==null?false:(cp.loading||cp.expiryTime>TimeUtil.getCurrentUnixTimestamp());
|
||||
}
|
||||
public boolean isExpired(String username){
|
||||
CachedProfile cp=cachedProfiles.get(username.toLowerCase());
|
||||
return cp==null?true:(cp.expiryTime<=TimeUtil.getCurrentUnixTimestamp());
|
||||
}
|
||||
|
||||
public UserProfile getProfile(String username){
|
||||
return getCachedProfile(username).profile;
|
||||
}
|
||||
public long getExpiry(String username){
|
||||
return getCachedProfile(username).expiryTime;
|
||||
}
|
||||
public UserProfile getLocalProfile(String username){
|
||||
if(localProfiles.containsKey(username.toLowerCase()))
|
||||
return localProfiles.get(username.toLowerCase());
|
||||
return loadLocalProfile(username);
|
||||
}
|
||||
|
||||
public void setLoading(String username,boolean loading){
|
||||
getCachedProfile(username).loading=loading;
|
||||
}
|
||||
public void updateCache(String username,UserProfile profile){
|
||||
updateCache(username,profile,CustomSkinLoader.config.enableLocalProfileCache);
|
||||
}
|
||||
public void updateCache(String username,UserProfile profile,boolean saveLocalProfile){
|
||||
CachedProfile cp=getCachedProfile(username);
|
||||
cp.profile=profile;
|
||||
cp.expiryTime=TimeUtil.getUnixTimestampRandomDelay(CustomSkinLoader.config.cacheExpiry);
|
||||
if(!saveLocalProfile)
|
||||
return;
|
||||
saveLocalProfile(username,profile);
|
||||
}
|
||||
|
||||
private CachedProfile getCachedProfile(String username){
|
||||
CachedProfile cp=cachedProfiles.get(username.toLowerCase());
|
||||
if(cp!=null)
|
||||
return cp;
|
||||
cp=new CachedProfile();
|
||||
cachedProfiles.put(username.toLowerCase(), cp);
|
||||
return cp;
|
||||
}
|
||||
private UserProfile loadLocalProfile(String username){
|
||||
File localProfile=new File(PROFILE_CACHE_DIR,username.toLowerCase()+".json");
|
||||
if(!localProfile.exists()){
|
||||
localProfiles.put(username.toLowerCase(), null);
|
||||
}
|
||||
try{
|
||||
String json=IOUtils.toString(new FileInputStream(localProfile));
|
||||
UserProfile profile=CustomSkinLoader.GSON.fromJson(json, UserProfile.class);
|
||||
localProfiles.put(username.toLowerCase(), profile);
|
||||
CustomSkinLoader.logger.info("Successfully load LocalProfile.");
|
||||
return profile;
|
||||
}catch(Exception e){
|
||||
CustomSkinLoader.logger.info("Failed to load LocalProfile.("+e.getMessage()+")");
|
||||
localProfiles.put(username.toLowerCase(), null);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private void saveLocalProfile(String username,UserProfile profile){
|
||||
String json=CustomSkinLoader.GSON.toJson(profile);
|
||||
File localProfile=new File(PROFILE_CACHE_DIR,username.toLowerCase()+".json");
|
||||
if(localProfile.exists())
|
||||
localProfile.delete();
|
||||
try {
|
||||
localProfile.createNewFile();
|
||||
IOUtils.write(json, new FileOutputStream(localProfile));
|
||||
CustomSkinLoader.logger.info("Successfully save LocalProfile.");
|
||||
} catch (Exception e) {
|
||||
CustomSkinLoader.logger.info("Failed to save LocalProfile.("+e.getMessage()+")");
|
||||
}
|
||||
}
|
||||
}
|
||||
class CachedProfile{
|
||||
public UserProfile profile;
|
||||
public long expiryTime=0;
|
||||
public boolean loading=false;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package customskinloader.profile;
|
||||
|
||||
/**The instance to storage user's profile in memory temporarily.
|
||||
* In this manner, it could be easier to pass profile in program.
|
||||
* @since 13.1
|
||||
*/
|
||||
public class UserProfile {
|
||||
|
||||
/**
|
||||
* Direct url for skin.
|
||||
* @since 13.1
|
||||
*/
|
||||
public String skinUrl=null;
|
||||
|
||||
/**
|
||||
* Model for skin.
|
||||
* default/slim
|
||||
* @since 13.1
|
||||
*/
|
||||
public String model=null;
|
||||
|
||||
/**
|
||||
* Direct url for cape.
|
||||
* @since 13.1
|
||||
*/
|
||||
public String capeUrl=null;
|
||||
|
||||
/**
|
||||
* Get parsed String to output the instance.
|
||||
*/
|
||||
@Override
|
||||
public String toString(){
|
||||
return toString(0);
|
||||
}
|
||||
public String toString(long expiry){
|
||||
return "(SkinUrl: "+skinUrl+" , Model: "+model+" , CapeUrl: "+capeUrl+(expiry==0?"":(" , Expiry: "+expiry))+")";
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the instance is empty.
|
||||
* @return status (true - empty)
|
||||
*/
|
||||
public boolean isEmpty(){
|
||||
return skinUrl==null && (model==null||model.equalsIgnoreCase("default")) && capeUrl==null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user