Created
August 11, 2020 02:23
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Example AccessTransformer | |
public main.MainClass field_1000_a # number | |
public main.MainClass func_1000_a # addToNumber |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mixin; | |
// Interface to cast to in order to access things | |
public interface MainInterface { | |
void setNumber(int number); | |
int getNumber(); | |
int addNumbers(int number); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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