44import lombok .CustomLog ;
55import me .melontini .andromeda .base .events .Bus ;
66import me .melontini .andromeda .base .events .MixinConfigEvent ;
7- import me .melontini .andromeda .base .util .annotations .SpecialEnvironment ;
7+ import me .melontini .andromeda .base .util .ModulePlugin ;
8+ import me .melontini .andromeda .util .CommonValues ;
89import me .melontini .andromeda .util .exceptions .AndromedaException ;
9- import me .melontini .andromeda .util .mixin .AndromedaMixins ;
1010import me .melontini .dark_matter .api .base .reflect .wrappers .GenericField ;
1111import me .melontini .dark_matter .api .base .reflect .wrappers .GenericMethod ;
12- import me .melontini .dark_matter .api .base .util .mixin .ExtendablePlugin ;
13- import me .melontini .dark_matter .api .base .util .mixin .IPluginPlugin ;
14- import org .objectweb .asm .Label ;
15- import org .objectweb .asm .Opcodes ;
16- import org .objectweb .asm .Type ;
17- import org .objectweb .asm .tree .AnnotationNode ;
18- import org .objectweb .asm .tree .ClassNode ;
19- import org .objectweb .asm .tree .InsnList ;
20- import org .objectweb .asm .tree .MethodNode ;
12+ import org .jetbrains .annotations .ApiStatus ;
2113import org .spongepowered .asm .mixin .FabricUtil ;
2214import org .spongepowered .asm .mixin .Mixins ;
23- import org .spongepowered .asm .mixin .Unique ;
24- import org .spongepowered .asm .mixin .extensibility .IMixinInfo ;
25- import org .spongepowered .asm .mixin .transformer .meta .MixinMerged ;
2615import org .spongepowered .asm .service .IMixinService ;
2716import org .spongepowered .asm .service .MixinService ;
28- import org .spongepowered .asm .util .Annotations ;
2917
3018import java .io .ByteArrayInputStream ;
3119import java .io .IOException ;
3220import java .io .InputStream ;
3321import java .lang .reflect .Proxy ;
34- import java .util .List ;
35- import java .util .Set ;
22+ import java .util .HashMap ;
23+ import java .util .Map ;
24+ import java .util .Optional ;
3625
3726/**
3827 * The MixinProcessor is responsible for injecting dynamic mixin configs.
4332public class MixinProcessor {
4433
4534 public static final String NOTICE = "## Mixin configs are internal mod components and are not the same as user configs! ##" ;
35+ public static final String JAVA_VERSION = "JAVA_17" ;
36+ public static final String MIXIN_VERSION = "0.8.5" ;
37+
4638 private static final ThreadLocal <InputStream > CONFIG = ThreadLocal .withInitial (() -> null );
47- private static boolean done = false ;
39+ private boolean done = false ;
40+ private final ModuleManager manager ;
41+ private final Map <String , Module <?>> mixinConfigs = new HashMap <>();
42+
43+ public MixinProcessor (ModuleManager manager ) {
44+ this .manager = manager ;
45+ }
4846
49- public static void addMixins (ModuleManager manager ) {
47+ @ ApiStatus .Internal
48+ public Optional <Module <?>> fromConfig (String name ) {
49+ return Optional .ofNullable (mixinConfigs .get (name ));
50+ }
51+
52+ public void addMixins () {
5053 if (done ) return ;
5154 IMixinService service = MixinService .getService ();
52- MixinProcessor .injectService (service );
53- manager .loaded ().forEach ((module ) -> {
55+ this .injectService (service );
56+ this . manager .loaded ().forEach ((module ) -> {
5457 JsonObject config = createConfig (module );
5558
5659 String cfg = "andromeda_dynamic$$" + module .meta ().dotted () + ".mixins.json" ;
5760 try (ByteArrayInputStream bais = new ByteArrayInputStream (config .toString ().getBytes ())) {
58- CONFIG .set (bais );
61+ CONFIG .set (bais );//Is there a safer way to do this?
5962 Mixins .addConfiguration (cfg );
60- manager .mixinConfigs .put (cfg , module );
63+ this .mixinConfigs .put (cfg , module );
6164 } catch (IOException e ) {
6265 throw AndromedaException .builder ()
6366 .message ("Couldn't inject mixin config for module '%s'" .formatted (module .meta ().id ())).message (NOTICE )
@@ -66,38 +69,39 @@ public static void addMixins(ModuleManager manager) {
6669 CONFIG .remove ();
6770 }
6871 });
69- MixinProcessor .dejectService (service );
72+ this .dejectService (service );
7073 done = true ;
7174
72- Mixins .getConfigs ().forEach (config1 -> {
73- if (manager .mixinConfigs .containsKey (config1 .getName ())) {
74- config1 .getConfig ().decorate (FabricUtil .KEY_MOD_ID , "andromeda" );
75+ Mixins .getConfigs ().forEach (config -> {
76+ if (this .mixinConfigs .containsKey (config .getName ())) {
77+ config .getConfig ().decorate (FabricUtil .KEY_MOD_ID , CommonValues . MODID );
7578 }
7679 });
7780 }
7881
79- public static JsonObject createConfig (Module <?> module ) {
82+ public JsonObject createConfig (Module <?> module ) {
8083 JsonObject object = new JsonObject ();
8184 object .addProperty ("required" , true );
82- object .addProperty ("minVersion" , "0.8" );
85+ object .addProperty ("minVersion" , MIXIN_VERSION );
8386 object .addProperty ("package" , module .getClass ().getPackageName () + ".mixin" );
84- object .addProperty ("compatibilityLevel" , "JAVA_17" );
85- object .addProperty ("plugin" , Plugin .class .getName ());
87+ object .addProperty ("compatibilityLevel" , JAVA_VERSION );
88+ object .addProperty ("plugin" , ModulePlugin .class .getName ());
8689 object .addProperty ("refmap" , "andromeda-refmap.json" );
8790 JsonObject injectors = new JsonObject ();
8891 injectors .addProperty ("defaultRequire" , 1 );
92+ injectors .addProperty ("maxShiftBy" , 3 );
8993 object .add ("injectors" , injectors );
9094
91- Bus <MixinConfigEvent > bus = module .getOrCreateBus (MixinConfigEvent . class , null );
95+ Bus <MixinConfigEvent > bus = module .getOrCreateBus ("mixin_config_event" , null );
9296 if (bus != null ) bus .invoker ().accept (object );
9397
9498 return object ;
9599 }
96100
97- private static final GenericMethod <?, MixinService > GET_INSTANCE = GenericMethod .of (MixinService .class , "getInstance" );
98- private static final GenericField <MixinService , IMixinService > SERVICE = GenericField .of (MixinService .class , "service" );
101+ private final GenericMethod <?, MixinService > GET_INSTANCE = GenericMethod .of (MixinService .class , "getInstance" );
102+ private final GenericField <MixinService , IMixinService > SERVICE = GenericField .of (MixinService .class , "service" );
99103
100- public static void injectService (IMixinService currentService ) {
104+ public void injectService (IMixinService currentService ) {
101105 IMixinService service = (IMixinService ) Proxy .newProxyInstance (MixinProcessor .class .getClassLoader (), new Class []{IMixinService .class }, (proxy , method , args ) -> {
102106 if (method .getName ().equals ("getResourceAsStream" )) {
103107 if (args [0 ] instanceof String s ) {
@@ -113,74 +117,8 @@ public static void injectService(IMixinService currentService) {
113117 SERVICE .accessible (true ).set (serviceProxy , service );
114118 }
115119
116- public static void dejectService (IMixinService realService ) {
120+ public void dejectService (IMixinService realService ) {
117121 MixinService serviceProxy = GET_INSTANCE .accessible (true ).invoke (null );
118122 SERVICE .accessible (true ).set (serviceProxy , realService );
119123 }
120-
121- @ SuppressWarnings ("UnstableApiUsage" )
122- public static class Plugin extends ExtendablePlugin {
123-
124- private static final String MIXIN_ENVIRONMENT_ANNOTATION = "L" + SpecialEnvironment .class .getName ().replace ("." , "/" ) + ";" ;
125-
126- private String mixinPackage ;
127-
128- @ Override
129- protected void collectPlugins (Set <IPluginPlugin > plugins ) {
130- plugins .add (DefaultPlugins .constructDummyPlugin ());
131- }
132-
133- protected void onPluginLoad (String mixinPackage ) {
134- this .mixinPackage = mixinPackage ;
135- }
136-
137- protected void getMixins (List <String > mixins ) {
138- mixins .addAll (AndromedaMixins .discoverInPackage (this .mixinPackage ));
139- }
140-
141- @ Override
142- protected void afterApply (String targetClassName , ClassNode targetClass , String mixinClassName , IMixinInfo mixinInfo ) {
143- if (targetClass .visibleAnnotations != null && !targetClass .visibleAnnotations .isEmpty ()) {//strip our annotation from the class
144- targetClass .visibleAnnotations .removeIf (node -> MIXIN_ENVIRONMENT_ANNOTATION .equals (node .desc ));
145- }
146-
147- for (MethodNode method : targetClass .methods ) {
148- AnnotationNode unique = Annotations .getVisible (method , Unique .class );
149- AnnotationNode mixinMerged = Annotations .getVisible (method , MixinMerged .class );
150- if (unique == null && mixinMerged != null ) {
151- String mixin = Annotations .getValue (mixinMerged , "mixin" );
152- if (mixin .startsWith (this .mixinPackage )) {
153- wrapNodeWithErrorHandling (method , ModuleManager .get ().moduleFromConfig (mixinInfo .getConfig ().getName ()).orElseThrow ().meta ().id ());
154- }
155- }
156- }
157- }
158-
159- private void wrapNodeWithErrorHandling (MethodNode handlerNode , String module ) {
160- Label start = new Label (), end = new Label (), handler = new Label (), handlerEnd = new Label ();
161-
162- String throwable = Type .getInternalName (Throwable .class );
163- handlerNode .visitTryCatchBlock (start , end , handler , throwable );
164-
165- InsnList old = handlerNode .instructions ;
166- handlerNode .instructions = new InsnList ();
167- handlerNode .visitLabel (start );
168- handlerNode .instructions .add (old );
169-
170- handlerNode .visitLabel (end );
171- handlerNode .visitJumpInsn (Opcodes .GOTO , handlerEnd );
172-
173- handlerNode .visitLabel (handler );
174- handlerNode .visitVarInsn (Opcodes .ASTORE , handlerNode .maxLocals );
175-
176- handlerNode .visitVarInsn (Opcodes .ALOAD , handlerNode .maxLocals );
177- handlerNode .visitLdcInsn (module );
178- handlerNode .visitMethodInsn (Opcodes .INVOKESTATIC , Type .getInternalName (AndromedaException .class ), "moduleException" , "(" + Type .getDescriptor (Throwable .class ) + Type .getDescriptor (String .class ) + ")" + Type .getDescriptor (AndromedaException .class ), false );
179-
180- handlerNode .visitInsn (Opcodes .ATHROW );
181- handlerNode .visitLabel (handlerEnd );
182-
183- handlerNode .visitLocalVariable ("exc" , "L" + throwable + ";" , null , start , handler , handlerNode .maxLocals );
184- }
185- }
186124}
0 commit comments