import MMOClient from "../mmocore/MMOClient" ; import MMOConfig from "../mmocore/MMOConfig" ; import MMOConnection from "../mmocore/MMOConnection" ; import LoginCrypt from "./LoginCrypt" ; import LoginPacketHandler from "./LoginPacketHandler" ; import L2Server from "../entities/L2Server" ; import LoginServerPacket from "./outgoing/login/LoginServerPacket" ; import IConnection from "../mmocore/IConnection" ; import mutators from "./mutators/login/index" ; import SocketFactory from "../socket/SocketFactory" ; export default class LoginClient extends MMOClient { private _loginCrypt : LoginCrypt = new LoginCrypt ( ) ; private _blowfishKey ! : Uint8Array ; Servers : L2Server [ ] = [ ] ; ServerId = 1 ; Config ! : MMOConfig ; get BlowfishKey ( ) : Uint8Array { return this . _blowfishKey ; } set BlowfishKey ( blowfishKey ) { this . _blowfishKey = blowfishKey ; this . _loginCrypt . setKey ( blowfishKey ) ; } constructor ( ) { super ( ) ; this . PacketHandler = new LoginPacketHandler ( ) ; mutators . forEach ( ( m ) => { const mutator = Object . create ( m [ 0 ] , { Client : { value : this } , PacketType : { value : ( m [ 1 ] as any ) . name } , } ) ; this . registerMutator ( mutator ) ; } ) ; } init ( config , connection ? ) { this . Connection = connection ?? new MMOConnection ( SocketFactory . getSocketAdapter ( config ) , this ) ; this . Config = config ; this . Session . username = config . Username ; if ( config . ServerId ) { this . ServerId = config . ServerId ; } return this ; } pack ( lsp ) { lsp . write ( ) ; if ( ! lsp . Buffer || lsp . Position === 0 ) { return new Uint8Array ( ) ; } const pos = lsp . Position + 4 ; const count = pos + ( 8 - ( pos % 8 ) ) ; const data = new Uint8Array ( count + 2 ) ; data . set ( lsp . Buffer . slice ( 0 , count ) , 2 ) ; this . encrypt ( data , 2 , count - 2 ) ; data [ 0 ] = ( count + 2 ) & 0xff ; data [ 1 ] = ( count + 2 ) >>> 8 ; return data ; } sendPacket ( lsp ) { const sendable = this . pack ( lsp ) ; this . logger . debug ( "Sending " , lsp . constructor . name ) ; return this . sendRaw ( sendable ) . then ( ( ) => { this . fire ( `PacketSent:${ lsp . constructor . name }` , { packet : lsp } ) ; } ) ; } encrypt ( buf , offset , size ) { this . _loginCrypt . encrypt ( buf , offset , size ) ; } decrypt ( buf , offset , size ) { this . _loginCrypt . decrypt ( buf , offset , size ) ; } }