-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce ModifyInstanceofValue injector
- Loading branch information
Showing
12 changed files
with
274 additions
and
44 deletions.
There are no files selected for viewing
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
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
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
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
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,20 @@ | ||
val runtimeVersion: String by project | ||
val versionMc: String by project | ||
|
||
version = "$runtimeVersion+$versionMc" | ||
|
||
println("Runtime version: $version") | ||
|
||
dependencies { | ||
implementation(annotationProcessor("io.github.llamalad7:mixinextras-common:0.3.6")!!) | ||
} | ||
|
||
tasks.jar { | ||
manifest { | ||
manifest.attributes( | ||
"Implementation-Version" to project.version, | ||
"MixinConfigs" to "adapter.init.mixins.json", | ||
"FMLModType" to "GAMELIBRARY" | ||
) | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
runtime/src/main/java/org/sinytra/adapter/runtime/AdapterMixinPlugin.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,30 @@ | ||
package org.sinytra.adapter.runtime; | ||
|
||
import org.objectweb.asm.tree.ClassNode; | ||
import org.sinytra.adapter.runtime.inject.InstanceOfInjectionPoint; | ||
import org.sinytra.adapter.runtime.inject.ModifyInstanceofValueInjectionInfo; | ||
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; | ||
import org.spongepowered.asm.mixin.extensibility.IMixinInfo; | ||
import org.spongepowered.asm.mixin.injection.InjectionPoint; | ||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class AdapterMixinPlugin implements IMixinConfigPlugin { | ||
|
||
static { | ||
InjectionPoint.register(InstanceOfInjectionPoint.class, null); | ||
InjectionInfo.register(ModifyInstanceofValueInjectionInfo.class); | ||
} | ||
|
||
//@formatter:off | ||
@Override public void onLoad(String mixinPackage) {} | ||
@Override public String getRefMapperConfig() {return "";} | ||
@Override public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {return true;} | ||
@Override public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {} | ||
@Override public List<String> getMixins() {return List.of();} | ||
@Override public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {} | ||
@Override public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {} | ||
//@formatter:on | ||
} |
38 changes: 38 additions & 0 deletions
38
runtime/src/main/java/org/sinytra/adapter/runtime/inject/InstanceOfInjectionPoint.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,38 @@ | ||
package org.sinytra.adapter.runtime.inject; | ||
|
||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.tree.AbstractInsnNode; | ||
import org.objectweb.asm.tree.InsnList; | ||
import org.objectweb.asm.tree.TypeInsnNode; | ||
import org.spongepowered.asm.mixin.injection.InjectionPoint; | ||
import org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorByName; | ||
import org.spongepowered.asm.mixin.injection.struct.InjectionPointData; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
@InjectionPoint.AtCode(value = "INSTANCEOF", namespace = "sinytra") | ||
public class InstanceOfInjectionPoint extends InjectionPoint { | ||
private final String target; | ||
|
||
public InstanceOfInjectionPoint(InjectionPointData data) { | ||
super(data); | ||
|
||
this.target = ((ITargetSelectorByName) data.getTarget()).getOwner(); | ||
} | ||
|
||
@Override | ||
public boolean find(String s, InsnList insns, Collection<AbstractInsnNode> nodes) { | ||
List<AbstractInsnNode> found = new ArrayList<>(); | ||
|
||
for (AbstractInsnNode insn : insns) { | ||
if (insn instanceof TypeInsnNode type && insn.getOpcode() == Opcodes.INSTANCEOF && type.desc.equals(this.target)) { | ||
found.add(insn); | ||
} | ||
} | ||
|
||
nodes.addAll(found); | ||
return !found.isEmpty(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
runtime/src/main/java/org/sinytra/adapter/runtime/inject/ModifyInstanceofValue.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,27 @@ | ||
package org.sinytra.adapter.runtime.inject; | ||
|
||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Slice; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface ModifyInstanceofValue { | ||
String[] method(); | ||
|
||
At[] at(); | ||
|
||
Slice[] slice() default {}; | ||
|
||
boolean remap() default true; | ||
|
||
int require() default -1; | ||
|
||
int expect() default 1; | ||
|
||
int allow() default -1; | ||
} |
22 changes: 22 additions & 0 deletions
22
.../src/main/java/org/sinytra/adapter/runtime/inject/ModifyInstanceofValueInjectionInfo.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.sinytra.adapter.runtime.inject; | ||
|
||
import com.llamalad7.mixinextras.injector.MixinExtrasInjectionInfo; | ||
import org.objectweb.asm.tree.AnnotationNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
import org.spongepowered.asm.mixin.injection.code.Injector; | ||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; | ||
import org.spongepowered.asm.mixin.transformer.MixinTargetContext; | ||
|
||
@InjectionInfo.AnnotationType(ModifyInstanceofValue.class) | ||
@InjectionInfo.HandlerPrefix("modifyInstanceofValue") | ||
public class ModifyInstanceofValueInjectionInfo extends MixinExtrasInjectionInfo { | ||
|
||
public ModifyInstanceofValueInjectionInfo(MixinTargetContext mixin, MethodNode method, AnnotationNode annotation) { | ||
super(mixin, method, annotation); | ||
} | ||
|
||
@Override | ||
protected Injector parseInjector(AnnotationNode injectAnnotation) { | ||
return new ModifyInstanceofValueInjector(this); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
runtime/src/main/java/org/sinytra/adapter/runtime/inject/ModifyInstanceofValueInjector.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,47 @@ | ||
package org.sinytra.adapter.runtime.inject; | ||
|
||
import com.llamalad7.mixinextras.injector.StackExtension; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.Type; | ||
import org.objectweb.asm.tree.AbstractInsnNode; | ||
import org.objectweb.asm.tree.InsnList; | ||
import org.objectweb.asm.tree.InsnNode; | ||
import org.objectweb.asm.tree.VarInsnNode; | ||
import org.spongepowered.asm.mixin.injection.code.Injector; | ||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; | ||
import org.spongepowered.asm.mixin.injection.struct.InjectionNodes; | ||
import org.spongepowered.asm.mixin.injection.struct.Target; | ||
|
||
public class ModifyInstanceofValueInjector extends Injector { | ||
|
||
public ModifyInstanceofValueInjector(InjectionInfo info) { | ||
super(info, "@ModifyInstanceofValue"); | ||
} | ||
|
||
@Override | ||
protected void inject(Target target, InjectionNodes.InjectionNode node) { | ||
AbstractInsnNode valueNode = node.getCurrentTarget(); | ||
StackExtension stack = new StackExtension(target); | ||
|
||
InjectorData handler = new InjectorData(target, "instanceof value modifier"); | ||
validateParams(handler, Type.BOOLEAN_TYPE, Type.BOOLEAN_TYPE); | ||
|
||
InsnList insns = new InsnList(); | ||
|
||
if (!this.isStatic) { | ||
insns.add(new VarInsnNode(Opcodes.ALOAD, 0)); | ||
insns.add(new InsnNode(Opcodes.SWAP)); | ||
} | ||
|
||
if (handler.captureTargetArgs > 0) { | ||
pushArgs(target.arguments, insns, target.getArgIndices(), 0, handler.captureTargetArgs); | ||
} | ||
|
||
stack.receiver(this.isStatic); | ||
stack.capturedArgs(target.arguments, handler.captureTargetArgs); | ||
|
||
invokeHandler(insns); | ||
|
||
target.insns.insert(valueNode, insns); | ||
} | ||
} |
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,5 @@ | ||
{ | ||
"minVersion": "0.8.5", | ||
"plugin": "org.sinytra.adapter.runtime.AdapterMixinPlugin", | ||
"package": "org.sinytra.adapter.runtime.mixin" | ||
} |
Oops, something went wrong.