diff --git a/package-lock.json b/package-lock.json index 064e15f..fec80c6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,7 @@ "events": "^3.3.0", "promise.allsettled": "^1.0.4", "react": "18.1.0", - "react-native": "0.70.4", + "react-native": "0.70.5", "react-native-crypto": "^2.2.0", "react-native-pager-view": "^5.4.9", "react-native-randombytes": "^3.6.1", @@ -15281,9 +15281,9 @@ "integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==" }, "node_modules/react-native": { - "version": "0.70.4", - "resolved": "https://registry.npmjs.org/react-native/-/react-native-0.70.4.tgz", - "integrity": "sha512-1e4jWotS20AJ/4lGVkZQs2wE0PvCpIRmPQEQ1FyH7wdyuewFFIxbUHqy6vAj1JWVFfAzbDakOQofrIkkHWLqNA==", + "version": "0.70.5", + "resolved": "https://registry.npmjs.org/react-native/-/react-native-0.70.5.tgz", + "integrity": "sha512-5NZM80LC3L+TIgQX/09yiyy48S73wMgpIgN5cCv3XTMR394+KpDI3rBZGH4aIgWWuwijz31YYVF5504+9n2Zfw==", "dependencies": { "@jest/create-cache-key-function": "^29.0.3", "@react-native-community/cli": "9.2.1", @@ -29026,9 +29026,9 @@ "integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==" }, "react-native": { - "version": "0.70.4", - "resolved": "https://registry.npmjs.org/react-native/-/react-native-0.70.4.tgz", - "integrity": "sha512-1e4jWotS20AJ/4lGVkZQs2wE0PvCpIRmPQEQ1FyH7wdyuewFFIxbUHqy6vAj1JWVFfAzbDakOQofrIkkHWLqNA==", + "version": "0.70.5", + "resolved": "https://registry.npmjs.org/react-native/-/react-native-0.70.5.tgz", + "integrity": "sha512-5NZM80LC3L+TIgQX/09yiyy48S73wMgpIgN5cCv3XTMR394+KpDI3rBZGH4aIgWWuwijz31YYVF5504+9n2Zfw==", "requires": { "@jest/create-cache-key-function": "^29.0.3", "@react-native-community/cli": "9.2.1", diff --git a/package.json b/package.json index 37baf32..93483fd 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "events": "^3.3.0", "promise.allsettled": "^1.0.4", "react": "18.1.0", - "react-native": "0.70.4", + "react-native": "0.70.5", "react-native-crypto": "^2.2.0", "react-native-pager-view": "^5.4.9", "react-native-randombytes": "^3.6.1", diff --git a/src/screens/chat/packetHandler.ts b/src/screens/chat/packetHandler.ts index a0a4074..054dbb2 100644 --- a/src/screens/chat/packetHandler.ts +++ b/src/screens/chat/packetHandler.ts @@ -21,6 +21,36 @@ type HandleError = ( translated: string ) => (error: unknown) => any +interface PlayerChatMessage { + signedChat: string + unsignedChat?: string + type: number + displayName: string +} + +const parsePlayerChatMessage = (data: Buffer): PlayerChatMessage => { + const [signedChatLength, signedChatVarIntLength] = readVarInt(data) + data = data.slice(signedChatVarIntLength) + const signedChat = data.slice(0, signedChatLength).toString('utf8') + data = data.slice(signedChatLength) + const hasUnsignedChat = data.readInt8() + data = data.slice(1) + let unsignedChat + if (hasUnsignedChat) { + const [unsignedChatLength, unsignedChatVarIntLength] = readVarInt(data) + data = data.slice(unsignedChatVarIntLength) + unsignedChat = data.slice(0, unsignedChatLength).toString('utf8') + data = data.slice(unsignedChatLength) + } + const [type, typeLength] = readVarInt(data) + data = data.slice(typeLength) + data = data.slice(16) // Skip sender UUID + const [displayNameLength, displayNameVarIntLength] = readVarInt(data) + data = data.slice(displayNameVarIntLength) + const displayName = data.slice(0, displayNameLength).toString('utf8') + return { signedChat, unsignedChat, type, displayName } +} + const handleSystemMessage = ( packet: Packet, addMessage: (text: MinecraftChat) => void, @@ -98,46 +128,18 @@ export const packetHandler = (packet.id === 0x33 && is1191) ) { try { - const [signedChatLen, signedChatViLen] = readVarInt(packet.data) - const signedChat = packet.data - .slice(signedChatViLen, signedChatViLen + signedChatLen) - .toString('utf8') - const totalSignedChatLen = signedChatViLen + signedChatLen - const hasUnsignedChat = packet.data.readInt8(totalSignedChatLen) - const moreData = packet.data.slice(totalSignedChatLen + 1) - if (hasUnsignedChat) { - const [unsignedChatLen, unsignedChatViLen] = readVarInt(moreData) - const unsignedChat = moreData - .slice(unsignedChatViLen, unsignedChatViLen + unsignedChatLen) - .toString('utf8') - const totalUnsignedChatLen = unsignedChatViLen + unsignedChatLen - const position = moreData.readInt8(totalUnsignedChatLen) - const [displayNameLen, displayNameViLen] = readVarInt(moreData, 17) - const nameViLenOffset = 17 + displayNameViLen - const displayName = moreData - .slice(nameViLenOffset, nameViLenOffset + displayNameLen) - .toString('utf8') - if (position === 0 || position === 1) { - addMessage({ - translate: 'chat.type.text', - with: [parseValidJson(displayName), parseValidJson(unsignedChat)] - }) - } - } else { - const position = moreData.readInt8() - const [displayNameLen, displayNameViLen] = readVarInt(moreData, 17) - const nameViLenOffset = 17 + displayNameViLen - const displayName = moreData - .slice(nameViLenOffset, nameViLenOffset + displayNameLen) - .toString('utf8') - if (position === 0 || position === 1) { - addMessage({ - translate: 'chat.type.text', - with: [parseValidJson(displayName), parseValidJson(signedChat)] - }) - } - } + const { type, displayName, signedChat, unsignedChat } = + parsePlayerChatMessage(packet.data) // TODO-1.19: Support sender team name + if (type === 0 || type === 1) { + addMessage({ + translate: 'chat.type.text', + with: [ + parseValidJson(displayName), + parseValidJson(unsignedChat ?? signedChat) + ] + }) + } } catch (e) { handleError(addMessage, parseMessageError)(e) } @@ -172,7 +174,6 @@ export const packetHandler = const deathMessage = parseValidJson( data.slice(chatViLength, chatViLength + chatLen).toString('utf8') ) - addMessage(deathRespawnMessage) if ( (typeof deathMessage === 'string' && deathMessage.trim()) || deathMessage?.text || @@ -182,6 +183,7 @@ export const packetHandler = } // Automatically respawn. // LOW-TODO: Should this be manual, or a dialog, like MC? + addMessage(deathRespawnMessage) connection // Client Status .writePacket(is1191 ? 0x07 : is119 ? 0x06 : 0x04, writeVarInt(0)) .catch(handleError(addMessage, respawnError)) diff --git a/yarn.lock b/yarn.lock index b300a82..bdf91b6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6580,10 +6580,10 @@ "escape-string-regexp" "2.0.0" "invariant" "2.2.4" -"react-native@*", "react-native@^0.0.0-0 || 0.60 - 0.70 || 1000.0.0", "react-native@>=0.57", "react-native@>=0.60.0", "react-native@0.70.4": - "integrity" "sha512-1e4jWotS20AJ/4lGVkZQs2wE0PvCpIRmPQEQ1FyH7wdyuewFFIxbUHqy6vAj1JWVFfAzbDakOQofrIkkHWLqNA==" - "resolved" "https://registry.npmjs.org/react-native/-/react-native-0.70.4.tgz" - "version" "0.70.4" +"react-native@*", "react-native@^0.0.0-0 || 0.60 - 0.70 || 1000.0.0", "react-native@>=0.57", "react-native@>=0.60.0", "react-native@0.70.5": + "integrity" "sha512-5NZM80LC3L+TIgQX/09yiyy48S73wMgpIgN5cCv3XTMR394+KpDI3rBZGH4aIgWWuwijz31YYVF5504+9n2Zfw==" + "resolved" "https://registry.npmjs.org/react-native/-/react-native-0.70.5.tgz" + "version" "0.70.5" dependencies: "@jest/create-cache-key-function" "^29.0.3" "@react-native-community/cli" "9.2.1"