mirror of
https://github.com/xfl03/MCCustomSkinLoader.git
synced 2026-11-26 09:12:59 +00:00
Add Cache for MojangAPI
This commit is contained in:
1 parent
c40bb38e20
commit
f7e000cb82
7 files changed
+46
-29
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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("")){
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<MinecraftProfileTexture.Type,MinecraftProfileTexture> getTextures(GameProfile gameProfile) throws Exception{
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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()));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user