0x04

OSRS Mouse Movement Packet Encoding

Oct 5th, 2025 (edited)
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.93 KB | None | 0 0
  1. int lastProcessedIndex;
  2. int accumulatedTimeOffset;
  3. int recordedMovements;
  4. int i;
  5. int rawY;
  6. int rawX;
  7. int deltaX;
  8. int deltaY;
  9. int timeDelta;
  10. synchronized (class399.mouseRecorder.lock) {
  11.    
  12.     if (!socketError) {
  13.         class399.mouseRecorder.index = 0;
  14.        
  15.     } else if (MouseInput.mouseLastButton != 0 || class399.mouseRecorder.index >= 40) {
  16.        
  17.         mousePacket = null;
  18.         packetStartOffset = 0;
  19.         lastProcessedIndex = 0;
  20.         accumulatedTimeOffset = 0;
  21.         recordedMovements = 0;
  22.  
  23.         for (i = 0; i < class399.mouseRecorder.index && (mousePacket == null || mousePacket.packetBuffer.offset - packetStartOffset < 246); ++i) {
  24.            
  25.             lastProcessedIndex = i;
  26.            
  27.             rawY = class399.mouseRecorder.ys[i];
  28.             if (rawY < -1) {
  29.                 rawY = -1;
  30.             } else if (rawY > 65534) {
  31.                 rawY = 65534;
  32.             }
  33.  
  34.             rawX = class399.mouseRecorder.xs[i];
  35.             if (rawX < -1) {
  36.                 rawX = -1;
  37.             } else if (rawX > 65534) {
  38.                 rawX = 65534;
  39.             }
  40.  
  41.             if (lastX != rawX || lastY != rawY) {
  42.                
  43.                 if (mousePacket == null) {
  44.                     mousePacket = class151.getTcpConnectionMessage_3250(ClientProt.MOUSE_MOVE, serverConnection.isaac);
  45.                     mousePacket.packetBuffer.putByte(0);
  46.                     packetStartOffset = mousePacket.packetBuffer.offset;
  47.                     mousePacket.packetBuffer.offset += 2;
  48.                     accumulatedTimeOffset = 0;
  49.                     recordedMovements = 0;
  50.                 }
  51.  
  52.                 if (lastTimeStamp != -1L) {
  53.                     deltaX = rawX - lastX;
  54.                     deltaY = rawY - lastY;
  55.                     timeDelta = (int) ((class399.mouseRecorder.millis[i] - lastTimeStamp) / 20L);
  56.                     accumulatedTimeOffset = (int) ((class399.mouseRecorder.millis[i] - lastTimeStamp) % 20L + (long) accumulatedTimeOffset);
  57.                    
  58.                 } else {
  59.                     deltaX = rawX;
  60.                     deltaY = rawY;
  61.                     timeDelta = Integer.MAX_VALUE;
  62.                 }
  63.  
  64.                 lastX = rawX;
  65.                 lastY = rawY;
  66.                
  67.                 if (timeDelta < 8 && deltaX >= -32 && deltaX <= 31 && deltaY >= -32 && deltaY <= 31) {
  68.                     deltaX += 32;
  69.                     deltaY += 32;
  70.                     mousePacket.packetBuffer.putShort((deltaX << 6) + (timeDelta << 12) + deltaY);
  71.                    
  72.                 } else if (timeDelta < 32 && deltaX >= -128 && deltaX <= 127 && deltaY >= -128 && deltaY <= 127) {
  73.                     deltaX += 128;
  74.                     deltaY += 128;
  75.                     mousePacket.packetBuffer.putByte(timeDelta + 128);
  76.                     mousePacket.packetBuffer.putShort((deltaX << 8) + deltaY);
  77.                    
  78.                 } else if (timeDelta < 32) {
  79.                     mousePacket.packetBuffer.putByte(timeDelta + 192);
  80.                     if (rawX != -1 && rawY != -1) {
  81.                         mousePacket.packetBuffer.putInt(rawX | rawY << 16);
  82.                     } else {
  83.                         mousePacket.packetBuffer.putInt(Integer.MIN_VALUE);
  84.                     }
  85.                    
  86.                 } else {
  87.                     mousePacket.packetBuffer.putShort((timeDelta & 8191) + '\ue000');
  88.                     if (rawX != -1 && rawY != -1) {
  89.                         mousePacket.packetBuffer.putInt(rawX | rawY << 16);
  90.                     } else {
  91.                         mousePacket.packetBuffer.putInt(Integer.MIN_VALUE);
  92.                     }
  93.                 }
  94.  
  95.                 ++recordedMovements;
  96.                 lastTimeStamp = class399.mouseRecorder.millis[i];
  97.             }
  98.         }
  99.  
  100.         if (mousePacket != null) {
  101.             mousePacket.packetBuffer.putByteSize(mousePacket.packetBuffer.offset - packetStartOffset);
  102.             i = mousePacket.packetBuffer.offset;
  103.             mousePacket.packetBuffer.offset = packetStartOffset;
  104.             mousePacket.packetBuffer.putByte(accumulatedTimeOffset / recordedMovements);
  105.             mousePacket.packetBuffer.putByte(accumulatedTimeOffset % recordedMovements);
  106.             mousePacket.packetBuffer.offset = i;
  107.             serverConnection.write(mousePacket);
  108.         }
  109.  
  110.         if (lastProcessedIndex >= class399.mouseRecorder.index) {
  111.             class399.mouseRecorder.index = 0;
  112.         } else {
  113.             MouseRecorder recorder = class399.mouseRecorder;
  114.             recorder.index -= lastProcessedIndex;
  115.             System.arraycopy(class399.mouseRecorder.xs, lastProcessedIndex, class399.mouseRecorder.xs, 0, class399.mouseRecorder.index);
  116.             System.arraycopy(class399.mouseRecorder.ys, lastProcessedIndex, class399.mouseRecorder.ys, 0, class399.mouseRecorder.index);
  117.             System.arraycopy(class399.mouseRecorder.millis, lastProcessedIndex, class399.mouseRecorder.millis, 0, class399.mouseRecorder.index);
  118.         }
  119.     }
  120. }
Advertisement