Attempt to fix fabric tool api via hacky means. Might try ASM later

This commit is contained in:
JFronny 2021-09-16 18:37:14 +02:00
parent aee2e52172
commit ef6b01d474
No known key found for this signature in database
GPG Key ID: BEC5ACBBD4EE17E5
11 changed files with 219 additions and 81 deletions

View File

@ -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 {

View File

@ -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

View File

@ -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();
}

View File

@ -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<Identifier> findResources(ResourceType type, String namespace, String prefix, int maxDepth, Predicate<String> 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<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();
}
public ResourcePack getUnderlying() {
return pack;
static WrappedPack create(ResourcePack underlying) {
if (underlying instanceof ModNioResourcePack mi)
return new WrappedModNioResourcePack(mi);
return new WrappedResourcePack(underlying);
}
}

View File

@ -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<Identifier> hookFindResources(WrappedPack pack, ResourceType type, String namespace, String prefix, int maxDepth, Predicate<String> 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);
}
}

View File

@ -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<Identifier> findResources(ResourceType type, String namespace, String path, int depth, Predicate<String> 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);
}
}

View File

@ -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<Identifier> findResources(ResourceType type, String namespace, String prefix, int maxDepth, Predicate<String> 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<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();
}
public ResourcePack getUnderlying() {
return pack;
}
}

View File

@ -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> T create(net.fabricmc.loader.api.ModContainer mod, String value, Class<T> type);

View File

@ -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;
}
}

View File

@ -14,6 +14,6 @@ public class ReloadableResourceManagerImplMixin {
if (pack instanceof WrappedPack) {
return pack;
}
return new WrappedPack(pack);
return WrappedPack.create(pack);
}
}

View File

@ -5,12 +5,12 @@
"compatibilityLevel": "JAVA_16",
"mixins": [
"EntityMixin",
"ModNioResourcePackMixin",
"RecipeManagerMixin",
"ShulkerBoxBlockEntityMixin",
"ShulkerBoxSlotMixin"
],
"client": [
"ReloadableResourceManagerImplMixin"
],
"injectors": {
"defaultRequire": 1