Optimize VanillaPack.hasResource
This commit is contained in:
parent
c0869fb1b2
commit
773800830f
|
|
@ -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<Pair<Path, Integer>, List<Path>> pathStreamLoadingCache = CacheBuilder.newBuilder()
|
||||
.build(FileWalker.INSTANCE);
|
||||
|
||||
private static Set<String> containedPaths = null;
|
||||
|
||||
@Inject(method = "<init>", 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<Path> 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<Path> 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<Boolean> 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<Boolean> cir) {
|
||||
cir.setReturnValue(containedPaths.contains(type.getDirectory() + "/" + location.getNamespace() + "/" + location.getPath()));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user