Merge remote-tracking branch 'origin/1.18' into 1.19.2
This commit is contained in:
commit
56d73ea9a2
|
|
@ -1,5 +1,9 @@
|
||||||
package org.embeddedt.modernfix.resources;
|
package org.embeddedt.modernfix.resources;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import com.mojang.datafixers.util.Pair;
|
||||||
|
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
||||||
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
|
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
import net.minecraft.resources.ResourceLocation;
|
||||||
import net.minecraft.server.packs.PackType;
|
import net.minecraft.server.packs.PackType;
|
||||||
|
|
@ -14,9 +18,16 @@ import java.util.function.Function;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The core of the resource pack cache system.
|
||||||
|
*
|
||||||
|
* Using a dedicated set and also separate lists is important; testing without this showed a huge performance
|
||||||
|
* drop.
|
||||||
|
*/
|
||||||
public class PackResourcesCacheEngine {
|
public class PackResourcesCacheEngine {
|
||||||
private final Map<PackType, Set<String>> namespacesByType;
|
private final Map<PackType, Set<String>> namespacesByType;
|
||||||
private final Set<CachedResourcePath> containedPaths;
|
private final Set<CachedResourcePath> containedPaths;
|
||||||
|
private final EnumMap<PackType, Map<String, List<CachedResourcePath>>> resourceListings;
|
||||||
|
|
||||||
public PackResourcesCacheEngine(Function<PackType, Set<String>> namespacesRetriever, BiFunction<PackType, String, Path> basePathRetriever) {
|
public PackResourcesCacheEngine(Function<PackType, Set<String>> namespacesRetriever, BiFunction<PackType, String, Path> basePathRetriever) {
|
||||||
this.namespacesByType = new EnumMap<>(PackType.class);
|
this.namespacesByType = new EnumMap<>(PackType.class);
|
||||||
|
|
@ -26,22 +37,31 @@ public class PackResourcesCacheEngine {
|
||||||
this.namespacesByType.put(type, namespacesRetriever.apply(type));
|
this.namespacesByType.put(type, namespacesRetriever.apply(type));
|
||||||
}
|
}
|
||||||
this.containedPaths = new ObjectOpenHashSet<>();
|
this.containedPaths = new ObjectOpenHashSet<>();
|
||||||
|
this.resourceListings = new EnumMap<>(PackType.class);
|
||||||
for(PackType type : PackType.values()) {
|
for(PackType type : PackType.values()) {
|
||||||
Collection<String> namespaces = PackTypeHelper.isVanillaPackType(type) ? this.namespacesByType.get(type) : namespacesRetriever.apply(type);
|
Collection<String> namespaces = PackTypeHelper.isVanillaPackType(type) ? this.namespacesByType.get(type) : namespacesRetriever.apply(type);
|
||||||
|
ImmutableMap.Builder<String, List<CachedResourcePath>> packTypedMap = ImmutableMap.builder();
|
||||||
for(String namespace : namespaces) {
|
for(String namespace : namespaces) {
|
||||||
try {
|
try {
|
||||||
|
ImmutableList.Builder<CachedResourcePath> namespacedList = ImmutableList.builder();
|
||||||
Path root = basePathRetriever.apply(type, namespace).toAbsolutePath();
|
Path root = basePathRetriever.apply(type, namespace).toAbsolutePath();
|
||||||
|
String[] prefix = new String[] { type.getDirectory(), namespace };
|
||||||
try (Stream<Path> stream = Files.walk(root)) {
|
try (Stream<Path> stream = Files.walk(root)) {
|
||||||
stream
|
stream
|
||||||
.map(path -> root.relativize(path.toAbsolutePath()))
|
.map(path -> root.relativize(path.toAbsolutePath()))
|
||||||
.filter(PackResourcesCacheEngine::isValidCachedResourcePath)
|
.filter(PackResourcesCacheEngine::isValidCachedResourcePath)
|
||||||
.forEach(path -> {
|
.forEach(path -> {
|
||||||
this.containedPaths.add(new CachedResourcePath(new String[] { type.getDirectory(), namespace }, path));
|
CachedResourcePath cachedPath = new CachedResourcePath(prefix, path);
|
||||||
|
this.containedPaths.add(cachedPath);
|
||||||
|
if(!cachedPath.getFileName().endsWith(".mcmeta"))
|
||||||
|
namespacedList.add(cachedPath);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
packTypedMap.put(namespace, namespacedList.build());
|
||||||
} catch(IOException ignored) {
|
} catch(IOException ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.resourceListings.put(type, packTypedMap.build());
|
||||||
}
|
}
|
||||||
((ObjectOpenHashSet<CachedResourcePath>)this.containedPaths).trim();
|
((ObjectOpenHashSet<CachedResourcePath>)this.containedPaths).trim();
|
||||||
}
|
}
|
||||||
|
|
@ -73,20 +93,16 @@ public class PackResourcesCacheEngine {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<ResourceLocation> getResources(PackType type, String resourceNamespace, String pathIn, int maxDepth, Predicate<ResourceLocation> filter) {
|
public Collection<ResourceLocation> getResources(PackType type, String resourceNamespace, String pathIn, int maxDepth, Predicate<ResourceLocation> filter) {
|
||||||
|
if(!PackTypeHelper.isVanillaPackType(type))
|
||||||
|
throw new IllegalArgumentException("Only vanilla PackTypes are supported");
|
||||||
|
List<CachedResourcePath> paths = resourceListings.get(type).getOrDefault(resourceNamespace, Collections.emptyList());
|
||||||
|
if(paths.isEmpty())
|
||||||
|
return Collections.emptyList();
|
||||||
String testPath = pathIn.endsWith("/") ? pathIn : (pathIn + "/");
|
String testPath = pathIn.endsWith("/") ? pathIn : (pathIn + "/");
|
||||||
ArrayList<ResourceLocation> resources = new ArrayList<>();
|
ArrayList<ResourceLocation> resources = new ArrayList<>();
|
||||||
String typeDirectory = CachedResourcePath.PATH_COMPONENT_INTERNER.intern(type.getDirectory());
|
for(CachedResourcePath cachePath : paths) {
|
||||||
resourceNamespace = CachedResourcePath.PATH_COMPONENT_INTERNER.intern(resourceNamespace);
|
|
||||||
for(CachedResourcePath cachePath : this.containedPaths) {
|
|
||||||
if(cachePath.getNameCount() < 2)
|
|
||||||
continue;
|
|
||||||
if((cachePath.getNameCount() - 2) > maxDepth)
|
if((cachePath.getNameCount() - 2) > maxDepth)
|
||||||
continue;
|
continue;
|
||||||
// we interned, so reference equality is safe
|
|
||||||
if(cachePath.getNameAt(0) != typeDirectory || cachePath.getNameAt(1) != resourceNamespace)
|
|
||||||
continue;
|
|
||||||
if(cachePath.getFileName().endsWith(".mcmeta"))
|
|
||||||
continue;
|
|
||||||
String fullPath = cachePath.getFullPath(2);
|
String fullPath = cachePath.getFullPath(2);
|
||||||
if(!fullPath.startsWith(testPath))
|
if(!fullPath.startsWith(testPath))
|
||||||
continue;
|
continue;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user