Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | None | 0 0
  1. package com.filterbubbles.requiemdebug;
  2.  
  3. import java.util.Properties;
  4. import java.util.logging.Logger;
  5.  
  6. import org.gotti.wurmunlimited.modloader.interfaces.Configurable;
  7. import org.gotti.wurmunlimited.modloader.interfaces.Initable;
  8. import org.gotti.wurmunlimited.modloader.interfaces.PlayerLoginListener;
  9. import org.gotti.wurmunlimited.modloader.interfaces.PlayerMessageListener;
  10. import org.gotti.wurmunlimited.modloader.interfaces.ServerStartedListener;
  11. import org.gotti.wurmunlimited.modloader.interfaces.WurmServerMod;
  12.  
  13. import com.wurmonline.server.creatures.Communicator;
  14. import com.wurmonline.server.players.Player;
  15.  
  16.  
  17. /**
  18.  * An attempt at getting more information on what causes the Requiem of Wurm to freeze up ...and then we can go from there.
  19.  *
  20.  * v1:  is it caused by a player doing an action?  This will log a brief view of all communication between client/server. Yes. Spammy.
  21.  *
  22.  * @author Friya, 18-Jul-2018
  23.  */
  24. public class Mod implements WurmServerMod, Initable, Configurable, ServerStartedListener, PlayerMessageListener, PlayerLoginListener
  25. {
  26.     private static Logger logger = Logger.getLogger(Mod.class.getName());
  27.  
  28.     @Override
  29.     public void configure(Properties arg0)
  30.     {
  31.     }
  32.  
  33.     @Override
  34.     public void init()
  35.     {
  36.         Patcher.getInstance().init();
  37.     }
  38.  
  39.     @Override
  40.     public void onServerStarted()
  41.     {
  42.     }
  43.  
  44.     @Override
  45.     public void onPlayerLogin(Player arg0)
  46.     {
  47.     }
  48.  
  49.     @Override
  50.     public boolean onPlayerMessage(Communicator arg0, String arg1)
  51.     {
  52.         return false;
  53.     }
  54.  
  55. }
  56.  
  57.  
  58. package com.filterbubbles.requiemdebug;
  59.  
  60. import java.lang.reflect.InvocationHandler;
  61. import java.lang.reflect.Method;
  62. import java.nio.ByteBuffer;
  63. import java.util.logging.Level;
  64. import java.util.logging.Logger;
  65.  
  66. import org.gotti.wurmunlimited.modloader.classhooks.HookManager;
  67. import org.gotti.wurmunlimited.modloader.classhooks.InvocationHandlerFactory;
  68.  
  69. import com.wurmonline.server.creatures.Communicator;
  70.  
  71. import javassist.CannotCompileException;
  72. import javassist.ClassPool;
  73. import javassist.CtClass;
  74. import javassist.CtMethod;
  75. import javassist.NotFoundException;
  76.  
  77. /**
  78.  * @author Friya, 18-Jul-2018
  79.  */
  80. public class Patcher
  81. {
  82.     private static Logger logger = Logger.getLogger(Patcher.class.getName());
  83.     private static Patcher instance = null;
  84.    
  85.     protected static Patcher getInstance()
  86.     {
  87.         if(instance == null) {
  88.             instance = new Patcher();
  89.         }
  90.         return instance;
  91.     }
  92.  
  93.     protected void init()
  94.     {
  95.         try {
  96.             interceptReallyHandle();
  97.             patchSends();
  98.         } catch (NotFoundException | CannotCompileException e) {
  99.             logger.log(Level.SEVERE, "failed to patch something...", e);
  100.         }
  101.     }
  102.    
  103.  
  104.     private void patchSends() throws NotFoundException, CannotCompileException
  105.     {
  106.         ClassPool cp = HookManager.getInstance().getClassPool();
  107.         CtClass cls = cp.get("com.wurmonline.server.creatures.Communicator");
  108.         CtMethod[] mets = cls.getDeclaredMethods();
  109.         for(CtMethod met : mets) {
  110.             if(met.getName().startsWith("send")
  111.                     && met.getName().equals("sendByteStringLength") == false
  112.                     && met.getName().equals("sendShortStringLength") == false
  113.                 ) {
  114.                 met.insertBefore(""
  115.                         + "{"
  116.                         + "     " + Patcher.class.getName() + ".logSend(this, \"" + met.getName() + "\");"
  117.                         + "}"
  118.                 );
  119.                 logger.info("Added logger for " + met.getName());
  120.             }
  121.         }
  122.     }
  123.    
  124.     private void interceptReallyHandle() throws NotFoundException, CannotCompileException
  125.     {
  126.         HookManager.getInstance().registerHook("com.wurmonline.server.creatures.Communicator", "reallyHandle", null,
  127.             new InvocationHandlerFactory() {
  128.                 @Override
  129.                 public InvocationHandler createInvocationHandler() {
  130.                     return new InvocationHandler() {
  131.                         @Override
  132.                         public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  133.                            
  134.                             ByteBuffer bb = (ByteBuffer)args[1];
  135.                             final byte[] buff = new byte[bb.remaining()];
  136.                             bb.duplicate().get(buff);
  137.                            
  138.                             if(buff.length > 0) {
  139.                                 Communicator com = (Communicator)proxy;
  140.                                 logger.info("cmd in:\t" + buff[0] + "\t" + com.getPlayer().getName() + "\t" + com.player.hasLink() + "\t" + buff.length);
  141.                             } else {
  142.                                 logger.info("cmd in: empty!");
  143.                             }
  144.                            
  145.                             return method.invoke(proxy, args);
  146.                         }
  147.                     };
  148.                 }
  149.             }
  150.         );
  151.        
  152.     } // interceptReallyHandle
  153.  
  154.  
  155.     /**
  156.      * Called from game.
  157.      *
  158.      * @param com
  159.      * @param method
  160.      */
  161.     static public void logSend(Communicator com, String method)
  162.     {
  163.         String pName;
  164.         if(com != null && com.getPlayer() != null) {
  165.             pName = com.getPlayer().getName();
  166.         } else {
  167.             pName = "<null>";
  168.         }
  169.         logger.info("cmd out:\t" + method + "\t" + pName + "\t" + ((com == null || com.getPlayer() == null) ? "null" : com.getPlayer().hasLink()));
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement