From ef6b01d474868fbb0ac0f078628d8c4cf3f1f3af Mon Sep 17 00:00:00 2001 From: JFronny Date: Thu, 16 Sep 2021 18:37:14 +0200 Subject: [PATCH] Attempt to fix fabric tool api via hacky means. Might try ASM later --- build.gradle | 5 +- gradle.properties | 4 +- .../libjf/data/IModNioResourcePack.java | 11 +++ .../jfronny/libjf/data/WrappedPack.java | 83 ++----------------- .../data/wrappedPackImpl/EventCallImpl.java | 45 ++++++++++ .../WrappedModNioResourcePack.java | 47 +++++++++++ .../wrappedPackImpl/WrappedResourcePack.java | 67 +++++++++++++++ .../libjf/entry/JfLanguageAdapter.java | 2 +- .../libjf/mixin/ModNioResourcePackMixin.java | 32 +++++++ .../ReloadableResourceManagerImplMixin.java | 2 +- src/main/resources/libjf.mixins.json | 2 +- 11 files changed, 219 insertions(+), 81 deletions(-) create mode 100644 src/main/java/io/gitlab/jfronny/libjf/data/IModNioResourcePack.java create mode 100644 src/main/java/io/gitlab/jfronny/libjf/data/wrappedPackImpl/EventCallImpl.java create mode 100644 src/main/java/io/gitlab/jfronny/libjf/data/wrappedPackImpl/WrappedModNioResourcePack.java create mode 100644 src/main/java/io/gitlab/jfronny/libjf/data/wrappedPackImpl/WrappedResourcePack.java create mode 100644 src/main/java/io/gitlab/jfronny/libjf/mixin/ModNioResourcePackMixin.java diff --git a/build.gradle b/build.gradle index a5ad194..7f1ec41 100644 --- a/build.gradle +++ b/build.gradle @@ -20,8 +20,9 @@ dependencies { mappings "net.fabricmc:yarn:${project.minecraft_version}+${project.yarn_mappings}:v2" modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" - include modImplementation(fabricApi.module("fabric-tag-extensions-v0", "0.38.1+1.17")) - modImplementation "com.terraformersmc:modmenu:2.0.5" + include modImplementation(fabricApi.module("fabric-tag-extensions-v0", "0.40.1+1.17")) + include modImplementation(fabricApi.module("fabric-resource-loader-v0", "0.40.1+1.17")) + modImplementation "com.terraformersmc:modmenu:2.0.10" } loom { diff --git a/gradle.properties b/gradle.properties index ebb0f73..031bf7d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,9 +1,9 @@ # Done to increase the memory available to gradle. org.gradle.jvmargs=-Xmx1G # Fabric Properties -# check these on https://modmuss50.me/fabric.html +# check these on https://fabricmc.net/versions.html minecraft_version=1.17.1 -yarn_mappings=build.40 +yarn_mappings=build.61 loader_version=0.11.6 # Mod Properties mod_version=1.2.0 diff --git a/src/main/java/io/gitlab/jfronny/libjf/data/IModNioResourcePack.java b/src/main/java/io/gitlab/jfronny/libjf/data/IModNioResourcePack.java new file mode 100644 index 0000000..7a8a50c --- /dev/null +++ b/src/main/java/io/gitlab/jfronny/libjf/data/IModNioResourcePack.java @@ -0,0 +1,11 @@ +package io.gitlab.jfronny.libjf.data; + +import net.minecraft.resource.ResourceType; + +import java.nio.file.Path; + +public interface IModNioResourcePack { + Path getBasePath(); + ResourceType getResourceType(); + AutoCloseable getCloser(); +} diff --git a/src/main/java/io/gitlab/jfronny/libjf/data/WrappedPack.java b/src/main/java/io/gitlab/jfronny/libjf/data/WrappedPack.java index cfe2a18..059b2e4 100644 --- a/src/main/java/io/gitlab/jfronny/libjf/data/WrappedPack.java +++ b/src/main/java/io/gitlab/jfronny/libjf/data/WrappedPack.java @@ -1,81 +1,16 @@ package io.gitlab.jfronny.libjf.data; +import io.gitlab.jfronny.libjf.data.wrappedPackImpl.WrappedModNioResourcePack; +import io.gitlab.jfronny.libjf.data.wrappedPackImpl.WrappedResourcePack; +import net.fabricmc.fabric.impl.resource.loader.ModNioResourcePack; 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 interface WrappedPack extends ResourcePack { + ResourcePack getUnderlying(); -public class WrappedPack implements ResourcePack { - ResourcePack pack; - public WrappedPack(ResourcePack pack) { - this.pack = pack; - } - - @Nullable - @Override - public InputStream openRoot(String fileName) throws IOException { - InputStream is = null; - IOException ex = null; - try { - pack.openRoot(fileName); - } - catch (IOException ex1) { - ex = ex1; - } - is = UserResourceEvents.OPEN_ROOT.invoker().openRoot(fileName, is, this); - if (is == null) - throw ex == null ? new FileNotFoundException(fileName) : ex; - return is; - } - - @Override - public InputStream open(ResourceType type, Identifier id) throws IOException { - InputStream is = UserResourceEvents.OPEN.invoker().open(type, id, pack.contains(type, id) ? pack.open(type, id) : null, this); - if (is == null) - throw new FileNotFoundException(new ResourcePath(type, id).getName()); - return is; - } - - @Override - public Collection findResources(ResourceType type, String namespace, String prefix, int maxDepth, Predicate pathFilter) { - return UserResourceEvents.FIND_RESOURCE.invoker().findResources(type, namespace, prefix, maxDepth, pathFilter, pack.findResources(type, namespace, prefix, maxDepth, pathFilter), this); - } - - @Override - public boolean contains(ResourceType type, Identifier id) { - return UserResourceEvents.CONTAINS.invoker().contains(type, id, pack.contains(type, id), this); - } - - @Override - public Set getNamespaces(ResourceType type) { - return pack.getNamespaces(type); - } - - @Nullable - @Override - public T parseMetadata(ResourceMetadataReader metaReader) throws IOException { - return pack.parseMetadata(metaReader); - } - - @Override - public String getName() { - return pack.getName(); - } - - @Override - public void close() { - pack.close(); - } - - public ResourcePack getUnderlying() { - return pack; + static WrappedPack create(ResourcePack underlying) { + if (underlying instanceof ModNioResourcePack mi) + return new WrappedModNioResourcePack(mi); + return new WrappedResourcePack(underlying); } } diff --git a/src/main/java/io/gitlab/jfronny/libjf/data/wrappedPackImpl/EventCallImpl.java b/src/main/java/io/gitlab/jfronny/libjf/data/wrappedPackImpl/EventCallImpl.java new file mode 100644 index 0000000..871be77 --- /dev/null +++ b/src/main/java/io/gitlab/jfronny/libjf/data/wrappedPackImpl/EventCallImpl.java @@ -0,0 +1,45 @@ +package io.gitlab.jfronny.libjf.data.wrappedPackImpl; + +import io.gitlab.jfronny.libjf.data.ResourcePath; +import io.gitlab.jfronny.libjf.data.UserResourceEvents; +import io.gitlab.jfronny.libjf.data.WrappedPack; +import net.minecraft.resource.ResourceType; +import net.minecraft.util.Identifier; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.Collection; +import java.util.function.Predicate; + +public class EventCallImpl { + public static InputStream hookOpenRoot(WrappedPack pack, String fileName) throws IOException { + InputStream is = null; + IOException ex = null; + try { + is = pack.getUnderlying().openRoot(fileName); + } + catch (IOException ex1) { + ex = ex1; + } + is = UserResourceEvents.OPEN_ROOT.invoker().openRoot(fileName, is, pack); + if (is == null) + throw ex == null ? new FileNotFoundException(fileName) : ex; + return is; + } + + public static InputStream hookOpen(WrappedPack pack, ResourceType type, Identifier id) throws IOException { + InputStream is = UserResourceEvents.OPEN.invoker().open(type, id, pack.getUnderlying().contains(type, id) ? pack.getUnderlying().open(type, id) : null, pack); + if (is == null) + throw new FileNotFoundException(new ResourcePath(type, id).getName()); + return is; + } + + public static Collection hookFindResources(WrappedPack pack, ResourceType type, String namespace, String prefix, int maxDepth, Predicate pathFilter) { + return UserResourceEvents.FIND_RESOURCE.invoker().findResources(type, namespace, prefix, maxDepth, pathFilter, pack.getUnderlying().findResources(type, namespace, prefix, maxDepth, pathFilter), pack); + } + + public static boolean hookContains(WrappedPack pack, ResourceType type, Identifier id) { + return UserResourceEvents.CONTAINS.invoker().contains(type, id, pack.getUnderlying().contains(type, id), pack); + } +} diff --git a/src/main/java/io/gitlab/jfronny/libjf/data/wrappedPackImpl/WrappedModNioResourcePack.java b/src/main/java/io/gitlab/jfronny/libjf/data/wrappedPackImpl/WrappedModNioResourcePack.java new file mode 100644 index 0000000..233c3db --- /dev/null +++ b/src/main/java/io/gitlab/jfronny/libjf/data/wrappedPackImpl/WrappedModNioResourcePack.java @@ -0,0 +1,47 @@ +package io.gitlab.jfronny.libjf.data.wrappedPackImpl; + +import io.gitlab.jfronny.libjf.data.IModNioResourcePack; +import io.gitlab.jfronny.libjf.data.WrappedPack; +import net.fabricmc.fabric.impl.resource.loader.ModNioResourcePack; +import net.minecraft.resource.ResourcePack; +import net.minecraft.resource.ResourceType; +import net.minecraft.util.Identifier; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Collection; +import java.util.function.Predicate; + +public class WrappedModNioResourcePack extends ModNioResourcePack implements WrappedPack { + private final ModNioResourcePack underlying; + + public WrappedModNioResourcePack(ModNioResourcePack underlying) { + super(underlying.getFabricModMetadata(), ((IModNioResourcePack)underlying).getBasePath(), ((IModNioResourcePack)underlying).getResourceType(), ((IModNioResourcePack)underlying).getCloser(), underlying.getActivationType()); + this.underlying = underlying; + } + + @Override + public ResourcePack getUnderlying() { + return underlying; + } + + @Override + public Collection findResources(ResourceType type, String namespace, String path, int depth, Predicate predicate) { + return EventCallImpl.hookFindResources(this, type, namespace, path, depth, predicate); + } + + @Override + public InputStream open(ResourceType type, Identifier id) throws IOException { + return EventCallImpl.hookOpen(this, type, id); + } + + @Override + public boolean contains(ResourceType type, Identifier id) { + return EventCallImpl.hookContains(this, type, id); + } + + @Override + public InputStream openRoot(String fileName) throws IOException { + return EventCallImpl.hookOpenRoot(this, fileName); + } +} diff --git a/src/main/java/io/gitlab/jfronny/libjf/data/wrappedPackImpl/WrappedResourcePack.java b/src/main/java/io/gitlab/jfronny/libjf/data/wrappedPackImpl/WrappedResourcePack.java new file mode 100644 index 0000000..c969085 --- /dev/null +++ b/src/main/java/io/gitlab/jfronny/libjf/data/wrappedPackImpl/WrappedResourcePack.java @@ -0,0 +1,67 @@ +package io.gitlab.jfronny.libjf.data.wrappedPackImpl; + +import io.gitlab.jfronny.libjf.data.WrappedPack; +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.IOException; +import java.io.InputStream; +import java.util.Collection; +import java.util.Set; +import java.util.function.Predicate; + +public class WrappedResourcePack implements WrappedPack { + ResourcePack pack; + public WrappedResourcePack(ResourcePack pack) { + this.pack = pack; + } + + @Nullable + @Override + public InputStream openRoot(String fileName) throws IOException { + return EventCallImpl.hookOpenRoot(this, fileName); + } + + @Override + public InputStream open(ResourceType type, Identifier id) throws IOException { + return EventCallImpl.hookOpen(this, type, id); + } + + @Override + public Collection findResources(ResourceType type, String namespace, String prefix, int maxDepth, Predicate pathFilter) { + return EventCallImpl.hookFindResources(this, type, namespace, prefix, maxDepth, pathFilter); + } + + @Override + public boolean contains(ResourceType type, Identifier id) { + return EventCallImpl.hookContains(this, type, id); + } + + @Override + public Set getNamespaces(ResourceType type) { + return pack.getNamespaces(type); + } + + @Nullable + @Override + public T parseMetadata(ResourceMetadataReader metaReader) throws IOException { + return pack.parseMetadata(metaReader); + } + + @Override + public String getName() { + return pack.getName(); + } + + @Override + public void close() { + pack.close(); + } + + public ResourcePack getUnderlying() { + return pack; + } +} diff --git a/src/main/java/io/gitlab/jfronny/libjf/entry/JfLanguageAdapter.java b/src/main/java/io/gitlab/jfronny/libjf/entry/JfLanguageAdapter.java index 2a1001e..059b909 100644 --- a/src/main/java/io/gitlab/jfronny/libjf/entry/JfLanguageAdapter.java +++ b/src/main/java/io/gitlab/jfronny/libjf/entry/JfLanguageAdapter.java @@ -4,7 +4,7 @@ import io.gitlab.jfronny.libjf.Libjf; import io.gitlab.jfronny.libjf.config.JfConfig; import net.fabricmc.loader.api.LanguageAdapter; -public class JfLanguageAdapter implements LanguageAdapter { +public class JfLanguageAdapter implements LanguageAdapter { @Override public native T create(net.fabricmc.loader.api.ModContainer mod, String value, Class type); diff --git a/src/main/java/io/gitlab/jfronny/libjf/mixin/ModNioResourcePackMixin.java b/src/main/java/io/gitlab/jfronny/libjf/mixin/ModNioResourcePackMixin.java new file mode 100644 index 0000000..cb7d3e4 --- /dev/null +++ b/src/main/java/io/gitlab/jfronny/libjf/mixin/ModNioResourcePackMixin.java @@ -0,0 +1,32 @@ +package io.gitlab.jfronny.libjf.mixin; + +import io.gitlab.jfronny.libjf.data.IModNioResourcePack; +import net.fabricmc.fabric.impl.resource.loader.ModNioResourcePack; +import net.minecraft.resource.ResourceType; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +import java.nio.file.Path; + +@Mixin(ModNioResourcePack.class) +public class ModNioResourcePackMixin implements IModNioResourcePack { + @Shadow @Final private ResourceType type; + @Shadow @Final private AutoCloseable closer; + @Shadow @Final private Path basePath; + + @Override + public Path getBasePath() { + return basePath; + } + + @Override + public ResourceType getResourceType() { + return type; + } + + @Override + public AutoCloseable getCloser() { + return closer; + } +} diff --git a/src/main/java/io/gitlab/jfronny/libjf/mixin/ReloadableResourceManagerImplMixin.java b/src/main/java/io/gitlab/jfronny/libjf/mixin/ReloadableResourceManagerImplMixin.java index 81e00e2..bfa8b07 100644 --- a/src/main/java/io/gitlab/jfronny/libjf/mixin/ReloadableResourceManagerImplMixin.java +++ b/src/main/java/io/gitlab/jfronny/libjf/mixin/ReloadableResourceManagerImplMixin.java @@ -14,6 +14,6 @@ public class ReloadableResourceManagerImplMixin { if (pack instanceof WrappedPack) { return pack; } - return new WrappedPack(pack); + return WrappedPack.create(pack); } } diff --git a/src/main/resources/libjf.mixins.json b/src/main/resources/libjf.mixins.json index ce3ba8e..3402e95 100644 --- a/src/main/resources/libjf.mixins.json +++ b/src/main/resources/libjf.mixins.json @@ -5,12 +5,12 @@ "compatibilityLevel": "JAVA_16", "mixins": [ "EntityMixin", + "ModNioResourcePackMixin", "RecipeManagerMixin", "ShulkerBoxBlockEntityMixin", "ShulkerBoxSlotMixin" ], "client": [ - "ReloadableResourceManagerImplMixin" ], "injectors": { "defaultRequire": 1