Advertisement
Guest User

Untitled

a guest
May 27th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. public static boolean canDoDamage(double damage, Creature attacker, Creature defender, Item weapon) {
  2. logger.info(String.format("canDoDamage from %s to %s with %s - %.1f", attacker.getName(), defender.getName(), weapon.getName(), damage));
  3. return damage > 1D;
  4. }
  5.  
  6. static void patchCombatDamageCheck(ClassPool classPool) throws NotFoundException, BadBytecode {
  7. CtClass cls = classPool.getCtClass("com.wurmonline.server.creatures.CombatHandler");
  8. CtMethod method = cls.getMethod("setDamage", "(Lcom/wurmonline/server/creatures/Creature;Lcom/wurmonline/server/items/Item;DBB)Z");
  9. MethodInfo methodInfo = method.getMethodInfo();
  10. CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
  11. ConstPool constPool = codeAttribute.getConstPool();
  12. CodeIterator codeIterator = codeAttribute.iterator();
  13.  
  14. // Scan through all the bytecode - look for a multiplication followed by comparing
  15. while (codeIterator.hasNext()) {
  16. int pos = codeIterator.next();
  17. int op = codeIterator.byteAt(pos);
  18. if (op != CodeIterator.DMUL) continue; // not multiplication - continue
  19. op = codeIterator.byteAt(++pos);
  20. if (op == CodeIterator.LDC2_W && codeIterator.byteAt(pos + 3) == CodeIterator.DCMPL) {
  21. // found the pattern, check the value it's comparing to
  22. int ref = codeIterator.u16bitAt(pos + 1);
  23. double val = constPool.getDoubleInfo(ref);
  24. if (val == 500.0) {
  25. // here it is, generate new code to insert
  26. // We'll be calling canDoDamage, the first parameter (damage) is already on the stack, prepare the rest
  27. Bytecode newCode = new Bytecode(constPool);
  28. newCode.add(Bytecode.ALOAD_0); // this
  29. newCode.addGetfield(cls, "creature", "Lcom/wurmonline/server/creatures/Creature;"); // this.creature
  30. newCode.add(Bytecode.ALOAD_1); // defender - first parameter of setDamage
  31. newCode.add(Bytecode.ALOAD_2); // weapon - second parameter of setDamage
  32.  
  33. // call our methor, result is left on the stack
  34. newCode.addInvokestatic(
  35. CombatChanges.class.getName(), "canDoDamage",
  36. "(DLcom/wurmonline/server/creatures/Creature;Lcom/wurmonline/server/creatures/Creature;Lcom/wurmonline/server/items/Item;)Z");
  37.  
  38. // The code we're replacing is 4 bytes - LDC2_W, 2byte reference and DCMPL
  39. // Insert a gap for to match the size of the new code
  40. codeIterator.insertGap(pos, newCode.getSize() - 4);
  41.  
  42. // And put the new code
  43. codeIterator.write(newCode.get(), pos);
  44. }
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement