Get ready for new JsonAPI

This commit is contained in:
xfl03 committed 2016-08-09 17:23:27 +08:00
1 parent 8bccab4f0b
commit bbc355365a
8 files changed
+171 -169

No files matched your search

+8 -14
View File
@@ -19,11 +19,7 @@ import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type;
import customskinloader.config.Config;
import customskinloader.config.SkinSiteProfile;
import customskinloader.loader.CustomSkinAPILoader;
import customskinloader.loader.IProfileLoader;
import customskinloader.loader.LegacyLoader;
import customskinloader.loader.MojangAPILoader;
import customskinloader.loader.UniSkinAPILoader;
import customskinloader.loader.*;
import customskinloader.profile.ModelManager0;
import customskinloader.profile.ProfileCache;
import customskinloader.profile.UserProfile;
@@ -32,10 +28,10 @@ import customskinloader.utils.MinecraftUtil;
/**
* Custom skin loader mod for Minecraft.
* @author (C) Jeremy Lam [JLChnToZ] 2013 & Alexander Xia [xfl03] 2014-2016
* @version 14.2 (2016.8.5)
* @version 14.3 (2016.8.9)
*/
public class CustomSkinLoader {
public static final String CustomSkinLoader_VERSION="14.2";
public static final String CustomSkinLoader_VERSION="14.3";
public static final File DATA_DIR=new File(MinecraftUtil.getMinecraftDataDir0(),"CustomSkinLoader"),
LOG_FILE=new File(DATA_DIR,"CustomSkinLoader.log"),
CONFIG_FILE=new File(DATA_DIR,"CustomSkinLoader.json");
@@ -72,10 +68,8 @@ public class CustomSkinLoader {
profile=profileCache.getProfile(username);
if(profile==null){
logger.warning("(!Cached Profile is empty!) Expiry:"+profileCache.getExpiry(username));
if(profileCache.isExpired(username)){//force load
profileCache.setLoading(username, true);
if(profileCache.isExpired(username))//force load
profile=loadProfile0(gameProfile);
}
}
else
logger.info(profile.toString(profileCache.getExpiry(username)));
@@ -164,9 +158,9 @@ public class CustomSkinLoader {
private static HashMap<String, IProfileLoader> initLoaders() {
HashMap<String, IProfileLoader> loaders=new HashMap<String, IProfileLoader>();
loaders.put("mojangapi", new MojangAPILoader());
loaders.put("customskinapi", new CustomSkinAPILoader());
loaders.put("customskinapi", new JsonAPILoader(JsonAPILoader.Type.CustomSkinAPI));
loaders.put("legacy", new LegacyLoader());
loaders.put("uniskinapi", new UniSkinAPILoader());
loaders.put("uniskinapi", new JsonAPILoader(JsonAPILoader.Type.UniSkinAPI));
return loaders;
}
@@ -200,7 +194,7 @@ public class CustomSkinLoader {
logger.info("Successfully load config.");
return config;
}catch (Exception e) {
logger.info("Failed to load config, use default instead.("+e.getMessage()+")");
logger.info("Failed to load config, use default instead.("+e.toString()+")");
File brokenFile=new File(DATA_DIR,"BROKEN-CustomSkinLoader.json");
if(brokenFile.exists())
brokenFile.delete();
@@ -223,7 +217,7 @@ public class CustomSkinLoader {
IOUtils.write(json, new FileOutputStream(CONFIG_FILE));
logger.info("Successfully "+(update?"update":"create")+" config.");
} catch (Exception e) {
logger.info("Failed to "+(update?"update":"create")+" config.("+e.getMessage()+")");
logger.info("Failed to "+(update?"update":"create")+" config.("+e.toString()+")");
}
}
}
@@ -1,92 +0,0 @@
package customskinloader.loader;
import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.Set;
import org.apache.commons.io.IOUtils;
import com.google.gson.Gson;
import com.mojang.authlib.GameProfile;
import customskinloader.CustomSkinLoader;
import customskinloader.config.SkinSiteProfile;
import customskinloader.profile.ModelManager0;
import customskinloader.profile.UserProfile;
import customskinloader.utils.HttpTextureUtil;
import customskinloader.utils.HttpUtil0;
public class CustomSkinAPILoader implements IProfileLoader {
public static final String TEXTURES="textures/";
@Override
public UserProfile loadProfile(SkinSiteProfile ssp, GameProfile gameProfile, boolean local) throws Exception {
String username=gameProfile.getName();
if(ssp.root==null||ssp.root.equals("")){
CustomSkinLoader.logger.info("Root not defined.");
return null;
}
String jsonUrl=ssp.root+username+".json";
String json;
if(local){
File jsonFile=new File(jsonUrl);
if(!jsonFile.exists()){
CustomSkinLoader.logger.info("Profile File not found.");
return null;
}
json=IOUtils.toString(new FileInputStream(jsonFile));
}else{
json=HttpUtil0.readHttp(jsonUrl,ssp.userAgent);
}
if(json==null||json.equals("")){
CustomSkinLoader.logger.info("Profile not found.");
return null;
}
UserProfile p=toUserProfile(ssp.root,json,false);
if(p.isEmpty()){
CustomSkinLoader.logger.info("Both skin and cape not found.");
return null;
}else{
return p;
}
}
private class CustomSkinAPI{
public String username;
public Map<String,String> skins;
public String skin;
public String cape;
}
private UserProfile toUserProfile(String root,String json,boolean local){
CustomSkinAPI profile=CustomSkinLoader.GSON.fromJson(json, CustomSkinAPI.class);
UserProfile p=new UserProfile();
if(profile.skins!=null && !profile.skins.isEmpty()){
Set<String> keys=profile.skins.keySet();
for(String s:keys){
//System.out.println(s+" "+ModelManager0.checkModel(s));
if(ModelManager0.checkModel(s)){
if(profile.skins.get(s)==null||profile.skins.get(s).equals(""))
continue;
p.skinUrl=root+TEXTURES+profile.skins.get(s);
p.model=s;
break;
}
}
if(p.skinUrl==null){
p.skinUrl=root+TEXTURES+profile.skins.get(keys.toArray()[0]);
p.model="default";
}
}else if(profile.skin!=null && !profile.skin.equals("")){
p.skinUrl=root+TEXTURES+profile.skin;
if(local)
p.skinUrl=HttpTextureUtil.getLocalFakeUrl(p.skinUrl);
}
if(profile.cape!=null && !profile.cape.equals("")){
p.capeUrl=root+TEXTURES+profile.cape;
if(local)
p.capeUrl=HttpTextureUtil.getLocalFakeUrl(p.capeUrl);
}
return p;
}
}
@@ -0,0 +1,65 @@
package customskinloader.loader;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.io.IOUtils;
import com.mojang.authlib.GameProfile;
import customskinloader.CustomSkinLoader;
import customskinloader.config.SkinSiteProfile;
import customskinloader.loader.jsonapi.*;
import customskinloader.profile.ModelManager0;
import customskinloader.profile.UserProfile;
import customskinloader.utils.HttpTextureUtil;
import customskinloader.utils.HttpUtil0;
public class JsonAPILoader implements IProfileLoader {
public static final String TEXTURES="textures/";
public static enum Type{
CustomSkinAPI(new CustomSkinAPI()),UniSkinAPI(new UniSkinAPI());
public IJsonAPI jsonAPI;
private Type(IJsonAPI jsonAPI){
this.jsonAPI=jsonAPI;
}
}
private Type type;
public JsonAPILoader(Type type){
this.type=type;
}
@Override
public UserProfile loadProfile(SkinSiteProfile ssp, GameProfile gameProfile, boolean local) throws Exception {
String username=gameProfile.getName();
if(ssp.root==null||ssp.root.equals("")){
CustomSkinLoader.logger.info("Root not defined.");
return null;
}
String jsonUrl=type.jsonAPI.toJsonUrl(ssp.root, username);
String json;
if(local){
File jsonFile=new File(jsonUrl);
if(!jsonFile.exists()){
CustomSkinLoader.logger.info("Profile File not found.");
return null;
}
json=IOUtils.toString(new FileInputStream(jsonFile));
}else{
json=HttpUtil0.readHttp(jsonUrl,ssp.userAgent);
}
if(json==null||json.equals("")){
CustomSkinLoader.logger.info("Profile not found.");
return null;
}
UserProfile p=type.jsonAPI.toUserProfile(ssp.root, json, local);
if(p==null||p.isEmpty()){
CustomSkinLoader.logger.info("Both skin and cape not found.");
return null;
}else{
return p;
}
}
}
@@ -0,0 +1,59 @@
package customskinloader.loader.jsonapi;
import java.util.Map;
import java.util.Set;
import customskinloader.CustomSkinLoader;
import customskinloader.profile.ModelManager0;
import customskinloader.profile.UserProfile;
import customskinloader.utils.HttpTextureUtil;
public class CustomSkinAPI implements IJsonAPI {
private static final String TEXTURES="textures/";
private static final String SUFFIX=".json";
@Override
public String toJsonUrl(String root, String username) {
return new StringBuilder().append(root).append(username).append(SUFFIX).toString();
}
@Override
public UserProfile toUserProfile(String root, String json, boolean local) {
CustomSkinAPIProfile profile=CustomSkinLoader.GSON.fromJson(json, CustomSkinAPIProfile.class);
UserProfile p=new UserProfile();
if(profile.skins!=null && !profile.skins.isEmpty()){
Set<String> keys=profile.skins.keySet();
for(String s:keys){
//System.out.println(s+" "+ModelManager0.checkModel(s));
if(ModelManager0.checkModel(s)){
if(profile.skins.get(s)==null||profile.skins.get(s).equals(""))
continue;
p.skinUrl=root+TEXTURES+profile.skins.get(s);
p.model=s;
break;
}
}
if(p.skinUrl==null){
p.skinUrl=root+TEXTURES+profile.skins.get(keys.toArray()[0]);
p.model="default";
}
}else if(profile.skin!=null && !profile.skin.equals("")){
p.skinUrl=root+TEXTURES+profile.skin;
if(local)
p.skinUrl=HttpTextureUtil.getLocalFakeUrl(p.skinUrl);
}
if(profile.cape!=null && !profile.cape.equals("")){
p.capeUrl=root+TEXTURES+profile.cape;
if(local)
p.capeUrl=HttpTextureUtil.getLocalFakeUrl(p.capeUrl);
}
return p;
}
private class CustomSkinAPIProfile{
public String username;
public Map<String,String> skins;
public String skin;
public String cape;
}
}
@@ -0,0 +1,8 @@
package customskinloader.loader.jsonapi;
import customskinloader.profile.UserProfile;
public interface IJsonAPI {
public String toJsonUrl(String root,String username);
public UserProfile toUserProfile(String root,String json,boolean local);
}
@@ -1,73 +1,25 @@
package customskinloader.loader;
package customskinloader.loader.jsonapi;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import com.google.gson.Gson;
import com.mojang.authlib.GameProfile;
import customskinloader.CustomSkinLoader;
import customskinloader.config.SkinSiteProfile;
import customskinloader.profile.ModelManager0;
import customskinloader.profile.UserProfile;
import customskinloader.utils.HttpTextureUtil;
import customskinloader.utils.HttpUtil0;
public class UniSkinAPILoader implements IProfileLoader {
public static final String TEXTURES="textures/";
public class UniSkinAPI implements IJsonAPI {
private static final String TEXTURES="textures/";
private static final String SUFFIX=".json";
@Override
public UserProfile loadProfile(SkinSiteProfile ssp, GameProfile gameProfile, boolean local) throws Exception {
String username=gameProfile.getName();
if(ssp.root==null||ssp.root.equals("")){
CustomSkinLoader.logger.info("Root not defined.");
return null;
}
String jsonUrl=ssp.root+username+".json";
String json;
if(local){
File jsonFile=new File(jsonUrl);
if(!jsonFile.exists()){
CustomSkinLoader.logger.info("Profile File not found.");
return null;
}
json=IOUtils.toString(new FileInputStream(jsonFile));
}else{
json=HttpUtil0.readHttp(jsonUrl,ssp.userAgent);
}
if(json==null||json.equals("")){
CustomSkinLoader.logger.info("Profile not found.");
return null;
}
UserProfile p=toUserProfile(ssp.root,json,false);
if(p==null||p.isEmpty()){
CustomSkinLoader.logger.info("Both skin and cape not found.");
return null;
}else{
return p;
}
public String toJsonUrl(String root, String username) {
return new StringBuilder().append(root).append(username).append(SUFFIX).toString();
}
/**
* Json profile for UniSkinAPI
* Source Code: https://github.com/RecursiveG/UniSkinMod/blob/master/src/main/java/org/devinprogress/uniskinmod/UniSkinApiProfile.java#L18-L22
* @author RecursiveG
*/
private class UniSkinAPI{
public String player_name;
public long last_update;
public List<String> model_preference;
public Map<String,String> skins;
public String cape;
public int errno;
public String msg;
}
private UserProfile toUserProfile(String root,String json,boolean local){
UniSkinAPI profile=CustomSkinLoader.GSON.fromJson(json, UniSkinAPI.class);
@Override
public UserProfile toUserProfile(String root, String json, boolean local) {
UniSkinAPIProfile profile=CustomSkinLoader.GSON.fromJson(json, UniSkinAPIProfile.class);
if(profile.errno!=0){
CustomSkinLoader.logger.info("Error: "+profile.msg);
return null;
@@ -108,4 +60,20 @@ public class UniSkinAPILoader implements IProfileLoader {
}
return p;
}
/**
* Json profile for UniSkinAPI
* Source Code: https://github.com/RecursiveG/UniSkinMod/blob/master/src/main/java/org/devinprogress/uniskinmod/UniSkinApiProfile.java#L18-L22
* @author RecursiveG
*/
private class UniSkinAPIProfile{
public String player_name;
public long last_update;
public List<String> model_preference;
public Map<String,String> skins;
public String cape;
public int errno;
public String msg;
}
}
@@ -83,7 +83,7 @@ public class ProfileCache {
CustomSkinLoader.logger.info("Successfully load LocalProfile.");
return profile;
}catch(Exception e){
CustomSkinLoader.logger.info("Failed to load LocalProfile.("+e.getMessage()+")");
CustomSkinLoader.logger.info("Failed to load LocalProfile.("+e.toString()+")");
localProfiles.put(username.toLowerCase(), null);
}
return null;
@@ -98,7 +98,7 @@ public class ProfileCache {
IOUtils.write(json, new FileOutputStream(localProfile));
CustomSkinLoader.logger.info("Successfully save LocalProfile.");
} catch (Exception e) {
CustomSkinLoader.logger.info("Failed to save LocalProfile.("+e.getMessage()+")");
CustomSkinLoader.logger.info("Failed to save LocalProfile.("+e.toString()+")");
}
}
}
+2 -2
View File
@@ -30,7 +30,7 @@ public class HttpUtil0 {
InputStream is = c.getInputStream();
return IOUtils.toString(is, Charsets.UTF_8);
} catch (Exception e) {
CustomSkinLoader.logger.info("Failed to read (Exception: "+e.getMessage()+")");
CustomSkinLoader.logger.info("Failed to read (Exception: "+e.toString()+")");
return null;
} finally {
if(c!=null)
@@ -62,7 +62,7 @@ public class HttpUtil0 {
+ "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()+")");
CustomSkinLoader.logger.info("Failed to get fake url (Exception: "+e.toString()+")");
return null;
} finally{
if(c!=null)