我正在尝试使用 expreditor 将代码中的 value 从 2.0 修改为 1.0。方法“alterSkill”下的嵌套 if 语句
if (player2.hasSleepBonus()) {
advanceMultiplicator *= 2.0;}
我有一个别人写的实用程序
public static void instrumentDescribed(Class<?> instrumentingClass, CtClass ctToInstrument, String declaredMethod, String descriptor, String methodCall, String replace){
try {
ctToInstrument.getMethod(declaredMethod, descriptor).instrument(new ExprEditor(){
public void edit(MethodCall m) throws CannotCompileException {
if (m.getMethodName().equals(methodCall)) {
m.replace(replace);
success = true;
}
}
});
checkSuccess(0, instrumentingClass, ctToInstrument, declaredMethod, methodCall);
} catch (CannotCompileException | NotFoundException e) {
//e.printStackTrace();
checkSuccess(0, instrumentingClass, ctToInstrument, declaredMethod, methodCall);
logger.severe(e.getMessage());
}
}
我的代码:
if (reduceSleepBonus) {
Util.setReason("Change sleep powder from 2x to 1.0x");
replace = "advanceMultiplicator *= 1.0;" +
"$_ = $proceed($$);";
Util.instrumentDescribed(thisClass, ctSkill, "alterSkill", desc4, "hasSleepBonus", replace);
}
我的程序说它已成功实施。但是我怀疑它只是将AdvanceMultiplicator 设置为1.0 而没有将2.0 修改为1.0。我在程序中测试过,确认hasSleepBonus()为真时技能加倍。
回答1
使用上述实用程序只是将正确的代码放入替换字符串中的问题。原始代码采用了 advanceMultiplicator 并将其乘以 1.0,它什么也没做,也没有访问该字段。
if (reduceSleepBonus) {
Util.setReason("Change sleep powder from 2.0 to 1.0");
replace = "if($proceed($$)){ advanceMultiplicator *= 1.0; }" +
"$_ = false;";
Util.instrumentDescribed(thisClass, ctSkill, "alterSkill", desc4, "hasSleepBonus", replace);
}