@@ -29,6 +29,7 @@ class ConnectionModule(reactContext: ReactApplicationContext)
2929 private val lock = ReentrantReadWriteLock ()
3030 private var socket: Socket ? = null
3131 private var connectionId: UUID ? = null
32+ private var packetIds: PacketIds ? = null
3233 private var compressionThreshold = - 1
3334 private var compressionEnabled = false
3435 private var aesDecipher: Cipher ? = null
@@ -43,6 +44,7 @@ class ConnectionModule(reactContext: ReactApplicationContext)
4344 } catch (_: Exception ) {}
4445 socket = null
4546 connectionId = null
47+ packetIds = null
4648 compressionThreshold = - 1
4749 compressionEnabled = false
4850 aesDecipher = null
@@ -92,7 +94,7 @@ class ConnectionModule(reactContext: ReactApplicationContext)
9294 aesDecipher = Cipher .getInstance(" AES/CFB8/NoPadding" ).apply {
9395 init (Cipher .DECRYPT_MODE , secretKey, iv)
9496 }
95- val result = directlyWritePacket(PacketIds ( - 1 ) .encryptionResponse, packetBytes)
97+ val result = directlyWritePacket(packetIds !! .encryptionResponse, packetBytes)
9698 aesCipher = Cipher .getInstance(" AES/CFB8/NoPadding" ).apply {
9799 init (Cipher .ENCRYPT_MODE , secretKey, iv)
98100 }
@@ -127,7 +129,29 @@ class ConnectionModule(reactContext: ReactApplicationContext)
127129 }
128130 hashSet
129131 }
130- val ids = PacketIds (protocolVersion) // TODO: Receive packet IDs from JavaScript.
132+ val ids = PacketIds (protocolVersion)
133+ // Receive packet IDs from JavaScript.
134+ opts.getMap(" packetIds" )?.let {
135+ for (entry in it.entryIterator) {
136+ val value = it.getDynamic(entry.key)
137+ if (value.type != ReadableType .Number ) continue
138+ when (entry.key) {
139+ " SERVERBOUND_LOGIN_START" -> ids.loginStart = value.asInt()
140+ " SERVERBOUND_ENCRYPTION_RESPONSE" -> ids.encryptionResponse = value.asInt()
141+ " CLIENTBOUND_LOGIN_SUCCESS" -> ids.loginSuccess = value.asInt()
142+ " SERVERBOUND_LOGIN_ACKNOWLEDGED" -> ids.loginAcknowledged = value.asInt()
143+ " CLIENTBOUND_SET_COMPRESSION" -> ids.setCompression = value.asInt()
144+ " CLIENTBOUND_KEEP_ALIVE_CONFIGURATION" -> ids.configurationKeepAliveClientBound = value.asInt()
145+ " SERVERBOUND_KEEP_ALIVE_CONFIGURATION" -> ids.configurationKeepAliveServerBound = value.asInt()
146+ " CLIENTBOUND_FINISH_CONFIGURATION" -> ids.finishConfigurationClientBound = value.asInt()
147+ " SERVERBOUND_ACK_FINISH_CONFIGURATION" -> ids.finishConfigurationServerBound = value.asInt()
148+ " CLIENTBOUND_START_CONFIGURATION" -> ids.startConfigurationClientBound = value.asInt()
149+ " SERVERBOUND_ACKNOWLEDGE_CONFIGURATION" -> ids.acknowledgeConfigurationServerBound = value.asInt()
150+ " CLIENTBOUND_KEEP_ALIVE_PLAY" -> ids.playKeepAliveClientBound = value.asInt()
151+ " SERVERBOUND_KEEP_ALIVE_PLAY" -> ids.playKeepAliveServerBound = value.asInt()
152+ }
153+ }
154+ }
131155
132156 // Start thread which handles creating the connection and then reads packets from it.
133157 // This avoids blocking the main thread on writeLock and keeps the UI thread responsive.
@@ -144,6 +168,7 @@ class ConnectionModule(reactContext: ReactApplicationContext)
144168 socket.soTimeout = 20 * 1000
145169 this @ConnectionModule.socket = socket
146170 this @ConnectionModule.connectionId = connectionId
171+ this @ConnectionModule.packetIds = ids
147172 promise.resolve(connectionId.toString())
148173 }
149174 } catch (e: Exception ) {
0 commit comments