Skip to content

Instantly share code, notes, and snippets.

@sschr15
Created August 11, 2020 02:23
Show Gist options
  • Save sschr15/46017a58b145a6e5b2728e0bc8f6f4a5 to your computer and use it in GitHub Desktop.
Save sschr15/46017a58b145a6e5b2728e0bc8f6f4a5 to your computer and use it in GitHub Desktop.
Access Transformers versus the more capable (in most ways) Mixin probably in a more complicated way than necessary
#Example AccessTransformer
public main.MainClass field_1000_a # number
public main.MainClass func_1000_a # addToNumber
package at;
import main.MainClass;
// This is a class that uses Access Transformers to access otherwise inaccessible fields/methods.
public class MyClass {
public static void main(String[] args) {
MainClass thing = new MainClass;
System.out.println(thing.addToNumber(thing.number)); //84 These lines use things declared public in
thing.number = -1; // the AccessTransformer configuration.
thing.printNumber(); // -1
}
}
package main;
// Fake obfuscated names are comments
public class MainClass {
private int number = 42; // field_1000_a
protected int addToNumber(int whatToAdd) { // func_1000_a
return number + whatToAdd;
}
public void printNumber() { // func_1001_a
// Java accepts methods with the same name as long as the parameter counts
// are different. Forge sticks the obfuscated (notch) name at the end of the
// intermediary (searge) name, but gives a unique numerical ID to them all.
System.out.println(number);
}
}
package mixin;
import main.MainClass;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;
@Mixin(MainClass.class)
public abstract class MainClassMixin implements MainInterface {
@Shadow private int number;
public void setNumber(int number) {
this.number = number;
}
@Accessor("number")
public abstract int getNumber();
@Invoker("addToNumber")
public abstract int addNumbers();
}
package mixin;
// Interface to cast to in order to access things
public interface MainInterface {
void setNumber(int number);
int getNumber();
int addNumbers(int number);
}
package mixin;
import main.MainClass;
public class MyClass {
public static void main(String[] args) {
MainClass thing = new MainClass;
System.out.println(((MainInterface) thing).addNumbers(((MainInterface) thing).getNumber)); // 84
((MainInterface) thing).setNumber(-1);
thing.printNumber(); // -1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment