From 4af85f40766e8cdb16ace458eb73b3476c8ae090 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Fri, 24 Feb 2023 12:42:35 -0500 Subject: [PATCH] Speed up VanillaPackResources.getResource --- .../VanillaPackResourcesMixin.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/org/embeddedt/modernfix/mixin/perf/modern_resourcepacks/VanillaPackResourcesMixin.java b/src/main/java/org/embeddedt/modernfix/mixin/perf/modern_resourcepacks/VanillaPackResourcesMixin.java index 3eec88fb..c0ca392e 100644 --- a/src/main/java/org/embeddedt/modernfix/mixin/perf/modern_resourcepacks/VanillaPackResourcesMixin.java +++ b/src/main/java/org/embeddedt/modernfix/mixin/perf/modern_resourcepacks/VanillaPackResourcesMixin.java @@ -11,6 +11,7 @@ import org.apache.commons.lang3.tuple.Pair; import org.embeddedt.modernfix.FileWalker; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -18,7 +19,9 @@ import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.nio.file.*; import java.util.*; import java.util.concurrent.ExecutionException; @@ -70,4 +73,20 @@ public class VanillaPackResourcesMixin { private void useCacheForExistence(PackType type, ResourceLocation location, CallbackInfoReturnable cir) { cir.setReturnValue(containedPaths.contains(type.getDirectory() + "/" + location.getNamespace() + "/" + location.getPath())); } + + /** + * @author embeddedt + * @reason avoid going through the module class loader when we know exactly what path this resource should come + * from + */ + @Overwrite + protected InputStream getResourceAsStream(PackType type, ResourceLocation location) { + Path rootPath = ROOT_DIR_BY_TYPE.get(type); + Path targetPath = rootPath.resolve(location.getNamespace() + "/" + location.getPath()); + try { + return Files.newInputStream(targetPath); + } catch(IOException e) { + return null; + } + } }