Fix resource pack optimization so it actually works

This commit is contained in:
embeddedt 2023-02-22 21:43:10 -05:00
parent da1017e61e
commit a22d36b485
No known key found for this signature in database
GPG Key ID: A69433EC199B5613

View File

@ -1,10 +1,12 @@
package org.embeddedt.modernfix.mixin.perf.modern_resourcepacks; package org.embeddedt.modernfix.mixin.perf.modern_resourcepacks;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.mojang.datafixers.util.Pair;
import net.minecraft.server.packs.PackType; import net.minecraft.server.packs.PackType;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.fml.loading.moddiscovery.ModFile; import net.minecraftforge.fml.loading.moddiscovery.ModFile;
import net.minecraftforge.resource.PathResourcePack; import net.minecraftforge.resource.PathResourcePack;
import org.embeddedt.modernfix.ModernFix;
import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Overwrite;
@ -29,9 +31,11 @@ public abstract class PathResourcePackMixin {
@Shadow protected abstract Path resolve(String... paths); @Shadow protected abstract Path resolve(String... paths);
@Shadow @Final private String packName;
@Shadow @Final private Path source;
private EnumMap<PackType, Set<String>> namespacesByType; private EnumMap<PackType, Set<String>> namespacesByType;
private EnumMap<PackType, HashMap<String, List<Path>>> rootListingByNamespaceAndType; private EnumMap<PackType, HashMap<String, List<Pair<Path, String>>>> rootListingByNamespaceAndType;
private boolean hasGeneratedListings = false; private boolean hasGeneratedListings;
private Set<String> containedPaths; private Set<String> containedPaths;
private FileSystem resourcePackFS; private FileSystem resourcePackFS;
@ -42,34 +46,29 @@ public abstract class PathResourcePackMixin {
private void cacheResources(String packName, Path source, CallbackInfo ci) { private void cacheResources(String packName, Path source, CallbackInfo ci) {
this.resourcePackFS = source.getFileSystem(); this.resourcePackFS = source.getFileSystem();
this.namespacesByType = new EnumMap<>(PackType.class); this.namespacesByType = new EnumMap<>(PackType.class);
this.hasGeneratedListings = false;
} }
private boolean generateResourceCache() { private void generateResourceCache() {
synchronized (this.namespacesByType) { synchronized (this) {
if(!this.namespacesByType.containsKey(PackType.CLIENT_RESOURCES) || !this.namespacesByType.containsKey(PackType.SERVER_DATA))
return false;
}
if(hasGeneratedListings) if(hasGeneratedListings)
return true; return;
EnumMap<PackType, HashMap<String, List<Path>>> rootListingByNamespaceAndType = new EnumMap<>(PackType.class); EnumMap<PackType, HashMap<String, List<Pair<Path, String>>>> rootListingByNamespaceAndType = new EnumMap<>(PackType.class);
HashSet<String> containedPaths = new HashSet<>(); HashSet<String> containedPaths = new HashSet<>();
for(PackType type : PackType.values()) { for(PackType type : PackType.values()) {
Set<String> namespaces; Set<String> namespaces = this.getNamespaces(type);
synchronized (this.namespacesByType) { HashMap<String, List<Pair<Path, String>>> rootListingForNamespaces = new HashMap<>();
namespaces = this.namespacesByType.get(type);
}
HashMap<String, List<Path>> rootListingForNamespaces = new HashMap<>();
for(String namespace : namespaces) { for(String namespace : namespaces) {
try { try {
Path root = this.resolve(type.getDirectory(), namespace).toAbsolutePath(); Path root = this.resolve(type.getDirectory(), namespace).toAbsolutePath();
try (Stream<Path> stream = Files.walk(root)) { try (Stream<Path> stream = Files.walk(root)) {
ArrayList<Path> rootListingPaths = new ArrayList<>(); ArrayList<Pair<Path, String>> rootListingPaths = new ArrayList<>();
stream stream
.map(path -> root.relativize(path.toAbsolutePath())) .map(path -> root.relativize(path.toAbsolutePath()))
.filter(this::isValidCachedResourcePath) .filter(this::isValidCachedResourcePath)
.forEach(path -> { .forEach(path -> {
if(!path.toString().endsWith(".mcmeta")) if(!path.toString().endsWith(".mcmeta"))
rootListingPaths.add(path); rootListingPaths.add(Pair.of(path, slashJoiner.join(path)));
String mergedPath = slashJoiner.join(type.getDirectory(), namespace, path); String mergedPath = slashJoiner.join(type.getDirectory(), namespace, path);
containedPaths.add(mergedPath); containedPaths.add(mergedPath);
}); });
@ -85,7 +84,7 @@ public abstract class PathResourcePackMixin {
this.rootListingByNamespaceAndType = rootListingByNamespaceAndType; this.rootListingByNamespaceAndType = rootListingByNamespaceAndType;
this.containedPaths = containedPaths; this.containedPaths = containedPaths;
this.hasGeneratedListings = true; this.hasGeneratedListings = true;
return true; }
} }
private boolean isValidCachedResourcePath(Path path) { private boolean isValidCachedResourcePath(Path path) {
@ -118,9 +117,7 @@ public abstract class PathResourcePackMixin {
@Inject(method = "hasResource(Ljava/lang/String;)Z", at = @At(value = "HEAD"), cancellable = true) @Inject(method = "hasResource(Ljava/lang/String;)Z", at = @At(value = "HEAD"), cancellable = true)
private void useCacheForExistence(String path, CallbackInfoReturnable<Boolean> cir) { private void useCacheForExistence(String path, CallbackInfoReturnable<Boolean> cir) {
if(!this.generateResourceCache()) { this.generateResourceCache();
return;
}
cir.setReturnValue(this.containedPaths.contains(path)); cir.setReturnValue(this.containedPaths.contains(path));
} }
@ -131,18 +128,18 @@ public abstract class PathResourcePackMixin {
@Inject(method = "getResources", at = @At("HEAD"), cancellable = true) @Inject(method = "getResources", at = @At("HEAD"), cancellable = true)
public void getResources(PackType type, String resourceNamespace, String pathIn, int maxDepth, Predicate<String> filter, CallbackInfoReturnable<Collection<ResourceLocation>> cir) public void getResources(PackType type, String resourceNamespace, String pathIn, int maxDepth, Predicate<String> filter, CallbackInfoReturnable<Collection<ResourceLocation>> cir)
{ {
if(!this.generateResourceCache()) { this.generateResourceCache();
return; if(!pathIn.endsWith("/"))
} pathIn = pathIn + "/";
Path inputPath = this.resourcePackFS.getPath(pathIn); final String testPath = pathIn;
Collection<ResourceLocation> cachedListing = this.rootListingByNamespaceAndType.get(type).getOrDefault(resourceNamespace, Collections.emptyList()).stream(). Collection<ResourceLocation> cachedListing = this.rootListingByNamespaceAndType.get(type).getOrDefault(resourceNamespace, Collections.emptyList()).stream().
filter(path -> path.getNameCount() <= maxDepth). // Make sure the depth is within bounds filter(path -> path.getFirst().getNameCount() <= maxDepth). // Make sure the depth is within bounds
filter(path -> path.startsWith(inputPath)). // Make sure the target path is inside this one filter(path -> path.getSecond().startsWith(testPath)). // Make sure the target path is inside this one
filter(path -> filter.test(path.getFileName().toString())). // Test the file name against the predicate filter(path -> filter.test(path.getFirst().getFileName().toString())). // 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 // 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 // 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 // Join the path names ourselves to force forward slashes
map(path -> new ResourceLocation(resourceNamespace, slashJoiner.join(path))). map(path -> new ResourceLocation(resourceNamespace, path.getSecond())).
collect(Collectors.toList()); collect(Collectors.toList());
cir.setReturnValue(cachedListing); cir.setReturnValue(cachedListing);
} }