Skip to content

Commit c3a3740

Browse files
committed
Pass packet IDs from TypeScript to native Kotlin
We should eliminate IDs in the PacketId class entirely down the line.
1 parent d21b24d commit c3a3740

3 files changed

Lines changed: 59 additions & 7 deletions

File tree

android/app/src/main/java/com/enderchat/modules/connection/ConnectionModule.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

android/app/src/main/java/com/enderchat/modules/connection/PacketIds.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ const val PROTOCOL_VERSION_1212 = 768
1414
data class PacketIds(
1515
val protocolVersion: Int,
1616

17+
/*
18+
Note:
19+
The ID values set here are unused, will not be updated, and are slated for removal!
20+
Refer to `packetIds` in the React Native project, whose values are used by the native module.
21+
*/
22+
1723
// Login state packet IDs
1824
var loginStart: Int = 0x00,
1925
var encryptionResponse: Int = 0x01,
@@ -24,19 +30,23 @@ data class PacketIds(
2430
// Configuration state packet IDs
2531
var configurationKeepAliveClientBound: Int =
2632
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x04
27-
else 0x03,
33+
else if (protocolVersion >= PROTOCOL_VERSION_1202) 0x03
34+
else -1,
2835

2936
var configurationKeepAliveServerBound: Int =
3037
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x04
31-
else 0x03,
38+
else if (protocolVersion >= PROTOCOL_VERSION_1202) 0x03
39+
else -1,
3240

3341
var finishConfigurationClientBound: Int =
3442
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x03
35-
else 0x02,
43+
else if (protocolVersion >= PROTOCOL_VERSION_1202) 0x02
44+
else -1,
3645

3746
var finishConfigurationServerBound: Int =
3847
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x03
39-
else 0x02,
48+
else if (protocolVersion >= PROTOCOL_VERSION_1202) 0x02
49+
else -1,
4050

4151
// Play state packet IDs
4252
var startConfigurationClientBound: Int =

src/minecraft/connection/native.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import packetIds from '../packets/ids'
1414

1515
interface NativeConnectionOptions extends ConnectionOptions {
1616
loginPacket: string
17-
packetFilter: number[]
17+
packetFilter?: number[]
18+
packetIds?: Record<string, number | null>
1819
}
1920

2021
interface NativeConnectionModule extends NativeModule {
@@ -204,6 +205,7 @@ const initiateNativeConnection = async (
204205
opts: ConnectionOptions,
205206
): Promise<NativeServerConnection> => {
206207
const [host, port] = await resolveHostname(opts.host, opts.port)
208+
const ver = opts.protocolVersion
207209
const id = await ConnectionModule.openConnection({
208210
loginPacket: getLoginPacket(opts).toString('base64'),
209211
...opts,
@@ -213,6 +215,21 @@ const initiateNativeConnection = async (
213215
.filter(name => name.startsWith('CLIENTBOUND'))
214216
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
215217
.map(name => packetIds[name as keyof typeof packetIds](opts.protocolVersion)!),
218+
packetIds: {
219+
SERVERBOUND_LOGIN_START: packetIds.SERVERBOUND_LOGIN_START(ver),
220+
SERVERBOUND_ENCRYPTION_RESPONSE: packetIds.SERVERBOUND_ENCRYPTION_RESPONSE(ver),
221+
CLIENTBOUND_LOGIN_SUCCESS: packetIds.CLIENTBOUND_LOGIN_SUCCESS(ver),
222+
SERVERBOUND_LOGIN_ACKNOWLEDGED: packetIds.SERVERBOUND_LOGIN_ACKNOWLEDGED(ver),
223+
CLIENTBOUND_SET_COMPRESSION: packetIds.CLIENTBOUND_SET_COMPRESSION(ver),
224+
CLIENTBOUND_KEEP_ALIVE_CONFIGURATION: packetIds.CLIENTBOUND_KEEP_ALIVE_CONFIGURATION(ver),
225+
SERVERBOUND_KEEP_ALIVE_CONFIGURATION: packetIds.SERVERBOUND_KEEP_ALIVE_CONFIGURATION(ver),
226+
CLIENTBOUND_FINISH_CONFIGURATION: packetIds.CLIENTBOUND_FINISH_CONFIGURATION(ver),
227+
SERVERBOUND_ACK_FINISH_CONFIGURATION: packetIds.SERVERBOUND_ACK_FINISH_CONFIGURATION(ver),
228+
CLIENTBOUND_START_CONFIGURATION: packetIds.CLIENTBOUND_START_CONFIGURATION(ver),
229+
SERVERBOUND_ACKNOWLEDGE_CONFIGURATION: packetIds.SERVERBOUND_ACKNOWLEDGE_CONFIGURATION(ver),
230+
CLIENTBOUND_KEEP_ALIVE_PLAY: packetIds.CLIENTBOUND_KEEP_ALIVE_PLAY(ver),
231+
SERVERBOUND_KEEP_ALIVE_PLAY: packetIds.SERVERBOUND_KEEP_ALIVE_PLAY(ver),
232+
},
216233
})
217234
return new NativeServerConnection(id, opts)
218235
}

0 commit comments

Comments
 (0)