Search in sources :

Example 46 with LDC

use of net.runelite.asm.attributes.code.instructions.LDC in project runelite by runelite.

the class InjectSetter method injectSetter.

/**
 * inject a setter into the vanilla classgroup
 *
 * @param targetClass Class where to inject the setter (field's class,
 * or client)
 * @param targetApiClass API targetClass implements, which may have the
 * setter declared
 * @param field Field of vanilla that will be set
 * @param exportedName exported name of field
 * @param setter
 */
public void injectSetter(ClassFile targetClass, Class<?> targetApiClass, Field field, String exportedName, Number setter) {
    java.lang.reflect.Method method = inject.findImportMethodOnApi(targetApiClass, exportedName, true);
    if (method == null) {
        logger.warn("Setter injection for field {} but an API method was not found on {}", exportedName, targetApiClass);
        return;
    }
    if (method.getParameterCount() != 1) {
        logger.warn("Setter {} with not parameter count != 1?", exportedName);
        return;
    }
    logger.info("Injecting setter for {} on {}", exportedName, targetApiClass);
    assert targetClass.findMethod(method.getName()) == null;
    assert field.isStatic() || field.getClassFile() == targetClass;
    Signature sig = new Signature.Builder().setReturnType(Type.VOID).addArgument(Inject.classToType(method.getParameterTypes()[0])).build();
    Method setterMethod = new Method(targetClass, method.getName(), sig);
    setterMethod.setAccessFlags(ACC_PUBLIC);
    targetClass.addMethod(setterMethod);
    ++injectedSetters;
    Code code = new Code(setterMethod);
    setterMethod.setCode(code);
    Instructions instructions = code.getInstructions();
    List<Instruction> ins = instructions.getInstructions();
    // load this
    if (!field.isStatic()) {
        ins.add(new ALoad(instructions, 0));
    }
    // load argument
    Type argumentType = sig.getTypeOfArg(0);
    ins.add(inject.createLoadForTypeIndex(instructions, argumentType, 1));
    // cast argument to field type
    Type fieldType = field.getType();
    if (!argumentType.equals(fieldType)) {
        CheckCast checkCast = new CheckCast(instructions);
        checkCast.setType(fieldType);
        ins.add(checkCast);
    }
    if (setter != null) {
        assert setter instanceof Integer || setter instanceof Long;
        if (setter instanceof Integer) {
            ins.add(new LDC(instructions, (int) setter));
            ins.add(new IMul(instructions));
        } else {
            ins.add(new LDC(instructions, (long) setter));
            ins.add(new LMul(instructions));
        }
    }
    if (field.isStatic()) {
        ins.add(new PutStatic(instructions, field));
    } else {
        ins.add(new PutField(instructions, field));
    }
    ins.add(new VReturn(instructions));
}
Also used : Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.instructions.LDC) Method(net.runelite.asm.Method) CheckCast(net.runelite.asm.attributes.code.instructions.CheckCast) Instruction(net.runelite.asm.attributes.code.Instruction) PutStatic(net.runelite.asm.attributes.code.instructions.PutStatic) Code(net.runelite.asm.attributes.Code) VReturn(net.runelite.asm.attributes.code.instructions.VReturn) Type(net.runelite.asm.Type) Signature(net.runelite.asm.signature.Signature) IMul(net.runelite.asm.attributes.code.instructions.IMul) LMul(net.runelite.asm.attributes.code.instructions.LMul) ALoad(net.runelite.asm.attributes.code.instructions.ALoad) PutField(net.runelite.asm.attributes.code.instructions.PutField)

Aggregations

LDC (net.runelite.asm.attributes.code.instructions.LDC)46 Instruction (net.runelite.asm.attributes.code.Instruction)39 Instructions (net.runelite.asm.attributes.code.Instructions)38 Code (net.runelite.asm.attributes.Code)32 ClassGroup (net.runelite.asm.ClassGroup)29 Test (org.junit.Test)27 VReturn (net.runelite.asm.attributes.code.instructions.VReturn)26 IMul (net.runelite.asm.attributes.code.instructions.IMul)23 IStore (net.runelite.asm.attributes.code.instructions.IStore)23 ILoad (net.runelite.asm.attributes.code.instructions.ILoad)22 Deobfuscator (net.runelite.deob.Deobfuscator)20 Execution (net.runelite.asm.execution.Execution)19 IAdd (net.runelite.asm.attributes.code.instructions.IAdd)14 Pop (net.runelite.asm.attributes.code.instructions.Pop)13 Method (net.runelite.asm.Method)11 Type (net.runelite.asm.Type)10 Label (net.runelite.asm.attributes.code.Label)10 Field (net.runelite.asm.Field)9 PushConstantInstruction (net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction)9 Signature (net.runelite.asm.signature.Signature)8