2021-10-25 15:16:47 +02:00
|
|
|
package io.gitlab.jfronny.quickmeth;
|
|
|
|
|
|
|
|
import io.gitlab.jfronny.libjf.unsafe.asm.AsmConfig;
|
|
|
|
import io.gitlab.jfronny.libjf.unsafe.asm.AsmTransformer;
|
|
|
|
import io.gitlab.jfronny.libjf.unsafe.asm.patch.Patch;
|
|
|
|
import io.gitlab.jfronny.libjf.unsafe.asm.patch.PatchUtil;
|
|
|
|
import org.objectweb.asm.Opcodes;
|
2021-10-25 18:58:41 +02:00
|
|
|
import org.objectweb.asm.tree.*;
|
2021-10-25 15:16:47 +02:00
|
|
|
|
2021-10-25 18:58:41 +02:00
|
|
|
import java.util.Map;
|
2021-10-25 15:16:47 +02:00
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public class BytecodeTransformer implements AsmConfig, Patch {
|
|
|
|
private static final String math = "java/lang/Math";
|
2021-10-25 18:58:41 +02:00
|
|
|
private static final String random = "java/util/Random";
|
2021-10-25 15:16:47 +02:00
|
|
|
private static final String mathUtil = "io/gitlab/jfronny/quickmeth/MathUtil";
|
2021-10-25 18:58:41 +02:00
|
|
|
private static final String mathHelperIntermediary = "net.minecraft.class_3532";
|
|
|
|
private static final String mathHelper = PatchUtil.getRemappedInternal(mathHelperIntermediary);
|
|
|
|
private static final Map<String, String> mth = Map.of(
|
|
|
|
mth("method_15374", "(F)F"), "sinM",
|
|
|
|
mth("method_15362", "(F)F"), "cosM",
|
|
|
|
mth("method_15355", "(F)F"), "sqrtM",
|
|
|
|
mth("method_15375", "(D)I"), "floor"
|
|
|
|
);
|
|
|
|
private static final Map<String, Boolean> stat = Map.of(
|
|
|
|
"sin", Cfg.corruptTrigonometry2,
|
|
|
|
"cos", Cfg.corruptTrigonometry2,
|
|
|
|
"sinM", Cfg.corruptTrigonometry,
|
|
|
|
"cosM", Cfg.corruptTrigonometry,
|
|
|
|
//"sqrt", Cfg.corruptGenericMath,
|
|
|
|
"sqrtM", Cfg.corruptGenericMath,
|
2022-02-28 20:35:54 +01:00
|
|
|
//"floor", Cfg.corruptGenericMath2,
|
2021-10-25 18:58:41 +02:00
|
|
|
"random", Cfg.corruptGenericMath2
|
|
|
|
);
|
2021-10-25 15:16:47 +02:00
|
|
|
|
2021-10-25 18:58:41 +02:00
|
|
|
private static String mth(String method, String descriptor) {
|
|
|
|
return AsmTransformer.MAPPING_RESOLVER.mapMethodName(AsmTransformer.INTERMEDIARY, mathHelperIntermediary, method, descriptor);
|
2021-10-25 15:16:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Set<String> skipClasses() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Set<Patch> getPatches() {
|
2021-10-25 18:58:41 +02:00
|
|
|
return Set.of(this);
|
2021-10-25 15:16:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void apply(ClassNode klazz) {
|
|
|
|
for (MethodNode method : klazz.methods) {
|
|
|
|
for (AbstractInsnNode insn : method.instructions.toArray()) {
|
2021-10-25 18:58:41 +02:00
|
|
|
if (insn.getOpcode() == Opcodes.INVOKESTATIC || insn.getOpcode() == Opcodes.INVOKEVIRTUAL) {
|
2021-10-25 15:16:47 +02:00
|
|
|
String insNew = null;
|
2021-10-25 18:58:41 +02:00
|
|
|
MethodInsnNode mIns = (MethodInsnNode) insn;
|
|
|
|
if (mIns.owner.equals(math)) {
|
|
|
|
if (stat.containsKey(mIns.name) && stat.get(mIns.name))
|
|
|
|
insNew = mIns.name;
|
2021-10-25 15:16:47 +02:00
|
|
|
}
|
2021-10-25 18:58:41 +02:00
|
|
|
else if (mIns.owner.equals(mathHelper)) {
|
|
|
|
if (mth.containsKey(mIns.name)) {
|
|
|
|
insNew = mth.get(mIns.name);
|
|
|
|
if (!stat.get(insNew))
|
|
|
|
insNew = null;
|
2021-10-25 15:16:47 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-25 18:58:41 +02:00
|
|
|
else if (mIns.owner.equals(random)) {
|
|
|
|
if (Cfg.corruptGenericMath2 && !klazz.name.equals(mathUtil)) {
|
|
|
|
insNew = switch (mIns.name) {
|
|
|
|
//case "nextInt" -> "nextInt";
|
|
|
|
case "nextLong" -> "nextLong";
|
|
|
|
case "nextBoolean" -> "nextBoolean";
|
|
|
|
//case "nextFloat" -> "nextFloat";
|
|
|
|
case "nextDouble", "nextGaussian" -> "random";
|
|
|
|
default -> null;
|
|
|
|
};
|
2021-10-25 15:16:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (insNew != null) {
|
2021-10-25 18:58:41 +02:00
|
|
|
if (mIns.getOpcode() == Opcodes.INVOKEVIRTUAL)
|
|
|
|
method.instructions.insertBefore(mIns, new InsnNode(Opcodes.POP));
|
|
|
|
mIns.setOpcode(Opcodes.INVOKESTATIC);
|
|
|
|
mIns.owner = mathUtil;
|
|
|
|
mIns.name = insNew;
|
2021-10-25 15:16:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|