From 68615ba724a5bd304d0de6f495f4874c42d2a755 Mon Sep 17 00:00:00 2001 From: Alexander Xia Date: Wed, 18 Sep 2019 22:15:58 +0800 Subject: [PATCH] Add custom Yggdrasil root --- .../customskinloader/config/Config.java | 2 +- .../config/SkinSiteProfile.java | 62 ++++++----- .../fake/FakeClientPlayer.java | 12 +-- .../loader/JsonAPILoader.java | 2 +- .../customskinloader/loader/LegacyLoader.java | 2 +- .../loader/MojangAPILoader.java | 102 ++++++++++-------- .../loader/ProfileLoader.java | 2 +- .../customskinloader/utils/MinecraftUtil.java | 94 +++++++++------- build.properties | 4 +- 9 files changed, 161 insertions(+), 121 deletions(-) diff --git a/Common/source/customskinloader/config/Config.java b/Common/source/customskinloader/config/Config.java index 659b727..427a1e9 100644 --- a/Common/source/customskinloader/config/Config.java +++ b/Common/source/customskinloader/config/Config.java @@ -168,7 +168,7 @@ public class Config { ProfileLoader.IProfileLoader loader=ProfileLoader.LOADERS.get(ssp.type.toLowerCase()); if(loader==null) continue; - loader.initLocalFolder(ssp); + loader.init(ssp); } } diff --git a/Common/source/customskinloader/config/SkinSiteProfile.java b/Common/source/customskinloader/config/SkinSiteProfile.java index 1fd0790..a717def 100644 --- a/Common/source/customskinloader/config/SkinSiteProfile.java +++ b/Common/source/customskinloader/config/SkinSiteProfile.java @@ -1,36 +1,45 @@ package customskinloader.config; public class SkinSiteProfile { + //Common public String name; public String type; public String userAgent; - + + //Mojang API - invisible in config + public String apiRoot; + public String seesionRoot; + + //Json API & Mojang API public String root; - + + //Legacy public Boolean checkPNG;//Not suitable for local skin public String skin; public String model; public String cape; public String elytra; - - public static SkinSiteProfile createMojangAPI(String name){ - SkinSiteProfile ssp=new SkinSiteProfile(); - ssp.name=name; - ssp.type="MojangAPI"; + + public static SkinSiteProfile createMojangAPI(String name) { + SkinSiteProfile ssp = new SkinSiteProfile(); + ssp.name = name; + ssp.type = "MojangAPI"; return ssp; } - public static SkinSiteProfile createCustomSkinAPI(String name,String root){ - SkinSiteProfile ssp=new SkinSiteProfile(); - ssp.name=name; - ssp.type="CustomSkinAPI"; - ssp.root=root; + + public static SkinSiteProfile createCustomSkinAPI(String name, String root) { + SkinSiteProfile ssp = new SkinSiteProfile(); + ssp.name = name; + ssp.type = "CustomSkinAPI"; + ssp.root = root; return ssp; } - public static SkinSiteProfile creatCustomSkinAPIPlus(String name,String root){ - SkinSiteProfile ssp=new SkinSiteProfile(); - ssp.name=name; - ssp.type="CustomSkinAPIPlus"; - ssp.root=root; + + public static SkinSiteProfile creatCustomSkinAPIPlus(String name, String root) { + SkinSiteProfile ssp = new SkinSiteProfile(); + ssp.name = name; + ssp.type = "CustomSkinAPIPlus"; + ssp.root = root; return ssp; } @@ -49,15 +58,16 @@ public class SkinSiteProfile { ssp.root = root; return ssp; } - public static SkinSiteProfile createLegacy(String name,String skin,String cape,String elytra){ - SkinSiteProfile ssp=new SkinSiteProfile(); - ssp.name=name; - ssp.type="Legacy"; - ssp.checkPNG=false; - ssp.skin=skin; - ssp.model="auto"; - ssp.cape=cape; - ssp.elytra=elytra; + + public static SkinSiteProfile createLegacy(String name, String skin, String cape, String elytra) { + SkinSiteProfile ssp = new SkinSiteProfile(); + ssp.name = name; + ssp.type = "Legacy"; + ssp.checkPNG = false; + ssp.skin = skin; + ssp.model = "auto"; + ssp.cape = cape; + ssp.elytra = elytra; return ssp; } diff --git a/Common/source/customskinloader/fake/FakeClientPlayer.java b/Common/source/customskinloader/fake/FakeClientPlayer.java index 0032ce6..f24d61e 100644 --- a/Common/source/customskinloader/fake/FakeClientPlayer.java +++ b/Common/source/customskinloader/fake/FakeClientPlayer.java @@ -9,7 +9,7 @@ import com.mojang.authlib.minecraft.MinecraftProfileTexture; import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type; import customskinloader.CustomSkinLoader; -import net.minecraft.client.Minecraft; +import customskinloader.utils.MinecraftUtil; import net.minecraft.client.renderer.ThreadDownloadImageData; import net.minecraft.client.renderer.texture.ITextureObject; import net.minecraft.client.renderer.texture.SimpleTexture; @@ -24,12 +24,12 @@ public class FakeClientPlayer { //For Legacy Skin public static ThreadDownloadImageData getDownloadImageSkin(ResourceLocation resourceLocationIn, String username) { //CustomSkinLoader.logger.debug("FakeClientPlayer/getDownloadImageSkin "+username); - TextureManager textman = Minecraft.getMinecraft().getTextureManager(); + TextureManager textman = MinecraftUtil.getTextureManager(); ITextureObject ito = textman.getTexture(resourceLocationIn); if (ito == null || !(ito instanceof ThreadDownloadImageData)) { //if Legacy Skin for username not loaded yet - SkinManager skinman = Minecraft.getMinecraft().getSkinManager(); + SkinManager skinman = MinecraftUtil.getSkinManager(); UUID offlineUUID = getOfflineUUID(username); GameProfile offlineProfile = new GameProfile(offlineUUID, username); @@ -52,8 +52,8 @@ public class FakeClientPlayer { //CustomSkinLoader.logger.debug("FakeClientPlayer/getLocationSkin "+username); return new ResourceLocation("skins/legacy-" + StringUtils.stripControlCodes(username)); } - public static UUID getOfflineUUID(String username) - { + + public static UUID getOfflineUUID(String username) { return UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes()); } @@ -73,7 +73,7 @@ public class FakeClientPlayer { if (typeIn != Type.SKIN || loaded) return; - TextureManager textman = Minecraft.getMinecraft().getTextureManager(); + TextureManager textman = MinecraftUtil.getTextureManager(); ITextureObject ito = textman.getTexture(location); if (ito == null) ito = textureCache.get(location); diff --git a/Common/source/customskinloader/loader/JsonAPILoader.java b/Common/source/customskinloader/loader/JsonAPILoader.java index 0fb8434..4f30f56 100644 --- a/Common/source/customskinloader/loader/JsonAPILoader.java +++ b/Common/source/customskinloader/loader/JsonAPILoader.java @@ -96,7 +96,7 @@ public class JsonAPILoader implements ProfileLoader.IProfileLoader { } @SuppressWarnings("ResultOfMethodCallIgnored") @Override - public void initLocalFolder(SkinSiteProfile ssp) { + public void init(SkinSiteProfile ssp) { if(HttpUtil0.isLocal(ssp.root)){ File f=new File(ssp.root); if(!f.exists()) diff --git a/Common/source/customskinloader/loader/LegacyLoader.java b/Common/source/customskinloader/loader/LegacyLoader.java index 24015fe..e6984a4 100644 --- a/Common/source/customskinloader/loader/LegacyLoader.java +++ b/Common/source/customskinloader/loader/LegacyLoader.java @@ -77,7 +77,7 @@ public class LegacyLoader implements ProfileLoader.IProfileLoader { } @Override - public void initLocalFolder(SkinSiteProfile ssp) { + public void init(SkinSiteProfile ssp) { if(HttpUtil0.isLocal(ssp.skin)) initFolder(ssp.skin); if(HttpUtil0.isLocal(ssp.cape)) diff --git a/Common/source/customskinloader/loader/MojangAPILoader.java b/Common/source/customskinloader/loader/MojangAPILoader.java index 009433e..1a52b1f 100644 --- a/Common/source/customskinloader/loader/MojangAPILoader.java +++ b/Common/source/customskinloader/loader/MojangAPILoader.java @@ -1,5 +1,7 @@ package customskinloader.loader; +import java.util.Arrays; +import java.util.Collections; import java.util.Map; import java.util.UUID; @@ -31,91 +33,107 @@ import customskinloader.utils.MinecraftUtil; public class MojangAPILoader implements ProfileLoader.IProfileLoader { - public static final MinecraftSessionService defaultSessionService=MinecraftUtil.getSessionService(); @Override public UserProfile loadProfile(SkinSiteProfile ssp, GameProfile gameProfile) throws Exception { - Map map=getTextures(gameProfile); - if(!map.isEmpty()){ + Map map = getTextures(gameProfile); + if (!map.isEmpty()) { CustomSkinLoader.logger.info("Default profile will be used."); return ModelManager0.toUserProfile(map); } - String username=gameProfile.getName(); - GameProfile newGameProfile=loadGameProfile(username); - if(newGameProfile==null){ - CustomSkinLoader.logger.info("Profile not found.("+username+"'s profile not found.)"); + String username = gameProfile.getName(); + GameProfile newGameProfile = loadGameProfile(ssp.apiRoot, username); + if (newGameProfile == null) { + CustomSkinLoader.logger.info("Profile not found.(" + username + "'s profile not found.)"); return null; } - newGameProfile=fillGameProfile(newGameProfile); - map=getTextures(newGameProfile); - if(!map.isEmpty()){ + newGameProfile = fillGameProfile(ssp.seesionRoot, newGameProfile); + map = getTextures(newGameProfile); + if (!map.isEmpty()) { gameProfile.getProperties().putAll(newGameProfile.getProperties()); return ModelManager0.toUserProfile(map); } - CustomSkinLoader.logger.info("Profile not found.("+username+" doesn't have skin/cape.)"); + CustomSkinLoader.logger.info("Profile not found.(" + username + " doesn't have skin/cape.)"); return null; } - + //Username -> UUID - public static GameProfile loadGameProfile(String username) { - //Doc (http://wiki.vg/Mojang_API#Username_-.3E_UUID_at_time) - HttpResponce responce=HttpRequestUtil.makeHttpRequest(new HttpRequest("https://api.mojang.com/users/profiles/minecraft/"+username).setCacheTime(0)); - if(StringUtils.isEmpty(responce.content)) + public static GameProfile loadGameProfile(String apiRoot, String username) { + //Doc (https://wiki.vg/Mojang_API#Playernames_-.3E_UUIDs) + Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create(); + + HttpResponce responce = HttpRequestUtil.makeHttpRequest( + new HttpRequest(apiRoot + "profiles/minecraft") + .setCacheTime(-1).setPayload(gson.toJson(Collections.singletonList(username))) + ); + 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) + + + GameProfile[] profiles = gson.fromJson(responce.content, GameProfile[].class); + if (profiles.length == 0) return null; + GameProfile gameProfile = profiles[0]; + + if (gameProfile.getId() == null) return null; - return new GameProfile(gameProfile.getId(),gameProfile.getName()); + return new GameProfile(gameProfile.getId(), gameProfile.getName()); } + //UUID -> Profile - public static GameProfile fillGameProfile(GameProfile profile) { + public static GameProfile fillGameProfile(String sessionRoot, GameProfile profile) { //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/" + new HttpRequest(sessionRoot + "session/minecraft/profile/" + UUIDTypeAdapter.fromUUID(profile.getId())).setCacheTime(90)); - if(StringUtils.isEmpty(responce.content)) + if (StringUtils.isEmpty(responce.content)) return profile; - - Gson gson=new GsonBuilder() + + 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()); + 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) { - if(gameProfile==null) + + public static Map getTextures(GameProfile gameProfile) { + if (gameProfile == null) return Maps.newHashMap(); - Property textureProperty=Iterables.getFirst(gameProfile.getProperties().get("textures"), null); - if (textureProperty==null) + Property textureProperty = Iterables.getFirst(gameProfile.getProperties().get("textures"), null); + if (textureProperty == null) return Maps.newHashMap(); - String value=textureProperty.getValue(); - if(StringUtils.isBlank(value)) + String value = textureProperty.getValue(); + if (StringUtils.isBlank(value)) return Maps.newHashMap(); @SuppressWarnings("deprecation") String json = new String(Base64.decodeBase64(value), Charsets.UTF_8); - Gson gson=new GsonBuilder().registerTypeAdapter(UUID.class,new UUIDTypeAdapter()).create(); - MinecraftTexturesPayload result=gson.fromJson(json,MinecraftTexturesPayload.class); - if (result==null||result.getTextures()==null) + Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create(); + MinecraftTexturesPayload result = gson.fromJson(json, MinecraftTexturesPayload.class); + if (result == null || result.getTextures() == null) return Maps.newHashMap(); return result.getTextures(); } - + @Override public boolean compare(SkinSiteProfile ssp0, SkinSiteProfile ssp1) { return true; } + @Override public String getName() { return "MojangAPI"; } @Override - public void initLocalFolder(SkinSiteProfile ssp) { + public void init(SkinSiteProfile ssp) { + //Init api & session root for custom ygg root + if (ssp.root != null) { + ssp.apiRoot = ssp.root + "api/"; + ssp.seesionRoot = ssp.root + "sessionserver/"; + } else { + ssp.apiRoot = "https://api.mojang.com/"; + ssp.seesionRoot = "https://sessionserver.mojang.com/"; + } } } diff --git a/Common/source/customskinloader/loader/ProfileLoader.java b/Common/source/customskinloader/loader/ProfileLoader.java index 44268cc..d9be744 100644 --- a/Common/source/customskinloader/loader/ProfileLoader.java +++ b/Common/source/customskinloader/loader/ProfileLoader.java @@ -33,6 +33,6 @@ public class ProfileLoader { UserProfile loadProfile(SkinSiteProfile ssp,GameProfile gameProfile) throws Exception; boolean compare(SkinSiteProfile ssp0,SkinSiteProfile ssp1); String getName(); - void initLocalFolder(SkinSiteProfile ssp); + void init(SkinSiteProfile ssp); } } diff --git a/Common/source/customskinloader/utils/MinecraftUtil.java b/Common/source/customskinloader/utils/MinecraftUtil.java index b5c8f0b..e1ef2bf 100644 --- a/Common/source/customskinloader/utils/MinecraftUtil.java +++ b/Common/source/customskinloader/utils/MinecraftUtil.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; +import net.minecraft.client.resources.SkinManager; import org.apache.commons.lang3.StringUtils; import com.mojang.authlib.GameProfile; @@ -18,108 +19,119 @@ import net.minecraft.client.renderer.texture.TextureManager; /** * @author Alexander Xia * @since 13.6 - * */ public class MinecraftUtil { - public static File getMinecraftDataDir(){ + public static File getMinecraftDataDir() { return Minecraft.getMinecraft().gameDir; } - public static MinecraftSessionService getSessionService(){ - return Minecraft.getMinecraft().getSessionService(); - } - public static TextureManager getTextureManager(){ + + public static TextureManager getTextureManager() { return Minecraft.getMinecraft().getTextureManager(); } - - private static ArrayList minecraftVersion=new ArrayList(); - private static String minecraftMainVersion=null; + + public static SkinManager getSkinManager() { + return Minecraft.getMinecraft().getSkinManager(); + } + + + private static ArrayList minecraftVersion = new ArrayList(); + private static String minecraftMainVersion = null; private final static Pattern MINECRAFT_VERSION_PATTERN = Pattern.compile(".*?(\\d+\\.\\d+[\\.]?\\d*).*?"); - public static ArrayList getMinecraftVersions(){ - if(minecraftVersion!=null&&!minecraftVersion.isEmpty()) + + public static ArrayList getMinecraftVersions() { + if (minecraftVersion != null && !minecraftVersion.isEmpty()) return minecraftVersion; testProbe(); return minecraftVersion; } - public static String getMinecraftVersionText(){ - StringBuilder sb=new StringBuilder(); - for(String version:getMinecraftVersions()) + + public static String getMinecraftVersionText() { + StringBuilder sb = new StringBuilder(); + for (String version : getMinecraftVersions()) sb.append(version).append(" "); return StringUtils.trim(sb.toString()); } - public static String getMinecraftMainVersion(){ - if(minecraftMainVersion!=null) + + public static String getMinecraftMainVersion() { + if (minecraftMainVersion != null) return minecraftMainVersion; - for(String version:getMinecraftVersions()){ + for (String version : getMinecraftVersions()) { Matcher m = null; - try{ - m=MINECRAFT_VERSION_PATTERN.matcher(version); - }catch(Exception e){ + try { + m = MINECRAFT_VERSION_PATTERN.matcher(version); + } catch (Exception e) { e.printStackTrace(); } - if(m==null || !m.matches()) + if (m == null || !m.matches()) continue; - minecraftMainVersion=m.group(m.groupCount()); + minecraftMainVersion = m.group(m.groupCount()); break; } return minecraftMainVersion; } - + // (domain|ip)(:port) - public static String getServerAddress(){ + public static String getServerAddress() { net.minecraft.client.multiplayer.ServerData data = Minecraft.getMinecraft().getCurrentServerData(); - if(data==null)//Single Player + if (data == null)//Single Player return null; return data.serverIP; } + // ip:port - public static String getStandardServerAddress(){ + public static String getStandardServerAddress() { return HttpUtil0.parseAddress(getServerAddress()); } - public static boolean isLanServer(){ + + public static boolean isLanServer() { return HttpUtil0.isLanServer(getStandardServerAddress()); } - public static String getCurrentUsername(){ + + public static String getCurrentUsername() { return Minecraft.getMinecraft().getSession().getProfile().getName(); } - + private final static Pattern MINECRAFT_CORE_FILE_PATTERN = Pattern.compile("^(.*?)/versions/([^\\/\\\\]*?)/([^\\/\\\\]*?).jar$"); - private static void testProbe(){ + + private static void testProbe() { minecraftVersion.clear(); URL urls[] = JavaUtil.getClasspath(); - for(URL url:urls){ + for (URL url : urls) { Matcher m = null; - try{ - m=MINECRAFT_CORE_FILE_PATTERN.matcher(URLDecoder.decode(url.getPath(),"UTF-8")); - }catch(Exception e){ + try { + m = MINECRAFT_CORE_FILE_PATTERN.matcher(URLDecoder.decode(url.getPath(), "UTF-8")); + } catch (Exception e) { e.printStackTrace(); } - if(m==null || !m.matches()) + if (m == null || !m.matches()) continue; minecraftVersion.add(m.group(2)); } } - + public static boolean isCoreFile(URL url) { return regexMatch(url, MINECRAFT_CORE_FILE_PATTERN); } - + private final static Pattern LIBRARY_FILE_PATTERN = Pattern.compile("^(.*?)/libraries/(.*?)/([^\\/\\\\]*?).jar$"); + public static boolean isLibraryFile(URL url) { return regexMatch(url, LIBRARY_FILE_PATTERN); } + private static boolean regexMatch(URL url, Pattern p) { Matcher m; try { - m=p.matcher(URLDecoder.decode(url.getPath(), "UTF-8")); + m = p.matcher(URLDecoder.decode(url.getPath(), "UTF-8")); } catch (Exception e) { e.printStackTrace(); return false; } return m.matches(); } - + public static String getCredential(GameProfile profile) { - return (profile==null || profile.hashCode()==0) ? null : - (profile.getId()==null ? profile.getName() : String.format("%s-%s", profile.getName(),profile.getId())); + return (profile == null || profile.hashCode() == 0) ? null : + (profile.getId() == null ? profile.getName() : String.format("%s-%s", profile.getName(), profile.getId())); } } diff --git a/build.properties b/build.properties index 2c73bfc..15e5b30 100644 --- a/build.properties +++ b/build.properties @@ -1,6 +1,6 @@ name=CustomSkinLoader group=customskinloader -version=14.11 +version=14.12 dev_version= #RELEASE: snapshot=0 -snapshot=0 +snapshot=1