Search in sources :

Example 51 with Field

use of net.runelite.asm.Field in project runelite by runelite.

the class InjectSetterTest method testInjectSetterInt.

@Test
public void testInjectSetterInt() throws NoSuchMethodException {
    Inject inject = mock(Inject.class);
    when(inject.findImportMethodOnApi(any(Class.class), anyString(), any(Boolean.class))).thenReturn(APIClass.class.getDeclaredMethod("setTest", int.class));
    when(inject.createLoadForTypeIndex(any(Instructions.class), any(Type.class), anyInt())).thenReturn(mock(Instruction.class));
    InjectSetter instance = new InjectSetter(inject);
    ClassFile targetClass = new ClassFile();
    targetClass.setName("test");
    Field field = new Field(targetClass, "test", Type.INT);
    targetClass.addField(field);
    instance.injectSetter(targetClass, APIClass.class, field, "test", null);
    Method injectedMethod = targetClass.findMethod("setTest");
    assertNotNull(injectedMethod);
    Code code = injectedMethod.getCode();
    Instructions instructions = code.getInstructions();
    assertFalse(instructions.getInstructions().stream().filter(i -> i.getType() == CHECKCAST).findAny().isPresent());
}
Also used : Assert.assertNotNull(org.junit.Assert.assertNotNull) Field(net.runelite.asm.Field) Assert.assertTrue(org.junit.Assert.assertTrue) Code(net.runelite.asm.attributes.Code) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Type(net.runelite.asm.Type) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.any(org.mockito.Matchers.any) ClassFile(net.runelite.asm.ClassFile) CHECKCAST(net.runelite.asm.attributes.code.InstructionType.CHECKCAST) Method(net.runelite.asm.Method) Assert.assertFalse(org.junit.Assert.assertFalse) Instructions(net.runelite.asm.attributes.code.Instructions) Matchers.anyInt(org.mockito.Matchers.anyInt) Instruction(net.runelite.asm.attributes.code.Instruction) Mockito.mock(org.mockito.Mockito.mock) ClassFile(net.runelite.asm.ClassFile) Instructions(net.runelite.asm.attributes.code.Instructions) Method(net.runelite.asm.Method) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code) Field(net.runelite.asm.Field) Type(net.runelite.asm.Type) Test(org.junit.Test)

Example 52 with Field

use of net.runelite.asm.Field in project runelite by runelite.

the class InjectSetterTest method testInjectSetterObject.

@Test
public void testInjectSetterObject() throws NoSuchMethodException {
    Inject inject = mock(Inject.class);
    when(inject.findImportMethodOnApi(any(Class.class), anyString(), any(Boolean.class))).thenReturn(APIClass.class.getDeclaredMethod("setTestObject", Object.class));
    when(inject.createLoadForTypeIndex(any(Instructions.class), any(Type.class), anyInt())).thenReturn(mock(Instruction.class));
    InjectSetter instance = new InjectSetter(inject);
    ClassFile targetClass = new ClassFile();
    targetClass.setName("test");
    Field field = new Field(targetClass, "testObject", Type.STRING);
    targetClass.addField(field);
    instance.injectSetter(targetClass, APIClass.class, field, "testObject", null);
    Method injectedMethod = targetClass.findMethod("setTestObject");
    assertNotNull(injectedMethod);
    Code code = injectedMethod.getCode();
    Instructions instructions = code.getInstructions();
    assertTrue(instructions.getInstructions().stream().filter(i -> i.getType() == CHECKCAST).findAny().isPresent());
}
Also used : Assert.assertNotNull(org.junit.Assert.assertNotNull) Field(net.runelite.asm.Field) Assert.assertTrue(org.junit.Assert.assertTrue) Code(net.runelite.asm.attributes.Code) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Type(net.runelite.asm.Type) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.any(org.mockito.Matchers.any) ClassFile(net.runelite.asm.ClassFile) CHECKCAST(net.runelite.asm.attributes.code.InstructionType.CHECKCAST) Method(net.runelite.asm.Method) Assert.assertFalse(org.junit.Assert.assertFalse) Instructions(net.runelite.asm.attributes.code.Instructions) Matchers.anyInt(org.mockito.Matchers.anyInt) Instruction(net.runelite.asm.attributes.code.Instruction) Mockito.mock(org.mockito.Mockito.mock) ClassFile(net.runelite.asm.ClassFile) Instructions(net.runelite.asm.attributes.code.Instructions) Method(net.runelite.asm.Method) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code) Field(net.runelite.asm.Field) Type(net.runelite.asm.Type) Test(org.junit.Test)

Example 53 with Field

use of net.runelite.asm.Field in project runelite by runelite.

the class MixinInjectorTest method testInject.

@Test
public void testInject() throws Exception {
    InputStream deobIn = getClass().getResourceAsStream("DeobTarget.class");
    ClassFile deobTarget = ClassUtil.loadClass(deobIn);
    ClassGroup deob = new ClassGroup();
    deob.addClass(deobTarget);
    InputStream vanillaIn = getClass().getResourceAsStream("VanillaTarget.class");
    ClassFile vanillaTarget = ClassUtil.loadClass(vanillaIn);
    ClassGroup vanilla = new ClassGroup();
    vanilla.addClass(vanillaTarget);
    Map<Class<?>, List<ClassFile>> mixinClasses = new HashMap<>();
    mixinClasses.put(Source.class, Collections.singletonList(vanillaTarget));
    mixinClasses.put(Source2.class, Collections.singletonList(vanillaTarget));
    Inject inject = new Inject(deob, vanilla);
    new MixinInjector(inject).inject(mixinClasses);
    // Check if "foo" has been injected
    Field foo = vanillaTarget.findField("foo");
    assertNotNull(foo);
    assertEquals(INT, foo.getType());
    assertEquals(ACC_PUBLIC | ACC_STATIC, foo.getAccessFlags());
    // Check if "foo2()V" has been injected
    Method foo2 = vanillaTarget.findMethod("foo2");
    assertNotNull(foo2);
    assertEquals(new Signature("()V"), foo2.getDescriptor());
    assertEquals(ACC_PUBLIC, foo2.getAccessFlags());
    // Check if "ob_foo3(I)V" was copied
    Method foo3 = vanillaTarget.findMethod("copy$foo3");
    assertNotNull(foo3);
    assertEquals(new Signature("(I)V"), foo3.getDescriptor());
    assertEquals(ACC_PUBLIC, foo3.getAccessFlags());
    // Check if "ob_foo3(I)V" was replaced
    Method ob_foo3 = vanillaTarget.findMethod("ob_foo3");
    assertNotNull(ob_foo3);
    assertEquals(new Signature("(I)V"), ob_foo3.getDescriptor());
    assertEquals(ob_foo3.getCode().getInstructions().getInstructions().stream().filter(i -> i instanceof LDC && ((LDC) i).getConstant().equals("replaced")).count(), 1);
    // Check that the "foo4" field access in the new code body was mapped correctly
    assertEquals(ob_foo3.getCode().getInstructions().getInstructions().stream().filter(i -> {
        if (!(i instanceof GetStatic)) {
            return false;
        }
        net.runelite.asm.pool.Field field = ((GetStatic) i).getField();
        if (!field.getClazz().getName().equals("net/runelite/injector/VanillaTarget")) {
            return false;
        }
        if (!field.getName().equals("ob_foo4")) {
            return false;
        }
        return true;
    }).count(), 1);
    // Check that the "foo3()" call in the new code body was mapped to the copy
    assertEquals(ob_foo3.getCode().getInstructions().getInstructions().stream().filter(i -> {
        if (!(i instanceof InvokeVirtual)) {
            return false;
        }
        net.runelite.asm.pool.Method method = ((InvokeVirtual) i).getMethod();
        if (!method.getClazz().getName().equals("net/runelite/injector/VanillaTarget")) {
            return false;
        }
        if (!method.getName().equals("copy$foo3")) {
            return false;
        }
        return true;
    }).count(), 1);
    // Check if "foo5()V" was injected
    Method foo5 = vanillaTarget.findMethod("foo5");
    assertNotNull(foo5);
    assertEquals(new Signature("()V"), foo5.getDescriptor());
    assertEquals(ACC_PUBLIC, foo5.getAccessFlags());
    // Check that the shadow "foo" field access was mapped correctly
    assertEquals(foo5.getCode().getInstructions().getInstructions().stream().filter(i -> {
        if (!(i instanceof GetStatic)) {
            return false;
        }
        net.runelite.asm.pool.Field field = ((GetStatic) i).getField();
        if (!field.getClazz().getName().equals("net/runelite/injector/VanillaTarget")) {
            return false;
        }
        if (!field.getName().equals("foo")) {
            return false;
        }
        return true;
    }).count(), 1);
}
Also used : ClassFile(net.runelite.asm.ClassFile) HashMap(java.util.HashMap) InputStream(java.io.InputStream) LDC(net.runelite.asm.attributes.code.instructions.LDC) Method(net.runelite.asm.Method) Field(net.runelite.asm.Field) GetStatic(net.runelite.asm.attributes.code.instructions.GetStatic) InvokeVirtual(net.runelite.asm.attributes.code.instructions.InvokeVirtual) ClassGroup(net.runelite.asm.ClassGroup) ObfuscatedSignature(net.runelite.mapping.ObfuscatedSignature) Signature(net.runelite.asm.signature.Signature) List(java.util.List) Test(org.junit.Test)

Example 54 with Field

use of net.runelite.asm.Field in project runelite by runelite.

the class ArrayStore method getMyField.

public Field getMyField(InstructionContext thisIc) {
    StackContext sctx = thisIc.getPops().get(2);
    InstructionContext pushed = sctx.getPushed();
    InstructionContext r = pushed.resolve(sctx);
    if (r.getInstruction() instanceof GetFieldInstruction) {
        GetFieldInstruction gf = (GetFieldInstruction) r.getInstruction();
        Field f = gf.getMyField();
        return f;
    }
    return null;
}
Also used : InstructionContext(net.runelite.asm.execution.InstructionContext) Field(net.runelite.asm.Field) StackContext(net.runelite.asm.execution.StackContext) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction)

Example 55 with Field

use of net.runelite.asm.Field in project runelite by runelite.

the class ArrayStore method isSame.

@Override
public boolean isSame(InstructionContext thisIc, InstructionContext otherIc) {
    if (thisIc.getInstruction().getClass() != otherIc.getInstruction().getClass()) {
        return false;
    }
    Field myField = this.getMyField(thisIc), otherField = ((ArrayStore) otherIc.getInstruction()).getMyField(otherIc);
    if (!MappingExecutorUtil.isMaybeEqual(myField, otherField)) {
        return false;
    }
    StackContext // value set to.
    object1 = thisIc.getPops().get(0), object2 = otherIc.getPops().get(0);
    InstructionContext base1 = MappingExecutorUtil.resolve(object1.getPushed(), object1);
    InstructionContext base2 = MappingExecutorUtil.resolve(object2.getPushed(), object2);
    if (base1.getInstruction() instanceof GetFieldInstruction && base2.getInstruction() instanceof GetFieldInstruction) {
        GetFieldInstruction gf1 = (GetFieldInstruction) base1.getInstruction(), gf2 = (GetFieldInstruction) base2.getInstruction();
        Field f1 = gf1.getMyField(), f2 = gf2.getMyField();
        if (!MappingExecutorUtil.isMaybeEqual(f1, f2)) {
            return false;
        }
    }
    return true;
}
Also used : Field(net.runelite.asm.Field) InstructionContext(net.runelite.asm.execution.InstructionContext) StackContext(net.runelite.asm.execution.StackContext) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction)

Aggregations

Field (net.runelite.asm.Field)65 ClassFile (net.runelite.asm.ClassFile)39 Method (net.runelite.asm.Method)33 Instruction (net.runelite.asm.attributes.code.Instruction)19 InstructionContext (net.runelite.asm.execution.InstructionContext)19 GetFieldInstruction (net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction)17 Instructions (net.runelite.asm.attributes.code.Instructions)16 StackContext (net.runelite.asm.execution.StackContext)16 Type (net.runelite.asm.Type)15 PushConstantInstruction (net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction)12 LDC (net.runelite.asm.attributes.code.instructions.LDC)11 Code (net.runelite.asm.attributes.Code)10 SetFieldInstruction (net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction)10 Signature (net.runelite.asm.signature.Signature)9 ClassGroup (net.runelite.asm.ClassGroup)8 FieldInstruction (net.runelite.asm.attributes.code.instruction.types.FieldInstruction)8 PutField (net.runelite.asm.attributes.code.instructions.PutField)8 Test (org.junit.Test)8 List (java.util.List)7 Annotation (net.runelite.asm.attributes.annotation.Annotation)7