Search in sources :

Example 16 with Field

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

the class HookImporter method importHooks.

@Test
@Ignore
public void importHooks() {
    int classes = 0, fields = 0, methods = 0, callbacks = 0;
    for (String deobfuscatedClassName : hooks.keySet()) {
        ClassHook ch = hooks.get(deobfuscatedClassName);
        ClassFile cf = this.findClassWithObfuscatedName(ch.getClazz());
        assert cf != null;
        String implementsName = getAnnotation(cf.getAnnotations(), IMPLEMENTS);
        if (implementsName.isEmpty()) {
            cf.getAnnotations().addAnnotation(IMPLEMENTS, "value", deobfuscatedClassName);
            ++classes;
        }
        for (String deobfuscatedFieldName : ch.getFields().keySet()) {
            FieldHook fh = ch.getFields().get(deobfuscatedFieldName);
            String[] s = fh.getField().split("\\.");
            Field f;
            if (s.length == 2) {
                ClassFile cf2 = this.findClassWithObfuscatedName(s[0]);
                assert cf2 != null;
                f = this.findFieldWithObfuscatedName(cf2, s[1]);
            } else if (s.length == 1) {
                f = this.findFieldWithObfuscatedName(cf, fh.getField());
            } else {
                throw new RuntimeException();
            }
            assert f != null;
            String exportedName = getAnnotation(f.getAnnotations(), EXPORT);
            if (exportedName.isEmpty()) {
                f.getAnnotations().addAnnotation(EXPORT, "value", deobfuscatedFieldName);
                ++fields;
            }
        }
        for (String deobfuscatedMethodName : ch.getMethods().keySet()) {
            MethodHook mh = ch.getMethods().get(deobfuscatedMethodName);
            String[] s = mh.getMethod().split("\\.");
            Method m;
            if (s.length == 2) {
                ClassFile cf2 = this.findClassWithObfuscatedName(s[0]);
                assert cf2 != null;
                m = this.findMethodWithObfuscatedName(cf2, s[1], mh.getClientDesc());
            } else if (s.length == 1) {
                m = this.findMethodWithObfuscatedName(cf, mh.getMethod(), mh.getClientDesc());
            } else {
                throw new RuntimeException();
            }
            assert m != null;
            String exportedName = getAnnotation(m.getAnnotations(), EXPORT);
            if (exportedName.isEmpty()) {
                m.getAnnotations().addAnnotation(EXPORT, "value", deobfuscatedMethodName);
                ++methods;
            }
        }
        for (String deobfuscatedMethodName : ch.getCallbacks().keySet()) {
            MethodHook mh = ch.getCallbacks().get(deobfuscatedMethodName);
            String[] s = mh.getMethod().split("\\.");
            Method m;
            if (s.length == 2) {
                ClassFile cf2 = this.findClassWithObfuscatedName(s[0]);
                assert cf2 != null;
                m = this.findMethodWithObfuscatedName(cf2, s[1], mh.getClientDesc());
            } else if (s.length == 1) {
                m = this.findMethodWithObfuscatedName(cf, mh.getMethod(), mh.getClientDesc());
            } else {
                throw new RuntimeException();
            }
            assert m != null;
            String exportedName = getAnnotation(m.getAnnotations(), EXPORT);
            if (exportedName.isEmpty()) {
                m.getAnnotations().addAnnotation(EXPORT, "value", deobfuscatedMethodName);
                ++callbacks;
            }
        }
    }
    System.out.println("Imported " + classes + " classes, " + fields + " fields, " + methods + " methods, " + callbacks + " callbacks");
}
Also used : MethodHook(net.runelite.osb.inject.MethodHook) Field(net.runelite.asm.Field) ClassHook(net.runelite.osb.inject.ClassHook) ClassFile(net.runelite.asm.ClassFile) FieldHook(net.runelite.osb.inject.FieldHook) Method(net.runelite.asm.Method) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 17 with Field

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

the class MappingImporter method makeMappings.

@Test
@Ignore
public void makeMappings() throws IOException {
    InjectionModscript mod = Injection.load(MappingImporter.class.getResourceAsStream(RL_INJECTION));
    int fields = 0, classes = 0;
    for (int i = 0; i < mod.getGetterInjects().size(); ++i) {
        GetterInjectInstruction gii = (GetterInjectInstruction) mod.getGetterInjects().get(i);
        ClassFile cf = this.findClassWithObfuscatedName(gii.getGetterClassName());
        Assert.assertNotNull(cf);
        Field f = this.findFieldWithObfuscatedName(cf, gii.getGetterFieldName());
        if (f == null) {
            // so they don't all exist
            continue;
        }
        String attrName = gii.getGetterName();
        attrName = Utils.toExportedName(attrName);
        Annotations an = f.getAnnotations();
        Annotation a = an.find(EXPORT);
        if (a != null) {
            String exportedName = a.getElement().getString();
            if (!attrName.equals(exportedName)) {
                logger.info("Exported field " + f + " with mismatched name. Theirs: " + attrName + ", mine: " + exportedName);
            }
        } else {
            an.addAnnotation(EXPORT, "value", attrName);
            logger.info("Exporting field " + f + " with name " + attrName);
            ++fields;
        }
    }
    for (AddInterfaceInstruction aii : mod.getAddInterfaceInjects()) {
        ClassFile cf = this.findClassWithObfuscatedName(aii.getClientClass());
        Assert.assertNotNull(cf);
        String iface = aii.getInterfaceClass();
        iface = iface.replace("com/runeloader/api/bridge/os/accessor/", "");
        Annotations an = cf.getAnnotations();
        Annotation a = an.find(IMPLEMENTS);
        if (a != null) {
            String implementsName = a.getElement().getString();
            if (!iface.equals(implementsName)) {
                logger.info("Implements class " + cf + " with mismatched name. Theirs: " + iface + ", mine: " + implementsName);
            }
        } else {
            an.addAnnotation(IMPLEMENTS, "value", iface);
            logger.info("Exporting class " + cf.getName() + " with name " + iface);
            ++classes;
        }
    }
    logger.info("Added {} fields, {} classes", fields, classes);
}
Also used : Field(net.runelite.asm.Field) ClassFile(net.runelite.asm.ClassFile) Annotations(net.runelite.asm.attributes.Annotations) InjectionModscript(net.runelite.runeloader.inject.InjectionModscript) AddInterfaceInstruction(net.runelite.runeloader.inject.AddInterfaceInstruction) GetterInjectInstruction(net.runelite.runeloader.inject.GetterInjectInstruction) Annotation(net.runelite.asm.attributes.annotation.Annotation) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 18 with Field

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

the class Inject method toObField.

Field toObField(Field field) {
    String obfuscatedClassName = DeobAnnotations.getObfuscatedName(field.getClassFile().getAnnotations());
    // obfuscated name of field
    String obfuscatedFieldName = DeobAnnotations.getObfuscatedName(field.getAnnotations());
    Type type = getFieldType(field);
    ClassFile obfuscatedClass = vanilla.findClass(obfuscatedClassName);
    assert obfuscatedClass != null;
    Field obfuscatedField = obfuscatedClass.findFieldDeep(obfuscatedFieldName, type);
    assert obfuscatedField != null;
    return obfuscatedField;
}
Also used : Field(net.runelite.asm.Field) Type(net.runelite.asm.Type) ClassFile(net.runelite.asm.ClassFile)

Example 19 with Field

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

the class ModArith method insertGetterSetterMuls.

private void insertGetterSetterMuls(Encryption encr) {
    // before setfield insert imul * getter
    for (ClassFile cf : group.getClasses()) {
        for (Method m : cf.getMethods()) {
            Code code = m.getCode();
            if (code == null) {
                continue;
            }
            Instructions ins = code.getInstructions();
            List<Instruction> ilist = ins.getInstructions();
            for (int i = 0; i < ilist.size(); ++i) {
                Instruction in = ilist.get(i);
                if (in instanceof SetFieldInstruction) {
                    SetFieldInstruction sfi = (SetFieldInstruction) in;
                    Field f = sfi.getMyField();
                    if (f == null) {
                        continue;
                    }
                    Pair p = encr.getField(f.getPoolField());
                    if (p == null) {
                        continue;
                    }
                    // insert imul
                    if (p.getType() == Integer.class) {
                        ilist.add(i++, new LDC(ins, (int) p.getter));
                        ilist.add(i++, new IMul(ins));
                    } else if (p.getType() == Long.class) {
                        ilist.add(i++, new LDC(ins, (long) p.getter));
                        ilist.add(i++, new LMul(ins));
                    } else {
                        throw new IllegalStateException();
                    }
                } else if (in instanceof GetFieldInstruction) {
                    GetFieldInstruction sfi = (GetFieldInstruction) in;
                    Field f = sfi.getMyField();
                    if (f == null) {
                        continue;
                    }
                    Pair p = encr.getField(f.getPoolField());
                    if (p == null) {
                        continue;
                    }
                    // imul
                    if (p.getType() == Integer.class) {
                        ilist.add(++i, new LDC(ins, (int) p.setter));
                        ilist.add(++i, new IMul(ins));
                    } else if (p.getType() == Long.class) {
                        ilist.add(++i, new LDC(ins, (long) p.setter));
                        ilist.add(++i, new LMul(ins));
                    } else {
                        throw new IllegalStateException();
                    }
                }
            }
        }
    }
}
Also used : SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) ClassFile(net.runelite.asm.ClassFile) Instructions(net.runelite.asm.attributes.code.Instructions) LDC(net.runelite.asm.attributes.code.instructions.LDC) Method(net.runelite.asm.Method) DivisionInstruction(net.runelite.asm.attributes.code.instruction.types.DivisionInstruction) FieldInstruction(net.runelite.asm.attributes.code.instruction.types.FieldInstruction) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) ArrayStoreInstruction(net.runelite.asm.attributes.code.instruction.types.ArrayStoreInstruction) InvokeInstruction(net.runelite.asm.attributes.code.instruction.types.InvokeInstruction) SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction) Instruction(net.runelite.asm.attributes.code.Instruction) Code(net.runelite.asm.attributes.Code) Field(net.runelite.asm.Field) IMul(net.runelite.asm.attributes.code.instructions.IMul) LMul(net.runelite.asm.attributes.code.instructions.LMul) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction)

Example 20 with Field

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

the class FieldInliner method inlineUse.

public int inlineUse() {
    int count = 0;
    for (Field f : fields) {
        // replace getfield with constant push
        List<FieldInstruction> fins = fieldInstructions.get(f).stream().filter(f2 -> f2 instanceof GetFieldInstruction).collect(Collectors.toList());
        Object value = f.getValue();
        for (FieldInstruction fin : fins) {
            // remove fin, add push constant
            Instruction i = (Instruction) fin;
            Instruction pushIns = new LDC(i.getInstructions(), value);
            List<Instruction> instructions = i.getInstructions().getInstructions();
            int idx = instructions.indexOf(i);
            assert idx != -1;
            i.getInstructions().remove(i);
            instructions.add(idx, pushIns);
            ++count;
        }
        f.getClassFile().removeField(f);
    }
    return count;
}
Also used : Logger(org.slf4j.Logger) FieldInstruction(net.runelite.asm.attributes.code.instruction.types.FieldInstruction) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) Field(net.runelite.asm.Field) SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) LoggerFactory(org.slf4j.LoggerFactory) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction) Code(net.runelite.asm.attributes.Code) Multimap(com.google.common.collect.Multimap) Type(net.runelite.asm.Type) Deobfuscator(net.runelite.deob.Deobfuscator) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ClassGroup(net.runelite.asm.ClassGroup) List(java.util.List) ClassFile(net.runelite.asm.ClassFile) HashMultimap(com.google.common.collect.HashMultimap) Method(net.runelite.asm.Method) LDC(net.runelite.asm.attributes.code.instructions.LDC) Instructions(net.runelite.asm.attributes.code.Instructions) Instruction(net.runelite.asm.attributes.code.Instruction) Field(net.runelite.asm.Field) LDC(net.runelite.asm.attributes.code.instructions.LDC) FieldInstruction(net.runelite.asm.attributes.code.instruction.types.FieldInstruction) SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction) FieldInstruction(net.runelite.asm.attributes.code.instruction.types.FieldInstruction) PushConstantInstruction(net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction) SetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.SetFieldInstruction) GetFieldInstruction(net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction) Instruction(net.runelite.asm.attributes.code.Instruction) 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