diff --git a/Common/source/customskinloader/CustomSkinLoader.java b/Common/source/customskinloader/CustomSkinLoader.java index ee37cbc..11ea147 100644 --- a/Common/source/customskinloader/CustomSkinLoader.java +++ b/Common/source/customskinloader/CustomSkinLoader.java @@ -7,6 +7,7 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; import com.google.common.collect.Maps; import com.google.gson.Gson; @@ -58,37 +59,48 @@ public class CustomSkinLoader { new LinkedBlockingQueue<>(333), customFactory, new ThreadPoolExecutor.DiscardOldestPolicy() ); + public static void loadProfileLazily(GameProfile gameProfile, Consumer> consumer) { + String username = gameProfile.getName(); + String credential = MinecraftUtil.getCredential(gameProfile); + // Fix: http://hopper.minecraft.net/crashes/minecraft/MCX-2773713 + if (username == null) { + logger.warning("Could not load profile: username is null."); + consumer.accept(Maps.newHashMap()); + return; + } + String tempName = Thread.currentThread().getName(); + Thread.currentThread().setName(username); // Change Thread Name + if (profileCache.isLoading(credential)) { + profileCache.putLoader(credential, consumer); + Thread.currentThread().setName(tempName); + return; + } + consumer.accept(loadProfile(gameProfile)); + Thread.currentThread().setName(tempName); + profileCache.getLastLoader(credential).ifPresent(c -> loadProfileLazily(gameProfile, c)); + } + //For User Skin public static Map loadProfile(GameProfile gameProfile){ - String username=gameProfile.getName(); - String credential=MinecraftUtil.getCredential(gameProfile); - //Fix: http://hopper.minecraft.net/crashes/minecraft/MCX-2773713 - if(username==null){ - logger.warning("Could not load profile: username is null."); - return Maps.newHashMap(); - } - String tempName=Thread.currentThread().getName(); - Thread.currentThread().setName(username);//Change Thread Name + String credential = MinecraftUtil.getCredential(gameProfile); UserProfile profile; - if(profileCache.isReady(credential)){ + if (profileCache.isReady(credential)) { logger.info("Cached profile will be used."); - profile=profileCache.getProfile(credential); - if(profile==null){ - logger.warning("(!Cached Profile is empty!) Expiry:"+profileCache.getExpiry(credential)); - if(profileCache.isExpired(credential))//force load - profile=loadProfile0(gameProfile); - } - else + profile = profileCache.getProfile(credential); + if (profile == null) { + logger.warning("(Cached Profile is empty!) Expiry: " + profileCache.getExpiry(credential)); + if (profileCache.isExpired(credential)) // force load + profile = loadProfile0(gameProfile, false); + } else logger.info(profile.toString(profileCache.getExpiry(credential))); - }else{ + } else { profileCache.setLoading(credential, true); - profile=loadProfile0(gameProfile); + profile = loadProfile0(gameProfile, false); } - Thread.currentThread().setName(tempName); return ModelManager0.fromUserProfile(profile); } //Core - public static UserProfile loadProfile0(GameProfile gameProfile){ + public static UserProfile loadProfile0(GameProfile gameProfile, boolean isSkull){ String username=gameProfile.getName(); String credential=MinecraftUtil.getCredential(gameProfile); @@ -121,11 +133,11 @@ public class CustomSkinLoader { } if(profile==null) continue; - if(!config.forceLoadAllTextures){ - profile0=profile; - break; - } profile0.mix(profile); + if(isSkull&&!profile0.hasSkinUrl()) + continue; + if(!config.forceLoadAllTextures) + break; if(profile0.isFull()) break; } @@ -172,7 +184,7 @@ public class CustomSkinLoader { Runnable loadThread = () -> { String tempName = Thread.currentThread().getName(); Thread.currentThread().setName(username + "'s skull"); - loadProfile0(gameProfile);//Load in thread + loadProfile0(gameProfile, true);//Load in thread Thread.currentThread().setName(tempName); }; if (config.forceUpdateSkull) { diff --git a/Common/source/customskinloader/fake/FakeCapeBuffer.java b/Common/source/customskinloader/fake/FakeCapeBuffer.java index 04723ec..235c484 100644 --- a/Common/source/customskinloader/fake/FakeCapeBuffer.java +++ b/Common/source/customskinloader/fake/FakeCapeBuffer.java @@ -97,12 +97,10 @@ public class FakeCapeBuffer extends FakeSkinBuffer { if (capeW < elytraW) { this.image = scaleImage(this.image, true, elytraW / (double) capeW, 1, capeW / 64.0D, capeH / 32.0D, elytraW, capeH, 0, 0, 22, 17); capeW = elytraW; - this.ratioX = capeW / 64.0D; } if (capeH < elytraH) { this.image = scaleImage(this.image, true, 1, elytraH / (double) capeH, capeW / 64.0D, capeH / 32.0D, capeW, elytraH, 0, 0, 22, 17); capeH = elytraH; - this.ratioY = capeH / 32.0D; } // elytra part ((22, 0), (45, 21)) -> (24 * 22) if (elytraW < capeW) { @@ -113,6 +111,8 @@ public class FakeCapeBuffer extends FakeSkinBuffer { elytraImage = scaleImage(elytraImage, false, 1, capeH / (double) elytraH, elytraW / 64.0D, elytraH / 32.0D, elytraW, capeH, 22, 0, 46, 22); elytraH = capeH; } + this.ratioX = capeW / 64.0D; + this.ratioY = capeH / 32.0D; // Overwrite pixels from elytra to cape FakeImage finalElytraImage = elytraImage; diff --git a/Common/source/customskinloader/fake/FakeSkinManager.java b/Common/source/customskinloader/fake/FakeSkinManager.java index e1d89f2..b6a7d48 100644 --- a/Common/source/customskinloader/fake/FakeSkinManager.java +++ b/Common/source/customskinloader/fake/FakeSkinManager.java @@ -13,21 +13,19 @@ import com.google.common.collect.Maps; import com.google.common.hash.Hashing; import com.mojang.authlib.GameProfile; import com.mojang.authlib.minecraft.MinecraftProfileTexture; -import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type; import com.mojang.authlib.minecraft.MinecraftSessionService; import customskinloader.CustomSkinLoader; import customskinloader.fake.itf.IFakeMinecraft; import customskinloader.fake.itf.IFakeTextureManager_1; import customskinloader.fake.itf.IFakeTextureManager_2; import customskinloader.utils.HttpTextureUtil; -import customskinloader.utils.HttpTextureUtil.HttpTextureInfo; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.IImageBuffer; import net.minecraft.client.renderer.texture.SimpleTexture; import net.minecraft.client.renderer.texture.Texture; import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.client.resources.DefaultPlayerSkin; -import net.minecraft.client.resources.SkinManager.SkinAvailableCallback; +import net.minecraft.client.resources.SkinManager; import net.minecraft.util.ResourceLocation; public class FakeSkinManager { @@ -41,15 +39,15 @@ public class FakeSkinManager { HttpTextureUtil.defaultCacheDir = skinCacheDirectory; } - public ResourceLocation loadSkin(MinecraftProfileTexture profileTexture, Type textureType) { + public ResourceLocation loadSkin(MinecraftProfileTexture profileTexture, MinecraftProfileTexture.Type textureType) { return this.loadSkin(profileTexture, textureType, null); } - public ResourceLocation loadSkin(final MinecraftProfileTexture profileTexture, final Type textureType, final SkinAvailableCallback skinAvailableCallback) { + public ResourceLocation loadSkin(final MinecraftProfileTexture profileTexture, final MinecraftProfileTexture.Type textureType, final SkinManager.SkinAvailableCallback skinAvailableCallback) { return this.loadSkin(profileTexture, HttpTextureUtil.toHttpTextureInfo(profileTexture.getUrl()), textureType, skinAvailableCallback); } - private ResourceLocation loadSkin(final MinecraftProfileTexture profileTexture, final HttpTextureInfo info, final Type textureType, final SkinAvailableCallback skinAvailableCallback) { + private ResourceLocation loadSkin(final MinecraftProfileTexture profileTexture, final HttpTextureUtil.HttpTextureInfo info, final MinecraftProfileTexture.Type textureType, final SkinManager.SkinAvailableCallback skinAvailableCallback) { final ResourceLocation resourcelocation = new ResourceLocation("skins/" + Hashing.sha1().hashUnencodedChars(info.hash).toString()); if (((IFakeTextureManager_1) this.textureManager).getTexture(resourcelocation, null) != null) {//Have already loaded @@ -63,15 +61,15 @@ public class FakeSkinManager { return resourcelocation; } - public void loadProfileTextures(final GameProfile profile, final SkinAvailableCallback skinAvailableCallback, final boolean requireSecure) { - THREAD_POOL.execute(() -> { - final Map map = Maps.newHashMap(); - map.putAll(CustomSkinLoader.loadProfile(profile)); + public void loadProfileTextures(final GameProfile profile, final SkinManager.SkinAvailableCallback skinAvailableCallback, final boolean requireSecure) { + THREAD_POOL.execute(() -> CustomSkinLoader.loadProfileLazily(profile, m -> { + final Map map = Maps.newHashMap(); + map.putAll(m); for (MinecraftProfileTexture.Type type : MinecraftProfileTexture.Type.values()) { MinecraftProfileTexture profileTexture = map.get(type); if (profileTexture != null) { - HttpTextureInfo info = HttpTextureUtil.toHttpTextureInfo(profileTexture.getUrl()); + HttpTextureUtil.HttpTextureInfo info = HttpTextureUtil.toHttpTextureInfo(profileTexture.getUrl()); FakeThreadDownloadImageData.downloadTexture(info.cacheFile, info.url); ((IFakeMinecraft) Minecraft.getMinecraft()).execute(() -> { @@ -84,14 +82,14 @@ public class FakeSkinManager { }); } } - }); + })); } - public Map loadSkinFromCache(GameProfile profile) { + public Map loadSkinFromCache(GameProfile profile) { return CustomSkinLoader.loadProfileFromCache(profile); } - private static void makeCallback(SkinAvailableCallback callback, Type type, ResourceLocation location, MinecraftProfileTexture texture) { + private static void makeCallback(SkinManager.SkinAvailableCallback callback, MinecraftProfileTexture.Type type, ResourceLocation location, MinecraftProfileTexture texture) { if (callback != null) callback.skinAvailable(type, location, texture); } @@ -99,12 +97,12 @@ public class FakeSkinManager { private class BaseBuffer implements IImageBuffer { private IImageBuffer buffer; - private SkinAvailableCallback callback; - private Type type; + private SkinManager.SkinAvailableCallback callback; + private MinecraftProfileTexture.Type type; private ResourceLocation location; private MinecraftProfileTexture texture; - public BaseBuffer(SkinAvailableCallback callback, Type type, ResourceLocation location, MinecraftProfileTexture texture) { + public BaseBuffer(SkinManager.SkinAvailableCallback callback, MinecraftProfileTexture.Type type, ResourceLocation location, MinecraftProfileTexture texture) { switch (type) { case SKIN: this.buffer = new FakeSkinBuffer(); break; case CAPE: this.buffer = new FakeCapeBuffer(location); break; diff --git a/Common/source/customskinloader/profile/ProfileCache.java b/Common/source/customskinloader/profile/ProfileCache.java index d357898..605bf34 100644 --- a/Common/source/customskinloader/profile/ProfileCache.java +++ b/Common/source/customskinloader/profile/ProfileCache.java @@ -1,20 +1,24 @@ package customskinloader.profile; import java.io.File; -import java.util.HashMap; +import java.util.Deque; import java.util.Map; +import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.function.Consumer; -import org.apache.commons.io.FileUtils; - +import com.mojang.authlib.minecraft.MinecraftProfileTexture; import customskinloader.CustomSkinLoader; import customskinloader.utils.TimeUtil; +import org.apache.commons.io.FileUtils; public class ProfileCache { public static File PROFILE_CACHE_DIR=new File(CustomSkinLoader.DATA_DIR,"ProfileCache"); private Map cachedProfiles = new ConcurrentHashMap<>(); private Map localProfiles = new ConcurrentHashMap<>(); + private Map>>> profileLoaders = new ConcurrentHashMap<>(); @SuppressWarnings("ResultOfMethodCallIgnored") public ProfileCache(){ @@ -49,6 +53,13 @@ public class ProfileCache { return localProfiles.get(username.toLowerCase()); return loadLocalProfile(username); } + public Optional>> getLastLoader(String username) { + Deque>> deque = this.profileLoaders.get(username); + if (deque != null) { + return Optional.ofNullable(deque.pollLast()); + } + return Optional.empty(); + } public void setLoading(String username,boolean loading){ getCachedProfile(username).loading=loading; @@ -64,6 +75,10 @@ public class ProfileCache { return; saveLocalProfile(username,profile); } + public void putLoader(String username, Consumer> loader) { + this.profileLoaders.putIfAbsent(username, new ConcurrentLinkedDeque<>()); + this.profileLoaders.get(username).offerLast(loader); + } private CachedProfile getCachedProfile(String username){ CachedProfile cp=cachedProfiles.get(username.toLowerCase());