Skip to content

Commit

Permalink
conditionals yes
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Jan 30, 2024
1 parent 35dfe81 commit 15a435a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/main/java/org/violetmoon/zeta/annotation/ConditionalMixin.java
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 src/main/java/org/violetmoon/zeta/annotation/Requirement.java
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 src/main/java/org/violetmoon/zeta/api/ConditionalMixinManager.java
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;
}
}

0 comments on commit 15a435a

Please sign in to comment.