chore: clean up events slightly
ci/woodpecker/push/docs Pipeline was successful Details
ci/woodpecker/push/jfmod Pipeline was successful Details

This commit is contained in:
Johannes Frohnmeyer 2024-04-24 20:58:11 +02:00
parent 8dba128843
commit 84b6588d57
Signed by: Johannes
GPG Key ID: E76429612C2929F4
3 changed files with 79 additions and 64 deletions

View File

@ -19,97 +19,108 @@ import net.minecraft.util.Identifier;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
public enum DirFilterEvents implements UserResourceEvents.Open, UserResourceEvents.FindResource { public enum DirFilterEvents implements UserResourceEvents.OpenRoot, UserResourceEvents.Open, UserResourceEvents.FindResource {
INSTANCE; INSTANCE;
public static void init() { public static void init() {
UserResourceEvents.OPEN_ROOT.register(INSTANCE);
UserResourceEvents.OPEN.register(INSTANCE); UserResourceEvents.OPEN.register(INSTANCE);
UserResourceEvents.FIND_RESOURCE.register(INSTANCE); UserResourceEvents.FIND_RESOURCE.register(INSTANCE);
} }
@Override
public InputSupplier<InputStream> openRoot(String[] fileName, InputSupplier<InputStream> previous, ResourcePack pack) {
if (skip(pack)) return previous;
return open(previous, pack, String.join("/", fileName));
}
@Override @Override
public InputSupplier<InputStream> open(ResourceType type, Identifier id, InputSupplier<InputStream> previous, ResourcePack pack) { public InputSupplier<InputStream> open(ResourceType type, Identifier id, InputSupplier<InputStream> previous, ResourcePack pack) {
if (!MetaCache.hasCapability(pack, PackCapability.DirFilter)) return previous; if (skip(pack)) return previous;
String path = new ResourcePath(type, id).getName(); return open(previous, pack, new ResourcePath(type, id).getName());
List<DirRpo> rpo = findDirRpos(pack, parent(path)); }
private InputSupplier<InputStream> open(InputSupplier<InputStream> previous, ResourcePack pack, String name) {
List<DirRpo> rpo = findDirRpos(pack, parent(name));
CacheKey key = MetaCache.getKeyByPack(pack); CacheKey key = MetaCache.getKeyByPack(pack);
RespackoptsFS fs = new RespackoptsFS(pack); RespackoptsFS fs = new RespackoptsFS(pack);
//TODO use pattern matching for switch return switch (DirRpoResult.compute(name, rpo, key, fs)) {
DirRpoResult result = DirRpoResult.compute(path, rpo, key, fs); case DirRpoResult.FixedState.ORIGINAL -> previous; // Using original file
if (result == DirRpoResult.ORIGINAL) return previous; // Using original file case DirRpoResult.FixedState.IGNORE -> null; // No fallback
if (result == DirRpoResult.IGNORE) return null; // No fallback case DirRpoResult.Replacement replacement -> {
// Use fallback // Use fallback
DirRpoResult.Replacement replacement = (DirRpoResult.Replacement) result; String fallback = replacement.toFallback(name);
String fallback = replacement.toFallback(path); MetaCache.addDependency(key, name, fallback);
MetaCache.addDependency(key, path, fallback); yield fs.open(fallback);
return fs.open(fallback); }
};
} }
@Override @Override
public ResourcePack.ResultConsumer findResources(ResourceType type, String namespace, String prefix, ResourcePack.ResultConsumer previous, ResourcePack pack) { public ResourcePack.ResultConsumer findResources(ResourceType type, String namespace, String prefix, ResourcePack.ResultConsumer previous, ResourcePack pack) {
// Warning: the Identifiers here DON'T CONTAIN THE TYPE! // Warning: the Identifiers here DON'T CONTAIN THE TYPE!
// Therefore, it needs to be added when calling a method that generates a ResourcePath! // Therefore, it needs to be added when calling a method that generates a ResourcePath!
if (!MetaCache.hasCapability(pack, PackCapability.DirFilter)) return previous; if (skip(pack)) return previous;
boolean dirFilterAdditive = MetaCache.hasCapability(pack, PackCapability.DirFilterAdditive); boolean dirFilterAdditive = MetaCache.hasCapability(pack, PackCapability.DirFilterAdditive);
String searchPrefix = type.getDirectory() + "/" + namespace + "/" + prefix; String searchPrefix = type.getDirectory() + "/" + namespace + "/" + prefix;
Set<String> additionalSearched = new HashSet<>(); Set<String> additionalSearched = new HashSet<>();
CacheKey key = MetaCache.getKeyByPack(pack); CacheKey key = MetaCache.getKeyByPack(pack);
RespackoptsFS fs = new RespackoptsFS(pack); RespackoptsFS fs = new RespackoptsFS(pack);
return (identifier, value) -> { return (identifier, value) -> {
String path = path(type, identifier); String path = new ResourcePath(type, identifier).getName();
//TODO use pattern matching for switch
List<DirRpo> relevantRpos = findDirRpos(pack, parent(path)); List<DirRpo> relevantRpos = findDirRpos(pack, parent(path));
DirRpoResult result = DirRpoResult.compute(path, relevantRpos, key, fs); switch (DirRpoResult.compute(path, relevantRpos, key, fs)) {
if (result == DirRpoResult.ORIGINAL) { // Using original file case DirRpoResult.FixedState.ORIGINAL -> {
previous.accept(identifier, value); // Using original file
return; previous.accept(identifier, value);
} }
if (result == DirRpoResult.IGNORE) return; // No fallback case DirRpoResult.FixedState.IGNORE -> {
// New search for fallback path // No fallback
DirRpoResult.Replacement replacement = (DirRpoResult.Replacement) result; }
String newPath = replacement.toFallback(path); case DirRpoResult.Replacement replacement -> {
if (newPath.split("/", 3).length != 3) { // New search for fallback path
Respackopts.LOGGER.error("Directory fallback path MUST be long enough to support representation as identifier (3 segments), but is too short: " + newPath); String newPath = replacement.toFallback(path);
return; if (newPath.split("/", 3).length != 3) {
} Respackopts.LOGGER.error("Directory fallback path MUST be long enough to support representation as identifier (3 segments), but is too short: " + newPath);
if (!dirFilterAdditive) { return;
// Only return this single result, don't search for others }
MetaCache.addDependency(key, path, newPath); if (!dirFilterAdditive) {
previous.accept(identifier, fs.open(newPath)); // Only return this single result, don't search for others
return; MetaCache.addDependency(key, path, newPath);
} previous.accept(identifier, fs.open(newPath));
// Find other files in the fallback directory return;
String fallbackDir = replacement.fallback.prefix(); }
if (!additionalSearched.add(fallbackDir)) return; // Already searched // Find other files in the fallback directory
int prefixSize = replacement.original.prefix().length(); String fallbackDir = replacement.fallback.prefix();
if (prefixSize < searchPrefix.length()) { if (!additionalSearched.add(fallbackDir)) return; // Already searched
if (!searchPrefix.startsWith(replacement.original.prefix())) { int prefixSize = replacement.original.prefix().length();
Respackopts.LOGGER.error("Unexpected prefix path " + replacement.original.prefix() + " for search prefix " + searchPrefix + ", skipping"); if (prefixSize < searchPrefix.length()) {
return; if (!searchPrefix.startsWith(replacement.original.prefix())) {
Respackopts.LOGGER.error("Unexpected prefix path " + replacement.original.prefix() + " for search prefix " + searchPrefix + ", skipping");
return;
}
fallbackDir += searchPrefix.substring(prefixSize);
} else if (!replacement.original.prefix().startsWith(searchPrefix)) {
Respackopts.LOGGER.error("Unexpected prefix path " + replacement.original.prefix() + " for search prefix " + searchPrefix + ", skipping");
return;
}
if (fallbackDir.split("/", 3).length != 3) {
Respackopts.LOGGER.error("Directory fallback path MUST be long enough to support representation as identifier (3 segments), but is too short: " + fallbackDir);
return;
}
ResourcePath rp = new ResourcePath(fallbackDir);
pack.findResources(rp.getType(), rp.getId().getNamespace(), rp.getId().getPath(), (resource, resVal) -> {
ResourceType type1 = rp.getType();
String fallbackPath = new ResourcePath(type1, resource).getName();
String orig = replacement.toOriginal(fallbackPath);
MetaCache.addDependency(key, orig, fallbackPath);
previous.accept(new ResourcePath(orig).getId(), resVal);
});
} }
fallbackDir += searchPrefix.substring(prefixSize);
} else if (!replacement.original.prefix().startsWith(searchPrefix)) {
Respackopts.LOGGER.error("Unexpected prefix path " + replacement.original.prefix() + " for search prefix " + searchPrefix + ", skipping");
return;
} }
if (fallbackDir.split("/", 3).length != 3) {
Respackopts.LOGGER.error("Directory fallback path MUST be long enough to support representation as identifier (3 segments), but is too short: " + fallbackDir);
return;
}
ResourcePath rp = new ResourcePath(fallbackDir);
pack.findResources(rp.getType(), rp.getId().getNamespace(), rp.getId().getPath(), (resource, resVal) -> {
String fallbackPath = path(rp.getType(), resource);
String orig = replacement.toOriginal(fallbackPath);
MetaCache.addDependency(key, orig, fallbackPath);
previous.accept(new ResourcePath(orig).getId(), resVal);
});
}; };
} }
private String path(ResourceType type, Identifier identifier) {
return type.getDirectory() + "/" + identifier.getNamespace() + "/" + identifier.getPath();
}
private String parent(String path) { private String parent(String path) {
int li = path.lastIndexOf('/'); int li = path.lastIndexOf('/');
return li <= 0 ? null : path.substring(0, li); return li <= 0 ? null : path.substring(0, li);
@ -154,4 +165,8 @@ public enum DirFilterEvents implements UserResourceEvents.Open, UserResourceEven
}); });
} }
} }
private boolean skip(ResourcePack pack) {
return !MetaCache.hasCapability(pack, PackCapability.DirFilter);
}
} }

View File

@ -83,6 +83,6 @@ public enum FileFilterEvents implements UserResourceEvents.OpenRoot, UserResourc
} }
private boolean skip(ResourcePack pack) { private boolean skip(ResourcePack pack) {
return !(pack instanceof AbstractFileResourcePack && MetaCache.hasCapability(pack, PackCapability.FileFilter)); return !MetaCache.hasCapability(pack, PackCapability.FileFilter);
} }
} }

View File

@ -23,7 +23,7 @@ public sealed interface DirRpoResult {
static DirRpoResult compute(String file, List<DirRpo> rpos, CacheKey key, RespackoptsFS fs) { static DirRpoResult compute(String file, List<DirRpo> rpos, CacheKey key, RespackoptsFS fs) {
int version = MetaCache.getMeta(key).version; int version = MetaCache.getMeta(key).version;
if (version < 11) rpos = rpos.isEmpty() ? rpos : List.of(rpos.get(rpos.size() - 1)); if (version < 11) rpos = rpos.isEmpty() ? rpos : List.of(rpos.getLast());
LazySupplier<Scope> scope = new LazySupplier<>(() -> MetaCache.getScope(key, fs)); LazySupplier<Scope> scope = new LazySupplier<>(() -> MetaCache.getScope(key, fs));
for (DirRpo rpo : rpos) { for (DirRpo rpo : rpos) {
if (rpo.condition == null) continue; if (rpo.condition == null) continue;