Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static boolean canDoDamage(double damage, Creature attacker, Creature defender, Item weapon) {
- logger.info(String.format("canDoDamage from %s to %s with %s - %.1f", attacker.getName(), defender.getName(), weapon.getName(), damage));
- return damage > 1D;
- }
- static void patchCombatDamageCheck(ClassPool classPool) throws NotFoundException, BadBytecode {
- CtClass cls = classPool.getCtClass("com.wurmonline.server.creatures.CombatHandler");
- CtMethod method = cls.getMethod("setDamage", "(Lcom/wurmonline/server/creatures/Creature;Lcom/wurmonline/server/items/Item;DBB)Z");
- MethodInfo methodInfo = method.getMethodInfo();
- CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
- ConstPool constPool = codeAttribute.getConstPool();
- CodeIterator codeIterator = codeAttribute.iterator();
- // Scan through all the bytecode - look for a multiplication followed by comparing
- while (codeIterator.hasNext()) {
- int pos = codeIterator.next();
- int op = codeIterator.byteAt(pos);
- if (op != CodeIterator.DMUL) continue; // not multiplication - continue
- op = codeIterator.byteAt(++pos);
- if (op == CodeIterator.LDC2_W && codeIterator.byteAt(pos + 3) == CodeIterator.DCMPL) {
- // found the pattern, check the value it's comparing to
- int ref = codeIterator.u16bitAt(pos + 1);
- double val = constPool.getDoubleInfo(ref);
- if (val == 500.0) {
- // here it is, generate new code to insert
- // We'll be calling canDoDamage, the first parameter (damage) is already on the stack, prepare the rest
- Bytecode newCode = new Bytecode(constPool);
- newCode.add(Bytecode.ALOAD_0); // this
- newCode.addGetfield(cls, "creature", "Lcom/wurmonline/server/creatures/Creature;"); // this.creature
- newCode.add(Bytecode.ALOAD_1); // defender - first parameter of setDamage
- newCode.add(Bytecode.ALOAD_2); // weapon - second parameter of setDamage
- // call our methor, result is left on the stack
- newCode.addInvokestatic(
- CombatChanges.class.getName(), "canDoDamage",
- "(DLcom/wurmonline/server/creatures/Creature;Lcom/wurmonline/server/creatures/Creature;Lcom/wurmonline/server/items/Item;)Z");
- // The code we're replacing is 4 bytes - LDC2_W, 2byte reference and DCMPL
- // Insert a gap for to match the size of the new code
- codeIterator.insertGap(pos, newCode.getSize() - 4);
- // And put the new code
- codeIterator.write(newCode.get(), pos);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement