-
-
Notifications
You must be signed in to change notification settings - Fork 179
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
10 changed files
with
171 additions
and
148 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
coremods/src/main/java/net/neoforged/neoforge/coremods/MethodRedirector.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,106 @@ | ||
package net.neoforged.neoforge.coremods; | ||
|
||
import cpw.mods.modlauncher.api.ITransformer; | ||
import cpw.mods.modlauncher.api.ITransformerVotingContext; | ||
import cpw.mods.modlauncher.api.TargetType; | ||
import cpw.mods.modlauncher.api.TransformerVoteResult; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.tree.ClassNode; | ||
import org.objectweb.asm.tree.MethodInsnNode; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Redirect calls to one method to another. | ||
*/ | ||
public class MethodRedirector implements ITransformer<ClassNode> { | ||
private static final Logger LOG = LoggerFactory.getLogger(MethodRedirector.class); | ||
private final Map<String, List<MethodRedirection>> redirectionsByClass = new HashMap<>(); | ||
private final Set<Target<ClassNode>> targets = new HashSet<>(); | ||
|
||
private static final List<MethodRedirection> REDIRECTIONS = List.of( | ||
new MethodRedirection( | ||
Opcodes.INVOKEVIRTUAL, | ||
"finalizeSpawn", | ||
"(Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/world/DifficultyInstance;Lnet/minecraft/world/entity/MobSpawnType;Lnet/minecraft/world/entity/SpawnGroupData;)Lnet/minecraft/world/entity/SpawnGroupData;", | ||
"finalize_spawn_targets.json", | ||
methodInsnNode -> new MethodInsnNode( | ||
Opcodes.INVOKESTATIC, | ||
"net/neoforged/neoforge/event/EventHooks", | ||
"finalizeMobSpawn", | ||
"(Lnet/minecraft/world/entity/Mob;Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/world/DifficultyInstance;Lnet/minecraft/world/entity/MobSpawnType;Lnet/minecraft/world/entity/SpawnGroupData;)Lnet/minecraft/world/entity/SpawnGroupData;", | ||
false) | ||
) | ||
); | ||
|
||
public MethodRedirector() { | ||
for (var redirection : REDIRECTIONS) { | ||
var targetClassNames = CoremodUtils.loadResource(redirection.targetClassListFile, String[].class); | ||
for (var targetClassName : targetClassNames) { | ||
targets.add(Target.targetClass(targetClassName)); | ||
var redirections = redirectionsByClass.computeIfAbsent(targetClassName, s -> new ArrayList<>()); | ||
redirections.add(redirection); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public TargetType<ClassNode> getTargetType() { | ||
return TargetType.CLASS; | ||
} | ||
|
||
@Override | ||
public Set<Target<ClassNode>> targets() { | ||
return targets; | ||
} | ||
|
||
@Override | ||
public ClassNode transform(ClassNode classNode, ITransformerVotingContext votingContext) { | ||
var redirections = redirectionsByClass.getOrDefault(classNode.name, Collections.emptyList()); | ||
|
||
var methods = classNode.methods; | ||
for (var i = 0; i < methods.size(); i++) { | ||
var instr = methods.get(i).instructions; | ||
for (var ix = 0; ix < instr.size(); ix++) { | ||
var node = instr.get(ix); | ||
if (node instanceof MethodInsnNode methodInsnNode) { | ||
for (var redirection : redirections) { | ||
if (redirection.invokeOpCode == methodInsnNode.getOpcode() | ||
&& redirection.methodName.equals(methodInsnNode.name) | ||
&& redirection.methodDescriptor.equals(methodInsnNode.desc)) { | ||
// Found a match for the target method | ||
instr.set( | ||
methodInsnNode, | ||
redirection.redirector.apply(methodInsnNode) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return classNode; | ||
} | ||
|
||
@Override | ||
public TransformerVoteResult castVote(ITransformerVotingContext context) { | ||
return TransformerVoteResult.YES; | ||
} | ||
|
||
private record MethodRedirection( | ||
int invokeOpCode, | ||
String methodName, | ||
String methodDescriptor, | ||
String targetClassListFile, | ||
Function<MethodInsnNode, MethodInsnNode> redirector | ||
) { | ||
} | ||
} |
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
58 changes: 0 additions & 58 deletions
58
coremods/src/main/java/net/neoforged/neoforge/coremods/field_to_instanceof.js
This file was deleted.
Oops, something went wrong.
40 changes: 0 additions & 40 deletions
40
coremods/src/main/java/net/neoforged/neoforge/coremods/field_to_method.js
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.