ci: add missing build artifacts

This commit is contained in:
Vitaly Turovsky 2024-07-07 01:31:33 +03:00
commit a6fc5de1f9
3 changed files with 102 additions and 0 deletions

14
src/packetsPatcher.ts Normal file
View file

@ -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)
}
}
})
})
}

32
src/packetsReplay.ts Normal file
View file

@ -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()
}

56
src/packetsReplayBase.ts Normal file
View file

@ -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
}