From 51aec265bee5a8149f9b0ba357c79556882440ce Mon Sep 17 00:00:00 2001 From: 33 Date: Fri, 29 Jul 2022 01:14:50 +0800 Subject: [PATCH] Tweak: LegacyLoader UUID fallback logic --- .../customskinloader/CustomSkinLoader.java | 2 +- .../customskinloader/loader/LegacyLoader.java | 186 ++++++++++-------- 2 files changed, 110 insertions(+), 78 deletions(-) diff --git a/Common/source/customskinloader/CustomSkinLoader.java b/Common/source/customskinloader/CustomSkinLoader.java index c2da567..15cfc4d 100644 --- a/Common/source/customskinloader/CustomSkinLoader.java +++ b/Common/source/customskinloader/CustomSkinLoader.java @@ -196,7 +196,7 @@ public class CustomSkinLoader { //CustomSkinLoader needs username to load standard skin, if username not exist, only textures in NBT can be used //Authlib 3.11.50 makes empty username to "\u0020" - if (username == null || username.equals("\u0020") || credential == null) { + if (username == null || username.isEmpty() || username.equals("\u0020") || credential == null) { return dynamicSkullManager.getTexture(gameProfile); } if (config.forceUpdateSkull ? profileCache.isReady(credential) : profileCache.isExist(credential)) { diff --git a/Common/source/customskinloader/loader/LegacyLoader.java b/Common/source/customskinloader/loader/LegacyLoader.java index 87a0ed2..6e78880 100644 --- a/Common/source/customskinloader/loader/LegacyLoader.java +++ b/Common/source/customskinloader/loader/LegacyLoader.java @@ -1,21 +1,19 @@ package customskinloader.loader; import java.io.File; +import java.util.Collections; import java.util.List; -import javax.annotation.Nonnull; +import java.util.Objects; +import java.util.function.Consumer; -import com.google.common.collect.Lists; import com.mojang.authlib.GameProfile; -import com.mojang.util.UUIDTypeAdapter; import customskinloader.CustomSkinLoader; import customskinloader.config.SkinSiteProfile; import customskinloader.plugin.ICustomSkinLoaderPlugin; -import customskinloader.profile.ModelManager0; import customskinloader.profile.UserProfile; import customskinloader.utils.HttpRequestUtil; import customskinloader.utils.HttpTextureUtil; import customskinloader.utils.HttpUtil0; -import org.apache.commons.lang3.StringUtils; public class LegacyLoader implements ICustomSkinLoaderPlugin, ProfileLoader.IProfileLoader { @@ -26,7 +24,7 @@ public class LegacyLoader implements ICustomSkinLoaderPlugin, ProfileLoader.IPro @Override public List getDefaultProfiles() { - return Lists.newArrayList(new LocalSkin(this)); + return Collections.singletonList(new LocalSkin(this)); } public abstract static class DefaultProfile implements ICustomSkinLoaderPlugin.IDefaultProfile { @@ -39,25 +37,61 @@ public class LegacyLoader implements ICustomSkinLoaderPlugin, ProfileLoader.IPro @Override public void updateSkinSiteProfile(SkinSiteProfile ssp) { ssp.type = this.loader.getName(); - if (ssp.checkPNG == null) ssp.checkPNG = false; - if (ssp.model == null) ssp.model = "auto"; - if (ssp.skin == null || !HttpUtil0.isLocal(this.getSkinRoot())) ssp.skin = this.getSkinRoot(); - if (ssp.cape == null || !HttpUtil0.isLocal(this.getCapeRoot())) ssp.cape = this.getCapeRoot(); - if (ssp.elytra == null || !HttpUtil0.isLocal(this.getElytraRoot())) ssp.elytra = this.getElytraRoot(); + if (ssp.checkPNG == null) { + ssp.checkPNG = false; + } + if (ssp.model == null) { + ssp.model = "auto"; + } + //Set texture url when it is empty + //Remote texture url could be changed, it will be auto updated here + if (ssp.skin == null || !HttpUtil0.isLocal(this.getSkinRoot())) { + ssp.skin = this.getSkinRoot(); + } + if (ssp.cape == null || !HttpUtil0.isLocal(this.getCapeRoot())) { + ssp.cape = this.getCapeRoot(); + } + if (ssp.elytra == null || !HttpUtil0.isLocal(this.getElytraRoot())) { + ssp.elytra = this.getElytraRoot(); + } } public abstract String getSkinRoot(); + public abstract String getCapeRoot(); + public abstract String getElytraRoot(); } public static class LocalSkin extends LegacyLoader.DefaultProfile { - public LocalSkin(LegacyLoader loader) { super(loader); } - @Override public String getName() { return "LocalSkin"; } - @Override public int getPriority() { return 600; } - @Override public String getSkinRoot() { return "LocalSkin/skins/{USERNAME}.png"; } - @Override public String getCapeRoot() { return "LocalSkin/capes/{USERNAME}.png"; } - @Override public String getElytraRoot() { return "LocalSkin/elytras/{USERNAME}.png"; } + public LocalSkin(LegacyLoader loader) { + super(loader); + } + + @Override + public String getName() { + return "LocalSkin"; + } + + @Override + public int getPriority() { + return 600; + } + + @Override + public String getSkinRoot() { + return "LocalSkin/skins/{USERNAME}.png"; + } + + @Override + public String getCapeRoot() { + return "LocalSkin/capes/{USERNAME}.png"; + } + + @Override + public String getElytraRoot() { + return "LocalSkin/elytras/{USERNAME}.png"; + } } // // Minecrack could not load skin correctly @@ -74,56 +108,56 @@ public class LegacyLoader implements ICustomSkinLoaderPlugin, ProfileLoader.IPro public static final String UUID_PLACEHOLDER = "{UUID}"; @Override - public UserProfile loadProfile(SkinSiteProfile ssp, GameProfile gameProfile) throws Exception { - String username = gameProfile.getName(); + public UserProfile loadProfile(SkinSiteProfile ssp, GameProfile gameProfile) { UserProfile profile = new UserProfile(); - if (StringUtils.isNoneEmpty(ssp.skin)) { - String skin = expandURL(ssp.skin, username); - if (HttpUtil0.isLocal(ssp.skin)) { - File skinFile = new File(CustomSkinLoader.DATA_DIR, skin); - if (skinFile.exists() && skinFile.isFile()) - profile.skinUrl = HttpTextureUtil.getLocalLegacyFakeUrl(skin, HttpTextureUtil.getHash(skin, skinFile.length(), skinFile.lastModified())); - } else { - HttpRequestUtil.HttpResponce responce = HttpRequestUtil.makeHttpRequest(new HttpRequestUtil.HttpRequest(skin).setUserAgent(ssp.userAgent).setCheckPNG(ssp.checkPNG != null && ssp.checkPNG).setLoadContent(false).setCacheTime(90)); - if (responce.success) - profile.skinUrl = HttpTextureUtil.getLegacyFakeUrl(skin); - } - profile.model = profile.hasSkinUrl() ? ssp.model : null; - } - if (StringUtils.isNoneEmpty(ssp.cape)) { - String cape = expandURL(ssp.cape, username); - if (HttpUtil0.isLocal(ssp.cape)) { - File capeFile = new File(CustomSkinLoader.DATA_DIR, cape); - if (capeFile.exists() && capeFile.isFile()) - profile.capeUrl = HttpTextureUtil.getLocalLegacyFakeUrl(cape, HttpTextureUtil.getHash(cape, capeFile.length(), capeFile.lastModified())); - } else { - HttpRequestUtil.HttpResponce responce = HttpRequestUtil.makeHttpRequest(new HttpRequestUtil.HttpRequest(cape).setUserAgent(ssp.userAgent).setCheckPNG(ssp.checkPNG != null && ssp.checkPNG).setLoadContent(false).setCacheTime(90)); - if (responce.success) - profile.capeUrl = HttpTextureUtil.getLegacyFakeUrl(cape); - } - } - if (ModelManager0.isElytraSupported() && StringUtils.isNoneEmpty(ssp.elytra)) { - String elytra = expandURL(ssp.elytra, username); - if (HttpUtil0.isLocal(ssp.elytra)) { - File elytraFile = new File(CustomSkinLoader.DATA_DIR, elytra); - if (elytraFile.exists() && elytraFile.isFile()) - profile.elytraUrl = HttpTextureUtil.getLocalLegacyFakeUrl(elytra, HttpTextureUtil.getHash(elytra, elytraFile.length(), elytraFile.lastModified())); - } else { - HttpRequestUtil.HttpResponce responce = HttpRequestUtil.makeHttpRequest(new HttpRequestUtil.HttpRequest(elytra).setUserAgent(ssp.userAgent).setCheckPNG(ssp.checkPNG != null && ssp.checkPNG).setLoadContent(false).setCacheTime(90)); - if (responce.success) - profile.elytraUrl = HttpTextureUtil.getLegacyFakeUrl(elytra); - } - } + //Try to load all textures + getTextureUrl(ssp, gameProfile, ssp.skin, it -> { + profile.skinUrl = it; + profile.model = ssp.model; + }); + getTextureUrl(ssp, gameProfile, ssp.cape, it -> profile.capeUrl = it); + getTextureUrl(ssp, gameProfile, ssp.elytra, it -> profile.elytraUrl = it); + if (profile.isEmpty()) { - CustomSkinLoader.logger.info("Both skin and cape not found."); + CustomSkinLoader.logger.info("No texture could be found."); return null; } return profile; } + private void getTextureUrl( + SkinSiteProfile ssp, GameProfile gameProfile, String baseUrl, Consumer onSuccess) { + //Base URL is empty + if (baseUrl == null || baseUrl.isEmpty()) { + return; + } + String url = expandURL(baseUrl, gameProfile.getName()); + //No texture can be loaded + if (url == null) { + return; + } + //Local texture file logic + if (HttpUtil0.isLocal(url)) { + File file = new File(CustomSkinLoader.DATA_DIR, url); + if (file.exists() && file.isFile()) { + String fakeUrl = HttpTextureUtil.getLocalLegacyFakeUrl(url, + HttpTextureUtil.getHash(url, file.length(), file.lastModified())); + onSuccess.accept(fakeUrl); + } + return; + } + //Remote texture logic + HttpRequestUtil.HttpResponce responce = HttpRequestUtil.makeHttpRequest(new HttpRequestUtil.HttpRequest(url) + .setUserAgent(ssp.userAgent).setCheckPNG(ssp.checkPNG != null && ssp.checkPNG).setLoadContent(false)); + if (responce.success) { + onSuccess.accept(HttpTextureUtil.getLegacyFakeUrl(url)); + } + } + @Override public boolean compare(SkinSiteProfile ssp0, SkinSiteProfile ssp1) { - return (!StringUtils.isNoneEmpty(ssp0.skin) || ssp0.skin.equalsIgnoreCase(ssp1.skin)) || (!StringUtils.isNoneEmpty(ssp0.cape) || ssp0.cape.equalsIgnoreCase(ssp1.cape)); + return Objects.equals(ssp0.skin, ssp1.skin) && Objects.equals(ssp0.cape, ssp1.cape) && + Objects.equals(ssp0.elytra, ssp1.elytra); } @Override @@ -133,36 +167,34 @@ public class LegacyLoader implements ICustomSkinLoaderPlugin, ProfileLoader.IPro @Override public void init(SkinSiteProfile ssp) { - if (HttpUtil0.isLocal(ssp.skin)) - initFolder(ssp.skin); - if (HttpUtil0.isLocal(ssp.cape)) - initFolder(ssp.cape); - if (HttpUtil0.isLocal(ssp.elytra)) - initFolder(ssp.elytra); + initFolder(ssp.skin); + initFolder(ssp.cape); + initFolder(ssp.elytra); } @SuppressWarnings("ResultOfMethodCallIgnored") private void initFolder(String target) { + //Only local skin should init folder + if (!HttpUtil0.isLocal(target)) { + return; + } String file = target.replace(USERNAME_PLACEHOLDER, "init"); File folder = new File(CustomSkinLoader.DATA_DIR, file).getParentFile(); - if (folder != null && !folder.exists()) + if (folder != null && !folder.exists()) { folder.mkdirs(); + } } private String expandURL(String url, String username) { String t = url.replace(USERNAME_PLACEHOLDER, username); - if (t.contains(UUID_PLACEHOLDER)) - t = t.replace(UUID_PLACEHOLDER, getMojangUUID(username)); - return t; - } - - @Nonnull - private String getMojangUUID(String username) { - GameProfile profile = MojangAPILoader.loadGameProfile(MojangAPILoader.getMojangApiRoot(), username); - if (profile == null) { - CustomSkinLoader.logger.info("UUID for %s not found.", username); - return "{ABORT}"; + if (!t.contains(UUID_PLACEHOLDER)) { + return t; } - return UUIDTypeAdapter.fromUUID(profile.getId()); + String uuid = MojangAPILoader.getMojangUuidByUsername(username); + //If Mojang uuid not found, won't load the texture + if (uuid == null) { + return null; + } + return t.replace(UUID_PLACEHOLDER, uuid); } }