-
-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add task to generate finalizeSpawn MethodRedirector coremod targets (#…
- Loading branch information
1 parent
368e69f
commit 4d3958a
Showing
6 changed files
with
144 additions
and
37 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
81 changes: 81 additions & 0 deletions
81
buildSrc/src/main/java/net/neoforged/neodev/GenerateFinalizeSpawnTargets.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,81 @@ | ||
package net.neoforged.neodev; | ||
|
||
import com.google.gson.GsonBuilder; | ||
import net.neoforged.neodev.utils.AsmUtils; | ||
import net.neoforged.neodev.utils.FileUtils; | ||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.file.RegularFileProperty; | ||
import org.gradle.api.tasks.InputFile; | ||
import org.gradle.api.tasks.OutputFile; | ||
import org.gradle.api.tasks.TaskAction; | ||
import org.objectweb.asm.ClassReader; | ||
import org.objectweb.asm.ClassVisitor; | ||
import org.objectweb.asm.MethodVisitor; | ||
import org.objectweb.asm.Opcodes; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
|
||
/** | ||
* This task is used to generate targets for the finalizeSpawn MethodRedirector coremod. | ||
*/ | ||
public abstract class GenerateFinalizeSpawnTargets extends DefaultTask { | ||
@InputFile | ||
public abstract RegularFileProperty getInput(); | ||
|
||
@OutputFile | ||
public abstract RegularFileProperty getOutput(); | ||
|
||
@TaskAction | ||
public void exec() throws IOException { | ||
var visitor = new Visitor(); | ||
AsmUtils.visitAllClasses( | ||
getInput().getAsFile().get(), | ||
visitor, | ||
ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES); | ||
|
||
var classList = List.copyOf(visitor.matchedClasses); | ||
|
||
FileUtils.writeStringSafe( | ||
getOutput().getAsFile().get().toPath(), | ||
new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(classList), | ||
StandardCharsets.UTF_8); | ||
} | ||
|
||
static class Visitor extends ClassVisitor { | ||
final Set<String> matchedClasses = new TreeSet<>(); | ||
String currentClass = null; | ||
|
||
protected Visitor() { | ||
super(Opcodes.ASM9); | ||
} | ||
|
||
@Override | ||
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { | ||
currentClass = name; | ||
} | ||
|
||
@Override | ||
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { | ||
// Ignore these classes as we special case them | ||
if (currentClass.equals("net/minecraft/world/level/BaseSpawner") | ||
|| currentClass.equals("net/minecraft/world/level/block/entity/trialspawner/TrialSpawner")) { | ||
return null; | ||
} | ||
|
||
return new MethodVisitor(api) { | ||
@Override | ||
public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) { | ||
if (opcode == Opcodes.INVOKEVIRTUAL | ||
&& name.equals("finalizeSpawn") | ||
&& descriptor.equals("(Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/world/DifficultyInstance;Lnet/minecraft/world/entity/EntitySpawnReason;Lnet/minecraft/world/entity/SpawnGroupData;)Lnet/minecraft/world/entity/SpawnGroupData;")) { | ||
matchedClasses.add(currentClass); | ||
} | ||
} | ||
}; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
buildSrc/src/main/java/net/neoforged/neodev/utils/AsmUtils.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 net.neoforged.neodev.utils; | ||
|
||
import org.objectweb.asm.ClassReader; | ||
import org.objectweb.asm.ClassVisitor; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.zip.ZipFile; | ||
|
||
public final class AsmUtils { | ||
private AsmUtils() {} | ||
|
||
public static void visitAllClasses(File jarFile, ClassVisitor visitor, int parsingOptions) throws IOException { | ||
try (var zip = new ZipFile(jarFile)) { | ||
var entries = zip.entries(); | ||
while (entries.hasMoreElements()) { | ||
var next = entries.nextElement(); | ||
if (next.isDirectory() || !next.getName().endsWith(".class")) continue; | ||
|
||
try (var in = zip.getInputStream(next)) { | ||
var reader = new ClassReader(in); | ||
reader.accept(visitor, parsingOptions); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
44 changes: 22 additions & 22 deletions
44
coremods/src/main/resources/net/neoforged/neoforge/coremods/finalize_spawn_targets.json
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 |
---|---|---|
@@ -1,24 +1,24 @@ | ||
[ | ||
"net/minecraft/server/commands/RaidCommand", | ||
"net/minecraft/server/commands/SummonCommand", | ||
"net/minecraft/world/entity/EntityType", | ||
"net/minecraft/world/entity/ai/village/VillageSiege", | ||
"net/minecraft/world/entity/animal/frog/Tadpole", | ||
"net/minecraft/world/entity/animal/horse/SkeletonTrapGoal", | ||
"net/minecraft/world/entity/monster/Evoker$EvokerSummonSpellGoal", | ||
"net/minecraft/world/entity/monster/Spider", | ||
"net/minecraft/world/entity/monster/Strider", | ||
"net/minecraft/world/entity/monster/Zombie", | ||
"net/minecraft/world/entity/monster/ZombieVillager", | ||
"net/minecraft/world/entity/npc/CatSpawner", | ||
"net/minecraft/world/entity/npc/Villager", | ||
"net/minecraft/world/entity/raid/Raid", | ||
"net/minecraft/world/level/NaturalSpawner", | ||
"net/minecraft/world/level/levelgen/PatrolSpawner", | ||
"net/minecraft/world/level/levelgen/PhantomSpawner", | ||
"net/minecraft/world/level/levelgen/structure/structures/OceanMonumentPieces$OceanMonumentPiece", | ||
"net/minecraft/world/level/levelgen/structure/structures/OceanRuinPieces$OceanRuinPiece", | ||
"net/minecraft/world/level/levelgen/structure/structures/SwampHutPiece", | ||
"net/minecraft/world/level/levelgen/structure/structures/WoodlandMansionPieces$WoodlandMansionPiece", | ||
"net/minecraft/world/level/levelgen/structure/templatesystem/StructureTemplate" | ||
"net/minecraft/server/commands/RaidCommand", | ||
"net/minecraft/server/commands/SummonCommand", | ||
"net/minecraft/world/entity/EntityType", | ||
"net/minecraft/world/entity/ai/village/VillageSiege", | ||
"net/minecraft/world/entity/animal/frog/Tadpole", | ||
"net/minecraft/world/entity/animal/horse/SkeletonTrapGoal", | ||
"net/minecraft/world/entity/monster/Evoker$EvokerSummonSpellGoal", | ||
"net/minecraft/world/entity/monster/Spider", | ||
"net/minecraft/world/entity/monster/Strider", | ||
"net/minecraft/world/entity/monster/Zombie", | ||
"net/minecraft/world/entity/monster/ZombieVillager", | ||
"net/minecraft/world/entity/npc/CatSpawner", | ||
"net/minecraft/world/entity/npc/Villager", | ||
"net/minecraft/world/entity/raid/Raid", | ||
"net/minecraft/world/level/NaturalSpawner", | ||
"net/minecraft/world/level/levelgen/PatrolSpawner", | ||
"net/minecraft/world/level/levelgen/PhantomSpawner", | ||
"net/minecraft/world/level/levelgen/structure/structures/OceanMonumentPieces$OceanMonumentPiece", | ||
"net/minecraft/world/level/levelgen/structure/structures/OceanRuinPieces$OceanRuinPiece", | ||
"net/minecraft/world/level/levelgen/structure/structures/SwampHutPiece", | ||
"net/minecraft/world/level/levelgen/structure/structures/WoodlandMansionPieces$WoodlandMansionPiece", | ||
"net/minecraft/world/level/levelgen/structure/templatesystem/StructureTemplate" | ||
] |
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