diff --git a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/faster_texture_loading/TextureAtlasMixin.java b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/faster_texture_loading/TextureAtlasMixin.java index 0c9e7199..2a749f5c 100644 --- a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/faster_texture_loading/TextureAtlasMixin.java +++ b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/faster_texture_loading/TextureAtlasMixin.java @@ -34,16 +34,17 @@ public abstract class TextureAtlasMixin { private Map> loadedImages; private boolean usingFasterLoad; + private Collection storedResults; /** * @author embeddedt * @reason simplify texture loading by loading whole image once, avoid slow PngInfo code */ - @Redirect(method = "prepareToStitch", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/texture/TextureAtlas;getBasicSpriteInfos(Lnet/minecraft/server/packs/resources/ResourceManager;Ljava/util/Set;)Ljava/util/Collection;")) - private Collection loadImages(TextureAtlas atlas, ResourceManager manager, Set imageLocations) { + @Inject(method = "getBasicSpriteInfos", at = @At("HEAD")) + private void loadImages(ResourceManager manager, Set imageLocations, CallbackInfoReturnable> cir) { usingFasterLoad = ModernFixPlatformHooks.isLoadingNormally(); // bail if Forge is erroring to avoid AT crashes if(!usingFasterLoad) { - return getBasicSpriteInfos(manager, imageLocations); + return; } List> futures = new ArrayList<>(); ConcurrentLinkedQueue results = new ConcurrentLinkedQueue<>(); @@ -71,12 +72,24 @@ public abstract class TextureAtlasMixin { }, ModernFix.resourceReloadExecutor())); } CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join(); - return results; + storedResults = results; + } + + @Redirect(method = "getBasicSpriteInfos", at = @At(value = "INVOKE", target = "Ljava/util/Set;iterator()Ljava/util/Iterator;", ordinal = 0)) + private Iterator skipIteration(Set instance) { + return usingFasterLoad ? Collections.emptyIterator() : instance.iterator(); + } + + @Inject(method = "getBasicSpriteInfos", at = @At("RETURN")) + private void injectFastSprites(ResourceManager resourceManager, Set spriteLocations, CallbackInfoReturnable> cir) { + if(usingFasterLoad) + cir.getReturnValue().addAll(storedResults); } @Inject(method = "prepareToStitch", at = @At("RETURN")) private void clearLoadedImages(CallbackInfoReturnable cir) { loadedImages = Collections.emptyMap(); + storedResults = null; } @Inject(method = "load(Lnet/minecraft/server/packs/resources/ResourceManager;Lnet/minecraft/client/renderer/texture/TextureAtlasSprite$Info;IIIII)Lnet/minecraft/client/renderer/texture/TextureAtlasSprite;",