First attempt at directory conditions. Very slow but I don't care
This commit is contained in:
parent
f6228b3aa4
commit
cc60048dd4
@ -6,7 +6,7 @@ minecraft_version=1.17.1
|
|||||||
yarn_mappings=build.43
|
yarn_mappings=build.43
|
||||||
loader_version=0.11.6
|
loader_version=0.11.6
|
||||||
# Mod Properties
|
# Mod Properties
|
||||||
mod_version=2.4.0
|
mod_version=2.5.0
|
||||||
maven_group=io.gitlab.jfronny
|
maven_group=io.gitlab.jfronny
|
||||||
archives_base_name=respackopts
|
archives_base_name=respackopts
|
||||||
|
|
||||||
|
4
run/resourcepacks/testpack/assets/minecraft/lang/.rpo
Normal file
4
run/resourcepacks/testpack/assets/minecraft/lang/.rpo
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"conditions": ["false"],
|
||||||
|
"fallback": "assets/minecraft/langer"
|
||||||
|
}
|
@ -3,7 +3,8 @@ package io.gitlab.jfronny.respackopts;
|
|||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import io.gitlab.jfronny.respackopts.data.entry.*;
|
import io.gitlab.jfronny.respackopts.data.entry.*;
|
||||||
import io.gitlab.jfronny.respackopts.filters.FilterProvider;
|
import io.gitlab.jfronny.respackopts.filters.DirFilterProvider;
|
||||||
|
import io.gitlab.jfronny.respackopts.filters.FileFilterProvider;
|
||||||
import io.gitlab.jfronny.respackopts.gson.*;
|
import io.gitlab.jfronny.respackopts.gson.*;
|
||||||
import meteordevelopment.starscript.Script;
|
import meteordevelopment.starscript.Script;
|
||||||
import meteordevelopment.starscript.StandardLib;
|
import meteordevelopment.starscript.StandardLib;
|
||||||
@ -85,7 +86,8 @@ public class Respackopts implements ClientModInitializer {
|
|||||||
STAR_SCRIPT.set(sanitizeString(e.getKey()), () -> e.getValue().buildStarscript());
|
STAR_SCRIPT.set(sanitizeString(e.getKey()), () -> e.getValue().buildStarscript());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
FilterProvider.init();
|
DirFilterProvider.init();
|
||||||
|
FileFilterProvider.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void save() {
|
public static void save() {
|
||||||
|
12
src/main/java/io/gitlab/jfronny/respackopts/data/DirRpo.java
Normal file
12
src/main/java/io/gitlab/jfronny/respackopts/data/DirRpo.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package io.gitlab.jfronny.respackopts.data;
|
||||||
|
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import io.gitlab.jfronny.libjf.gson.GsonHidden;
|
||||||
|
|
||||||
|
public class DirRpo {
|
||||||
|
public JsonArray conditions;
|
||||||
|
public String fallback;
|
||||||
|
|
||||||
|
@GsonHidden
|
||||||
|
public String path;
|
||||||
|
}
|
@ -58,6 +58,11 @@ public class ConfigBranch extends ConfigEntry<Map<String, ConfigEntry<?>>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (mode == SyncMode.RESPACK_LOAD)
|
||||||
|
for (Map.Entry<String, ConfigEntry<?>> e : super.getValue().entrySet()) {
|
||||||
|
if (!source.getValue().containsKey(e.getKey()))
|
||||||
|
getValue().remove(e.getKey());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> void syncSub(ConfigEntry<T> current, ConfigEntry<T> next, SyncMode mode) {
|
private <T> void syncSub(ConfigEntry<T> current, ConfigEntry<T> next, SyncMode mode) {
|
||||||
|
@ -0,0 +1,100 @@
|
|||||||
|
package io.gitlab.jfronny.respackopts.filters;
|
||||||
|
|
||||||
|
import io.gitlab.jfronny.libjf.data.ResourcePath;
|
||||||
|
import io.gitlab.jfronny.libjf.data.UserResourceEvents;
|
||||||
|
import io.gitlab.jfronny.libjf.data.WrappedPack;
|
||||||
|
import io.gitlab.jfronny.respackopts.Respackopts;
|
||||||
|
import io.gitlab.jfronny.respackopts.data.DirRpo;
|
||||||
|
import io.gitlab.jfronny.respackopts.data.RpoError;
|
||||||
|
import io.gitlab.jfronny.respackopts.filters.conditions.ConditionEvaluator;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class DirFilterProvider {
|
||||||
|
public static void init() {
|
||||||
|
UserResourceEvents.OPEN.register((type, id, previous, pack) -> {
|
||||||
|
String path = new ResourcePath(type, id).getName();
|
||||||
|
DirRpo rpo = findDirRpo(pack, path);
|
||||||
|
if (rpo != null && dirHidden(rpo)) {
|
||||||
|
path = findReplacementDir(path, rpo);
|
||||||
|
if (path == null) throw new FileNotFoundException();
|
||||||
|
ResourcePath rp = new ResourcePath(path);
|
||||||
|
return pack.open(rp.getType(), rp.getId());
|
||||||
|
}
|
||||||
|
return previous;
|
||||||
|
});
|
||||||
|
UserResourceEvents.FIND_RESOURCE.register((type, namespace, prefix, maxDepth, pathFilter, previous, pack) -> {
|
||||||
|
// Warning: the Identifiers here DON'T CONTAIN THE TYPE!
|
||||||
|
// Therefore, it needs to be added when calling a method that generates a ResourcePath!
|
||||||
|
//TODO support files that weren't there at the original location
|
||||||
|
for (Identifier identifier : previous) {
|
||||||
|
String path = type.getDirectory() + "/" + identifier.getNamespace() + "/" + identifier.getPath();
|
||||||
|
DirRpo rpo = findDirRpo(pack, path);
|
||||||
|
if (rpo != null && dirHidden(rpo)) {
|
||||||
|
path = findReplacementDir(path, rpo);
|
||||||
|
if (path == null)
|
||||||
|
previous.remove(identifier);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return previous;
|
||||||
|
});
|
||||||
|
UserResourceEvents.CONTAINS.register((type, id, previous, pack) -> {
|
||||||
|
String path = new ResourcePath(type, id).getName();
|
||||||
|
DirRpo rpo = findDirRpo(pack, path);
|
||||||
|
if (rpo != null && dirHidden(rpo)) {
|
||||||
|
path = findReplacementDir(path, rpo);
|
||||||
|
if (path == null)
|
||||||
|
return false;
|
||||||
|
ResourcePath rp = new ResourcePath(path);
|
||||||
|
return pack.contains(rp.getType(), rp.getId());
|
||||||
|
}
|
||||||
|
return previous;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String findReplacementDir(String dir, DirRpo rpo) {
|
||||||
|
if (rpo.fallback == null) return null;
|
||||||
|
return dir.replace(rpo.path + "/", rpo.fallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean dirHidden(DirRpo rpo) {
|
||||||
|
if (rpo.conditions == null)
|
||||||
|
return false;
|
||||||
|
try {
|
||||||
|
return !ConditionEvaluator.evaluate(rpo.conditions);
|
||||||
|
} catch (RpoError e) {
|
||||||
|
Respackopts.LOGGER.error("Couldn't parse dir conditions", e);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static DirRpo findDirRpo(WrappedPack pack, String name) {
|
||||||
|
int li = name.lastIndexOf('/');
|
||||||
|
if (li <= 0)
|
||||||
|
return null;
|
||||||
|
name = name.substring(0, li);
|
||||||
|
DirRpo drp = findDirRpo(pack, name);
|
||||||
|
if (drp != null)
|
||||||
|
return drp;
|
||||||
|
ResourcePath rp;
|
||||||
|
try {
|
||||||
|
rp = new ResourcePath(name + "/.rpo");
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (pack.getUnderlying().contains(rp.getType(), rp.getId())) {
|
||||||
|
try (InputStream stream = pack.getUnderlying().open(rp.getType(), rp.getId()); Reader w = new InputStreamReader(stream)) {
|
||||||
|
drp = Respackopts.GSON.fromJson(w, DirRpo.class);
|
||||||
|
drp.path = name;
|
||||||
|
if (drp.fallback != null && !drp.fallback.endsWith("/"))
|
||||||
|
drp.fallback += "/";
|
||||||
|
return drp;
|
||||||
|
} catch (IOException e) {
|
||||||
|
Respackopts.LOGGER.error("Couldn't open dir rpo", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -13,22 +13,25 @@ import java.io.FileNotFoundException;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
public class FilterProvider {
|
public class FileFilterProvider {
|
||||||
public static void init() {
|
public static void init() {
|
||||||
AtomicBoolean containsFileWasFallback = new AtomicBoolean(false);
|
AtomicBoolean containsFileWasFallback = new AtomicBoolean(false);
|
||||||
UserResourceEvents.OPEN.register((type, id, previous, pack) -> {
|
UserResourceEvents.OPEN.register((type, id, previous, pack) -> {
|
||||||
if (skip(pack)) return previous;
|
if (skip(pack)) return previous;
|
||||||
String name = new ResourcePath(type, id).getName();
|
String name = new ResourcePath(type, id).getName();
|
||||||
IOException ex = new FileNotFoundException();
|
IOException ex = new FileNotFoundException();
|
||||||
if (pack.contains(type, id) && containsFileWasFallback.get()) {
|
if (pack.contains(type, id)) {
|
||||||
|
if (containsFileWasFallback.get())
|
||||||
return FallbackFilter.getReplacement(pack, name, ex);
|
return FallbackFilter.getReplacement(pack, name, ex);
|
||||||
}
|
|
||||||
return DataExpander.replace(previous, pack, name, ex);
|
return DataExpander.replace(previous, pack, name, ex);
|
||||||
|
}
|
||||||
|
throw ex;
|
||||||
});
|
});
|
||||||
UserResourceEvents.FIND_RESOURCE.register((type, namespace, prefix, maxDepth, pathFilter, previous, pack) -> {
|
UserResourceEvents.FIND_RESOURCE.register((type, namespace, prefix, maxDepth, pathFilter, previous, pack) -> {
|
||||||
// Warning: the Identifiers here DON'T CONTAIN THE TYPE! Therefore, it needs to be added when calling a method that generates a ResourcePath!
|
// Warning: the Identifiers here DON'T CONTAIN THE TYPE!
|
||||||
|
// Therefore, it needs to be added when calling a method that generates a ResourcePath!
|
||||||
if (skip(pack)) return previous;
|
if (skip(pack)) return previous;
|
||||||
previous.removeIf(s -> ResourcePackFilter.fileHidden(pack, type.getDirectory() + "/" + s.getPath()) && !FallbackFilter.fileVisible(pack, namespace));
|
previous.removeIf(s -> ResourcePackFilter.fileHidden(pack, type.getDirectory() + "/" + s.getNamespace() + "/" + s.getPath()) && !FallbackFilter.fileVisible(pack, namespace));
|
||||||
// Completion of the path is handled separately here
|
// Completion of the path is handled separately here
|
||||||
FallbackFilter.addFallbackResources(pack, previous, namespace, type);
|
FallbackFilter.addFallbackResources(pack, previous, namespace, type);
|
||||||
return previous;
|
return previous;
|
Loading…
Reference in New Issue
Block a user