Skip to content

Commit

Permalink
Fix compile errors & make some annotations repeatable
Browse files Browse the repository at this point in the history
  • Loading branch information
Moulberry committed Nov 16, 2024
1 parent b55f373 commit 9d19a93
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.moulberry.mixinconstraints.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.moulberry.mixinconstraints.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.moulberry.mixinconstraints.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Repeatable(IfModAbsents.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
public @interface IfModAbsent {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.moulberry.mixinconstraints.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
public @interface IfModAbsents {

IfModAbsent[] value();

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.moulberry.mixinconstraints.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Repeatable(IfModLoadeds.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
public @interface IfModLoaded {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.moulberry.mixinconstraints.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
public @interface IfModLoadeds {

IfModLoaded[] value();

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
import com.moulberry.mixinconstraints.annotations.IfDevEnvironment;
import com.moulberry.mixinconstraints.annotations.IfMinecraftVersion;
import com.moulberry.mixinconstraints.annotations.IfModAbsent;
import com.moulberry.mixinconstraints.annotations.IfModAbsents;
import com.moulberry.mixinconstraints.annotations.IfModLoaded;
import org.jetbrains.annotations.ApiStatus;
import com.moulberry.mixinconstraints.annotations.IfModLoadeds;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AnnotationNode;

import java.util.List;

@ApiStatus.Internal
public class AnnotationChecker {

private static final String IF_MOD_LOADED_DESC = Type.getDescriptor(IfModLoaded.class);
private static final String IF_MOD_LOADEDS_DESC = Type.getDescriptor(IfModLoadeds.class);
private static final String IS_MOD_ABSENT_DESC = Type.getDescriptor(IfModAbsent.class);
private static final String IF_MOD_ABSENTS_DESC = Type.getDescriptor(IfModAbsents.class);
private static final String IF_DEV_ENVIRONMENT_DESC = Type.getDescriptor(IfDevEnvironment.class);
private static final String IF_MINECRAFT_VERSION_DESC = Type.getDescriptor(IfMinecraftVersion.class);

Expand All @@ -24,7 +26,6 @@ public static boolean isConstraintAnnotationNode(AnnotationNode node) {
IF_DEV_ENVIRONMENT_DESC.equals(node.desc) || IF_MINECRAFT_VERSION_DESC.equals(node.desc);
}


@SuppressWarnings({"BooleanMethodIsAlwaysInverted", "DuplicatedCode"})
public static boolean checkAnnotationNode(AnnotationNode node) {
if (IF_MOD_LOADED_DESC.equals(node.desc)) {
Expand All @@ -43,6 +44,21 @@ public static boolean checkAnnotationNode(AnnotationNode node) {
}

return pass;
} else if (IF_MOD_LOADEDS_DESC.equals(node.desc)) {
List<IfModLoaded> ifModLoadeds = getAnnotationValue(node, "value", List.of());
for (IfModLoaded ifModLoaded : ifModLoadeds) {
boolean pass = ConstraintChecker.checkModLoaded(ifModLoaded.value(), List.of(ifModLoaded.aliases()), ifModLoaded.minVersion(), ifModLoaded.maxVersion());

if (MixinConstraints.VERBOSE) {
String result = pass ? "PASS" : "FAILED";
MixinConstraints.LOGGER.info("@IfModLoaded(value={}, minVersion={}, maxVersion={}) {}", ifModLoaded.value(), ifModLoaded.minVersion(), ifModLoaded.maxVersion(), result);
}

if (!pass) {
return false;
}
}
return true;
} else if (IS_MOD_ABSENT_DESC.equals(node.desc)) {
String value = getAnnotationValue(node, "value", "");
if (value.isEmpty()) throw new IllegalArgumentException("modid must not be empty");
Expand All @@ -59,6 +75,21 @@ public static boolean checkAnnotationNode(AnnotationNode node) {
}

return pass;
} else if (IF_MOD_ABSENTS_DESC.equals(node.desc)) {
List<IfModAbsent> ifModAbsents = getAnnotationValue(node, "value", List.of());
for (IfModAbsent ifModAbsent : ifModAbsents) {
boolean pass = ConstraintChecker.checkModAbsent(ifModAbsent.value(), List.of(ifModAbsent.aliases()), ifModAbsent.minVersion(), ifModAbsent.maxVersion());

if (MixinConstraints.VERBOSE) {
String result = pass ? "PASS" : "FAILED";
MixinConstraints.LOGGER.info("@IfModAbsent(value={}, minVersion={}, maxVersion={}) {}", ifModAbsent.value(), ifModAbsent.minVersion(), ifModAbsent.maxVersion(), result);
}

if (!pass) {
return false;
}
}
return true;
} else if (IF_DEV_ENVIRONMENT_DESC.equals(node.desc)) {
boolean negate = getAnnotationValue(node, "negate", false);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.moulberry.mixinconstraints.checker;

import com.moulberry.mixinconstraints.util.Abstractions;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public class ConstraintChecker {
/**
* Check if *ANY* of the modIds provided are loaded
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.moulberry.mixinconstraints.util;

import org.jetbrains.annotations.ApiStatus;
import org.objectweb.asm.tree.*;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
Expand All @@ -23,7 +22,6 @@
* Most of this class is adapted from MixinExtras's {@code MixinInternals} class, by LlamaLad7.
* MixinExtras is licensed under the MIT License: <a href="https://github.com/LlamaLad7/MixinExtras/blob/master/LICENSE">here</a>.
*/
@ApiStatus.Internal
@SuppressWarnings("unchecked")
public final class MixinHacks {
private static final MethodHandle TARGET_CLASS_CONTEXT_MIXINS;
Expand Down Expand Up @@ -90,7 +88,7 @@ public static List<Pair<IMixinInfo, ClassNode>> getMixinsFor(ITargetClassContext
}

private static void addExtension(List<IExtension> extensions, IExtension newExtension) {
extensions.addFirst(newExtension);
extensions.add(0, newExtension);

// If this runs before our extensions it will fail since we're not done generating our bytecode.
List<IExtension> lateExtensions = new ArrayList<>();
Expand Down

0 comments on commit 9d19a93

Please sign in to comment.