Use libjf pack wrap
This commit is contained in:
parent
4966024847
commit
0a20db4de2
|
@ -1,7 +1,7 @@
|
|||
apply from: "https://gitlab.com/-/snippets/2121059/raw/master/jfbase.gradle"
|
||||
|
||||
repositories {
|
||||
maven { url = "https://maven.terraformersmc.com/"; name = "ModMenu" }
|
||||
maven { url = "https://raw.githubusercontent.com/TerraformersMC/Archive/main/releases/"; name = "ModMenu" }
|
||||
maven { url = "https://maven.shedaniel.me/"; name = "Cloth Config" }
|
||||
maven { url = "https://maven.dblsaiko.net/"; name = "Canvas" }
|
||||
maven { url = 'https://server.bbkr.space/artifactory/libs-release'; name = "Required for canvas" }
|
||||
|
@ -27,6 +27,8 @@ dependencies {
|
|||
exclude(group: "me.shedaniel.cloth")
|
||||
}*/
|
||||
|
||||
download("https://gitlab.com/jfmods/LibJF/-/jobs/artifacts/master/raw/latest-dev.jar?job=build_test", "libjf")
|
||||
|
||||
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
|
||||
}
|
||||
|
||||
|
|
|
@ -11,8 +11,8 @@ maven_group=io.gitlab.jfronny
|
|||
archives_base_name=respackopts
|
||||
|
||||
modrinth_id=TiF5QWZY
|
||||
modrinth_required_dependencies=mzVbb1XI
|
||||
modrinth_required_dependencies=mzVbb1XI, dOW0jmMj
|
||||
modrinth_optional_dependencies=
|
||||
curseforge_id=430090
|
||||
curseforge_required_dependencies=cloth-config, modmenu
|
||||
curseforge_required_dependencies=cloth-config, modmenu, libjf
|
||||
curseforge_optional_dependencies=
|
||||
|
|
|
@ -3,6 +3,7 @@ package io.gitlab.jfronny.respackopts;
|
|||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import io.gitlab.jfronny.respackopts.data.entry.*;
|
||||
import io.gitlab.jfronny.respackopts.filters.FilterProvider;
|
||||
import io.gitlab.jfronny.respackopts.gson.BooleanEntrySerializer;
|
||||
import io.gitlab.jfronny.respackopts.gson.ConfigBranchSerializer;
|
||||
import io.gitlab.jfronny.respackopts.gson.EnumEntrySerializer;
|
||||
|
@ -66,6 +67,7 @@ public class Respackopts implements ClientModInitializer {
|
|||
}
|
||||
if (FabricLoader.getInstance().isDevelopmentEnvironment())
|
||||
SAVE_ACTIONS.add(() -> LOGGER.info("Save"));
|
||||
FilterProvider.init();
|
||||
}
|
||||
|
||||
public static void save() {
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
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.filters.conditions.ResourcePackFilter;
|
||||
import io.gitlab.jfronny.respackopts.filters.fallback.FallbackFilter;
|
||||
import net.minecraft.resource.AbstractFileResourcePack;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class FilterProvider {
|
||||
public static void init() {
|
||||
AtomicBoolean containsFileWasFallback = new AtomicBoolean(false);
|
||||
UserResourceEvents.OPEN.register((type, id, previous, pack) -> {
|
||||
if (skip(pack)) return previous;
|
||||
if (pack.contains(type, id) && containsFileWasFallback.get()) {
|
||||
return FallbackFilter.getReplacement(pack, new ResourcePath(type, id).getName(), new FileNotFoundException());
|
||||
}
|
||||
return previous;
|
||||
});
|
||||
UserResourceEvents.FIND_RESOURCE.register((type, namespace, prefix, maxDepth, pathFilter, previous, pack) -> {
|
||||
if (skip(pack)) return previous;
|
||||
previous.removeIf(s -> ResourcePackFilter.fileHidden(pack, s.getPath()) && !FallbackFilter.fileVisible(pack, namespace));
|
||||
FallbackFilter.addFallbackResources(pack, previous, namespace);
|
||||
return previous;
|
||||
});
|
||||
UserResourceEvents.CONTAINS.register((type, id, previous, pack) -> {
|
||||
if (skip(pack)) return previous;
|
||||
containsFileWasFallback.set(false);
|
||||
String name = new ResourcePath(type, id).getName();
|
||||
if (previous) {
|
||||
if (ResourcePackFilter.fileHidden(pack, name)) {
|
||||
if (FallbackFilter.fileVisible(pack, name)) {
|
||||
containsFileWasFallback.set(true);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
if (pack.getUnderlying().contains(type, new Identifier(id.getNamespace(), id.getPath() + ".rpo")) && FallbackFilter.fileVisible(pack, name)) {
|
||||
containsFileWasFallback.set(true);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static boolean skip(WrappedPack pack) {
|
||||
return !(pack.getUnderlying() instanceof AbstractFileResourcePack);
|
||||
}
|
||||
}
|
|
@ -1,123 +0,0 @@
|
|||
package io.gitlab.jfronny.respackopts.filters;
|
||||
|
||||
import io.gitlab.jfronny.respackopts.filters.lambda.FileContainedProvider;
|
||||
import io.gitlab.jfronny.respackopts.filters.lambda.Tuple;
|
||||
import io.gitlab.jfronny.respackopts.filters.conditions.ResourcePackFilter;
|
||||
import io.gitlab.jfronny.respackopts.filters.fallback.FallbackFilter;
|
||||
import io.gitlab.jfronny.respackopts.filters.lambda.FileOpenProvider;
|
||||
import io.gitlab.jfronny.respackopts.filters.lambda.PathSplitter;
|
||||
import net.minecraft.resource.ResourcePack;
|
||||
import net.minecraft.resource.ResourceType;
|
||||
import net.minecraft.resource.metadata.ResourceMetadataReader;
|
||||
import net.minecraft.util.Identifier;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class WrappedPack implements ResourcePack {
|
||||
public WrappedPack(ResourcePack pack) {
|
||||
this.pack = pack;
|
||||
PathSplitter pathGetter = s -> {
|
||||
String[] s1 = s.split("/", 3);
|
||||
if (s1.length != 3) {
|
||||
throw new IOException("Could not split path string into resource type and ID due to insufficient length: " + s);
|
||||
}
|
||||
ResourceType rt = switch (s1[0]) {
|
||||
case "assets" -> ResourceType.CLIENT_RESOURCES;
|
||||
case "data" -> ResourceType.SERVER_DATA;
|
||||
default -> throw new IllegalStateException("Unexpected value for resource type: " + s1[0] + " in: " + s);
|
||||
};
|
||||
return new Tuple<>(rt, new Identifier(s1[1], s1[2]));
|
||||
};
|
||||
FileContainedProvider contains = s -> {
|
||||
Tuple<ResourceType, Identifier> p = pathGetter.split(s);
|
||||
return this.contains(p.left(), p.right());
|
||||
};
|
||||
FileOpenProvider open = s -> {
|
||||
Tuple<ResourceType, Identifier> p = pathGetter.split(s);
|
||||
return this.open(p.left(), p.right());
|
||||
};
|
||||
rpo = new ResourcePackFilter(contains, open);
|
||||
fbt = new FallbackFilter(contains, open);
|
||||
}
|
||||
|
||||
ResourcePack pack;
|
||||
boolean containsFileWasFallback = false;
|
||||
ResourcePackFilter rpo;
|
||||
FallbackFilter fbt;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public InputStream openRoot(String fileName) throws IOException {
|
||||
return pack.openRoot(fileName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream open(ResourceType type, Identifier id) throws IOException {
|
||||
if (contains(type, id) && containsFileWasFallback) {
|
||||
return fbt.getReplacement(getFilename(type, id), new FileNotFoundException());
|
||||
}
|
||||
return pack.open(type, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Identifier> findResources(ResourceType type, String namespace, String prefix, int maxDepth, Predicate<String> pathFilter) {
|
||||
Collection<Identifier> ret = pack.findResources(type, namespace, prefix, maxDepth, pathFilter);
|
||||
ret.removeIf(s -> rpo.fileHidden(s.getPath()) && !fbt.fileVisible(namespace));
|
||||
fbt.addFallbackResources(ret, namespace);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(ResourceType type, Identifier id) {
|
||||
containsFileWasFallback = false;
|
||||
String name = getFilename(type, id);
|
||||
if (pack.contains(type, id)) {
|
||||
if (rpo.fileHidden(name)) {
|
||||
if (fbt.fileVisible(name)) {
|
||||
containsFileWasFallback = true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
if (pack.contains(type, new Identifier(id.getNamespace(), id.getPath() + ".rpo")) && fbt.fileVisible(name)) { // only try to check fbt if .rpo exists
|
||||
containsFileWasFallback = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getNamespaces(ResourceType type) {
|
||||
return pack.getNamespaces(type);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public <T> T parseMetadata(ResourceMetadataReader<T> metaReader) throws IOException {
|
||||
return pack.parseMetadata(metaReader);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return pack.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
pack.close();
|
||||
}
|
||||
|
||||
private static String getFilename(ResourceType type, Identifier id) {
|
||||
return String.format("%s/%s/%s", type.getDirectory(), id.getNamespace(), id.getPath());
|
||||
}
|
||||
}
|
|
@ -1,34 +1,28 @@
|
|||
package io.gitlab.jfronny.respackopts.filters.conditions;
|
||||
|
||||
import io.gitlab.jfronny.libjf.data.ResourcePath;
|
||||
import io.gitlab.jfronny.libjf.data.WrappedPack;
|
||||
import io.gitlab.jfronny.respackopts.Respackopts;
|
||||
import io.gitlab.jfronny.respackopts.data.Rpo;
|
||||
import io.gitlab.jfronny.respackopts.filters.lambda.FileContainedProvider;
|
||||
import io.gitlab.jfronny.respackopts.filters.lambda.FileOpenProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
|
||||
public class ResourcePackFilter {
|
||||
FileContainedProvider containsFileBase;
|
||||
FileOpenProvider openFileBase;
|
||||
public ResourcePackFilter(FileContainedProvider containsFileBase, FileOpenProvider openFileBase) {
|
||||
this.containsFileBase = containsFileBase;
|
||||
this.openFileBase = openFileBase;
|
||||
}
|
||||
|
||||
public boolean fileHidden(String name) {
|
||||
public static boolean fileHidden(WrappedPack pack, String name) {
|
||||
if (name.endsWith(Respackopts.FILE_EXTENSION))
|
||||
return false;
|
||||
ResourcePath rpoPath;
|
||||
try {
|
||||
if (!containsFileBase.contains(name + Respackopts.FILE_EXTENSION))
|
||||
rpoPath = new ResourcePath(name + Respackopts.FILE_EXTENSION);
|
||||
if (!pack.contains(rpoPath.getType(), rpoPath.getId()))
|
||||
return false;
|
||||
} catch (IOException e) {
|
||||
} catch (Throwable e) {
|
||||
Respackopts.LOGGER.error(e);
|
||||
return false;
|
||||
}
|
||||
try (InputStream stream = openFileBase.open(name + Respackopts.FILE_EXTENSION); Reader w = new InputStreamReader(stream)) {
|
||||
try (InputStream stream = pack.open(rpoPath.getType(), rpoPath.getId()); Reader w = new InputStreamReader(stream)) {
|
||||
Rpo rpo = Respackopts.GSON.fromJson(w, Rpo.class);
|
||||
if (rpo.conditions == null)
|
||||
return false;
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package io.gitlab.jfronny.respackopts.filters.fallback;
|
||||
|
||||
import io.gitlab.jfronny.libjf.data.ResourcePath;
|
||||
import io.gitlab.jfronny.libjf.data.WrappedPack;
|
||||
import io.gitlab.jfronny.respackopts.Respackopts;
|
||||
import io.gitlab.jfronny.respackopts.data.Rpo;
|
||||
import io.gitlab.jfronny.respackopts.filters.lambda.FileContainedProvider;
|
||||
import io.gitlab.jfronny.respackopts.filters.lambda.FileOpenProvider;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -14,59 +14,52 @@ import java.util.Collection;
|
|||
import java.util.List;
|
||||
|
||||
public class FallbackFilter {
|
||||
FileContainedProvider containsFileBase;
|
||||
FileOpenProvider openFileBase;
|
||||
public FallbackFilter(FileContainedProvider containsFileBase, FileOpenProvider openFileBase) {
|
||||
this.containsFileBase = containsFileBase;
|
||||
this.openFileBase = openFileBase;
|
||||
}
|
||||
|
||||
public boolean fileVisible(String name) {
|
||||
public static boolean fileVisible(WrappedPack pack, String name) {
|
||||
if (name.endsWith(Respackopts.FILE_EXTENSION))
|
||||
return false;
|
||||
String fbt = name + Respackopts.FILE_EXTENSION;
|
||||
try (InputStream stream = openFileBase.open(fbt); Reader w = new InputStreamReader(stream)) {
|
||||
if (containsFileBase.contains(fbt)) {
|
||||
try (ResourcePath fbt = new ResourcePath(name + Respackopts.FILE_EXTENSION); InputStream stream = pack.open(fbt.getType(), fbt.getId()); Reader w = new InputStreamReader(stream)) {
|
||||
if (pack.contains(fbt.getType(), fbt.getId())) {
|
||||
Rpo rpo = Respackopts.GSON.fromJson(w, Rpo.class);
|
||||
if (rpo.fallbacks != null) {
|
||||
List<String> arr = rpo.fallbacks;
|
||||
for (String s : arr) {
|
||||
if (containsFileBase.contains(s))
|
||||
ResourcePath tmp = new ResourcePath(s);
|
||||
if (pack.contains(tmp.getType(), tmp.getId()))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
catch (Exception e) {
|
||||
Respackopts.LOGGER.error("Could not determine visibility of " + name, e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public InputStream getReplacement(String name, IOException ex) throws IOException {
|
||||
String fbt = name + Respackopts.FILE_EXTENSION;
|
||||
try (InputStream stream = openFileBase.open(fbt); Reader w = new InputStreamReader(stream)) {
|
||||
public static InputStream getReplacement(WrappedPack pack, String name, IOException ex) throws IOException {
|
||||
try (ResourcePath fbt = new ResourcePath(name + Respackopts.FILE_EXTENSION); InputStream stream = pack.open(fbt.getType(), fbt.getId()); Reader w = new InputStreamReader(stream)) {
|
||||
Rpo rpo = Respackopts.GSON.fromJson(w, Rpo.class);
|
||||
if (rpo.fallbacks != null) {
|
||||
List<String> arr = rpo.fallbacks;
|
||||
for (String s : arr) {
|
||||
if (containsFileBase.contains(s))
|
||||
return openFileBase.open(s);
|
||||
ResourcePath tmp = new ResourcePath(s);
|
||||
if (pack.contains(tmp.getType(), tmp.getId()))
|
||||
return pack.open(tmp.getType(), tmp.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
catch (Exception e) {
|
||||
Respackopts.LOGGER.error("Could not determine replacement for " + name, e);
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
|
||||
public void addFallbackResources(Collection<Identifier> ret, String namespace) {
|
||||
public static void addFallbackResources(WrappedPack pack, Collection<Identifier> ret, String namespace) {
|
||||
for (Identifier identifier : ret) {
|
||||
String path = identifier.getPath();
|
||||
if (path.endsWith(Respackopts.FILE_EXTENSION)) {
|
||||
String expectedTarget = path.substring(0, path.length() - Respackopts.FILE_EXTENSION.length());
|
||||
if (ret.stream().noneMatch(s -> s.getPath().equals(expectedTarget)) && fileVisible(expectedTarget)) {
|
||||
if (ret.stream().noneMatch(s -> s.getPath().equals(expectedTarget)) && fileVisible(pack, expectedTarget)) {
|
||||
ret.add(new Identifier(namespace, expectedTarget));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
package io.gitlab.jfronny.respackopts.filters.lambda;
|
||||
|
||||
import net.minecraft.resource.ResourceType;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface PathSplitter {
|
||||
Tuple<ResourceType, Identifier> split(String name) throws IOException;
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
package io.gitlab.jfronny.respackopts.filters.lambda;
|
||||
|
||||
public record Tuple<T1, T2>(T1 left, T2 right) {
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
package io.gitlab.jfronny.respackopts.mixin;
|
||||
|
||||
import io.gitlab.jfronny.respackopts.filters.WrappedPack;
|
||||
import net.minecraft.resource.AbstractFileResourcePack;
|
||||
import net.minecraft.resource.ReloadableResourceManagerImpl;
|
||||
import net.minecraft.resource.ResourcePack;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.ModifyVariable;
|
||||
|
||||
@Mixin(ReloadableResourceManagerImpl.class)
|
||||
public class ReloadableResourceManagerImplMixin {
|
||||
@ModifyVariable(method = "addPack(Lnet/minecraft/resource/ResourcePack;)V", at = @At("HEAD"), argsOnly = true, ordinal = 0)
|
||||
private ResourcePack modifyPack(ResourcePack pack) {
|
||||
if (pack instanceof AbstractFileResourcePack) {
|
||||
return new WrappedPack(pack);
|
||||
}
|
||||
return pack;
|
||||
}
|
||||
}
|
|
@ -7,7 +7,6 @@
|
|||
],
|
||||
"client": [
|
||||
"OptionsScreenMixin",
|
||||
"ReloadableResourceManagerImplMixin",
|
||||
"ResourcePackEntryMixin",
|
||||
"ResourcePackManagerMixin"
|
||||
],
|
||||
|
|
Loading…
Reference in New Issue
Block a user