Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 29 additions & 40 deletions Common/src/main/java/customskinloader/CustomSkinLoader.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -117,10 +97,12 @@ public static UserProfile loadProfile0(GameProfile gameProfile, boolean isSkull)
return null;
}

int size = config.loadlist.size();
LinkedList<CompletableFuture<UserProfile>> 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;
Expand All @@ -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<UserProfile> profileGetter : profileGetters) {
UserProfile profile = profileGetter.join();
if (profile == null) {
continue;
}
Expand All @@ -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) {
Expand Down Expand Up @@ -206,11 +199,7 @@ public static Map<MinecraftProfileTexture.Type, MinecraftProfileTexture> 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;
}
Expand Down
2 changes: 0 additions & 2 deletions Common/src/main/java/customskinloader/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
50 changes: 0 additions & 50 deletions Common/src/main/java/customskinloader/utils/LIFOBlockingQueue.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down