|
| 1 | +package zone.moddev.mc.orespawn.client; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.io.InputStream; |
| 6 | +import java.lang.reflect.Constructor; |
| 7 | +import java.lang.reflect.Method; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.jupiter.params.ParameterizedTest; |
| 11 | +import org.junit.jupiter.params.provider.ValueSource; |
| 12 | +import org.objectweb.asm.ClassReader; |
| 13 | +import org.objectweb.asm.ClassVisitor; |
| 14 | +import org.objectweb.asm.ClassWriter; |
| 15 | +import org.objectweb.asm.MethodVisitor; |
| 16 | +import org.objectweb.asm.Opcodes; |
| 17 | + |
| 18 | +import net.minecraft.client.gui.FontRenderer; |
| 19 | + |
| 20 | +/** Exercises the name collision introduced when another mod uses official mappings. */ |
| 21 | +public class TextFieldRemappingTest { |
| 22 | + private static final String WRAPPER = TextFieldWidget.class.getName().replace('.', '/'); |
| 23 | + private static final String NATIVE = "net/minecraft/client/gui/widget/TextFieldWidget"; |
| 24 | + private static final String REMAPPED_NATIVE = OfficialTextField.class.getName().replace('.', '/'); |
| 25 | + |
| 26 | + @ParameterizedTest |
| 27 | + @ValueSource(strings = { "setValue", "getValue", "setMaxLength" }) |
| 28 | + void eachOfficialNameStillDelegatesToTheNativeWidget(String method) throws Exception { |
| 29 | + Object field = remappedField(); |
| 30 | + if (method.equals("setValue")) { |
| 31 | + invoke(field.getClass(), field, method, String.class, "minecraft:stone"); |
| 32 | + assertEquals("minecraft:stone", ((OfficialTextField) field).value); |
| 33 | + } else if (method.equals("setMaxLength")) { |
| 34 | + invoke(field.getClass(), field, method, int.class, 1024); |
| 35 | + assertEquals(1024, ((OfficialTextField) field).maximum); |
| 36 | + } else { |
| 37 | + assertEquals("", readValue(field.getClass(), field)); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void remappedWidgetPreservesLongValuesAndNativeLimits() throws Exception { |
| 43 | + Object field = remappedField(); |
| 44 | + Class<?> remapped = field.getClass(); |
| 45 | + String value = "minecraft:stone,minecraft:granite,minecraft:diorite,minecraft:andesite"; |
| 46 | + |
| 47 | + invoke(remapped, field, "setMaxLength", int.class, 1024); |
| 48 | + invoke(remapped, field, "setValue", String.class, value); |
| 49 | + assertEquals(value, readValue(remapped, field)); |
| 50 | + invoke(remapped, field, "setMaxLength", int.class, 12); |
| 51 | + assertEquals(value.substring(0, 12), readValue(remapped, field)); |
| 52 | + invoke(remapped, field, "setValue", String.class, "minecraft:granite"); |
| 53 | + assertEquals("minecraft:gr", readValue(remapped, field)); |
| 54 | + } |
| 55 | + |
| 56 | + private Object remappedField() throws Exception { |
| 57 | + // Use the real compiled wrapper, remapping the three native MCP names as |
| 58 | + // ForgeGradle does when importing an SRG jar into an official-mapped project. |
| 59 | + ClassWriter writer = new ClassWriter(0); |
| 60 | + try (InputStream input = TextFieldWidget.class.getResourceAsStream("TextFieldWidget.class")) { |
| 61 | + new ClassReader(input).accept(new ClassVisitor(Opcodes.ASM6, writer) { |
| 62 | + @Override |
| 63 | + public void visit(int version, int access, String name, String signature, |
| 64 | + String superName, String[] interfaces) { |
| 65 | + super.visit(version, access, name, signature, REMAPPED_NATIVE, interfaces); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public MethodVisitor visitMethod(int access, String name, String descriptor, |
| 70 | + String signature, String[] exceptions) { |
| 71 | + return new MethodVisitor(Opcodes.ASM6, |
| 72 | + super.visitMethod(access, name, descriptor, signature, exceptions)) { |
| 73 | + @Override |
| 74 | + public void visitMethodInsn(int opcode, String owner, String name, |
| 75 | + String descriptor, boolean isInterface) { |
| 76 | + if (owner.equals(WRAPPER) || owner.equals(NATIVE)) { |
| 77 | + if (name.equals("setText")) name = "setValue"; |
| 78 | + if (name.equals("getText")) name = "getValue"; |
| 79 | + if (name.equals("setMaxStringLength")) name = "setMaxLength"; |
| 80 | + } |
| 81 | + super.visitMethodInsn(opcode, owner.equals(NATIVE) ? REMAPPED_NATIVE : owner, |
| 82 | + name, descriptor, isInterface); |
| 83 | + } |
| 84 | + }; |
| 85 | + } |
| 86 | + }, 0); |
| 87 | + } |
| 88 | + Class<?> remapped = new ClassLoader(getClass().getClassLoader()) { |
| 89 | + Class<?> defineWrapper() { |
| 90 | + byte[] bytes = writer.toByteArray(); |
| 91 | + return defineClass(TextFieldWidget.class.getName(), bytes, 0, bytes.length); |
| 92 | + } |
| 93 | + }.defineWrapper(); |
| 94 | + Constructor<?> constructor = remapped.getDeclaredConstructor(FontRenderer.class, |
| 95 | + int.class, int.class, int.class, int.class, String.class); |
| 96 | + constructor.setAccessible(true); |
| 97 | + return constructor.newInstance(null, 0, 0, 200, 20, "host_blocks"); |
| 98 | + } |
| 99 | + |
| 100 | + private static void invoke(Class<?> type, Object field, String name, Class<?> parameter, |
| 101 | + Object value) throws Exception { |
| 102 | + Method method = type.getDeclaredMethod(name, parameter); |
| 103 | + method.setAccessible(true); |
| 104 | + method.invoke(field, value); |
| 105 | + } |
| 106 | + |
| 107 | + private static Object readValue(Class<?> type, Object field) throws Exception { |
| 108 | + Method method = type.getDeclaredMethod("getValue"); |
| 109 | + method.setAccessible(true); |
| 110 | + return method.invoke(field); |
| 111 | + } |
| 112 | + |
| 113 | + /** Small native-widget stand-in: only naming and virtual dispatch matter here. */ |
| 114 | + public static class OfficialTextField { |
| 115 | + public int x, y, width, height; |
| 116 | + private int maximum = 32; |
| 117 | + private String value = ""; |
| 118 | + |
| 119 | + public OfficialTextField(FontRenderer font, int x, int y, int width, int height, String label) { } |
| 120 | + public void setValue(String value) { |
| 121 | + this.value = value.substring(0, Math.min(maximum, value.length())); |
| 122 | + } |
| 123 | + public String getValue() { return value; } |
| 124 | + public void setMaxLength(int maximum) { |
| 125 | + this.maximum = maximum; |
| 126 | + value = value.substring(0, Math.min(maximum, value.length())); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments