Skip to content

Commit

Permalink
feat: Load atlas textures from cache off-thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveplays28 committed Aug 5, 2024
1 parent 4059d6f commit e9d6d47
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.steveplays28.blinkload.BlinkLoad;
import io.github.steveplays28.blinkload.client.event.ClientLifecycleEvent;
import io.github.steveplays28.blinkload.util.CacheUtil;
import io.github.steveplays28.blinkload.util.ThreadUtil;
import io.github.steveplays28.blinkload.util.resource.json.AtlasTextureIdentifier;
import io.github.steveplays28.blinkload.util.resource.json.JsonUtil;
import io.github.steveplays28.blinkload.util.resource.json.StitchResult;
Expand All @@ -14,6 +15,7 @@
import java.io.*;
import java.nio.file.Files;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

Expand All @@ -22,6 +24,7 @@ public class BlinkLoadCache {
private static final @NotNull File CACHED_DATA_FILE = new File(
String.format("%s/atlas_textures_cache.json", CacheUtil.getCachePath()));

private static @Nullable CompletableFuture<Void> cachedDataCompletableFuture = null;
private static @Nullable Map<AtlasTextureIdentifier, StitchResult> cachedData = null;
private static @Nullable Boolean isUpToDate = null;

Expand All @@ -38,33 +41,23 @@ public static boolean isUpToDate() {
return isUpToDate;
}

@SuppressWarnings("ForLoopReplaceableByForEach")
public static @NotNull Map<AtlasTextureIdentifier, StitchResult> getCachedData() {
if (cachedData == null) {
var startTime = System.nanoTime();

cachedData = new ConcurrentHashMap<>();
// Read JSON from the cached data file
try (@NotNull Reader reader = new FileReader(CACHED_DATA_FILE)) {
// Convert the JSON data to a Java object
@NotNull var stitchResults = JsonUtil.getGson().fromJson(reader, StitchResult[].class);
for (int stitchResultIndex = 0; stitchResultIndex < stitchResults.length; stitchResultIndex++) {
@NotNull var stitchResult = stitchResults[stitchResultIndex];
cachedData.put(new AtlasTextureIdentifier(stitchResult.getAtlasTextureId(), stitchResult.getMipLevel()), stitchResult);
}

BlinkLoad.LOGGER.info(
"Loaded atlas textures from cache (took {}ms).",
TimeUnit.MILLISECONDS.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS)
);
} catch (IOException e) {
throw new RuntimeException(e);
}
loadCachedDataAsync().join();
}

return cachedData;
}

public static @NotNull CompletableFuture<Void> loadCachedDataAsync() {
if (cachedDataCompletableFuture == null) {
cachedDataCompletableFuture = CompletableFuture.runAsync(
BlinkLoadCache::loadCachedData, ThreadUtil.getAtlasTextureLoaderThreadPoolExecutor());
}

return cachedDataCompletableFuture;
}

public static void cacheData(@NotNull StitchResult stitchResult) {
getCachedData().put(new AtlasTextureIdentifier(stitchResult.getAtlasTextureId(), stitchResult.getMipLevel()), stitchResult);
}
Expand All @@ -73,6 +66,29 @@ private static void onClientResourceReloadFinished() {
writeCacheDataToFile();
}

@SuppressWarnings("ForLoopReplaceableByForEach")
private static void loadCachedData() {
var startTime = System.nanoTime();

cachedData = new ConcurrentHashMap<>();
// Read JSON from the cached data file
try (@NotNull Reader reader = new FileReader(CACHED_DATA_FILE)) {
// Convert the JSON data to a Java object
@NotNull var stitchResults = JsonUtil.getGson().fromJson(reader, StitchResult[].class);
for (int stitchResultIndex = 0; stitchResultIndex < stitchResults.length; stitchResultIndex++) {
@NotNull var stitchResult = stitchResults[stitchResultIndex];
cachedData.put(new AtlasTextureIdentifier(stitchResult.getAtlasTextureId(), stitchResult.getMipLevel()), stitchResult);
}

BlinkLoad.LOGGER.info(
"Loaded atlas textures from cache (took {}ms).",
TimeUnit.MILLISECONDS.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS)
);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static void writeCacheDataToFile() {
if (!isUpToDate()) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.steveplays28.blinkload.client.launch.pre;

import io.github.steveplays28.blinkload.client.cache.BlinkLoadCache;

public class BlinkLoadClientPreLaunch {
/**
* Runs the entrypoint. Ran before the game has bootstrapped.
*/
public static void onPreLaunch() {
BlinkLoadCache.loadCachedDataAsync();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.steveplays28.blinkload.util;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.github.steveplays28.blinkload.BlinkLoad;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class ThreadUtil {
private static final @NotNull Executor ATLAS_TEXTURE_LOADER_THREAD_POOL_EXECUTOR = Executors.newFixedThreadPool(
1, new ThreadFactoryBuilder().setNameFormat(String.format("%s Atlas Texture Loader", BlinkLoad.MOD_NAME)).build());

public static @NotNull Executor getAtlasTextureLoaderThreadPoolExecutor() {
return ATLAS_TEXTURE_LOADER_THREAD_POOL_EXECUTOR;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.steveplays28.blinkload.fabric.client;

import io.github.steveplays28.blinkload.client.launch.pre.BlinkLoadClientPreLaunch;
import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint;

public class BlinkLoadClientPreLaunchFabric implements PreLaunchEntrypoint {
/**
* Runs the entrypoint. Ran before the game has bootstrapped.
*/
@Override
public void onPreLaunch() {
BlinkLoadClientPreLaunch.onPreLaunch();
}
}
3 changes: 3 additions & 0 deletions fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
],
"main": [
"io.github.steveplays28.blinkload.fabric.BlinkLoadFabric"
],
"preLaunch": [
"io.github.steveplays28.blinkload.fabric.client.BlinkLoadClientPreLaunchFabric"
]
},
"mixins": [
Expand Down

0 comments on commit e9d6d47

Please sign in to comment.