Improve memory usage of 1.18 resource pack cache
This commit is contained in:
parent
4d9852234a
commit
26c690595e
|
|
@ -6,6 +6,7 @@ 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.apache.commons.lang3.tuple.Triple;
|
||||||
import org.embeddedt.modernfix.ModernFix;
|
import org.embeddedt.modernfix.ModernFix;
|
||||||
import org.embeddedt.modernfix.util.FileUtil;
|
import org.embeddedt.modernfix.util.FileUtil;
|
||||||
import org.spongepowered.asm.mixin.Final;
|
import org.spongepowered.asm.mixin.Final;
|
||||||
|
|
@ -35,7 +36,7 @@ public abstract class PathResourcePackMixin {
|
||||||
@Shadow @Final private String packName;
|
@Shadow @Final private String packName;
|
||||||
@Shadow @Final private Path source;
|
@Shadow @Final private Path source;
|
||||||
private EnumMap<PackType, Set<String>> namespacesByType;
|
private EnumMap<PackType, Set<String>> namespacesByType;
|
||||||
private EnumMap<PackType, HashMap<String, List<Pair<Path, String>>>> rootListingByNamespaceAndType;
|
private EnumMap<PackType, HashMap<String, List<Triple<Integer, String, String>>>> rootListingByNamespaceAndType;
|
||||||
private boolean hasGeneratedListings;
|
private boolean hasGeneratedListings;
|
||||||
private Set<String> containedPaths;
|
private Set<String> containedPaths;
|
||||||
|
|
||||||
|
|
@ -54,22 +55,22 @@ public abstract class PathResourcePackMixin {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if(hasGeneratedListings)
|
if(hasGeneratedListings)
|
||||||
return;
|
return;
|
||||||
EnumMap<PackType, HashMap<String, List<Pair<Path, String>>>> rootListingByNamespaceAndType = new EnumMap<>(PackType.class);
|
EnumMap<PackType, HashMap<String, List<Triple<Integer, String, 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 = this.getNamespaces(type);
|
Set<String> namespaces = this.getNamespaces(type);
|
||||||
HashMap<String, List<Pair<Path, String>>> rootListingForNamespaces = new HashMap<>();
|
HashMap<String, List<Triple<Integer, String, String>>> 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<Pair<Path, String>> rootListingPaths = new ArrayList<>();
|
ArrayList<Triple<Integer, String, 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(Pair.of(path, slashJoiner.join(path)));
|
rootListingPaths.add(Triple.of(path.getNameCount(), path.getFileName().toString(), 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);
|
||||||
});
|
});
|
||||||
|
|
@ -89,6 +90,8 @@ public abstract class PathResourcePackMixin {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValidCachedResourcePath(Path path) {
|
private boolean isValidCachedResourcePath(Path path) {
|
||||||
|
if(path.getFileName() == null)
|
||||||
|
return false;
|
||||||
String str = path.toString();
|
String str = path.toString();
|
||||||
for(int i = 0; i < str.length(); i++) {
|
for(int i = 0; i < str.length(); i++) {
|
||||||
if(!ResourceLocation.validPathChar(str.charAt(i))) {
|
if(!ResourceLocation.validPathChar(str.charAt(i))) {
|
||||||
|
|
@ -134,13 +137,13 @@ public abstract class PathResourcePackMixin {
|
||||||
pathIn = pathIn + "/";
|
pathIn = pathIn + "/";
|
||||||
final String testPath = 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.getFirst().getNameCount() <= maxDepth). // Make sure the depth is within bounds
|
filter(path -> path.getLeft() <= maxDepth). // Make sure the depth is within bounds
|
||||||
filter(path -> path.getSecond().startsWith(testPath)). // Make sure the target path is inside this one
|
filter(path -> path.getRight().startsWith(testPath)). // Make sure the target path is inside this one
|
||||||
filter(path -> filter.test(path.getFirst().getFileName().toString())). // Test the file name against the predicate
|
filter(path -> filter.test(path.getMiddle())). // 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, path.getSecond())).
|
map(path -> new ResourceLocation(resourceNamespace, path.getRight())).
|
||||||
collect(Collectors.toList());
|
collect(Collectors.toList());
|
||||||
cir.setReturnValue(cachedListing);
|
cir.setReturnValue(cachedListing);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user