diff --git a/Common/src/main/java/customskinloader/CustomSkinLoader.java b/Common/src/main/java/customskinloader/CustomSkinLoader.java index 97906c04..909a3edc 100644 --- a/Common/src/main/java/customskinloader/CustomSkinLoader.java +++ b/Common/src/main/java/customskinloader/CustomSkinLoader.java @@ -1,15 +1,11 @@ package customskinloader; import java.io.File; +import java.util.LinkedList; import java.util.Map; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.LinkedBlockingDeque; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; -import java.util.function.Function; import com.google.common.collect.ImmutableMap; import com.google.gson.Gson; @@ -25,7 +21,6 @@ import customskinloader.profile.ModelManager0; import customskinloader.profile.ProfileCache; import customskinloader.profile.UserProfile; -import customskinloader.utils.LIFOBlockingQueue; import customskinloader.utils.MinecraftUtil; import customskinloader.utils.TextureUtil; @@ -52,22 +47,7 @@ public class CustomSkinLoader { private static final ProfileCache profileCache = new ProfileCache(); private static final DynamicSkullManager dynamicSkullManager = new DynamicSkullManager(); - public static final ExecutorService THREAD_POOL = new ThreadPoolExecutor(config.threadPoolSize, config.threadPoolSize, 1L, TimeUnit.MINUTES, new LIFOBlockingQueue<>(new LinkedBlockingDeque<>())); - - //Correct thread name in thread pool - private static final ThreadFactory defaultFactory = Executors.defaultThreadFactory(); - private static final ThreadFactory customFactory = r -> { - Thread t = defaultFactory.newThread(r); - if (r instanceof Thread) { - t.setName(((Thread) r).getName()); - } - return t; - }; - //Thread pool will discard oldest task when queue reaches 333 tasks - private static final ThreadPoolExecutor threadPool = new ThreadPoolExecutor( - config.threadPoolSize, config.threadPoolSize, 1L, TimeUnit.MINUTES, - new LinkedBlockingQueue<>(333), customFactory, new ThreadPoolExecutor.DiscardOldestPolicy() - ); + public static final ExecutorService THREAD_POOL = Executors.newCachedThreadPool(); public static void loadProfileTextures(Runnable runnable) { THREAD_POOL.execute(runnable); @@ -117,10 +97,12 @@ public static UserProfile loadProfile0(GameProfile gameProfile, boolean isSkull) return null; } + int size = config.loadlist.size(); + LinkedList> profileGetters = new LinkedList<>(); UserProfile profile0 = new UserProfile(); - for (int i = 0; i < config.loadlist.size(); i++) { + for (int i = 0; i < size; i++) { SkinSiteProfile ssp = config.loadlist.get(i); - logger.info((i + 1) + "/" + config.loadlist.size() + " Try to load profile from '" + ssp.name + "'."); + logger.info((i + 1) + "/" + size + " Try to load profile from '" + ssp.name + "'."); if (ssp.type == null) { logger.info("The type of '" + ssp.name + "' is null."); continue; @@ -130,17 +112,27 @@ public static UserProfile loadProfile0(GameProfile gameProfile, boolean isSkull) logger.info("Type '" + ssp.type + "' is not defined."); continue; } - UserProfile profile = null; - try { - profile = loader.loadProfile(ssp, gameProfile); - } catch (Exception e) { - logger.warning("Exception occurs while loading."); - logger.warning(e); - if (e.getCause() != null) { - logger.warning("Caused By:"); - logger.warning(e.getCause()); + profileGetters.add(CompletableFuture.supplyAsync(() -> { + String tempName = Thread.currentThread().getName(); + Thread.currentThread().setName(username + " (" + ssp.name + ")"); // Change Thread Name + UserProfile profile = null; + try { + profile = loader.loadProfile(ssp, gameProfile); + } catch (Exception e) { + logger.warning("Exception occurs while loading."); + logger.warning(e); + if (e.getCause() != null) { + logger.warning("Caused By:"); + logger.warning(e.getCause()); + } } - } + Thread.currentThread().setName(tempName); + return profile; + }, THREAD_POOL)); + } + + for (CompletableFuture profileGetter : profileGetters) { + UserProfile profile = profileGetter.join(); if (profile == null) { continue; } @@ -155,6 +147,7 @@ public static UserProfile loadProfile0(GameProfile gameProfile, boolean isSkull) break; } } + if (!profile0.isEmpty()) { logger.info(username + "'s profile loaded."); if (!config.enableCape) { @@ -206,11 +199,7 @@ public static Map loadPro loadProfile0(gameProfile, true);//Load in thread Thread.currentThread().setName(tempName); }; - if (config.forceUpdateSkull) { - new Thread(loadThread).start(); - } else { - threadPool.execute(loadThread); - } + THREAD_POOL.execute(loadThread); } return INCOMPLETED; } diff --git a/Common/src/main/java/customskinloader/config/Config.java b/Common/src/main/java/customskinloader/config/Config.java index bf3efceb..3de2d429 100644 --- a/Common/src/main/java/customskinloader/config/Config.java +++ b/Common/src/main/java/customskinloader/config/Config.java @@ -29,7 +29,6 @@ public class Config { public boolean enableTransparentSkin = true; public boolean forceLoadAllTextures = true; public boolean enableCape = true; - public int threadPoolSize = 8; /** * Can logger write message to standard output(System.out). * Because standard output won't write to latest.log after Forge 1.17, @@ -77,7 +76,6 @@ public static Config loadConfig0() { config.loadExtraList(); config.updateLoadlist(); config.initLocalFolder(); - config.threadPoolSize = Math.max(config.threadPoolSize, 1); if (config.enableCacheAutoClean && !config.enableLocalProfileCache) { try { FileUtils.deleteDirectory(HttpRequestUtil.CACHE_DIR); diff --git a/Common/src/main/java/customskinloader/utils/LIFOBlockingQueue.java b/Common/src/main/java/customskinloader/utils/LIFOBlockingQueue.java deleted file mode 100644 index d8f5be1f..00000000 --- a/Common/src/main/java/customskinloader/utils/LIFOBlockingQueue.java +++ /dev/null @@ -1,50 +0,0 @@ -package customskinloader.utils; - -import java.util.concurrent.BlockingDeque; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.TimeUnit; - -import com.google.common.util.concurrent.ForwardingBlockingQueue; - -public class LIFOBlockingQueue extends ForwardingBlockingQueue { - private final BlockingDeque deque; - - public LIFOBlockingQueue(BlockingDeque deque) { - this.deque = deque; - } - - @Override - protected BlockingQueue delegate() { - return deque; - } - - @Override - public boolean offer(E e) { - return deque.offerFirst(e); - } - - @Override - public void put(E e) throws InterruptedException { - deque.putFirst(e); - } - - @Override - public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { - return deque.offerFirst(e, timeout, unit); - } - - @Override - public E take() throws InterruptedException { - return deque.takeFirst(); - } - - @Override - public E poll(long timeout, TimeUnit unit) throws InterruptedException { - return deque.pollFirst(timeout, unit); - } - - @Override - public E peek() { - return deque.peekFirst(); - } -} \ No newline at end of file diff --git a/Common/src/main/java/customskinloader/utils/MinecraftUtil.java b/Common/src/main/java/customskinloader/utils/MinecraftUtil.java index dfee05f8..ff882f16 100644 --- a/Common/src/main/java/customskinloader/utils/MinecraftUtil.java +++ b/Common/src/main/java/customskinloader/utils/MinecraftUtil.java @@ -85,6 +85,6 @@ public static boolean isLanServer() { } public static String getCredential(GameProfile profile) { - return profile == null ? null : String.format("%s-%s", TextureUtil.AuthlibField.GAME_PROFILE_NAME.get(profile), TextureUtil.AuthlibField.GAME_PROFILE_ID.get(profile)); + return profile == null ? null : profile.toString(); } } diff --git a/Dummy/src/main/java/com/google/errorprone/annotations/CanIgnoreReturnValue.java b/Dummy/src/main/java/com/google/errorprone/annotations/CanIgnoreReturnValue.java deleted file mode 100644 index 5c542b7b..00000000 --- a/Dummy/src/main/java/com/google/errorprone/annotations/CanIgnoreReturnValue.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.google.errorprone.annotations; - -public @interface CanIgnoreReturnValue { - -} diff --git a/Vanilla/Common/src/main/java/customskinloader/mixin/core/MixinConfigPlugin.java b/Vanilla/Common/src/main/java/customskinloader/mixin/core/MixinConfigPlugin.java index 079580a8..264af5e7 100644 --- a/Vanilla/Common/src/main/java/customskinloader/mixin/core/MixinConfigPlugin.java +++ b/Vanilla/Common/src/main/java/customskinloader/mixin/core/MixinConfigPlugin.java @@ -65,7 +65,7 @@ public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { } else if (mixinClassName.endsWith(".MixinSkinManager$V3")) { result = this.world_version >= 3684 && ((this.protocol_version >= 765 && this.protocol_version < 801) || (this.protocol_version > 803 && this.protocol_version < 0x40000001) || this.protocol_version >= 0x4000009D); // 23w42a+ } else if (mixinClassName.endsWith(".MixinSkinManager$1") || mixinClassName.endsWith(".MixinSkinManager$TextureCache")) { - result = this.world_version >= 3567 && ((this.protocol_version >= 764 && this.protocol_version < 801) || (this.protocol_version > 803 && this.protocol_version < 0x40000001) || this.protocol_version >= 0x40000090); // 23w31a+ + result = this.world_version >= 3567 && ((this.protocol_version >= 764 && this.protocol_version < 801) || (this.protocol_version > 803 && this.protocol_version < 0x40000001) || this.protocol_version >= 0x40000090); // 23w31a+ } else if (mixinClassName.endsWith(".MixinSkinTextureDownloader")) { result = this.world_version >= 4178 && ((this.protocol_version >= 769 && this.protocol_version < 801) || (this.protocol_version > 803 && this.protocol_version < 0x40000001) || this.protocol_version >= 0x400000DE); // 24w46a+ } else if (mixinClassName.endsWith(".MixinThreadDownloadImageData$V1")) {