Skip to content

Commit

Permalink
Add thread pool for pack conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Apr 2, 2024
1 parent 08fdc8f commit 5ce5598
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
26 changes: 24 additions & 2 deletions shared/src/main/java/org/geysermc/hydraulic/pack/PackListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@
import org.geysermc.hydraulic.util.FormatUtil;
import org.geysermc.hydraulic.util.PackUtil;
import org.geysermc.pack.bedrock.resource.Manifest;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.ZipFile;

/**
Expand All @@ -31,10 +35,28 @@
public class PackListener {
private static final Logger LOGGER = LogUtils.getLogger();
private static final Gson GSON = new Gson();
private static final ExecutorService THREAD_POOL;

private final HydraulicImpl hydraulic;
private final PackManager manager;

static {
int threads = Runtime.getRuntime().availableProcessors() * 3 / 8;
THREAD_POOL = Executors.newFixedThreadPool(threads, new ThreadFactory() {
private final AtomicInteger threadCounter = new AtomicInteger();

@Override
public Thread newThread(final @NotNull Runnable run) {
Thread ret = new Thread(run);

ret.setName("Hydraulic Conversion Thread #" + this.threadCounter.getAndIncrement());
ret.setUncaughtExceptionHandler((thread, throwable) -> LOGGER.error("Uncaught exception in thread " + thread.getName(), throwable));

return ret;
}
});
}

public PackListener(HydraulicImpl hydraulic, PackManager manager) {
this.hydraulic = hydraulic;
this.manager = manager;
Expand Down Expand Up @@ -80,7 +102,7 @@ public void onLoadResourcePacks(GeyserLoadResourcePacksEvent event) {
} catch (Throwable t) {
LOGGER.error("Failed to convert pack for mod {}", entry.getKey(), t);
}
}));
}, THREAD_POOL));
}

// Wait for all futures to complete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class PackUtil {
protected static final Logger LOGGER = LogUtils.getLogger();

public static String getTextureName(@NotNull String modelName) {
// TODO Sometimes things end up in the minecraft namespace when they shouldn't.
// We should look at the current mods resources to see if we find a match there first
// EG: betternether:wall_mushroom_red refrencing both mushroom_red_new (its own) and mushroom_block_inside (mc)
if (modelName.startsWith(Key.MINECRAFT_NAMESPACE)) {
String modelValue = modelName.split(":")[1];

Expand Down

0 comments on commit 5ce5598

Please sign in to comment.