All Projects → Yamakaja → RuntimeTransformer

Yamakaja / RuntimeTransformer

Licence: MIT license
Library for easily modifying loaded classes at runtime

Programming Languages

java
68154 projects - #9 most used programming language

Labels

Projects that are alternatives of or similar to RuntimeTransformer

APIInfo-Plugin-x86
APIInfo Plugin (x86) - A Plugin For x64dbg
Stars: ✭ 42 (+23.53%)
Mutual labels:  asm
z80-sample-program
This is a small Z80 assembler program that just puts some colored lines on the ZX Spectrum's screen. The intention is to use this as a kind of tutorial for DeZog (Z80 debugger).
Stars: ✭ 14 (-58.82%)
Mutual labels:  asm
asmdot
[Unstable] Fast, zero-copy and lightweight (Arm | Mips | x86) assembler in (C | C++ | C# | Go | Haskell | Javascript | Nim | OCaml | Python | Rust).
Stars: ✭ 23 (-32.35%)
Mutual labels:  asm
512-byte-vm
A VM image in 512 bytes. Yes, you read it right.
Stars: ✭ 29 (-14.71%)
Mutual labels:  asm
DevSound
Game Boy sound driver
Stars: ✭ 48 (+41.18%)
Mutual labels:  asm
ICE
ICE Compiler for the TI-84 Plus CE
Stars: ✭ 12 (-64.71%)
Mutual labels:  asm
boot2flappy
Flappy Bird as bootable UEFI executable
Stars: ✭ 48 (+41.18%)
Mutual labels:  asm
asm
🏃 An x86-64 assembler written in Go.
Stars: ✭ 76 (+123.53%)
Mutual labels:  asm
cmake-nasm-test
Building a nasm hello world app with cmake
Stars: ✭ 18 (-47.06%)
Mutual labels:  asm
CopyToAsm-Plugin-x86
CopyToAsm (x86) - A Plugin For x64dbg
Stars: ✭ 23 (-32.35%)
Mutual labels:  asm
discovery
辅助Android开发者在多模块工程间跨模块获取接口(或抽象类)的实现类的开源库,可实现模块的顺序初始化、业务的动态组合等实现。基于AGP和ASM开发。
Stars: ✭ 46 (+35.29%)
Mutual labels:  asm
z80count
A tool to annotate Z80 assembler with cycle counts
Stars: ✭ 28 (-17.65%)
Mutual labels:  asm
asm-inline
Inline raw ASM instructions in Java
Stars: ✭ 23 (-32.35%)
Mutual labels:  asm
dcc
Direct/Interactive C Compiler
Stars: ✭ 18 (-47.06%)
Mutual labels:  asm
creating-controls-in-assembler
Gitbook: https://mrfearless.gitbooks.io/creating-controls-in-assembler
Stars: ✭ 20 (-41.18%)
Mutual labels:  asm
Uatu
Android方法调用跟踪 ; 方法耗时统计 ; 方法调用参数以及返回值跟踪 ; 方法调用替换;方法hook
Stars: ✭ 93 (+173.53%)
Mutual labels:  asm
arTIfiCE
arTIfiCE is a jailbreak for TI CE calculators with OS 5.5 and 5.6. It brings back ASM programs and games.
Stars: ✭ 60 (+76.47%)
Mutual labels:  asm
Etripator
A PC-Engine disassembler
Stars: ✭ 16 (-52.94%)
Mutual labels:  asm
cortexm-AES
high performance AES implementations optimized for cortex-m microcontrollers
Stars: ✭ 18 (-47.06%)
Mutual labels:  asm
c8c
The chip8 compiler, assembler, and virtual machine
Stars: ✭ 110 (+223.53%)
Mutual labels:  asm

RuntimeTransformer

A tool allowing for easy class modification at runtime, when using a normal javaagent at startup would be too inconvenient. Note, this method comes with disadvantages, for example method modifiers may not be altered, new methods can not be created and neither can class inheritance be changed.

Usage

To install the artifact into your local maven repo execute the correct gradle wrapper with the "publishToMavenLocal" task, that is gradlew.bat publishToMavenLocal under Windows, and ./gradlew publishToMavenLocal under *nix;

Lets assume we want to inject an event handler into the setHealth method of EntityLiving, therefore the method should something like this after transformation:

public void setHealth(float newHealth) {
    ImaginaryEvent event = ImaginaryEventBus.callEvent(new ImaginaryEvent(this, newHealth));
    
    if (event.isCancelled())
        return;
        
    newHealth = event.getNewHealth();
    
    // Minecraft Code
}

To get there, we first need to define a transformer, this should optimally be in its own class and look something like this:

@Transform(EntityLiving.class) // The class we want to transform
public class EntityLivingTransformer extends EntityLiving { // Extending EntityLiving in our transformer makes things easier, but isn't required (Which, for example, allows you to transform final classes)
    
    @Inject(InjectionType.INSERT) // Our goal is to insert code at the beginning of the method, and leave everything else intact
    public void setHealth(float newHealth) { // Then just "override" the method as usual, if it is final add an _INJECTED to the method name
        ImaginaryEvent event = ImaginaryEventBus.callEvent(new ImaginaryEvent(this, newHealth)); // Our event handling code from above
            
        if (event.isCancelled())
            return;
            
        newHealth = event.getNewHealth();
        
        throw null; // Pass execution on to the rest of the method. This will be removed at runtime but is required for compilation (At least when the method doesn't return void, so it's not necessary in this case)
        
    }
    
} 

And that's pretty much it, now we just need to create our runtime transformer:

new RuntimeTransformer( EntityLivingTransformer.class );

For Java 9+ runtimes, you have to allow self attaching by adding this startup -Djdk.attach.allowAttachSelf=true parameter or creating the RuntimeTransformer instance in a separate process.

And we're done.

You can find more examples in the example plugin.

"Documentation"

There are three types of Injection:

  • INSERT (Inserts your code at the beginning of the method)
  • OVERWRITE (Overwrites the method with your code)
  • APPEND (Adds code to the end of the method, only works on methods returning void)

Compiling

Run this command to build the api project. ./gradlew jar

If you want to build the example project add -Pbuild-example ./gradlew jar -Pbuild-example

Installation

To install the api jar into your local maven repo run ./gradlew publishToMavenLocal

The correct artifact can then be included using the following dependency definition:

        <dependency>
            <groupId>me.yamakaja.runtimetransformer</groupId>
            <artifactId>api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

Don't forget to actually include the artifact in your final jar, using the maven-shade-plugin or an equivalent alternative!

Alternative: Maven repository

@sgdc3 has offered to host the artifacts on their build server, you can access them by adding the following to your <repositories> (This way you wont have to compile it locally):

        <repository>
            <id>codemc</id>
            <url>https://repo.codemc.org/repository/maven-public/</url>
        </repository>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].