Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit be310b5

Browse files
committed
Add special handler for enchantment registration
1 parent 3dfd536 commit be310b5

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/main/java/org/spongepowered/obfuscation/merge/operation/CustomMethodMergers.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,62 @@ private static void findBlockTypes(StatementBlock block, SourceSet src, Map<Stri
368368
}
369369
}
370370

371+
private static void register_enchantments(MethodMatchEntry match, MergeEngine set) {
372+
if (match.getOldMethod().getInstructions() == null || match.getNewMethod().getInstructions() == null) {
373+
return;
374+
}
375+
Map<String, TypeEntry> old_types = new HashMap<>();
376+
Map<String, TypeEntry> new_types = new HashMap<>();
377+
findEnchantments(match.getOldMethod().getInstructions(), set.getOldSourceSet(), old_types);
378+
findEnchantments(match.getNewMethod().getInstructions(), set.getNewSourceSet(), new_types);
379+
for (Map.Entry<String, TypeEntry> e : new_types.entrySet()) {
380+
TypeEntry old = old_types.get(e.getKey());
381+
if (old != null) {
382+
set.vote(old, e.getValue());
383+
}
384+
}
385+
}
386+
387+
private static void findEnchantments(StatementBlock block, SourceSet src, Map<String, TypeEntry> types) {
388+
for (Statement stmt : block) {
389+
if (stmt instanceof InvokeStatement) {
390+
Instruction inner = ((InvokeStatement) stmt).getInstruction();
391+
if (inner instanceof InstanceMethodInvoke) {
392+
InstanceMethodInvoke reg = (InstanceMethodInvoke) inner;
393+
if (reg.getParameters().length != 3) {
394+
continue;
395+
}
396+
Instruction key_val = reg.getParameters()[1];
397+
String key = null;
398+
if (key_val instanceof New) {
399+
New n = (New) key_val;
400+
if (n.getParameters().length == 1 && n.getParameters()[0] instanceof StringConstant) {
401+
key = ((StringConstant) n.getParameters()[0]).getConstant();
402+
}
403+
}
404+
if (key == null) {
405+
continue;
406+
}
407+
Instruction val = reg.getParameters()[2];
408+
if (val instanceof Cast) {
409+
val = ((Cast) val).getValue();
410+
}
411+
while (val instanceof InstanceMethodInvoke) {
412+
val = ((InstanceMethodInvoke) val).getCallee();
413+
}
414+
if (!(val instanceof New)) {
415+
continue;
416+
}
417+
String type = TypeHelper.descToType(((New) val).getType().getDescriptor());
418+
TypeEntry block_type = src.get(type);
419+
if (block_type != null) {
420+
types.put(key, block_type);
421+
}
422+
}
423+
}
424+
}
425+
}
426+
371427
static {
372428
custom_mergers.put("Lnet/minecraft/init/SoundEvents;<clinit>()V", CustomMethodMergers::bootstrap_handler);
373429
custom_mergers.put("Lnet/minecraft/init/Blocks;<clinit>()V", CustomMethodMergers::bootstrap_handler);
@@ -388,6 +444,7 @@ private static void findBlockTypes(StatementBlock block, SourceSet src, Map<Stri
388444
custom_mergers.put("Lnet/minecraft/block/Block;func_149671_p()V", CustomMethodMergers::register_blocks);
389445
custom_mergers.put("Lnet/minecraft/item/Item;func_150900_l()V", CustomMethodMergers::register_items);
390446
custom_mergers.put("Lnet/minecraft/world/biome/Biome;func_185358_q()V", CustomMethodMergers::register_blocks);
447+
custom_mergers.put("Lnet/minecraft/enchantment/Enchantment;func_185257_f()V", CustomMethodMergers::register_enchantments);
391448
}
392449

393450
}

0 commit comments

Comments
 (0)