Skip to content

Commit

Permalink
Rework some String remapper
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatcore committed Aug 21, 2023
1 parent ba9b122 commit ae73bc2
Show file tree
Hide file tree
Showing 6 changed files with 391 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,42 +47,4 @@ private void remap() {
this.s_desc = Constants.remapIndividualType(pair.second());
}
}

// /**
// * @author CatCore
// * @reason fix match because of the way methods are added to classes
// */
// @Overwrite(remap = false)
// public boolean matches(MethodNode node) {
// String className = this.getClassNameForMethod();
//
// System.out.println(this.s_owner + " -> " + className);
//
// return this.s_name.equals(node.name) && this.s_desc.equals(node.desc);
// }
//
// /**
// * @author CatCore
// * @reason fix match because of the way methods are added to classes
// */
// @Overwrite(remap = false)
// public boolean matches(MethodInsnNode node) {
// String className = this.getClassNameForMethod();
//
// System.out.println(this.s_name + " -> " + className + " : " + node.owner);
//
// return className.equals(node.owner) && this.s_name.equals(node.name) && this.s_desc.equals(node.desc);
// }
//
// @Unique
// private String getClassNameForMethod() {
// if (this.s_owner.equals("net/minecraft/class_197") && (
// this.s_name.equals("canBeReplacedByLeaves")
// || this.s_name.equals("isAirBlock")
// )) {
// return "fr/catcore/fabricatedforge/mixininterface/IBlock";
// }
//
// return this.s_name;
// }
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,62 @@
package fr.catcore.fabricatedforge.compat.mixin.codechickencore;

import codechicken.core.asm.ASMHelper;
import codechicken.core.asm.FeatureHackTransformer;
import codechicken.core.asm.InstructionComparator;
import codechicken.core.asm.ObfuscationMappings;
import org.objectweb.asm.tree.*;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;

import java.util.List;

@Mixin(FeatureHackTransformer.class)
public class FeatureHackTransformerMixin {
@ModifyArg(method = "<init>", index = 2,
remap = false,
at = @At(value = "INVOKE",
remap = false,
target = "Lcodechicken/core/asm/ObfuscationMappings$DescriptorMapping;<init>(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", ordinal = 0))
private String fixMethodArg(String arg) {
return "(Lnet/minecraft/class_1069;)V";
@Shadow(remap = false)
ObfuscationMappings.DescriptorMapping f_lastBrightness;

/**
* @author CatCore
* @reason fix mappings
*/
@Overwrite(remap = false)
private byte[] transformer002(String name, byte[] bytes) {
ClassNode cnode = ASMHelper.createClassNode(bytes);
FieldNode fnode = ASMHelper.findField(this.f_lastBrightness, cnode);
if (fnode == null) {
cnode.fields.add(new FieldNode(9, this.f_lastBrightness.s_name, this.f_lastBrightness.s_desc, null, null));
MethodNode mlightmap = ASMHelper.findMethod(new ObfuscationMappings.DescriptorMapping("bfe", "a", "(IFF)V"), cnode);
InsnList hook = new InsnList();
LabelNode lend = new LabelNode();
hook.add(new VarInsnNode(21, 0));
hook.add(new ObfuscationMappings.DescriptorMapping("bfe", "b", "I").toFieldInsn(178));
hook.add(new JumpInsnNode(160, lend));
hook.add(new VarInsnNode(23, 2));
hook.add(new InsnNode(139));
hook.add(new IntInsnNode(16, 16));
hook.add(new InsnNode(120));
hook.add(new VarInsnNode(23, 1));
hook.add(new InsnNode(139));
hook.add(new InsnNode(128));
hook.add(this.f_lastBrightness.toFieldInsn(179));
hook.add(lend);
InsnList needle = new InsnList();
needle.add(new InsnNode(177));
List ret = InstructionComparator.insnListFindEnd(mlightmap.instructions, needle);
if (ret.size() != 1) {
throw new RuntimeException(
"Needle not found in Haystack: " + ASMHelper.printInsnList(mlightmap.instructions) + "\n" + ASMHelper.printInsnList(needle)
);
}

mlightmap.instructions.insertBefore((AbstractInsnNode)ret.get(0), hook);
bytes = ASMHelper.createBytes(cnode, 3);
System.out.println("Brightness hook injected");
}

return bytes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package fr.catcore.fabricatedforge.compat.mixin.codechickencore;

import codechicken.core.asm.*;
import codechicken.core.config.ConfigFile;
import codechicken.core.config.ConfigTag;
import org.objectweb.asm.tree.*;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

import java.io.File;
import java.util.List;

@Mixin(TweakTransformer.class)
public abstract class TweakTransformerMixin {

@Shadow(remap = false) public static ConfigTag tweaks;

@Shadow(remap = false)
private static void alterMethod(ASMHelper.MethodAltercator ma) {
}

/**
* @author CatCore
* @reason fix mappings
*/
@Overwrite(remap = false)
public static void load() {
File cfgDir = new File(CodeChickenCorePlugin.minecraftDir + "/config");
if (!cfgDir.exists()) {
cfgDir.mkdirs();
}

ConfigFile config = new ConfigFile(new File(cfgDir, "CodeChickenCore.cfg")).setComment("CodeChickenCore configuration file.");
tweaks = config.getTag("tweaks").setComment("Various tweaks that can be applied to game mechanics.").useBraces();
if (!tweaks.getTag("persistantLava")
.setComment("Set to false to make lava fade away like water if all the source blocks are destroyed")
.getBooleanValue(true)) {
ObfuscationMappings.ClassMapping blockFlowing = new ObfuscationMappings.ClassMapping("aky");
alterMethod(
new ASMHelper.MethodAltercator(new ObfuscationMappings.DescriptorMapping(blockFlowing.s_class, new ObfuscationMappings.DescriptorMapping("amq", "b", "(Lyc;IIILjava/util/Random;)V"))) {
public void alter(MethodNode mv) {
InsnList needle = new InsnList();
needle.add(new VarInsnNode(21, 6));
needle.add(new VarInsnNode(54, -1));
needle.add(new InsnNode(3));
needle.add(new VarInsnNode(54, 8));
List lists = InstructionComparator.insnListFindL(mv.instructions, needle);
if (lists.size() != 1) {
throw new RuntimeException(
"Needle found " + lists.size() + " times in Haystack: " + mv.instructions + "\n" + ASMHelper.printInsnList(needle)
);
} else {
InstructionComparator.InsnListSection subsection = (InstructionComparator.InsnListSection)lists.get(0);
AbstractInsnNode insn = subsection.first;

while(true) {
AbstractInsnNode next = insn.getNext();
mv.instructions.remove(insn);
if (insn == subsection.last) {
return;
}

insn = next;
}
}
}
}
);
}

if (tweaks.getTag("environmentallyFriendlyCreepers")
.setComment("If set to true, creepers will not destroy landscape. (A version of mobGreifing setting just for creepers)")
.getBooleanValue(false)) {
final ObfuscationMappings.ClassMapping entityCreeper = new ObfuscationMappings.ClassMapping("qc");
alterMethod(
new ASMHelper.MethodAltercator(new ObfuscationMappings.DescriptorMapping(entityCreeper.s_class, new ObfuscationMappings.DescriptorMapping("lq", "j_", "()V"))) {
public void alter(MethodNode mv) {
InsnList needle = new InsnList();
needle.add(new VarInsnNode(25, 0));
needle.add(new ObfuscationMappings.DescriptorMapping(entityCreeper.s_class, new ObfuscationMappings.DescriptorMapping("lq", "p", "Lyc;")).toFieldInsn(180));
needle.add(new ObfuscationMappings.DescriptorMapping("yc", "L", "()Lxz;").toInsn(182));
needle.add(new LdcInsnNode("mobGriefing"));
needle.add(new ObfuscationMappings.DescriptorMapping("xz", "b", "(Ljava/lang/String;)Z").toInsn(182));
List lists = InstructionComparator.insnListFindL(mv.instructions, needle);
if (lists.size() != 1) {
throw new RuntimeException(
"Needle found " + lists.size() + " times in Haystack: " + mv.instructions + "\n" + ASMHelper.printInsnList(needle)
);
} else {
InstructionComparator.InsnListSection subsection = (InstructionComparator.InsnListSection)lists.get(0);
mv.instructions.insertBefore(subsection.first, new InsnNode(3));
AbstractInsnNode insn = subsection.first;

while(true) {
AbstractInsnNode next = insn.getNext();
mv.instructions.remove(insn);
if (insn == subsection.last) {
return;
}

insn = next;
}
}
}
}
);
}

if (!tweaks.getTag("softLeafReplace").setComment("If set to false, leaves will only replace air when growing").getBooleanValue(false)) {
alterMethod(new ASMHelper.MethodAltercator(new ObfuscationMappings.DescriptorMapping("amq", "canBeReplacedByLeaves", "(Lyc;III)Z")) {
public void alter(MethodNode mv) {
InsnList replacement = new InsnList();
replacement.add(new VarInsnNode(25, 0));
replacement.add(new VarInsnNode(25, 1));
replacement.add(new VarInsnNode(21, 2));
replacement.add(new VarInsnNode(21, 3));
replacement.add(new VarInsnNode(21, 4));
replacement.add(new ObfuscationMappings.DescriptorMapping("amq", "isAirBlock", "(Lyc;III)Z").toInsn(182));
replacement.add(new InsnNode(172));
mv.instructions = replacement;
}
});
}

if (tweaks.getTag("doFireTickOut")
.setComment("If set to true and doFireTick is disabed in the game rules, fire will still dissipate if it's not over a fire source")
.getBooleanValue(true)) {
alterMethod(
new ASMHelper.MethodAltercator(new ObfuscationMappings.DescriptorMapping("akf", "b", "(Lyc;IIILjava/util/Random;)V")) {
public void alter(MethodNode mv) {
InsnList needle = new InsnList();
needle.add(new LdcInsnNode("doFireTick"));
needle.add(new ObfuscationMappings.DescriptorMapping("xz", "b", "(Ljava/lang/String;)Z").toInsn(182));
needle.add(new JumpInsnNode(153, new LabelNode()));
List lists = InstructionComparator.insnListFindL(mv.instructions, needle);
if (lists.size() != 1) {
throw new RuntimeException(
"Needle found " + lists.size() + " times in Haystack: " + mv.instructions + "\n" + ASMHelper.printInsnList(needle)
);
} else {
InstructionComparator.InsnListSection subsection = (InstructionComparator.InsnListSection)lists.get(0);
LabelNode jlabel = ((JumpInsnNode)subsection.last).label;
LabelNode ret = new LabelNode();
mv.instructions.insertBefore(jlabel, new JumpInsnNode(167, ret));
InsnList inject = new InsnList();
inject.add(new VarInsnNode(25, 1));
inject.add(new VarInsnNode(21, 2));
inject.add(new VarInsnNode(21, 3));
inject.add(new VarInsnNode(21, 4));
inject.add(new VarInsnNode(25, 5));
inject.add(
new ObfuscationMappings.DescriptorMapping("codechicken/core/featurehack/TweakTransformerHelper", "quenchFireTick", "(Lyc;IIILjava/util/Random;)V").toInsn(184)
);
inject.add(ret);
mv.instructions.insert(jlabel, inject);
}
}
}
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"codechickencore.ClassOverriderMixin",
"codechickencore.DescriptorMappingMixin",
"codechickencore.FeatureHackTransformerMixin",
"codechickencore.TweakTransformerMixin",
"nei.FurnaceRecipeHandlerMixin",
"nei.NEITransformerMixin",
"nei.TMIUninstallerMixin"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ public boolean processLine(String input) throws IOException {
String nameReference = descriptor.get(1);
int parenIdx = nameReference.indexOf(40);
if (parenIdx > 0) {
Pair<String, String> o = Constants.getRemappedMethodNameNative(className,
Pair<String, String> o = Constants.getRemappedMethodName(className,
nameReference.substring(0, parenIdx), nameReference.substring(parenIdx));
m.desc = o.second();
m.name = o.first();
} else {
m.name = Constants.getRemappedFieldNameNative(className, nameReference);
m.name = Constants.getRemappedFieldName(className, nameReference);
}
}

Expand Down
Loading

0 comments on commit ae73bc2

Please sign in to comment.