From a6fc5de1f9bc2d2cca3de2b10d5d17f95ad3aedc Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Sun, 7 Jul 2024 01:31:33 +0300 Subject: [PATCH] ci: add missing build artifacts --- src/packetsPatcher.ts | 14 ++++++++++ src/packetsReplay.ts | 32 +++++++++++++++++++++++ src/packetsReplayBase.ts | 56 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 src/packetsPatcher.ts create mode 100644 src/packetsReplay.ts create mode 100644 src/packetsReplayBase.ts diff --git a/src/packetsPatcher.ts b/src/packetsPatcher.ts new file mode 100644 index 00000000..f877d6ed --- /dev/null +++ b/src/packetsPatcher.ts @@ -0,0 +1,14 @@ +// todo it should not be there, most likely it will be more automatically updated in the future +// todo these fixes should be ported to mineflayer + +export default () => { + customEvents.on('mineflayerBotCreated', () => { + bot._client.on('packet', (data, meta) => { + if (meta.name === 'map_chunk') { + if (data.groundUp && data.bitMap === 1 && data.chunkData.every(x => x === 0)) { + data.chunkData = Buffer.from(Array.from({ length: 12_544 }).fill(0) as any) + } + } + }) + }) +} diff --git a/src/packetsReplay.ts b/src/packetsReplay.ts new file mode 100644 index 00000000..57d0805e --- /dev/null +++ b/src/packetsReplay.ts @@ -0,0 +1,32 @@ +import { proxy } from 'valtio' +import { PacketsLogger } from './packetsReplayBase' + +export const packetsReplaceSessionState = proxy({ + active: false, +}) + +const replayLogger = new PacketsLogger() +export default () => { + customEvents.on('mineflayerBotCreated', () => { + replayLogger.contents = '' + bot._client.on('packet', (data, { name, state }) => { + if (!packetsReplaceSessionState.active) { + return + } + replayLogger.log(true, { name, state }, data) + }) + bot._client.on('writePacket' as any, (name, data) => { + if (!packetsReplaceSessionState.active) { + return + } + replayLogger.log(false, { name, state: bot._client.state }, data) + }) + }) +} + +export const downloadPacketsReplay = async () => { + const a = document.createElement('a') + a.href = `data:text/plain;charset=utf-8,${encodeURIComponent(replayLogger.contents)}` + a.download = `packets-replay-${new Date().toISOString()}.txt` + a.click() +} diff --git a/src/packetsReplayBase.ts b/src/packetsReplayBase.ts new file mode 100644 index 00000000..5e2bed09 --- /dev/null +++ b/src/packetsReplayBase.ts @@ -0,0 +1,56 @@ +export class PacketsLogger { + lastPacketTime = -1 + contents = '' + logOnly = [] as string[] + skip = [] as string[] + + logStr (str: string) { + this.contents += `${str}\n` + } + + log (isFromServer: boolean, packet: { name; state }, data: any) { + if (this.logOnly.length > 0 && !this.logOnly.includes(packet.name)) { + return + } + if (this.skip.length > 0 && this.skip.includes(packet.name)) { + return + } + if (this.lastPacketTime === -1) { + this.lastPacketTime = Date.now() + } + + const diff = `+${Date.now() - this.lastPacketTime}` + const str = `${isFromServer ? 'S' : 'C'} ${packet.state}:${packet.name} ${diff} ${JSON.stringify(data)}` + this.logStr(str) + this.lastPacketTime = Date.now() + } +} + +export type ParsedReplayPacket = { + name: string + params: any + state: string + diff: number + isFromServer: boolean +} +export function parseReplayContents (contents: string) { + const lines = contents.split('\n') + + const packets = [] as ParsedReplayPacket[] + for (let line of lines) { + line = line.trim() + if (!line || line.startsWith('#')) continue + const [side, nameState, diff, ...data] = line.split(' ') + const parsed = JSON.parse(data.join(' ')) + const [state, name] = nameState.split(':') + packets.push({ + name, + state, + params: parsed, + isFromServer: side.toUpperCase() === 'S', + diff: Number.parseInt(diff.slice(1), 10), + }) + } + + return packets +}