Merge remote-tracking branch 'origin/main' into 1.18
This commit is contained in:
commit
0e5b952ea6
|
|
@ -8,6 +8,7 @@ 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.CachedResourcePath;
|
||||
import org.embeddedt.modernfix.util.FileUtil;
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
|
|
@ -36,9 +37,9 @@ 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<Triple<Integer, String, String>>>> rootListingByNamespaceAndType;
|
||||
private EnumMap<PackType, HashMap<String, List<CachedResourcePath>>> rootListingByNamespaceAndType;
|
||||
private boolean hasGeneratedListings;
|
||||
private Set<String> containedPaths;
|
||||
private Set<CachedResourcePath> containedPaths;
|
||||
|
||||
private FileSystem resourcePackFS;
|
||||
|
||||
|
|
@ -55,24 +56,24 @@ public abstract class PathResourcePackMixin {
|
|||
synchronized (this) {
|
||||
if(hasGeneratedListings)
|
||||
return;
|
||||
EnumMap<PackType, HashMap<String, List<Triple<Integer, String, String>>>> rootListingByNamespaceAndType = new EnumMap<>(PackType.class);
|
||||
HashSet<String> containedPaths = new HashSet<>();
|
||||
EnumMap<PackType, HashMap<String, List<CachedResourcePath>>> rootListingByNamespaceAndType = new EnumMap<>(PackType.class);
|
||||
HashSet<CachedResourcePath> containedPaths = new HashSet<>();
|
||||
for(PackType type : PackType.values()) {
|
||||
Set<String> namespaces = this.getNamespaces(type);
|
||||
HashMap<String, List<Triple<Integer, String, String>>> rootListingForNamespaces = new HashMap<>();
|
||||
HashMap<String, List<CachedResourcePath>> rootListingForNamespaces = new HashMap<>();
|
||||
for(String namespace : namespaces) {
|
||||
try {
|
||||
Path root = this.resolve(type.getDirectory(), namespace).toAbsolutePath();
|
||||
try (Stream<Path> stream = Files.walk(root)) {
|
||||
ArrayList<Triple<Integer, String, String>> rootListingPaths = new ArrayList<>();
|
||||
ArrayList<CachedResourcePath> rootListingPaths = new ArrayList<>();
|
||||
stream
|
||||
.map(path -> root.relativize(path.toAbsolutePath()))
|
||||
.filter(this::isValidCachedResourcePath)
|
||||
.forEach(path -> {
|
||||
if(!path.toString().endsWith(".mcmeta"))
|
||||
rootListingPaths.add(Triple.of(path.getNameCount(), path.getFileName().toString(), slashJoiner.join(path)));
|
||||
rootListingPaths.add(new CachedResourcePath(path));
|
||||
String mergedPath = slashJoiner.join(type.getDirectory(), namespace, path);
|
||||
containedPaths.add(mergedPath);
|
||||
containedPaths.add(new CachedResourcePath(mergedPath));
|
||||
});
|
||||
rootListingPaths.trimToSize();
|
||||
rootListingForNamespaces.put(namespace, rootListingPaths);
|
||||
|
|
@ -122,7 +123,7 @@ public abstract class PathResourcePackMixin {
|
|||
@Inject(method = "hasResource(Ljava/lang/String;)Z", at = @At(value = "HEAD"), cancellable = true)
|
||||
private void useCacheForExistence(String path, CallbackInfoReturnable<Boolean> cir) {
|
||||
this.generateResourceCache();
|
||||
cir.setReturnValue(this.containedPaths.contains(FileUtil.normalize(path)));
|
||||
cir.setReturnValue(this.containedPaths.contains(new CachedResourcePath(path)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -137,13 +138,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.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
|
||||
filter(path -> path.getNameCount() <= maxDepth). // Make sure the depth is within bounds
|
||||
filter(path -> path.getFullPath().startsWith(testPath)). // Make sure the target path is inside this one
|
||||
filter(path -> filter.test(path.getFileName())). // 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.getRight())).
|
||||
map(path -> new ResourceLocation(resourceNamespace, path.getFullPath())).
|
||||
collect(Collectors.toList());
|
||||
cir.setReturnValue(cachedListing);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,73 @@
|
|||
package org.embeddedt.modernfix.util;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Interner;
|
||||
import com.google.common.collect.Interners;
|
||||
import com.google.common.collect.Streams;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class CachedResourcePath {
|
||||
private final ImmutableList<String> pathComponents;
|
||||
private int hashCode = 0;
|
||||
|
||||
private static final Interner<String> PATH_COMPONENT_INTERNER = Interners.newStrongInterner();
|
||||
private static final Splitter SLASH_SPLITTER = Splitter.on('/');
|
||||
private static final Joiner SLASH_JOINER = Joiner.on('/');
|
||||
private WeakReference<String> fullPathCache = new WeakReference<>(null);
|
||||
|
||||
public CachedResourcePath(Iterable<String> components) {
|
||||
ImmutableList.Builder<String> b = ImmutableList.builder();
|
||||
for(String s : components) {
|
||||
if(s == null || s.length() == 0)
|
||||
continue;
|
||||
b.add(PATH_COMPONENT_INTERNER.intern(s));
|
||||
}
|
||||
pathComponents = b.build();
|
||||
}
|
||||
|
||||
public CachedResourcePath(Path path) {
|
||||
this(() -> Streams.stream(path.iterator()).map(Path::toString).iterator());
|
||||
}
|
||||
|
||||
public CachedResourcePath(String s) {
|
||||
this(SLASH_SPLITTER.split(s));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = hashCode;
|
||||
if(result != 0)
|
||||
return result;
|
||||
hashCode = pathComponents.hashCode();
|
||||
return hashCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
CachedResourcePath that = (CachedResourcePath) o;
|
||||
return pathComponents.equals(that.pathComponents);
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return pathComponents.get(pathComponents.size() - 1);
|
||||
}
|
||||
|
||||
public int getNameCount() {
|
||||
return pathComponents.size();
|
||||
}
|
||||
|
||||
public String getFullPath() {
|
||||
String fPath = fullPathCache.get();
|
||||
if(fPath == null) {
|
||||
fPath = SLASH_JOINER.join(pathComponents);
|
||||
fullPathCache = new WeakReference<>(fPath);
|
||||
}
|
||||
return fPath;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user