LibJF/libjf-data-manipulation-v0/src/main/java/io/gitlab/jfronny/libjf/data/manipulation/impl/ResourcePackHookPatch.java

58 lines
3.6 KiB
Java
Raw Normal View History

2021-10-03 13:43:16 +02:00
package io.gitlab.jfronny.libjf.data.manipulation.impl;
2021-09-29 18:52:43 +02:00
import io.gitlab.jfronny.libjf.unsafe.asm.AsmConfig;
import io.gitlab.jfronny.libjf.unsafe.asm.patch.Patch;
import io.gitlab.jfronny.libjf.unsafe.asm.patch.PatchUtil;
2021-09-29 18:52:43 +02:00
import io.gitlab.jfronny.libjf.unsafe.asm.patch.modification.MethodModificationPatch;
import io.gitlab.jfronny.libjf.unsafe.asm.patch.targeting.InterfaceImplTargetPatch;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.MethodNode;
2021-09-29 18:52:43 +02:00
import java.io.IOException;
import java.io.InputStream;
2021-09-29 18:52:43 +02:00
import java.util.Set;
2021-10-03 13:43:16 +02:00
public class ResourcePackHookPatch implements AsmConfig {
2021-09-29 18:52:43 +02:00
private static final String TARGET_CLASS_INTERMEDIARY = "net.minecraft.class_3262";
2021-10-03 13:43:16 +02:00
private static final String HOOK_IMPLEMENTATION = Type.getInternalName(ResourcePackHook.class);
2021-09-29 18:52:43 +02:00
@Override
public Set<String> skipClasses() {
return Set.of(PatchUtil.getRemapped(TARGET_CLASS_INTERMEDIARY),
"io.gitlab.jfronny.libjf.data.wrappedPackImpl.WrappedResourcePack",
"io.gitlab.jfronny.libjf.data.wrappedPackImpl.SafeWrappedResourcePack");
2021-09-29 18:52:43 +02:00
}
@Override
public Set<Patch> getPatches() {
// https://maven.fabricmc.net/docs/yarn-21w38a+build.9/net/minecraft/resource/ResourcePack.html
return Set.of(new InterfaceImplTargetPatch(TARGET_CLASS_INTERMEDIARY, new MethodModificationPatch(TARGET_CLASS_INTERMEDIARY, Set.of(
// open root
2021-11-25 17:20:59 +01:00
new MethodModificationPatch.MethodDescriptorPatch("method_14410", "(Ljava/lang/String;)Ljava/io/InputStream;", (method, klazz) -> {
2021-09-29 18:52:43 +02:00
catchEx(method, "hookOpenRootEx", new String[]{"java/lang/String"});
hookReturn(method, "hookOpenRoot", "Ljava/io/InputStream;", new String[]{"java/lang/String"});
}),
// open
2021-11-25 17:20:59 +01:00
new MethodModificationPatch.MethodDescriptorPatch("method_14405", "(Lnet/minecraft/class_3264;Lnet/minecraft/class_2960;)Ljava/io/InputStream;", (method, klazz) -> {
2021-09-29 18:52:43 +02:00
catchEx(method, "hookOpenEx", new String[]{"net/minecraft/class_3264", "net/minecraft/class_2960"});
hookReturn(method, "hookOpen", "Ljava/io/InputStream;", new String[]{"net/minecraft/class_3264", "net/minecraft/class_2960"});
}),
// find resource
new MethodModificationPatch.MethodDescriptorPatch("method_14408", "(Lnet/minecraft/class_3264;Ljava/lang/String;Ljava/lang/String;Ljava/util/function/Predicate;)Ljava/util/Collection;", (method, klazz) -> {
hookReturn(method, "hookFindResources", "Ljava/util/Collection;", new String[]{"net/minecraft/class_3264", "java/lang/String", "java/lang/String", "java/util/function/Predicate"});
2021-09-29 18:52:43 +02:00
}),
// contains
2021-11-25 17:20:59 +01:00
new MethodModificationPatch.MethodDescriptorPatch("method_14411", "(Lnet/minecraft/class_3264;Lnet/minecraft/class_2960;)Z", (method, klazz) -> {
2021-09-29 18:52:43 +02:00
hookReturn(method, "hookContains", "Z", new String[]{"net/minecraft/class_3264", "net/minecraft/class_2960"});
})
))));
}
private void hookReturn(MethodNode method, String targetMethod, String targetType, String[] extraParamTypes) {
PatchUtil.redirectReturn(method, TARGET_CLASS_INTERMEDIARY, HOOK_IMPLEMENTATION, targetMethod, targetType, extraParamTypes);
2021-09-29 18:52:43 +02:00
}
private void catchEx(MethodNode method, String targetMethod, String[] extraParamTypes) {
PatchUtil.redirectExceptions(method, TARGET_CLASS_INTERMEDIARY, HOOK_IMPLEMENTATION, Type.getInternalName(IOException.class), Type.getInternalName(InputStream.class), targetMethod, extraParamTypes);
2021-09-29 18:52:43 +02:00
}
}