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

67 lines
3.7 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;
2023-03-11 21:24:59 +01:00
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;
2022-12-07 19:54:35 +01:00
import org.objectweb.asm.Opcodes;
2021-09-29 18:52:43 +02:00
import org.objectweb.asm.Type;
2022-12-07 19:54:35 +01:00
import org.objectweb.asm.tree.*;
2021-09-29 18:52:43 +02:00
import java.io.IOException;
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);
2022-12-07 19:54:35 +01:00
2021-09-29 18:52:43 +02:00
@Override
public Set<String> skipClasses() {
2022-12-07 19:54:35 +01:00
return Set.of(PatchUtil.mapClassName(TARGET_CLASS_INTERMEDIARY));
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
2022-12-07 19:54:35 +01:00
new MethodModificationPatch.MethodDescriptorPatch("method_14410", "([Ljava/lang/String;)Lnet/minecraft/class_7367;", (method, klazz) -> {
hookReturn(method, "hookOpenRoot", "Lnet/minecraft/class_7367;", "[java/lang/String");
2021-09-29 18:52:43 +02:00
}),
// open
2022-12-07 19:54:35 +01:00
new MethodModificationPatch.MethodDescriptorPatch("method_14405", "(Lnet/minecraft/class_3264;Lnet/minecraft/class_2960;)Lnet/minecraft/class_7367;", (method, klazz) -> {
hookReturn(method, "hookOpen", "Lnet/minecraft/class_7367;", "net/minecraft/class_3264", "net/minecraft/class_2960");
2021-09-29 18:52:43 +02:00
}),
// find resource
2022-12-07 19:54:35 +01:00
new MethodModificationPatch.MethodDescriptorPatch("method_14408", "(Lnet/minecraft/class_3264;Ljava/lang/String;Ljava/lang/String;Lnet/minecraft/class_3262$class_7664;)V", (method, klazz) -> {
InsnList head = PatchUtil.buildParamPassingInvoker(
TARGET_CLASS_INTERMEDIARY,
HOOK_IMPLEMENTATION,
"hookFindResources",
null,
"Lnet/minecraft/class_3262$class_7664;",
"net/minecraft/class_3264", "java/lang/String", "java/lang/String", "net/minecraft/class_3262$class_7664");
head.add(new VarInsnNode(Opcodes.ASTORE, 4));
method.instructions.insert(head);
2021-09-29 18:52:43 +02:00
}),
2022-12-07 19:54:35 +01:00
// parse metadata
new MethodModificationPatch.MethodDescriptorPatch("method_14407", "(Lnet/minecraft/class_3270;)Ljava/lang/Object;", (method, klazz) -> {
hookReturn(method, "hookParseMetadata", "Ljava/lang/Object;", "net/minecraft/class_3270");
PatchUtil.redirectExceptions(
method,
TARGET_CLASS_INTERMEDIARY,
HOOK_IMPLEMENTATION,
Type.getInternalName(IOException.class),
Type.getInternalName(Object.class),
"hookParseMetadataEx",
"net/minecraft/class_3270");
2021-09-29 18:52:43 +02:00
})
))));
}
2022-12-07 19:54:35 +01:00
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
}
}