From 773800830ff4b8782e7a2d3488482e1eb762ba9b Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Wed, 4 Jan 2023 15:28:58 -0500 Subject: [PATCH] Optimize VanillaPack.hasResource --- .../modernfix/mixin/VanillaPackMixin.java | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/embeddedt/modernfix/mixin/VanillaPackMixin.java b/src/main/java/org/embeddedt/modernfix/mixin/VanillaPackMixin.java index ad3b5e7b..d9f0e1ed 100644 --- a/src/main/java/org/embeddedt/modernfix/mixin/VanillaPackMixin.java +++ b/src/main/java/org/embeddedt/modernfix/mixin/VanillaPackMixin.java @@ -1,5 +1,6 @@ package org.embeddedt.modernfix.mixin; +import com.google.common.base.Joiner; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; @@ -8,6 +9,7 @@ import net.minecraft.resources.VanillaPack; import net.minecraft.util.ResourceLocation; import org.apache.commons.lang3.tuple.Pair; import org.embeddedt.modernfix.FileWalker; +import org.embeddedt.modernfix.ModernFix; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -20,10 +22,7 @@ import org.spongepowered.asm.mixin.injection.callback.LocalCapture; import java.io.IOException; import java.nio.file.*; -import java.util.EnumMap; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.ExecutionException; import java.util.stream.Stream; @@ -33,6 +32,32 @@ public class VanillaPackMixin { private static LoadingCache, List> pathStreamLoadingCache = CacheBuilder.newBuilder() .build(FileWalker.INSTANCE); + private static Set containedPaths = null; + + @Inject(method = "", at = @At("TAIL")) + private void cacheContainedPaths(String[] p_i47912_1_, CallbackInfo ci) { + if(containedPaths != null) + return; + containedPaths = new HashSet<>(); + Joiner slashJoiner = Joiner.on('/'); + for(ResourcePackType type : ResourcePackType.values()) { + FileSystem fs = JAR_FILESYSTEM_BY_TYPE.get(type); + if(fs == null) + throw new IllegalStateException("No filesystem for vanilla " + type.name() + " assets"); + try { + Path root = fs.getPath(type.getDirectory()).toAbsolutePath(); + try(Stream stream = Files.walk(root)) { + stream + .map(path -> root.relativize(path.toAbsolutePath())) + .filter(path -> !path.toString().endsWith(".mcmeta")) + .forEach(path -> containedPaths.add(slashJoiner.join(type.getDirectory(), path))); + } + } catch(IOException e) { + e.printStackTrace(); + } + } + } + @Redirect(method = "getResources(Ljava/util/Collection;ILjava/lang/String;Ljava/nio/file/Path;Ljava/lang/String;Ljava/util/function/Predicate;)V", at = @At(value = "INVOKE", target = "Ljava/nio/file/Files;walk(Ljava/nio/file/Path;I[Ljava/nio/file/FileVisitOption;)Ljava/util/stream/Stream;")) private static Stream useCacheForLoading(Path path, int maxDepth, FileVisitOption[] fileVisitOptions) throws IOException { try { @@ -45,9 +70,8 @@ public class VanillaPackMixin { } } - @Inject(method = "hasResource", at = @At(value = "INVOKE", target = "Ljava/lang/Class;getResource(Ljava/lang/String;)Ljava/net/URL;"), cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD) - private void useCacheForExistence(ResourcePackType type, ResourceLocation location, CallbackInfoReturnable cir, String path) { - FileSystem fs = JAR_FILESYSTEM_BY_TYPE.get(type); - cir.setReturnValue(Files.exists(fs.getPath(path))); + @Inject(method = "hasResource", at = @At(value = "INVOKE", target = "Ljava/lang/Class;getResource(Ljava/lang/String;)Ljava/net/URL;"), cancellable = true) + private void useCacheForExistence(ResourcePackType type, ResourceLocation location, CallbackInfoReturnable cir) { + cir.setReturnValue(containedPaths.contains(type.getDirectory() + "/" + location.getNamespace() + "/" + location.getPath())); } }