Search in sources :

Example 96 with ClassFile

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

the class MixinInjector method injectFields.

/**
 * Finds fields that are marked @Inject and inject them into the target
 *
 * @param mixinClasses
 * @throws InjectionException
 */
private void injectFields(Map<Class<?>, List<ClassFile>> mixinClasses) throws InjectionException {
    // Inject fields, and put them in injectedFields if they can be used by other mixins
    for (Class<?> mixinClass : mixinClasses.keySet()) {
        ClassFile mixinCf;
        try {
            mixinCf = loadClass(mixinClass);
        } catch (IOException ex) {
            throw new InjectionException(ex);
        }
        List<ClassFile> targetCfs = mixinClasses.get(mixinClass);
        for (ClassFile cf : targetCfs) {
            for (Field field : mixinCf.getFields()) {
                // Always inject $assertionsEnabled if its missing.
                if (ASSERTION_FIELD.equals(field.getName())) {
                    if (cf.findField(ASSERTION_FIELD, Type.BOOLEAN) != null) {
                        continue;
                    }
                } else {
                    Annotation inject = field.getAnnotations().find(INJECT);
                    if (inject == null) {
                        continue;
                    }
                }
                Field copy = new Field(cf, field.getName(), field.getType());
                copy.setAccessFlags(field.getAccessFlags());
                copy.setPublic();
                copy.setValue(field.getValue());
                cf.addField(copy);
                if (injectedFields.containsKey(field.getName())) {
                    throw new InjectionException("Injected field names must be globally unique");
                }
                injectedFields.put(field.getName(), copy);
            }
        }
    }
}
Also used : GetField(net.runelite.asm.attributes.code.instructions.GetField) Field(net.runelite.asm.Field) PutField(net.runelite.asm.attributes.code.instructions.PutField) ClassFile(net.runelite.asm.ClassFile) IOException(java.io.IOException) Annotation(net.runelite.asm.attributes.annotation.Annotation)

Example 97 with ClassFile

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

the class MixinInjector method inject.

public void inject(Map<Class<?>, List<ClassFile>> mixinClasses) throws InjectionException {
    injectFields(mixinClasses);
    findShadowFields(mixinClasses);
    for (Class<?> mixinClass : mixinClasses.keySet()) {
        try {
            for (ClassFile cf : mixinClasses.get(mixinClass)) {
                // Make a new mixin ClassFile copy every time,
                // so they don't share Code references
                ClassFile mixinCf = loadClass(mixinClass);
                injectMethods(mixinCf, cf, shadowFields);
            }
        } catch (IOException ex) {
            throw new InjectionException(ex);
        }
    }
    injectFieldHooks(mixinClasses);
}
Also used : ClassFile(net.runelite.asm.ClassFile) IOException(java.io.IOException)

Example 98 with ClassFile

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

the class ScriptVM method findDeobField.

private Field findDeobField(String name) throws InjectionException {
    for (ClassFile c : inject.getDeobfuscated().getClasses()) {
        for (Field f : c.getFields()) {
            if (!f.getName().equals(name)) {
                continue;
            }
            String obfuscatedName = DeobAnnotations.getObfuscatedName(f.getAnnotations());
            ClassFile c2 = inject.toObClass(c);
            return c2.findField(obfuscatedName);
        }
    }
    throw new InjectionException(String.format("Mapped field \"%s\" could not be found.", name));
}
Also used : InjectionException(net.runelite.injector.InjectionException) GetField(net.runelite.asm.attributes.code.instructions.GetField) Field(net.runelite.asm.Field) PutField(net.runelite.asm.attributes.code.instructions.PutField) ClassFile(net.runelite.asm.ClassFile)

Example 99 with ClassFile

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

the class DrawAfterWidgetsTest method testInjectDrawWidgetsRev160.

@Test
public void testInjectDrawWidgetsRev160() throws Exception {
    // Rev 160 does not have the drawWidgets call inlined
    ClassFile deobClient = ClassUtil.loadClass(getClass().getResourceAsStream("/drawafterwidgets/Client_deob160.class"));
    ClassFile deobRasterizer = ClassUtil.loadClass(getClass().getResourceAsStream("/drawafterwidgets/Rasterizer2D_deob160.class"));
    ClassGroup deob = new ClassGroup();
    deob.addClass(deobClient);
    deob.addClass(deobRasterizer);
    ClassFile obClient = ClassUtil.loadClass(getClass().getResourceAsStream("/drawafterwidgets/Client_ob160.class"));
    ClassFile obRasterizer = ClassUtil.loadClass(getClass().getResourceAsStream("/drawafterwidgets/Rasterizer2D_ob160.class"));
    ClassGroup vanilla = new ClassGroup();
    vanilla.addClass(obClient);
    vanilla.addClass(obRasterizer);
    Inject inject = new Inject(deob, vanilla);
    new DrawAfterWidgets(inject).inject();
}
Also used : Inject(net.runelite.injector.Inject) ClassFile(net.runelite.asm.ClassFile) ClassGroup(net.runelite.asm.ClassGroup) Test(org.junit.Test)

Example 100 with ClassFile

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

the class DrawAfterWidgetsTest method testInjectDrawWidgetsRev153.

@Test
public void testInjectDrawWidgetsRev153() throws Exception {
    // Rev 153 has the drawWidgets call inlined
    ClassFile deobClient = ClassUtil.loadClass(getClass().getResourceAsStream("/drawafterwidgets/Client_deob153.class"));
    ClassFile deobRasterizer = ClassUtil.loadClass(getClass().getResourceAsStream("/drawafterwidgets/Rasterizer2D_deob153.class"));
    ClassGroup deob = new ClassGroup();
    deob.addClass(deobClient);
    deob.addClass(deobRasterizer);
    ClassFile obClient = ClassUtil.loadClass(getClass().getResourceAsStream("/drawafterwidgets/Client_ob153.class"));
    ClassFile obRasterizer = ClassUtil.loadClass(getClass().getResourceAsStream("/drawafterwidgets/Rasterizer2D_ob153.class"));
    ClassGroup vanilla = new ClassGroup();
    vanilla.addClass(obClient);
    vanilla.addClass(obRasterizer);
    Inject inject = new Inject(deob, vanilla);
    new DrawAfterWidgets(inject).inject();
}
Also used : Inject(net.runelite.injector.Inject) ClassFile(net.runelite.asm.ClassFile) ClassGroup(net.runelite.asm.ClassGroup) Test(org.junit.Test)

Aggregations

ClassFile (net.runelite.asm.ClassFile)103 Method (net.runelite.asm.Method)62 Field (net.runelite.asm.Field)39 ClassGroup (net.runelite.asm.ClassGroup)32 Code (net.runelite.asm.attributes.Code)21 Instruction (net.runelite.asm.attributes.code.Instruction)18 Test (org.junit.Test)18 Signature (net.runelite.asm.signature.Signature)17 Type (net.runelite.asm.Type)16 Instructions (net.runelite.asm.attributes.code.Instructions)16 ArrayList (java.util.ArrayList)14 List (java.util.List)13 Logger (org.slf4j.Logger)10 LoggerFactory (org.slf4j.LoggerFactory)10 IOException (java.io.IOException)9 InputStream (java.io.InputStream)9 Annotation (net.runelite.asm.attributes.annotation.Annotation)9 PushConstantInstruction (net.runelite.asm.attributes.code.instruction.types.PushConstantInstruction)9 HashMap (java.util.HashMap)8 GetFieldInstruction (net.runelite.asm.attributes.code.instruction.types.GetFieldInstruction)7