From f7e000cb82c1264abbb381e3d46fc6cdd8f9c746 Mon Sep 17 00:00:00 2001 From: Alexander Xia Date: Mon, 12 Feb 2018 15:02:12 +0800 Subject: [PATCH] Add Cache for MojangAPI --- .../customskinloader/config/Config.java | 2 +- .../fake/FakeClientPlayer.java | 16 +++++++++---- .../loader/JsonAPILoader.java | 2 +- .../customskinloader/loader/LegacyLoader.java | 6 ++--- .../loader/MojangAPILoader.java | 19 +++++++++++---- .../utils/HttpRequestUtil.java | 23 ++++++++++--------- .../customskinloader/utils/MinecraftUtil.java | 7 ++---- 7 files changed, 46 insertions(+), 29 deletions(-) diff --git a/Common/source/customskinloader/config/Config.java b/Common/source/customskinloader/config/Config.java index af44d36..e9f0edc 100644 --- a/Common/source/customskinloader/config/Config.java +++ b/Common/source/customskinloader/config/Config.java @@ -26,7 +26,7 @@ public class Config { public boolean forceLoadAllTextures=false; //Cache - public int cacheExpiry=10; + public int cacheExpiry=20; public boolean enableUpdateSkull=false; public boolean enableLocalProfileCache=false; public boolean enableCacheAutoClean=false; diff --git a/Common/source/customskinloader/fake/FakeClientPlayer.java b/Common/source/customskinloader/fake/FakeClientPlayer.java index a784fa8..0e46f8a 100644 --- a/Common/source/customskinloader/fake/FakeClientPlayer.java +++ b/Common/source/customskinloader/fake/FakeClientPlayer.java @@ -57,20 +57,28 @@ public class FakeClientPlayer { private static class LegacyBuffer implements SkinAvailableCallback{ ResourceLocation resourceLocationIn; + boolean loaded=false; public LegacyBuffer(ResourceLocation resourceLocationIn) { - CustomSkinLoader.logger.debug("Loading Legacy Texture "+resourceLocationIn); + CustomSkinLoader.logger.debug("Loading Legacy Texture ("+resourceLocationIn+")"); this.resourceLocationIn=resourceLocationIn; } @Override public void skinAvailable(Type typeIn, ResourceLocation location, MinecraftProfileTexture profileTexture) { - if(typeIn!=Type.SKIN) + if(typeIn!=Type.SKIN || loaded) return; - CustomSkinLoader.logger.debug("Legacy Texture Loaded "+resourceLocationIn); + + loaded=true; + + TextureManager textman = Minecraft.getMinecraft().getTextureManager(); ITextureObject ito = textman.getTexture(location); - textman.loadTexture(resourceLocationIn, ito); + + if(ito!=null) + textman.loadTexture(resourceLocationIn, ito); + CustomSkinLoader.logger.debug("Legacy Texture ("+resourceLocationIn+") Loaded as "+ + ito==null?null:ito.toString()+" ("+location+")"); //textman.bindTexture(resourceLocationIn); } diff --git a/Common/source/customskinloader/loader/JsonAPILoader.java b/Common/source/customskinloader/loader/JsonAPILoader.java index 7067706..8e5c66b 100644 --- a/Common/source/customskinloader/loader/JsonAPILoader.java +++ b/Common/source/customskinloader/loader/JsonAPILoader.java @@ -60,7 +60,7 @@ public class JsonAPILoader implements ProfileLoader.IProfileLoader { } json=IOUtils.toString(new FileInputStream(jsonFile)); }else{ - HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(jsonUrl).setCacheTime(60).setUserAgent(ssp.userAgent).setPayload(type.jsonAPI.getPayload(ssp))); + HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(jsonUrl).setCacheTime(90).setUserAgent(ssp.userAgent).setPayload(type.jsonAPI.getPayload(ssp))); json=responce.content; } if(json==null||json.equals("")){ diff --git a/Common/source/customskinloader/loader/LegacyLoader.java b/Common/source/customskinloader/loader/LegacyLoader.java index f55a7de..cc07d84 100644 --- a/Common/source/customskinloader/loader/LegacyLoader.java +++ b/Common/source/customskinloader/loader/LegacyLoader.java @@ -30,7 +30,7 @@ public class LegacyLoader implements ProfileLoader.IProfileLoader { if(skinFile.exists()&&skinFile.isFile()) profile.skinUrl=HttpTextureUtil.getLocalLegacyFakeUrl(skin, HttpTextureUtil.getHash(skin, skinFile.length(), skinFile.lastModified())); }else{ - HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(skin).setUserAgent(ssp.userAgent).setCheckPNG(ssp.checkPNG!=null&&ssp.checkPNG!=false).setLoadContent(false).setCacheTime(60)); + HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(skin).setUserAgent(ssp.userAgent).setCheckPNG(ssp.checkPNG!=null&&ssp.checkPNG!=false).setLoadContent(false).setCacheTime(90)); if(responce.success) profile.skinUrl=HttpTextureUtil.getLegacyFakeUrl(skin); } @@ -43,7 +43,7 @@ public class LegacyLoader implements ProfileLoader.IProfileLoader { if(capeFile.exists()&&capeFile.isFile()) profile.capeUrl=HttpTextureUtil.getLocalLegacyFakeUrl(cape, HttpTextureUtil.getHash(cape, capeFile.length(), capeFile.lastModified())); }else{ - HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(cape).setUserAgent(ssp.userAgent).setCheckPNG(ssp.checkPNG!=null&&ssp.checkPNG!=false).setLoadContent(false).setCacheTime(60)); + HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(cape).setUserAgent(ssp.userAgent).setCheckPNG(ssp.checkPNG!=null&&ssp.checkPNG!=false).setLoadContent(false).setCacheTime(90)); if(responce.success) profile.capeUrl=HttpTextureUtil.getLegacyFakeUrl(cape); } @@ -55,7 +55,7 @@ public class LegacyLoader implements ProfileLoader.IProfileLoader { if(elytraFile.exists()&&elytraFile.isFile()) profile.elytraUrl=HttpTextureUtil.getLocalLegacyFakeUrl(elytra, HttpTextureUtil.getHash(elytra, elytraFile.length(), elytraFile.lastModified())); }else{ - HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(elytra).setUserAgent(ssp.userAgent).setCheckPNG(ssp.checkPNG!=null&&ssp.checkPNG!=false).setLoadContent(false).setCacheTime(60)); + HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest(elytra).setUserAgent(ssp.userAgent).setCheckPNG(ssp.checkPNG!=null&&ssp.checkPNG!=false).setLoadContent(false).setCacheTime(90)); if(responce.success) profile.elytraUrl=HttpTextureUtil.getLegacyFakeUrl(elytra); } diff --git a/Common/source/customskinloader/loader/MojangAPILoader.java b/Common/source/customskinloader/loader/MojangAPILoader.java index 8e834c1..94447b8 100644 --- a/Common/source/customskinloader/loader/MojangAPILoader.java +++ b/Common/source/customskinloader/loader/MojangAPILoader.java @@ -15,6 +15,8 @@ import com.mojang.authlib.GameProfile; import com.mojang.authlib.minecraft.MinecraftProfileTexture; import com.mojang.authlib.minecraft.MinecraftSessionService; import com.mojang.authlib.properties.Property; +import com.mojang.authlib.properties.PropertyMap; +import com.mojang.authlib.yggdrasil.response.MinecraftProfilePropertiesResponse; import com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload; import com.mojang.util.UUIDTypeAdapter; @@ -59,8 +61,10 @@ public class MojangAPILoader implements ProfileLoader.IProfileLoader { HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest("https://api.mojang.com/users/profiles/minecraft/"+username).setCacheTime(0)); if(StringUtils.isEmpty(responce.content)) return null; + Gson gson=new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create(); GameProfile gameProfile=gson.fromJson(responce.content, GameProfile.class); + if(gameProfile.getId()==null) return null; return new GameProfile(gameProfile.getId(),gameProfile.getName()); @@ -68,12 +72,19 @@ public class MojangAPILoader implements ProfileLoader.IProfileLoader { //UUID -> Profile public static GameProfile fillGameProfile(GameProfile profile) throws Exception{ //Doc (http://wiki.vg/Mojang_API#UUID_-.3E_Profile_.2B_Skin.2FCape) - HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest("https://sessionserver.mojang.com/session/minecraft/profile/"+UUIDTypeAdapter.fromUUID(profile.getId())).setCacheTime(60)); + HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest("https://sessionserver.mojang.com/session/minecraft/profile/"+UUIDTypeAdapter.fromUUID(profile.getId())).setCacheTime(90)); if(StringUtils.isEmpty(responce.content)) return profile; - Gson gson=new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create(); - GameProfile gameProfile=gson.fromJson(responce.content, GameProfile.class); - return gameProfile; + + Gson gson=new GsonBuilder() + .registerTypeAdapter(UUID.class, new UUIDTypeAdapter()) + .registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()) + .create(); + MinecraftProfilePropertiesResponse propertiesResponce=gson.fromJson(responce.content, MinecraftProfilePropertiesResponse.class); + GameProfile newGameProfile=new GameProfile(propertiesResponce.getId(),propertiesResponce.getName()); + newGameProfile.getProperties().putAll(propertiesResponce.getProperties()); + + return newGameProfile; } public static Map getTextures(GameProfile gameProfile) throws Exception{ diff --git a/Common/source/customskinloader/utils/HttpRequestUtil.java b/Common/source/customskinloader/utils/HttpRequestUtil.java index acce848..1e4cee6 100644 --- a/Common/source/customskinloader/utils/HttpRequestUtil.java +++ b/Common/source/customskinloader/utils/HttpRequestUtil.java @@ -75,7 +75,7 @@ public class HttpRequestUtil { public static HttpResponce makeHttpRequest(HttpRequest request){ try{ - CustomSkinLoader.logger.info("Try to request '"+request.url+(request.userAgent==null?"'.":"' with user agent '"+request.userAgent+"'.")); + CustomSkinLoader.logger.debug("Try to request '"+request.url+(request.userAgent==null?"'.":"' with user agent '"+request.userAgent+"'.")); //Check Cache if(StringUtils.isNotEmpty(request.payload)) request.cacheTime=-1;//No Cache @@ -101,7 +101,7 @@ public class HttpRequestUtil { //Init Connection String url=new URI(request.url).toASCIIString(); if(!url.equalsIgnoreCase(request.url)) - CustomSkinLoader.logger.info("Encoded URL: "+url); + CustomSkinLoader.logger.debug("Encoded URL: "+url); HttpURLConnection c = (HttpURLConnection) (new URL(url)).openConnection(); c.setReadTimeout(1000 * 10); c.setConnectTimeout(1000 * 10); @@ -131,11 +131,11 @@ public class HttpRequestUtil { responce.responceCode=c.getResponseCode(); int res=c.getResponseCode()/100; if(res==4||res==5){//Failed - CustomSkinLoader.logger.info("Failed to request (Response Code: "+c.getResponseCode()+")"); + CustomSkinLoader.logger.debug("Failed to request (Response Code: "+c.getResponseCode()+")"); return responce; } responce.success=true; - CustomSkinLoader.logger.info("Successfully request (Response Code: "+c.getResponseCode()+" , Content Length: "+c.getContentLength()+")"); + CustomSkinLoader.logger.debug("Successfully request (Response Code: "+c.getResponseCode()+" , Content Length: "+c.getContentLength()+")"); if(responce.responceCode==HttpURLConnection.HTTP_NOT_MODIFIED) return loadFromCache(request,responce); @@ -143,7 +143,7 @@ public class HttpRequestUtil { InputStream is="gzip".equals(c.getContentEncoding())?new GZIPInputStream(c.getInputStream()):c.getInputStream(); byte [] bytes = IOUtils.toByteArray(is); if(request.checkPNG&&(bytes.length<=4||bytes[1]!=(byte)'P'||bytes[2]!=(byte)'N'||bytes[3]!=(byte)'G')){ - CustomSkinLoader.logger.info("Failed to request (Not Standard PNG)"); + CustomSkinLoader.logger.debug("Failed to request (Not Standard PNG)"); responce.success=false; return responce; } @@ -156,16 +156,16 @@ public class HttpRequestUtil { cacheInfo.expire=getExpire(c,request.cacheTime); FileUtils.write(cacheInfoFile, CustomSkinLoader.GSON.toJson(cacheInfo)); } - CustomSkinLoader.logger.info("Saved to cache (Length: "+request.cacheFile.length()+" , Path: '"+request.cacheFile.getAbsolutePath()+"' , Expire: "+cacheInfo.expire+")"); + CustomSkinLoader.logger.debug("Saved to cache (Length: "+request.cacheFile.length()+" , Path: '"+request.cacheFile.getAbsolutePath()+"' , Expire: "+cacheInfo.expire+")"); } if(!request.loadContent) return responce; responce.content=new String(bytes,Charsets.UTF_8); - CustomSkinLoader.logger.info("Content: "+responce.content); + CustomSkinLoader.logger.debug("Content: "+responce.content); return responce; }catch(Exception e){ - CustomSkinLoader.logger.info("Failed to request (Exception: "+e.toString()+")"); + CustomSkinLoader.logger.debug("Failed to request (Exception: "+e.toString()+")"); return loadFromCache(request,new HttpResponce()); } } @@ -179,7 +179,7 @@ public class HttpRequestUtil { private static HttpResponce loadFromCache(HttpRequest request,HttpResponce responce,long expireTime){ if(request.cacheFile==null||!request.cacheFile.isFile()) return responce; - CustomSkinLoader.logger.info("Cache file found (Length: "+request.cacheFile.length()+" , Path: '"+request.cacheFile.getAbsolutePath()+"' , Expire: "+expireTime+"')"); + CustomSkinLoader.logger.debug("Cache file found (Length: "+request.cacheFile.length()+" , Path: '"+request.cacheFile.getAbsolutePath()+"' , Expire: "+expireTime+"')"); responce.fromCache=true; responce.success=true; if(!request.loadContent) @@ -188,9 +188,10 @@ public class HttpRequestUtil { CustomSkinLoader.logger.info("Try to load from cache '"+request.cacheFile.getAbsolutePath()+"'."); try { responce.content=FileUtils.readFileToString(request.cacheFile); - CustomSkinLoader.logger.info("Successfully load from cache"); + CustomSkinLoader.logger.debug("Successfully load from cache"); + CustomSkinLoader.logger.debug("Content: "+responce.content); } catch (IOException e) { - CustomSkinLoader.logger.info("Failed to load from cache (Exception: "+e.toString()+")"); + CustomSkinLoader.logger.debug("Failed to load from cache (Exception: "+e.toString()+")"); responce.success=false; } return responce; diff --git a/Common/source/customskinloader/utils/MinecraftUtil.java b/Common/source/customskinloader/utils/MinecraftUtil.java index a9f78f2..db6d491 100644 --- a/Common/source/customskinloader/utils/MinecraftUtil.java +++ b/Common/source/customskinloader/utils/MinecraftUtil.java @@ -124,10 +124,7 @@ public class MinecraftUtil { } public static String getCredential(GameProfile profile) { - if(profile!=null&&profile.hashCode()!=0) - return new StringBuilder(). - append(profile.getName()).append("-").append(profile.getId().toString()).toString(); - else - return null; + return (profile==null || profile.hashCode()==0) ? null : + (profile.getId()==null ? profile.getName() : String.format("%s-%s", profile.getName(),profile.getId())); } }