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,50 +46,45 @@ 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)) if(hasGeneratedListings)
return false; return;
} EnumMap<PackType, HashMap<String, List<Pair<Path, String>>>> rootListingByNamespaceAndType = new EnumMap<>(PackType.class);
if(hasGeneratedListings) HashSet<String> containedPaths = new HashSet<>();
return true; for(PackType type : PackType.values()) {
EnumMap<PackType, HashMap<String, List<Path>>> rootListingByNamespaceAndType = new EnumMap<>(PackType.class); Set<String> namespaces = this.getNamespaces(type);
HashSet<String> containedPaths = new HashSet<>(); HashMap<String, List<Pair<Path, String>>> rootListingForNamespaces = new HashMap<>();
for(PackType type : PackType.values()) { for(String namespace : namespaces) {
Set<String> namespaces; try {
synchronized (this.namespacesByType) { Path root = this.resolve(type.getDirectory(), namespace).toAbsolutePath();
namespaces = this.namespacesByType.get(type); try (Stream<Path> stream = Files.walk(root)) {
} ArrayList<Pair<Path, String>> rootListingPaths = new ArrayList<>();
HashMap<String, List<Path>> rootListingForNamespaces = new HashMap<>(); stream
for(String namespace : namespaces) { .map(path -> root.relativize(path.toAbsolutePath()))
try { .filter(this::isValidCachedResourcePath)
Path root = this.resolve(type.getDirectory(), namespace).toAbsolutePath(); .forEach(path -> {
try (Stream<Path> stream = Files.walk(root)) { if(!path.toString().endsWith(".mcmeta"))
ArrayList<Path> rootListingPaths = new ArrayList<>(); rootListingPaths.add(Pair.of(path, slashJoiner.join(path)));
stream String mergedPath = slashJoiner.join(type.getDirectory(), namespace, path);
.map(path -> root.relativize(path.toAbsolutePath())) containedPaths.add(mergedPath);
.filter(this::isValidCachedResourcePath) });
.forEach(path -> { rootListingPaths.trimToSize();
if(!path.toString().endsWith(".mcmeta")) rootListingForNamespaces.put(namespace, rootListingPaths);
rootListingPaths.add(path); }
String mergedPath = slashJoiner.join(type.getDirectory(), namespace, path); } catch(IOException e) {
containedPaths.add(mergedPath); rootListingForNamespaces.put(namespace, Collections.emptyList());
});
rootListingPaths.trimToSize();
rootListingForNamespaces.put(namespace, rootListingPaths);
} }
} catch(IOException e) {
rootListingForNamespaces.put(namespace, Collections.emptyList());
} }
rootListingByNamespaceAndType.put(type, rootListingForNamespaces);
} }
rootListingByNamespaceAndType.put(type, rootListingForNamespaces); this.rootListingByNamespaceAndType = rootListingByNamespaceAndType;
this.containedPaths = containedPaths;
this.hasGeneratedListings = true;
} }
this.rootListingByNamespaceAndType = rootListingByNamespaceAndType;
this.containedPaths = containedPaths;
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);
} }