Skip to content

Commit

Permalink
Buffer chat messages, fix bugs, update TODOs.
Browse files Browse the repository at this point in the history
Fix log event handler not being unregistered.
Disable login start with certificates (for now).
  • Loading branch information
retrixe committed Sep 14, 2022
1 parent 9597fc4 commit ae1a54b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/minecraft/connection/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export class NativeServerConnection
this.eventEmitter.removeAllListeners('ecm:packet')
this.eventEmitter.removeAllListeners('ecm:error')
this.eventEmitter.removeAllListeners('ecm:close')
this.eventEmitter.removeAllListeners('ecm:log')
this.closed = true
this.emit('close')
})
Expand All @@ -150,6 +151,7 @@ export class NativeServerConnection
this.eventEmitter.removeAllListeners('ecm:packet')
this.eventEmitter.removeAllListeners('ecm:error')
this.eventEmitter.removeAllListeners('ecm:close')
this.eventEmitter.removeAllListeners('ecm:log')
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/minecraft/connection/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const parseEncryptionRequestPacket = (packet: Packet) => {
export const getLoginPacket = (opts: ConnectionOptions) => {
const data: PacketDataTypes[] = [opts.username]
if (opts.protocolVersion >= protocolMap[1.19]) {
data.push(false)
/* TODO: Support chat signing properly.
data.push(!!opts.certificate)
if (opts.certificate) {
let buf = Buffer.alloc(8)
Expand All @@ -46,7 +48,7 @@ export const getLoginPacket = (opts: ConnectionOptions) => {
buf = Buffer.from(opts.certificate.publicKeySignature, 'base64')
data.push(writeVarInt(buf.byteLength))
data.push(buf)
}
} */
if (opts.protocolVersion >= protocolMap['1.19.1']) {
if (opts.selectedProfile) {
const msb = Buffer.from(opts.selectedProfile.substring(0, 16), 'hex')
Expand Down
20 changes: 16 additions & 4 deletions src/screens/chat/ChatScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ const ChatScreen = ({ navigation, route }: Props) => {
const [messages, setMessages] = useState<Message[]>([])
const [loggedIn, setLoggedIn] = useState(false)
const [message, setMessage] = useState('')
const messagesBufferRef = useRef<Message[]>([])
const healthRef = useRef<number | null>(null)
const statusRef = useRef<Status>(connection ? 'CONNECTING' : 'OPENING')
const idRef = useRef(0)
Expand All @@ -117,16 +118,27 @@ const ChatScreen = ({ navigation, route }: Props) => {
? 256
: 100
const addMessage = (text: MinecraftChat) =>
setMessages(m => {
const trunc = m.length > 500 ? m.slice(0, 499) : m
return [{ key: idRef.current++, text }].concat(trunc)
})
messagesBufferRef.current.unshift({ key: idRef.current++, text })
const closeChatScreen = () => {
if (navigation.canGoBack() && statusRef.current !== 'CLOSED') {
navigation.goBack()
}
}

// Chat message buffering function.
useEffect(() => {
const interval = setInterval(() => {
if (messagesBufferRef.current.length) {
setMessages(m => {
const concat = messagesBufferRef.current.concat(m)
messagesBufferRef.current = []
return concat.length > 500 ? concat.slice(0, 499) : concat
})
}
}, 50)
return () => clearInterval(interval)
}, [])

// Screen cleanup function.
useEffect(() =>
navigation.addListener('beforeRemove', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/screens/chat/sessionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export const createConnection = async (
// Create an updated "session" containing access tokens and certificates.
let session = sessions[activeAccount]
const is119 = version >= protocolMap[1.19]
// TODO: We should store session store in a persistent cache. Certificates and access tokens should be updated regularly.
if (uuid && (!session || (!session.certificate && is119))) {
// LOW-TODO: Certificates and access tokens should be updated regularly.
// We should probably lock access to them via a semaphore.
try {
// Create a session with the latest access token.
Expand Down Expand Up @@ -91,6 +91,7 @@ export const createConnection = async (
}
}

// TODO: Better connection cancellation support. The session load can take a lot of time.
// Connect to server after setting up the session.
try {
const newConn = await initiateConnection({
Expand Down

0 comments on commit ae1a54b

Please sign in to comment.