-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/main/java/org/violetmoon/zeta/annotation/ConditionalMixin.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.violetmoon.zeta.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Make a mixin depend on conditions to work | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface ConditionalMixin { | ||
/** | ||
* Only load mixin if requirements are fulfilled | ||
*/ | ||
Requirement[] require() default {}; | ||
|
||
/** | ||
* Don't load mixin if any of the requirements are fulfilled, higher priority then require | ||
*/ | ||
Requirement[] conflict() default {}; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/org/violetmoon/zeta/annotation/Requirement.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.violetmoon.zeta.annotation; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Requirement { | ||
/** | ||
* List of mod-id's | ||
*/ | ||
String[] value(); | ||
|
||
/** | ||
* Versions | Doesnt work yet | ||
*/ | ||
String[] versionPredicates() default {}; | ||
|
||
/** | ||
* if true, then mixin is applied when x mod is present otherwise not | ||
*/ | ||
boolean applyIfPresent() default true; | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/org/violetmoon/zeta/api/ConditionalMixinManager.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.violetmoon.zeta.api; | ||
|
||
import org.objectweb.asm.Type; | ||
import org.objectweb.asm.tree.AnnotationNode; | ||
import org.spongepowered.asm.service.MixinService; | ||
import org.spongepowered.asm.util.Annotations; | ||
import org.violetmoon.zeta.Zeta; | ||
import org.violetmoon.zeta.annotation.ConditionalMixin; | ||
import org.violetmoon.zeta.annotation.Requirement; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class ConditionalMixinManager { | ||
public static boolean shouldApply(Zeta zeta, String targetClassName, String mixinClassName) { | ||
try { | ||
List<AnnotationNode> annotationNodes = MixinService.getService().getBytecodeProvider().getClassNode(targetClassName).visibleAnnotations; | ||
if (annotationNodes == null) return true; | ||
|
||
boolean shouldApply = true; | ||
for (AnnotationNode node : annotationNodes) { | ||
if (node.desc.equals(Type.getDescriptor(ConditionalMixin.class))) { | ||
List<Requirement> requirements = Annotations.getValue(node, "require", Requirement.class); | ||
for (Requirement req : requirements) { | ||
String[] modids = req.value(); | ||
boolean applyIfPresent = req.applyIfPresent(); | ||
|
||
boolean areModsLoaded = areModsLoaded(zeta, modids); | ||
|
||
shouldApply = areModsLoaded == applyIfPresent; | ||
Zeta.GLOBAL_LOG.info("{}: {} is{}being applied because the mod(s) {} are{}loaded", zeta.modid, targetClassName, shouldApply ? " " : " not ", modids, areModsLoaded ? " " : " not "); | ||
} | ||
} | ||
} | ||
|
||
return shouldApply; | ||
} catch (IOException | ClassNotFoundException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static boolean areModsLoaded(Zeta zeta, String[] modids) { | ||
for (String mod : modids) { | ||
if (zeta.isModLoaded(mod)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |