Guest User

Untitled

a guest
Apr 24th, 2021
21
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package br.com.guedez.wurm.server;
  2.  
  3. import java.lang.reflect.InvocationHandler;
  4. import java.lang.reflect.Method;
  5. import java.nio.ByteBuffer;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8.  
  9. import org.gotti.wurmunlimited.modloader.ReflectionUtil;
  10. import org.gotti.wurmunlimited.modloader.classhooks.HookException;
  11. import org.gotti.wurmunlimited.modloader.classhooks.HookManager;
  12. import org.gotti.wurmunlimited.modloader.classhooks.InvocationHandlerFactory;
  13. import org.gotti.wurmunlimited.modloader.interfaces.Initable;
  14. import org.gotti.wurmunlimited.modloader.interfaces.WurmServerMod;
  15.  
  16. import javassist.ClassPool;
  17. import javassist.CtClass;
  18. import javassist.CtPrimitiveType;
  19. import javassist.bytecode.Descriptor;
  20.  
  21. public class ServerBufferLogger implements WurmServerMod, Initable {
  22.  
  23. private Logger Logger;
  24.  
  25. public void init() {
  26. try {
  27. Logger = java.util.logging.Logger.getLogger(ServerBufferLogger.class.getName());
  28. CtClass cByteBuffer = ClassPool.getDefault().getCtClass("java.nio.ByteBuffer");
  29. // HookManager.getInstance().registerHook("com.wurmonline.communication.SocketConnection", //
  30. // "getBuffer", Descriptor.ofMethod(cByteBuffer, new CtClass[] {}), new BufferListener());
  31. HookManager.getInstance().registerHook("com.wurmonline.communication.SocketConnection", //
  32. "flush", Descriptor.ofMethod(CtPrimitiveType.voidType, new CtClass[] {}), new FlushListener());
  33.  
  34. } catch (Exception e) {
  35. throw new HookException(e);
  36. }
  37. }
  38.  
  39. // public class BufferListener implements InvocationHandlerFactory {
  40. // public class BufferListenerHandler implements InvocationHandler {
  41. // @Override
  42. // public Object invoke(Object object, Method method, Object[] args) throws Throwable {
  43. // ByteBuffer buffer = (ByteBuffer) method.invoke(object, args);
  44. // Logger.log(Level.INFO, "GetBufferCalled, position: " + buffer.position(), new RuntimeException());
  45. // return buffer;
  46. // }
  47. // }
  48. //
  49. // public InvocationHandler createInvocationHandler() {
  50. // return new BufferListenerHandler();
  51. // }
  52. // }
  53.  
  54. public class FlushListener implements InvocationHandlerFactory {
  55. public class FlushListenerHandler implements InvocationHandler {
  56. @Override
  57. public Object invoke(Object object, Method method, Object[] args) throws Throwable {
  58. ByteBuffer buffer = (ByteBuffer) ReflectionUtil.getPrivateField(object, object.getClass().getDeclaredField("writeBufferTmp"));
  59. if (buffer.position() > 2048) {
  60. Logger.log(Level.INFO, "Flush called with lots to write, size: " + buffer.position(), new RuntimeException());
  61. }
  62. return method.invoke(object, args);
  63. }
  64. }
  65.  
  66. public InvocationHandler createInvocationHandler() {
  67. return new FlushListenerHandler();
  68. }
  69. }
  70. }
RAW Paste Data