mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-18 11:29:15 +00:00
Clean and fix Forge's internal coremods, fixes finalizeSpawn hook. (#10271)
This commit is contained in:
1 parent
0d3b488728
commit
3024ef7707
5 files changed
+265
-88
No files matched your search
+19
-3
@@ -3,6 +3,7 @@ package net.minecraftforge.forge.tasks
|
||||
import groovy.transform.CompileStatic
|
||||
import org.gradle.api.provider.Property
|
||||
import org.gradle.api.tasks.Internal
|
||||
import org.objectweb.asm.Opcodes
|
||||
import org.objectweb.asm.tree.ClassNode
|
||||
import org.objectweb.asm.tree.MethodNode
|
||||
|
||||
@@ -12,13 +13,19 @@ abstract class BytecodePredicateFinder extends BytecodeFinder {
|
||||
@Internal
|
||||
abstract Property<Closure<Boolean>> getPredicate()
|
||||
|
||||
private final Set<String> matches = new HashSet<>()
|
||||
private final Map<String, List<String>> matches = new TreeMap<>(Comparator.naturalOrder())
|
||||
|
||||
@Override
|
||||
protected process(ClassNode parent, MethodNode node) {
|
||||
for (final current : node.instructions) {
|
||||
if (predicate.get().call(parent, node, current)) {
|
||||
matches.add(parent.name)
|
||||
var methods = matches.compute(parent.name, { k, v -> v ?: new ArrayList<>() })
|
||||
|
||||
// only add non-synthetic methods
|
||||
if ((node.access & Opcodes.ACC_SYNTHETIC) == 0) {
|
||||
methods.add(node.name + node.desc)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -27,6 +34,15 @@ abstract class BytecodePredicateFinder extends BytecodeFinder {
|
||||
@Internal
|
||||
@Override
|
||||
protected Object getData() {
|
||||
return matches.toSorted(Comparator.naturalOrder()) ?: {throw new RuntimeException('Failed to find any targets, please ensure that method names and descriptors are correct.')}()
|
||||
var array = new ArrayList<Object>()
|
||||
matches.forEach { c, m ->
|
||||
{
|
||||
array.add(
|
||||
'class': c,
|
||||
'methods': m
|
||||
)
|
||||
}
|
||||
}
|
||||
return array ?: { throw new RuntimeException('Failed to find any targets, please ensure that method names and descriptors are correct.') }()
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -413,13 +413,13 @@ tasks.register('extractInheritance', ExtractInheritance) {
|
||||
}
|
||||
}
|
||||
tasks.register("findFinalizeSpawnTargets", BytecodePredicateFinder) {
|
||||
jar = createJoinedSRG.output
|
||||
jar = createClientOfficial.output
|
||||
output = rootProject.file('src/main/resources/coremods/finalize_spawn_targets.json')
|
||||
predicate = {
|
||||
parent, node, insn ->
|
||||
return 'net/minecraft/world/level/BaseSpawner' != parent.name // Ignore this class as we special case it.
|
||||
&& insn.getOpcode().equals(Opcodes.INVOKEVIRTUAL)
|
||||
&& insn.name == 'm_6518_'
|
||||
&& insn.name == 'finalizeSpawn'
|
||||
&& insn.desc == '(Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/world/DifficultyInstance;Lnet/minecraft/world/entity/EntitySpawnReason;Lnet/minecraft/world/entity/SpawnGroupData;)Lnet/minecraft/world/entity/SpawnGroupData;';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
var ASMAPI = Java.type('net.minecraftforge.coremod.api.ASMAPI')
|
||||
|
||||
function initializeCoreMod() {
|
||||
|
||||
@@ -1,25 +1,144 @@
|
||||
[
|
||||
"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/block/entity/trialspawner/TrialSpawner",
|
||||
"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"
|
||||
{
|
||||
"class": "net/minecraft/server/commands/RaidCommand",
|
||||
"methods": [
|
||||
"spawnLeader(Lnet/minecraft/commands/CommandSourceStack;)I"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/server/commands/SummonCommand",
|
||||
"methods": [
|
||||
"createEntity(Lnet/minecraft/commands/CommandSourceStack;Lnet/minecraft/core/Holder$Reference;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/nbt/CompoundTag;Z)Lnet/minecraft/world/entity/Entity;"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/entity/EntityType",
|
||||
"methods": [
|
||||
"create(Lnet/minecraft/server/level/ServerLevel;Ljava/util/function/Consumer;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/entity/EntitySpawnReason;ZZ)Lnet/minecraft/world/entity/Entity;"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/entity/ai/village/VillageSiege",
|
||||
"methods": [
|
||||
"trySpawn(Lnet/minecraft/server/level/ServerLevel;)V"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/entity/animal/frog/Tadpole",
|
||||
"methods": [
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/entity/animal/horse/SkeletonTrapGoal",
|
||||
"methods": [
|
||||
"createHorse(Lnet/minecraft/world/DifficultyInstance;)Lnet/minecraft/world/entity/animal/horse/AbstractHorse;",
|
||||
"createSkeleton(Lnet/minecraft/world/DifficultyInstance;Lnet/minecraft/world/entity/animal/horse/AbstractHorse;)Lnet/minecraft/world/entity/monster/Skeleton;"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/entity/monster/Evoker$EvokerSummonSpellGoal",
|
||||
"methods": [
|
||||
"performSpellCasting()V"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/entity/monster/Spider",
|
||||
"methods": [
|
||||
"finalizeSpawn(Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/world/DifficultyInstance;Lnet/minecraft/world/entity/EntitySpawnReason;Lnet/minecraft/world/entity/SpawnGroupData;)Lnet/minecraft/world/entity/SpawnGroupData;"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/entity/monster/Strider",
|
||||
"methods": [
|
||||
"spawnJockey(Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/world/DifficultyInstance;Lnet/minecraft/world/entity/Mob;Lnet/minecraft/world/entity/SpawnGroupData;)Lnet/minecraft/world/entity/SpawnGroupData;"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/entity/monster/Zombie",
|
||||
"methods": [
|
||||
"hurtServer(Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/world/damagesource/DamageSource;F)Z",
|
||||
"finalizeSpawn(Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/world/DifficultyInstance;Lnet/minecraft/world/entity/EntitySpawnReason;Lnet/minecraft/world/entity/SpawnGroupData;)Lnet/minecraft/world/entity/SpawnGroupData;"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/entity/monster/ZombieVillager",
|
||||
"methods": [
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/entity/npc/CatSpawner",
|
||||
"methods": [
|
||||
"spawnCat(Lnet/minecraft/core/BlockPos;Lnet/minecraft/server/level/ServerLevel;)I"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/entity/npc/Villager",
|
||||
"methods": [
|
||||
"getBreedOffspring(Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/world/entity/AgeableMob;)Lnet/minecraft/world/entity/npc/Villager;"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/entity/raid/Raid",
|
||||
"methods": [
|
||||
"joinRaid(ILnet/minecraft/world/entity/raid/Raider;Lnet/minecraft/core/BlockPos;Z)V"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/level/NaturalSpawner",
|
||||
"methods": [
|
||||
"spawnCategoryForPosition(Lnet/minecraft/world/entity/MobCategory;Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/world/level/chunk/ChunkAccess;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/NaturalSpawner$SpawnPredicate;Lnet/minecraft/world/level/NaturalSpawner$AfterSpawnCallback;)V",
|
||||
"spawnMobsForChunkGeneration(Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/core/Holder;Lnet/minecraft/world/level/ChunkPos;Lnet/minecraft/util/RandomSource;)V"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/level/block/entity/trialspawner/TrialSpawner",
|
||||
"methods": [
|
||||
"spawnMob(Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/core/BlockPos;)Ljava/util/Optional;"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/level/levelgen/PatrolSpawner",
|
||||
"methods": [
|
||||
"spawnPatrolMember(Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/core/BlockPos;Lnet/minecraft/util/RandomSource;Z)Z"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/level/levelgen/PhantomSpawner",
|
||||
"methods": [
|
||||
"tick(Lnet/minecraft/server/level/ServerLevel;ZZ)I"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/level/levelgen/structure/structures/OceanMonumentPieces$OceanMonumentPiece",
|
||||
"methods": [
|
||||
"spawnElder(Lnet/minecraft/world/level/WorldGenLevel;Lnet/minecraft/world/level/levelgen/structure/BoundingBox;III)V"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/level/levelgen/structure/structures/OceanRuinPieces$OceanRuinPiece",
|
||||
"methods": [
|
||||
"handleDataMarker(Ljava/lang/String;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/util/RandomSource;Lnet/minecraft/world/level/levelgen/structure/BoundingBox;)V"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/level/levelgen/structure/structures/SwampHutPiece",
|
||||
"methods": [
|
||||
"postProcess(Lnet/minecraft/world/level/WorldGenLevel;Lnet/minecraft/world/level/StructureManager;Lnet/minecraft/world/level/chunk/ChunkGenerator;Lnet/minecraft/util/RandomSource;Lnet/minecraft/world/level/levelgen/structure/BoundingBox;Lnet/minecraft/world/level/ChunkPos;Lnet/minecraft/core/BlockPos;)V",
|
||||
"spawnCat(Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/world/level/levelgen/structure/BoundingBox;)V"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/level/levelgen/structure/structures/WoodlandMansionPieces$WoodlandMansionPiece",
|
||||
"methods": [
|
||||
"handleDataMarker(Ljava/lang/String;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/util/RandomSource;Lnet/minecraft/world/level/levelgen/structure/BoundingBox;)V"
|
||||
]
|
||||
},
|
||||
{
|
||||
"class": "net/minecraft/world/level/levelgen/structure/templatesystem/StructureTemplate",
|
||||
"methods": [
|
||||
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -1,77 +1,114 @@
|
||||
var ASMAPI = Java.type('net.minecraftforge.coremod.api.ASMAPI');
|
||||
var Opcodes = Java.type('org.objectweb.asm.Opcodes');
|
||||
var Handle = Java.type('org.objectweb.asm.Handle');
|
||||
var MethodInsnNode = Java.type('org.objectweb.asm.tree.MethodInsnNode');
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
function finalizeSpawnNode(node){
|
||||
return new MethodInsnNode(
|
||||
Opcodes.INVOKESTATIC,
|
||||
"net/minecraftforge/event/ForgeEventFactory",
|
||||
"onFinalizeSpawn",
|
||||
"(Lnet/minecraft/world/entity/Mob;Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/world/DifficultyInstance;Lnet/minecraft/world/entity/EntitySpawnReason;Lnet/minecraft/world/entity/SpawnGroupData;)Lnet/minecraft/world/entity/SpawnGroupData;",
|
||||
false);
|
||||
}
|
||||
'use strict';
|
||||
|
||||
function contains(list, target) {
|
||||
for(var i = 0; i < list.length; i++) {
|
||||
if(list[i] == target) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
const ASMAPI = Java.type('net.minecraftforge.coremod.api.ASMAPI');
|
||||
const Opcodes = Java.type('org.objectweb.asm.Opcodes');
|
||||
|
||||
function search(className, node, replacements) {
|
||||
for(var i = 0; i < replacements.length; i++){
|
||||
var r = replacements[i];
|
||||
if(contains(r.targets, className) && node.getOpcode() == r.opcode && node.name == r.name && node.desc == r.desc) {
|
||||
return r;
|
||||
// we can't run ASMAPI.loadData in the global context, so we do it here
|
||||
// this function is called inside of initializeCoreMod
|
||||
const replacements = [];
|
||||
function initReplacements() {
|
||||
ASMAPI.log('DEBUG', 'Gathering Forge method redirector replacements');
|
||||
replacements.push({
|
||||
// finalizeSpawn redirection to ForgeEventFactory.onFinalizeSpawn
|
||||
'type': ASMAPI.MethodType.VIRTUAL,
|
||||
'name': 'finalizeSpawn',
|
||||
'desc': '(Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/world/DifficultyInstance;Lnet/minecraft/world/entity/EntitySpawnReason;Lnet/minecraft/world/entity/SpawnGroupData;)Lnet/minecraft/world/entity/SpawnGroupData;',
|
||||
'targets': ASMAPI.loadData('coremods/finalize_spawn_targets.json'),
|
||||
'factory': function(insn) {
|
||||
return ASMAPI.buildMethodCall(
|
||||
ASMAPI.MethodType.STATIC,
|
||||
'net/minecraftforge/event/ForgeEventFactory',
|
||||
'onFinalizeSpawn',
|
||||
'(Lnet/minecraft/world/entity/Mob;Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/world/DifficultyInstance;Lnet/minecraft/world/entity/EntitySpawnReason;Lnet/minecraft/world/entity/SpawnGroupData;)Lnet/minecraft/world/entity/SpawnGroupData;');
|
||||
}
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
var replacements = [
|
||||
{
|
||||
'opcode': Opcodes.INVOKEVIRTUAL,
|
||||
'name': ASMAPI.mapMethod('m_6518_'),
|
||||
'desc': '(Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/world/DifficultyInstance;Lnet/minecraft/world/entity/MobSpawnType;Lnet/minecraft/world/entity/SpawnGroupData;)Lnet/minecraft/world/entity/SpawnGroupData;',
|
||||
'targets': 'coremods/finalize_spawn_targets.json',
|
||||
'factory': finalizeSpawnNode
|
||||
}
|
||||
];
|
||||
|
||||
function initializeCoreMod() {
|
||||
var mergedTargets = [];
|
||||
for(var i = 0; i < replacements.length; i++){
|
||||
var r = replacements[i];
|
||||
r.targets = ASMAPI.loadData(r.targets);
|
||||
for(var k = 0; k < r.targets.length; k++){
|
||||
mergedTargets.push(r.targets[k]);
|
||||
}
|
||||
}
|
||||
initReplacements();
|
||||
|
||||
return {
|
||||
'forge_method_redirector': {
|
||||
'target': {
|
||||
'type': 'CLASS',
|
||||
'names': function(listofclasses) {
|
||||
return mergedTargets
|
||||
}
|
||||
'names': getTargets
|
||||
},
|
||||
'transformer': function(classNode) {
|
||||
var methods = classNode.methods;
|
||||
var count = 0;
|
||||
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);
|
||||
var temp = search(classNode.name, node, replacements);
|
||||
if (temp != null) {
|
||||
instr.set(node, temp.factory(node));
|
||||
}
|
||||
}
|
||||
'transformer': applyMethodRedirects
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getTargets(classes) {
|
||||
const mergedTargets = [];
|
||||
for (let replacement of replacements) {
|
||||
for (let target of replacement.targets) {
|
||||
mergedTargets.push(target.class);
|
||||
}
|
||||
}
|
||||
|
||||
return mergedTargets;
|
||||
}
|
||||
|
||||
function applyMethodRedirects(clazz) {
|
||||
for (let replacement of replacements) {
|
||||
for (let methodString of getClassTargetMethods(clazz, replacement)) {
|
||||
// cut up the string since we've put both the name and desc as the target
|
||||
const splitPos = methodString.indexOf('(');
|
||||
const methodName = methodString.substring(0, splitPos);
|
||||
const methodDesc = methodString.substring(splitPos);
|
||||
const method = ASMAPI.findMethodNode(clazz, methodName, methodDesc);
|
||||
|
||||
// if we can't find the method, get out now!
|
||||
if (method === null) {
|
||||
ASMAPI.log('ERROR', 'Failed to redirect method call for {}! Method {} not found in class {}! This is a Forge bug, and is likely due to a Minecraft update changing something.', replacement.name, methodString, clazz.name);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (let insn of method.instructions) {
|
||||
if (shouldReplace(insn, replacement)) {
|
||||
const redirection = replacement.factory(insn);
|
||||
ASMAPI.log('DEBUG', 'Redirecting method call {}{} to {}{} inside of {}.{}', insn.name, insn.desc, redirection.name, redirection.desc, clazz.name, method.name);
|
||||
ASMAPI.insertInsn(method, insn, redirection, ASMAPI.InsertMode.REMOVE_ORIGINAL);
|
||||
}
|
||||
return classNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return clazz;
|
||||
}
|
||||
|
||||
|
||||
/* HELPER FUNCTIONS FOR TARGET SEARCHING */
|
||||
|
||||
function shouldReplace(insn, replacement) {
|
||||
return insn.getOpcode() === replacement.type.toOpcode()
|
||||
&& insn.name === replacement.name
|
||||
&& insn.desc === replacement.desc;
|
||||
}
|
||||
|
||||
function getClassTargetMethods(clazz, replacement) {
|
||||
for (let t of replacement.targets) {
|
||||
if (t.class === clazz.name) {
|
||||
const targets = [];
|
||||
|
||||
// declared methods
|
||||
for (let method of t.methods) {
|
||||
targets.push(method);
|
||||
}
|
||||
|
||||
// synthetic methods
|
||||
for (let method of clazz.methods) {
|
||||
if ((method.access & Opcodes.ACC_SYNTHETIC) != 0) {
|
||||
targets.push(method.name + method.desc);
|
||||
}
|
||||
}
|
||||
|
||||
return targets;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user