Skip to content

Commit

Permalink
Add conditional mixin application logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Bawnorton committed Jul 8, 2023
1 parent 2b2404f commit 1fde096
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 12 deletions.
35 changes: 32 additions & 3 deletions common/src/main/java/com/bawnorton/neruina/NeruinaMixinPlugin.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.bawnorton.neruina;

import com.bawnorton.neruina.annotation.ConditionalMixin;
import dev.architectury.injectables.annotations.ExpectPlatform;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
import org.spongepowered.asm.service.MixinService;
import org.spongepowered.asm.util.Annotations;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import static org.spongepowered.asm.util.Bytecode.getDescriptor;

public class NeruinaMixinPlugin implements IMixinConfigPlugin {
@Override
public void onLoad(String mixinPackage) {
Expand All @@ -20,9 +28,30 @@ public String getRefMapperConfig() {
}

@Override
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
if(mixinClassName.equals("com.bawnorton.neruina.mixin.WorldMixin")) {
return !isModLoaded("noseenotick");
public boolean shouldApplyMixin(String targetName, String className) {
return testClass(className);
}

public static boolean testClass(String className) {
try {
List<AnnotationNode> annotationNodes = MixinService.getService().getBytecodeProvider().getClassNode(className).visibleAnnotations;
if(annotationNodes == null) return true;

for(AnnotationNode node: annotationNodes) {
if(node.desc.equals(Type.getDescriptor(ConditionalMixin.class))) {
String modid = Annotations.getValue(node, "modid");
boolean applyIfPresent = Annotations.getValue(node, "applyIfPresent", Boolean.TRUE);
if(isModLoaded(modid)) {
Neruina.LOGGER.info("NeruinaMixinPlugin: " + className + " is" + (applyIfPresent ? " " : " not ") + "being applied because " + modid + " is loaded");
return applyIfPresent;
} else {
Neruina.LOGGER.info("NeruinaMixinPlugin: " + className + " is" + (!applyIfPresent ? " " : " not ") + "being applied because " + modid + " is not loaded");
return !applyIfPresent;
}
}
}
} catch (ClassNotFoundException | IOException e) {
throw new RuntimeException(e);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.bawnorton.neruina.annotation;

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

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ConditionalMixin {
String modid();
boolean applyIfPresent() default true;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bawnorton.neruina.mixin;

import com.bawnorton.neruina.Neruina;
import com.bawnorton.neruina.annotation.ConditionalMixin;
import com.bawnorton.neruina.thread.ConditionalRunnable;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
Expand All @@ -19,6 +20,7 @@
import java.util.function.Consumer;

@Mixin(World.class)
@ConditionalMixin(modid = "noseenotick", applyIfPresent = false)
public abstract class WorldMixin {
@Inject(method = "shouldUpdatePostDeath", at = @At("HEAD"), cancellable = true)
public void shouldUpdatePostDeath(Entity entity, CallbackInfoReturnable<Boolean> cir) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bawnorton.neruina.fabric;

import com.bawnorton.neruina.NeruinaMixinPlugin;
import com.bawnorton.neruina.annotation.ConditionalMixin;
import net.fabricmc.loader.api.FabricLoader;
import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
Expand All @@ -20,8 +22,8 @@ public String getRefMapperConfig() {
}

@Override
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
return true;
public boolean shouldApplyMixin(String targetName, String className) {
return NeruinaMixinPlugin.testClass(className);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion forge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies {

implementation(annotationProcessor("com.github.llamalad7.mixinextras:mixinextras-common:${rootProject.mixin_extras_version}"))
implementation(include("com.github.llamalad7.mixinextras:mixinextras-forge:${rootProject.mixin_extras_version}"))

implementation forgeRuntimeLibrary("curse.maven:no-see-no-tick-833405:4596138")

common(project(path: ":common", configuration: "namedElements")) { transitive false }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bawnorton.neruina.forge;

import com.bawnorton.neruina.NeruinaMixinPlugin;
import net.minecraftforge.fml.loading.LoadingModList;
import net.minecraftforge.fml.loading.moddiscovery.ModInfo;
import org.objectweb.asm.tree.ClassNode;
Expand All @@ -22,10 +23,7 @@ public String getRefMapperConfig() {

@Override
public boolean shouldApplyMixin(String targetName, String className) {
if(className.contains("noseenotick")) {
return isModLoaded("noseenotick");
}
return true;
return NeruinaMixinPlugin.testClass(className);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bawnorton.neruina.mixin.forge.noseenotick;

import com.bawnorton.neruina.Neruina;
import com.bawnorton.neruina.annotation.ConditionalMixin;
import com.bawnorton.neruina.thread.ConditionalRunnable;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
Expand All @@ -18,6 +19,7 @@

@Pseudo
@Mixin(targets = "net.minecraft.entity.TickOptimizer", remap = false)
@ConditionalMixin(modid = "noseenotick")
public abstract class TickOptimizerMixin {
@WrapOperation(method = "handleGuardEntityTick", at = @At(value = "INVOKE", target = "Ljava/util/function/Consumer;accept(Ljava/lang/Object;)V"))
private static void catchTickingEntities(Consumer<Entity> consumer, Object param, Operation<Void> original) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bawnorton.neruina.mixin.forge.noseenotick;

import com.bawnorton.neruina.Neruina;
import com.bawnorton.neruina.annotation.ConditionalMixin;
import net.minecraft.entity.Entity;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -9,6 +10,7 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(World.class)
@ConditionalMixin(modid = "noseenotick")
public abstract class WorldMixin {
@Inject(method = "shouldUpdatePostDeath", at = @At("HEAD"), cancellable = true)
public void shouldUpdatePostDeath(Entity entity, CallbackInfoReturnable<Boolean> cir) {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ minecraft_version=1.20
enabled_platforms=fabric,forge

archives_base_name=neruina
mod_version=1.1.0
mod_version=1.1.1
maven_group=com.bawnorton.neruina

architectury_version=9.0.8
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ include("common")
include("fabric")
include("forge")

rootProject.name = "architectury-neruina"
rootProject.name = "Neruina"

0 comments on commit 1fde096

Please sign in to comment.