/ login-server / source / packets / ReadableClientPacket.ts
ReadableClientPacket.ts
 1  import { IClientPacket } from './IClientPacket'
 2  
 3  const enum PacketWritableChunks {
 4      C = 1,
 5      D = 4,
 6      F = 8,
 7      H = 2,
 8      Q = 8
 9  }
10  
11  export class ReadableClientPacket implements IClientPacket {
12      currentBuffer: Buffer
13      data: Array<any> = []
14      offset: number
15  
16      constructor( buffer, startOffset = 0 ) {
17          this.currentBuffer = buffer
18          this.offset = startOffset
19      }
20  
21      readC(): number {
22          let value = this.currentBuffer.readUInt8( this.offset )
23          this.offset += PacketWritableChunks.C
24  
25          return value
26      }
27  
28      readH(): number {
29          let value = this.currentBuffer.readUInt16LE( this.offset )
30          this.offset += PacketWritableChunks.H
31  
32          return value
33      }
34  
35      readD(): number {
36          let value = this.currentBuffer.readInt32LE( this.offset )
37          this.offset += PacketWritableChunks.D
38  
39          return value
40      }
41  
42      readF(): number {
43          let value = this.currentBuffer.readDoubleLE( this.offset )
44          this.offset += PacketWritableChunks.F
45  
46          return value
47      }
48  
49      readB( length: number ): Buffer {
50          let value = this.currentBuffer.subarray( this.offset, this.offset + length )
51          this.offset += length
52  
53          return value
54      }
55  
56      readS(): string {
57          let currentIndex
58  
59          for ( currentIndex = this.offset; currentIndex < this.currentBuffer.length; currentIndex += 2 ) {
60              if ( this.currentBuffer.readUInt16LE( currentIndex ) === 0x00 ) {
61                  break
62              }
63          }
64  
65          let value = this.currentBuffer.toString( 'ucs2', this.offset, currentIndex )
66          this.offset = currentIndex + 2
67  
68          return value
69      }
70  
71      readQ(): bigint {
72          let value = this.currentBuffer.readBigInt64LE( this.offset )
73          this.offset += PacketWritableChunks.Q
74  
75          return value
76      }
77  
78      hasBytes( amount : number ) : boolean {
79          return ( this.offset + amount ) <= ( this.currentBuffer.length - 1 )
80      }
81  
82      skipD( amount : number = 1 ) : void {
83          this.offset += ( amount * PacketWritableChunks.D )
84      }
85  }