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.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.FileUtil;
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
|
|
@ -35,7 +36,7 @@ public abstract class PathResourcePackMixin {
|
|||
@Shadow @Final private String packName;
|
||||
@Shadow @Final private Path source;
|
||||
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 Set<String> containedPaths;
|
||||
|
||||
|
|
@ -54,22 +55,22 @@ public abstract class PathResourcePackMixin {
|
|||
synchronized (this) {
|
||||
if(hasGeneratedListings)
|
||||
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<>();
|
||||
for(PackType type : PackType.values()) {
|
||||
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) {
|
||||
try {
|
||||
Path root = this.resolve(type.getDirectory(), namespace).toAbsolutePath();
|
||||
try (Stream<Path> stream = Files.walk(root)) {
|
||||
ArrayList<Pair<Path, String>> rootListingPaths = new ArrayList<>();
|
||||
ArrayList<Triple<Integer, String, String>> rootListingPaths = new ArrayList<>();
|
||||
stream
|
||||
.map(path -> root.relativize(path.toAbsolutePath()))
|
||||
.filter(this::isValidCachedResourcePath)
|
||||
.forEach(path -> {
|
||||
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);
|
||||
containedPaths.add(mergedPath);
|
||||
});
|
||||
|
|
@ -89,6 +90,8 @@ public abstract class PathResourcePackMixin {
|
|||
}
|
||||
|
||||
private boolean isValidCachedResourcePath(Path path) {
|
||||
if(path.getFileName() == null)
|
||||
return false;
|
||||
String str = path.toString();
|
||||
for(int i = 0; i < str.length(); i++) {
|
||||
if(!ResourceLocation.validPathChar(str.charAt(i))) {
|
||||
|
|
@ -134,13 +137,13 @@ public abstract class PathResourcePackMixin {
|
|||
pathIn = pathIn + "/";
|
||||
final String testPath = pathIn;
|
||||
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.getSecond().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 -> 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
|
||||
// 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.getSecond())).
|
||||
map(path -> new ResourceLocation(resourceNamespace, path.getRight())).
|
||||
collect(Collectors.toList());
|
||||
cir.setReturnValue(cachedListing);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user