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:
xfl03 committed 2016-07-31 22:00:46 +08:00
1 parent b34a5b3343
commit 5c12463c94
30 files changed
+766 -751

No files matched your search

@@ -0,0 +1,73 @@
package customskinloader.utils;
import java.io.File;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FilenameUtils;
import customskinloader.CustomSkinLoader;
public class HttpTextureUtil {
public static class HttpTextureInfo{
public String url="";
public File cacheFile;
public String hash;
}
private static final String
LEGACY_MARK="(LEGACY)",
LOCAL_MARK="(LOCAL)",
LOCAL_LEGACY_MARK="(LOCAL_LEGACY)";
public static HttpTextureInfo toHttpTextureInfo(File defaultCacheDir,String fakeUrl){
HttpTextureInfo info=new HttpTextureInfo();
if(fakeUrl.startsWith("http")){
info.url=fakeUrl;
info.hash=FilenameUtils.getBaseName(fakeUrl);
info.cacheFile=getCacheFile(defaultCacheDir,info.hash);
return info;
}
if(fakeUrl.startsWith(LOCAL_LEGACY_MARK)){
fakeUrl=fakeUrl.replaceFirst(LOCAL_LEGACY_MARK, "");
String[] t=fakeUrl.split(",",2);
if(t.length!=2)
return info;
info.cacheFile=new File(CustomSkinLoader.DATA_DIR,t[1]);
info.hash=t[0];
return info;
}
if(fakeUrl.startsWith(LOCAL_MARK)){
fakeUrl=fakeUrl.replaceFirst(LOCAL_MARK, "");
info.cacheFile=new File(CustomSkinLoader.DATA_DIR,fakeUrl);
info.hash=FilenameUtils.getBaseName(fakeUrl);
return info;
}
if(fakeUrl.startsWith(LEGACY_MARK)){
fakeUrl=fakeUrl.replaceFirst(LEGACY_MARK, "");
String[] t=fakeUrl.split(",",2);
if(t.length!=2)
return info;
info.url=t[1];
info.hash=t[0];
info.cacheFile=getCacheFile(defaultCacheDir,info.hash);
return info;
}
return info;
}
public static String getLegacyFakeUrl(String url,String hash){
return LEGACY_MARK+hash+","+url;
}
public static String getLocalFakeUrl(String path){
return LOCAL_MARK+path;
}
public static String getLocalLegacyFakeUrl(String path,String hash){
return LOCAL_LEGACY_MARK+hash+","+path;
}
public static String getHash(String url,long size,long lastModified){
return DigestUtils.sha1Hex(size+url+lastModified);
}
private static File getCacheFile(File cacheDir,String hash){
return new File(new File(cacheDir,hash.length()>2?hash.substring(0,2):"xx"), hash);
}
}
+49 -48
View File
@@ -19,64 +19,65 @@ public class HttpUtil0 {
public static String readHttp(String url,String userAgent){
HttpURLConnection c=null;
try {
CustomSkinLoader.logger.info("Try to read '"+url+"' with user agent '"+userAgent+"'.");
c=createConnection(url,userAgent);
int res = c.getResponseCode()/100;
if (res==4||res==5||c.getResponseCode()==HttpURLConnection.HTTP_NO_CONTENT) {
CustomSkinLoader.logger.info("Failed to read (Response Code: "+c.getResponseCode()+")");
return null;
}
CustomSkinLoader.logger.info("Successfully read (Response Code: "+c.getResponseCode()+" , Content Length: "+c.getContentLength()+")");
InputStream is = c.getInputStream();
return IOUtils.toString(is, Charsets.UTF_8);
} catch (Exception e) {
CustomSkinLoader.logger.info("Failed to read (Exception: "+e.getMessage()+")");
return null;
} finally {
if(c!=null)
c.disconnect();
}
CustomSkinLoader.logger.info("Try to read '"+url+(userAgent==null?" without user agent.":"' with user agent '"+userAgent+"'."));
c=createConnection(url,userAgent);
int res = c.getResponseCode()/100;
if (res==4||res==5||c.getResponseCode()==HttpURLConnection.HTTP_NO_CONTENT) {
CustomSkinLoader.logger.info("Failed to read (Response Code: "+c.getResponseCode()+")");
return null;
}
CustomSkinLoader.logger.info("Successfully read (Response Code: "+c.getResponseCode()+" , Content Length: "+c.getContentLength()+")");
InputStream is = c.getInputStream();
return IOUtils.toString(is, Charsets.UTF_8);
} catch (Exception e) {
CustomSkinLoader.logger.info("Failed to read (Exception: "+e.getMessage()+")");
return null;
} finally {
if(c!=null)
c.disconnect();
}
}
/**
* Get url after redirect.
* Get faked url after redirect.
* Using this method, can also check if it is exist.
* @param url - before redirect
* @return url - after redirect (null for not exist)
* @since 13.11
* @return url - faked after redirect (null for not exist)
* @since 14.1
*/
public static String getRealUrl(String url,String userAgent){
public static String getFakeUrl(String url,String userAgent){
HttpURLConnection c=null;
try {
CustomSkinLoader.logger.info("Try to get real url of '"+url+"' with user agent '"+userAgent+"'.");
c=createConnection(url,userAgent);
int res = c.getResponseCode()/100;
if (res==4||res==5) {
CustomSkinLoader.logger.info("Failed to get real url (Response Code: "+c.getResponseCode()+")");
return null;
}
CustomSkinLoader.logger.info("Successfully get real url ("
+ "Response Code: "+c.getResponseCode()+" , "
+ "Content Length: "+c.getContentLength()+" , "
+ "Real URL: "+c.getURL().toString()+")");
return c.getURL().toString();
} catch (Exception e) {
CustomSkinLoader.logger.info("Failed to get real url (Exception: "+e.getMessage()+")");
return null;
} finally{
if(c!=null)
c.disconnect();
}
CustomSkinLoader.logger.info("Try to get fake url of '"+url+"' with user agent '"+userAgent+"'.");
c=createConnection(url,userAgent);
int res = c.getResponseCode()/100;
if (res==4||res==5) {
CustomSkinLoader.logger.info("Failed to get fake url (Response Code: "+c.getResponseCode()+")");
return null;
}
String url1=c.getURL().toString();
CustomSkinLoader.logger.info("Successfully get fake url ("
+ "Response Code: "+c.getResponseCode()+" , "
+ "Content Length: "+c.getContentLength()+" , "
+ "Real URL: "+url1+")");
return HttpTextureUtil.getLegacyFakeUrl(url1, HttpTextureUtil.getHash(url1, c.getContentLength(), c.getLastModified()));
} catch (Exception e) {
CustomSkinLoader.logger.info("Failed to get fake url (Exception: "+e.getMessage()+")");
return null;
} finally{
if(c!=null)
c.disconnect();
}
}
private static HttpURLConnection createConnection(String url,String userAgent) throws MalformedURLException, IOException{
HttpURLConnection c = (HttpURLConnection) (new URL(url)).openConnection();
c.setReadTimeout(1000 * 10);
c.setConnectTimeout(1000 * 10);
if(userAgent!=null)
c.setRequestProperty("User-Agent",userAgent);
c.setUseCaches(false);
c.setInstanceFollowRedirects(true);
c.connect();
return c;
c.setReadTimeout(1000 * 10);
c.setConnectTimeout(1000 * 10);
if(userAgent!=null)
c.setRequestProperty("User-Agent",userAgent);
c.setUseCaches(false);
c.setInstanceFollowRedirects(true);
c.connect();
return c;
}
}
@@ -68,4 +68,16 @@ public class MinecraftUtil {
break;
}
}
public static boolean isCoreFile(URL url){
Matcher m = null;
try{
m=MINECRAFT_CORE_FILE_PATTERN.matcher(URLDecoder.decode(url.getPath(),"UTF-8"));
}catch(Exception e){
e.printStackTrace();
return false;
}
if(m==null)
return false;
return m.matches();
}
}
@@ -0,0 +1,17 @@
package customskinloader.utils;
public class TimeUtil {
public static long getCurrentUnixTimestamp(){
return System.currentTimeMillis()/1000l;
}
public static long getUnixTimestamp(long offset){
return getCurrentUnixTimestamp()+offset;
}
public static long getUnixTimestampRandomDelay(long offset){
return getCurrentUnixTimestamp()+offset+random(0,5);
}
private static int random(int min,int max){
return (int)(Math.random()*(max-min+1))+min;
}
}