diff --git a/src/main/java/org/embeddedt/modernfix/mixin/perf/modern_resourcepacks/PathResourcePackMixin.java b/src/main/java/org/embeddedt/modernfix/mixin/perf/modern_resourcepacks/PathResourcePackMixin.java index 53236ea6..9ef386fb 100644 --- a/src/main/java/org/embeddedt/modernfix/mixin/perf/modern_resourcepacks/PathResourcePackMixin.java +++ b/src/main/java/org/embeddedt/modernfix/mixin/perf/modern_resourcepacks/PathResourcePackMixin.java @@ -8,6 +8,7 @@ import net.minecraftforge.fml.loading.moddiscovery.ModFile; import net.minecraftforge.resource.PathResourcePack; import org.apache.commons.lang3.tuple.Triple; import org.embeddedt.modernfix.ModernFix; +import org.embeddedt.modernfix.util.CachedResourcePath; import org.embeddedt.modernfix.util.FileUtil; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; @@ -36,9 +37,9 @@ public abstract class PathResourcePackMixin { @Shadow @Final private String packName; @Shadow @Final private Path source; private EnumMap> namespacesByType; - private EnumMap>>> rootListingByNamespaceAndType; + private EnumMap>> rootListingByNamespaceAndType; private boolean hasGeneratedListings; - private Set containedPaths; + private Set containedPaths; private FileSystem resourcePackFS; @@ -55,24 +56,24 @@ public abstract class PathResourcePackMixin { synchronized (this) { if(hasGeneratedListings) return; - EnumMap>>> rootListingByNamespaceAndType = new EnumMap<>(PackType.class); - HashSet containedPaths = new HashSet<>(); + EnumMap>> rootListingByNamespaceAndType = new EnumMap<>(PackType.class); + HashSet containedPaths = new HashSet<>(); for(PackType type : PackType.values()) { Set namespaces = this.getNamespaces(type); - HashMap>> rootListingForNamespaces = new HashMap<>(); + HashMap> rootListingForNamespaces = new HashMap<>(); for(String namespace : namespaces) { try { Path root = this.resolve(type.getDirectory(), namespace).toAbsolutePath(); try (Stream stream = Files.walk(root)) { - ArrayList> rootListingPaths = new ArrayList<>(); + ArrayList rootListingPaths = new ArrayList<>(); stream .map(path -> root.relativize(path.toAbsolutePath())) .filter(this::isValidCachedResourcePath) .forEach(path -> { if(!path.toString().endsWith(".mcmeta")) - rootListingPaths.add(Triple.of(path.getNameCount(), path.getFileName().toString(), slashJoiner.join(path))); + rootListingPaths.add(new CachedResourcePath(path)); String mergedPath = slashJoiner.join(type.getDirectory(), namespace, path); - containedPaths.add(mergedPath); + containedPaths.add(new CachedResourcePath(mergedPath)); }); rootListingPaths.trimToSize(); rootListingForNamespaces.put(namespace, rootListingPaths); @@ -122,7 +123,7 @@ public abstract class PathResourcePackMixin { @Inject(method = "hasResource(Ljava/lang/String;)Z", at = @At(value = "HEAD"), cancellable = true) private void useCacheForExistence(String path, CallbackInfoReturnable cir) { this.generateResourceCache(); - cir.setReturnValue(this.containedPaths.contains(FileUtil.normalize(path))); + cir.setReturnValue(this.containedPaths.contains(new CachedResourcePath(path))); } /** @@ -137,13 +138,13 @@ public abstract class PathResourcePackMixin { pathIn = pathIn + "/"; final String testPath = pathIn; Collection cachedListing = this.rootListingByNamespaceAndType.get(type).getOrDefault(resourceNamespace, Collections.emptyList()).stream(). - filter(path -> path.getLeft() <= maxDepth). // Make sure the depth is within bounds - filter(path -> path.getRight().startsWith(testPath)). // Make sure the target path is inside this one - filter(path -> filter.test(path.getMiddle())). // Test the file name against the predicate + filter(path -> path.getNameCount() <= maxDepth). // Make sure the depth is within bounds + filter(path -> path.getFullPath().startsWith(testPath)). // Make sure the target path is inside this one + filter(path -> filter.test(path.getFileName())). // Test the file name against the predicate // Finally we need to form the RL, so use the first name as the domain, and the rest as the path // It is VERY IMPORTANT that we do not rely on Path.toString as this is inconsistent between operating systems // Join the path names ourselves to force forward slashes - map(path -> new ResourceLocation(resourceNamespace, path.getRight())). + map(path -> new ResourceLocation(resourceNamespace, path.getFullPath())). collect(Collectors.toList()); cir.setReturnValue(cachedListing); } diff --git a/src/main/java/org/embeddedt/modernfix/util/CachedResourcePath.java b/src/main/java/org/embeddedt/modernfix/util/CachedResourcePath.java new file mode 100644 index 00000000..9c210f17 --- /dev/null +++ b/src/main/java/org/embeddedt/modernfix/util/CachedResourcePath.java @@ -0,0 +1,73 @@ +package org.embeddedt.modernfix.util; + +import com.google.common.base.Joiner; +import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Interner; +import com.google.common.collect.Interners; +import com.google.common.collect.Streams; + +import java.lang.ref.WeakReference; +import java.nio.file.Path; + +public class CachedResourcePath { + private final ImmutableList pathComponents; + private int hashCode = 0; + + private static final Interner PATH_COMPONENT_INTERNER = Interners.newStrongInterner(); + private static final Splitter SLASH_SPLITTER = Splitter.on('/'); + private static final Joiner SLASH_JOINER = Joiner.on('/'); + private WeakReference fullPathCache = new WeakReference<>(null); + + public CachedResourcePath(Iterable components) { + ImmutableList.Builder b = ImmutableList.builder(); + for(String s : components) { + if(s == null || s.length() == 0) + continue; + b.add(PATH_COMPONENT_INTERNER.intern(s)); + } + pathComponents = b.build(); + } + + public CachedResourcePath(Path path) { + this(() -> Streams.stream(path.iterator()).map(Path::toString).iterator()); + } + + public CachedResourcePath(String s) { + this(SLASH_SPLITTER.split(s)); + } + + @Override + public int hashCode() { + int result = hashCode; + if(result != 0) + return result; + hashCode = pathComponents.hashCode(); + return hashCode; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + CachedResourcePath that = (CachedResourcePath) o; + return pathComponents.equals(that.pathComponents); + } + + public String getFileName() { + return pathComponents.get(pathComponents.size() - 1); + } + + public int getNameCount() { + return pathComponents.size(); + } + + public String getFullPath() { + String fPath = fullPathCache.get(); + if(fPath == null) { + fPath = SLASH_JOINER.join(pathComponents); + fullPathCache = new WeakReference<>(fPath); + } + return fPath; + } +}