Fix resource pack cache stability

This commit is contained in:
kltyton
2026-07-02 05:13:55 +08:00
parent 6fd505c867
commit 9873074847
2 changed files with 138 additions and 68 deletions
@@ -17,7 +17,9 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import javax.annotation.Nullable;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -29,8 +31,11 @@ public abstract class FilePackResourcesMixin implements IPackResources {
@Shadow
@Nullable
protected abstract ZipFile getOrCreateZipFile();
@Shadow
@Nullable
private ZipFile zipFile;
@Unique
private final Map<PackType, List<ZipEntry>> lightspeed$entriesByPackType = Maps.newConcurrentMap();
private final Map<PackType, List<String>> lightspeed$entriesByPackType = Maps.newConcurrentMap();
@Inject(method = "<init>", at = @At("RETURN"))
public void initReturnInjected(String name, File file, boolean builtin, CallbackInfo ci) {
@@ -43,7 +48,7 @@ public abstract class FilePackResourcesMixin implements IPackResources {
if (!GlobalCache.isEnabled || FusionPackCompat.hasOverrides(this))
return;
ZipFile zip = this.getOrCreateZipFile();
ZipFile zip = lightspeed$getOpenZipFile();
if (zip == null) {
return;
}
@@ -51,28 +56,67 @@ public abstract class FilePackResourcesMixin implements IPackResources {
String s = packType.getDirectory() + "/" + namespace + "/";
String s1 = s + path + "/";
List<ZipEntry> entries;
List<String> entries;
if ((entries = lightspeed$entriesByPackType.get(packType)) == null) {
entries = zip.stream()
.filter(e -> !e.isDirectory())
.map(ZipEntry::getName)
.collect(Collectors.toList());
lightspeed$entriesByPackType.put(packType, entries);
}
entries.stream()
.filter(e -> e.getName().startsWith(s1))
.filter(entry -> entry.startsWith(s1))
.forEach(entry -> {
String s3 = entry.getName().substring(s.length());
String s3 = entry.substring(s.length());
ResourceLocation resourcelocation = ResourceLocation.tryBuild(namespace, s3);
if (resourcelocation != null) {
resourceOutput.accept(resourcelocation, IoSupplier.create(zip, entry));
resourceOutput.accept(resourcelocation, lightspeed$openResource(packType, resourcelocation));
}
});
ci.cancel();
}
@Unique
private ZipFile lightspeed$getOpenZipFile() {
ZipFile zip = this.getOrCreateZipFile();
if (zip == null || lightspeed$isOpen(zip)) {
return zip;
}
this.zipFile = null;
zip = this.getOrCreateZipFile();
return zip != null && lightspeed$isOpen(zip) ? zip : null;
}
@Unique
private static boolean lightspeed$isOpen(ZipFile zip) {
try {
zip.size();
return true;
} catch (IllegalStateException ignored) {
return false;
}
}
@Unique
private IoSupplier<InputStream> lightspeed$openResource(PackType packType, ResourceLocation location) {
return () -> {
ZipFile zip = lightspeed$getOpenZipFile();
if (zip == null) {
throw new FileNotFoundException(location.toString());
}
String entryName = packType.getDirectory() + "/" + location.getNamespace() + "/" + location.getPath();
ZipEntry entry = zip.getEntry(entryName);
if (entry == null) {
throw new FileNotFoundException(location.toString());
}
return zip.getInputStream(entry);
};
}
@Override
public void lightspeed$persistAndClearCache() {
lightspeed$entriesByPackType.clear();
@@ -47,9 +47,6 @@ public abstract class PathResourcePackMixin implements IPathResourcePack, IPackR
@Unique
private Map<PackType, Set<String>> lightspeed$namespacesByPackType;
@Unique
private Map<PackType, Map<String, List<Path>>> lightspeed$filePathsByRootByPackType;
@Unique
private Map<PackType, Map<String, List<String>>> lightspeed$relativeFilePathsByPackType;
@@ -64,21 +61,10 @@ public abstract class PathResourcePackMixin implements IPathResourcePack, IPackR
if (GlobalCache.isEnabled) {
lightspeed$resolvedPaths();
lightspeed$namespaces();
lightspeed$filePathsByRoot();
GlobalCache.add(this);
}
}
@Unique
private static Map<PackType, Map<String, List<Path>>> lightspeed$initPathsMap() {
Map<PackType, Map<String, List<Path>>> map = Maps.newConcurrentMap();
for (PackType packType : PackType.values()) {
map.put(packType, Maps.newConcurrentMap());
}
return map;
}
@Inject(method = "resolve", at = @At("HEAD"), cancellable = true, remap = false)
public void resolveHeadInjected(String[] paths, CallbackInfoReturnable<Path> cir) {
if (lightspeed$modFile == null) {
@@ -94,8 +80,12 @@ public abstract class PathResourcePackMixin implements IPathResourcePack, IPackR
return;
}
Path path = lightspeed$getResolvedPath(paths);
if (path == null)
lightspeed$resolvedPaths().put(Arrays.toString(paths), path = lightspeed$modFile.findResource(paths));
if (path == null) {
path = lightspeed$modFile.findResource(paths);
if (lightspeed$isUsablePath(path)) {
lightspeed$resolvedPaths().put(Arrays.toString(paths), path);
}
}
cir.setReturnValue(path);
}
@@ -104,7 +94,10 @@ public abstract class PathResourcePackMixin implements IPathResourcePack, IPackR
public void resolveReturnInjected(String[] paths, CallbackInfoReturnable<Path> cir) {
if (!GlobalCache.isEnabled || lightspeed$modFile != null)
return;
lightspeed$resolvedPaths().put(Arrays.toString(paths), cir.getReturnValue());
Path path = cir.getReturnValue();
if (lightspeed$isUsablePath(path)) {
lightspeed$resolvedPaths().put(Arrays.toString(paths), path);
}
}
@Inject(method = "getNamespaces", at = @At("HEAD"), cancellable = true)
@@ -134,8 +127,7 @@ public abstract class PathResourcePackMixin implements IPathResourcePack, IPackR
if (exists != null) {
if (exists) {
Path path = this.resolve(paths);
cir.setReturnValue(IoSupplier.create(path));
cir.setReturnValue(lightspeed$openRootResource(paths.clone()));
} else {
cir.setReturnValue(null);
}
@@ -158,27 +150,41 @@ public abstract class PathResourcePackMixin implements IPathResourcePack, IPackR
return;
}
boolean[] fallbackToVanilla = {false};
FileUtil.decomposePath(path).get().ifLeft(parts -> {
Path root = resolve(type.getDirectory(), namespace).toAbsolutePath();
Path requestedRoot = FileUtil.resolvePath(root, parts);
List<Path> cachedPaths = lightspeed$getFilePaths(type, namespace, root);
for (Path candidate : cachedPaths) {
if (!candidate.startsWith(requestedRoot)) {
continue;
try {
Path root = resolve(type.getDirectory(), namespace).toAbsolutePath();
Path requestedRoot = FileUtil.resolvePath(root, parts);
List<String> cachedPaths = lightspeed$getFilePaths(type, namespace, root);
if (cachedPaths == null) {
fallbackToVanilla[0] = true;
return;
}
String relativePath = root.relativize(candidate).toString().replace(candidate.getFileSystem().getSeparator(), "/");
ResourceLocation location = ResourceLocation.tryBuild(namespace, relativePath);
if (location == null) {
Util.logAndPauseIfInIde(String.format(Locale.ROOT, "Invalid path in pack: %s:%s, ignoring", namespace, relativePath));
} else {
resourceOutput.accept(location, IoSupplier.create(candidate));
for (String cachedPath : cachedPaths) {
Path candidate = root.resolve(cachedPath);
if (!candidate.startsWith(requestedRoot)) {
continue;
}
String resourcePath = cachedPath.replace(candidate.getFileSystem().getSeparator(), "/");
ResourceLocation location = ResourceLocation.tryBuild(namespace, resourcePath);
if (location == null) {
Util.logAndPauseIfInIde(String.format(Locale.ROOT, "Invalid path in pack: %s:%s, ignoring", namespace, resourcePath));
} else {
resourceOutput.accept(location, lightspeed$openResource(type, location));
}
}
} catch (RuntimeException e) {
fallbackToVanilla[0] = true;
LOGGER.warn("Lightspeed path index lookup failed for {}:{}; falling back to vanilla resource enumeration",
namespace, path, e);
}
}).ifRight(dataResult -> LOGGER.error("Invalid path {}: {}", path, dataResult.message()));
ci.cancel();
if (!fallbackToVanilla[0]) {
ci.cancel();
}
}
@Override
@@ -191,7 +197,6 @@ public abstract class PathResourcePackMixin implements IPathResourcePack, IPackR
lightspeed$getExistenceByResource().clear();
lightspeed$resolvedPaths().clear();
lightspeed$namespaces().clear();
lightspeed$filePathsByRoot().clear();
lightspeed$relativeFilePaths().clear();
}
@@ -236,7 +241,13 @@ public abstract class PathResourcePackMixin implements IPathResourcePack, IPackR
@Unique
public Path lightspeed$getResolvedPath(String... paths) {
return lightspeed$resolvedPaths().get(Arrays.toString(paths));
String key = Arrays.toString(paths);
Path path = lightspeed$resolvedPaths().get(key);
if (path != null && !lightspeed$isUsablePath(path)) {
lightspeed$resolvedPaths().remove(key);
return null;
}
return path;
}
@@ -252,23 +263,18 @@ public abstract class PathResourcePackMixin implements IPathResourcePack, IPackR
}
@Unique
public List<Path> lightspeed$getFilePaths(PackType packType, String resourceNamespace, Path root) {
return lightspeed$getFilePathsMap(packType).computeIfAbsent(resourceNamespace, ignored -> {
List<String> cachedRelativePaths = lightspeed$getRelativeFilePathsMap(packType).get(resourceNamespace);
if (cachedRelativePaths != null) {
return cachedRelativePaths.stream()
.map(root::resolve)
.collect(Collectors.toList());
}
List<Path> scannedPaths = lightspeed$scanFilePaths(root);
lightspeed$getRelativeFilePathsMap(packType).put(resourceNamespace, lightspeed$toRelativePaths(root, scannedPaths));
return scannedPaths;
});
}
public List<String> lightspeed$getFilePaths(PackType packType, String resourceNamespace, Path root) {
Map<String, List<String>> relativePaths = lightspeed$getRelativeFilePathsMap(packType);
List<String> cachedRelativePaths = relativePaths.get(resourceNamespace);
if (cachedRelativePaths != null) {
return cachedRelativePaths;
}
@Unique
private Map<String, List<Path>> lightspeed$getFilePathsMap(PackType packType) {
return lightspeed$filePathsByRoot().computeIfAbsent(packType, ignored -> Maps.newConcurrentMap());
List<String> scannedPaths = lightspeed$scanRelativeFilePaths(root);
if (scannedPaths != null) {
relativePaths.put(resourceNamespace, scannedPaths);
}
return scannedPaths;
}
@Unique
@@ -303,14 +309,6 @@ public abstract class PathResourcePackMixin implements IPathResourcePack, IPackR
return lightspeed$namespacesByPackType;
}
@Unique
private Map<PackType, Map<String, List<Path>>> lightspeed$filePathsByRoot() {
if (lightspeed$filePathsByRootByPackType == null) {
lightspeed$filePathsByRootByPackType = lightspeed$initPathsMap();
}
return lightspeed$filePathsByRootByPackType;
}
@Unique
private Map<PackType, Map<String, List<String>>> lightspeed$relativeFilePaths() {
if (lightspeed$relativeFilePathsByPackType == null) {
@@ -323,14 +321,17 @@ public abstract class PathResourcePackMixin implements IPathResourcePack, IPackR
}
@Unique
private List<Path> lightspeed$scanFilePaths(Path root) {
private List<String> lightspeed$scanRelativeFilePaths(Path root) {
try (Stream<Path> stream = Files.find(root, Integer.MAX_VALUE, (candidate, attributes) -> attributes.isRegularFile())) {
return stream.collect(Collectors.toList());
return lightspeed$toRelativePaths(root, stream.collect(Collectors.toList()));
} catch (NoSuchFileException ignored) {
return Collections.emptyList();
} catch (IOException e) {
LOGGER.error("Failed to list path {}", root, e);
return Collections.emptyList();
return null;
} catch (RuntimeException e) {
LOGGER.warn("Failed to list path {}; falling back to vanilla resource enumeration", root, e);
return null;
}
}
@@ -364,4 +365,29 @@ public abstract class PathResourcePackMixin implements IPathResourcePack, IPackR
return Collections.emptySet();
}
}
@Unique
private IoSupplier<InputStream> lightspeed$openResource(PackType type, ResourceLocation location) {
return () -> {
IoSupplier<InputStream> supplier = ((PathPackResources) (Object) this).getResource(type, location);
if (supplier == null) {
throw new NoSuchFileException(location.toString());
}
return supplier.get();
};
}
@Unique
private IoSupplier<InputStream> lightspeed$openRootResource(String[] paths) {
return () -> Files.newInputStream(this.resolve(paths));
}
@Unique
private static boolean lightspeed$isUsablePath(Path path) {
try {
return path != null && path.getFileSystem().isOpen();
} catch (RuntimeException ignored) {
return false;
}
}
}