Skip to content

Commit

Permalink
Hotfix React Native, improve chat message parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Nov 7, 2022
1 parent caf449a commit c73557d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 52 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
82 changes: 42 additions & 40 deletions src/screens/chat/packetHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 ||
Expand All @@ -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))
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[email protected].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", "[email protected].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"
Expand Down

0 comments on commit c73557d

Please sign in to comment.