yep that's a chunk alright

This commit is contained in:
Holly Stubbs 2024-07-14 14:23:30 +01:00
parent 64cba49392
commit 4f521aad0c
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
4 changed files with 202 additions and 134 deletions

View file

@ -1,4 +1,5 @@
local util = require("util") local util = require("util")
local sp = require("string_pack")
-- quick and dirty BIG ENDIAN ONLY port of bufferstuff to lua -- quick and dirty BIG ENDIAN ONLY port of bufferstuff to lua
@ -24,7 +25,7 @@ function bufferStuff.Writer:new()
end end
function bufferStuff.Writer:toBuffer() function bufferStuff.Writer:toBuffer()
return string.pack(table.concat(self.typeList, ""), table.unpack(self.dataValues)) return sp.pack(table.concat(self.typeList, ""), table.unpack(self.dataValues))
end end
function bufferStuff.Writer:writeByte(value) function bufferStuff.Writer:writeByte(value)
@ -132,30 +133,30 @@ function bufferStuff.Reader:new(data)
end end
function bufferStuff.Reader:readByte() function bufferStuff.Reader:readByte()
local value = string.unpack(">b", string.sub(self.bufferData, self.offset, self.offset + 1)) local value = sp.unpack(">b", string.sub(self.bufferData, self.offset, self.offset + 1))
self.offset = self.offset + 1 self.offset = self.offset + 1
return value return value
end end
function bufferStuff.Reader:readBool() function bufferStuff.Reader:readBool()
local value = string.unpack(">b", string.sub(self.bufferData, self.offset, self.offset + 1)) local value = sp.unpack(">b", string.sub(self.bufferData, self.offset, self.offset + 1))
self.offset = self.offset + 1 self.offset = self.offset + 1
return value >= 1 return value >= 1
end end
function bufferStuff.Reader:readUByte() function bufferStuff.Reader:readUByte()
local value = string.unpack(">B", string.sub(self.bufferData, self.offset, self.offset + 1)) local value = sp.unpack(">B", string.sub(self.bufferData, self.offset, self.offset + 1))
self.offset = self.offset + 1 self.offset = self.offset + 1
return value return value
end end
function bufferStuff.Reader:readShortString() function bufferStuff.Reader:readShortString()
local length = string.unpack(">B", string.sub(self.bufferData, self.offset, self.offset + 1)) local length = sp.unpack(">B", string.sub(self.bufferData, self.offset, self.offset + 1))
self.offset = self.offset + 1 self.offset = self.offset + 1
local text = "" local text = ""
for i = 1, length do for i = 1, length do
text = text .. string.char(string.unpack(">B", string.sub(self.bufferData, self.offset, self.offset + 1))) text = text .. string.char(sp.unpack(">B", string.sub(self.bufferData, self.offset, self.offset + 1)))
self.offset = self.offset + 1 self.offset = self.offset + 1
end end
@ -163,60 +164,60 @@ function bufferStuff.Reader:readShortString()
end end
function bufferStuff.Reader:readShort() function bufferStuff.Reader:readShort()
local value = string.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2)) local value = sp.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2))
self.offset = self.offset + 2 self.offset = self.offset + 2
return value return value
end end
function bufferStuff.Reader:readUShort() function bufferStuff.Reader:readUShort()
local value = string.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2)) local value = sp.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2))
self.offset = self.offset + 2 self.offset = self.offset + 2
return value return value
end end
function bufferStuff.Reader:readInt() function bufferStuff.Reader:readInt()
local value = string.unpack(">i", string.sub(self.bufferData, self.offset, self.offset + 4)) local value = sp.unpack(">i", string.sub(self.bufferData, self.offset, self.offset + 4))
self.offset = self.offset + 4 self.offset = self.offset + 4
return value return value
end end
function bufferStuff.Reader:readUInt() function bufferStuff.Reader:readUInt()
local value = string.unpack(">I", string.sub(self.bufferData, self.offset, self.offset + 4)) local value = sp.unpack(">I", string.sub(self.bufferData, self.offset, self.offset + 4))
self.offset = self.offset + 4 self.offset = self.offset + 4
return value return value
end end
function bufferStuff.Reader:readLong() function bufferStuff.Reader:readLong()
local value = string.unpack(">l", string.sub(self.bufferData, self.offset, self.offset + 8)) local value = sp.unpack(">l", string.sub(self.bufferData, self.offset, self.offset + 8))
self.offset = self.offset + 8 self.offset = self.offset + 8
return value return value
end end
function bufferStuff.Reader:readULong() function bufferStuff.Reader:readULong()
local value = string.unpack(">L", string.sub(self.bufferData, self.offset, self.offset + 8)) local value = sp.unpack(">L", string.sub(self.bufferData, self.offset, self.offset + 8))
self.offset = self.offset + 8 self.offset = self.offset + 8
return value return value
end end
function bufferStuff.Reader:readFloat() function bufferStuff.Reader:readFloat()
local value = string.unpack(">f", string.sub(self.bufferData, self.offset, self.offset + 4)) local value = sp.unpack(">f", string.sub(self.bufferData, self.offset, self.offset + 4))
self.offset = self.offset + 4 self.offset = self.offset + 4
return value return value
end end
function bufferStuff.Reader:readDouble() function bufferStuff.Reader:readDouble()
local value = string.unpack(">d", string.sub(self.bufferData, self.offset, self.offset + 8)) local value = sp.unpack(">d", string.sub(self.bufferData, self.offset, self.offset + 8))
self.offset = self.offset + 8 self.offset = self.offset + 8
return value return value
end end
function bufferStuff.Reader:readString() function bufferStuff.Reader:readString()
local length = string.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2)) local length = sp.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2))
self.offset = self.offset + 2 self.offset = self.offset + 2
local text = "" local text = ""
for i = 1, length do for i = 1, length do
text = text .. string.char(string.unpack(">B", string.sub(self.bufferData, self.offset, self.offset + 1))) text = text .. string.char(sp.unpack(">B", string.sub(self.bufferData, self.offset, self.offset + 1)))
self.offset = self.offset + 1 self.offset = self.offset + 1
end end
@ -224,12 +225,12 @@ function bufferStuff.Reader:readString()
end end
function bufferStuff.Reader:readString16() function bufferStuff.Reader:readString16()
local length = string.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2)) local length = sp.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2))
self.offset = self.offset + 2 self.offset = self.offset + 2
local text = "" local text = ""
for i = 1, length do for i = 1, length do
text = text .. string.char(string.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2))) text = text .. string.char(sp.unpack(">h", string.sub(self.bufferData, self.offset, self.offset + 2)))
self.offset = self.offset + 2 self.offset = self.offset + 2
end end

9
mc/packet_names.lua Normal file
View file

@ -0,0 +1,9 @@
local packet = require("packet")
local packet_names = {}
for key, value in pairs(packet) do
packet_names[value] = key
end
return packet_names

View file

@ -1,14 +1,22 @@
--local chunk = require("chunk") local chunk = require("chunk")
local packet = require("packet") local packet = require("packet")
local packet_names = require("packet_names")
local bufferStuff = require("bufferstuff") local bufferStuff = require("bufferstuff")
--local libDeflate = require("LibDeflate")
-- local tempChunk = chunk:new() local libDeflate = require("LibDeflate")
-- for x = 0, 15 do
-- for z = 0, 15 do local monitor = peripheral.wrap("back")
-- tempChunk:setBlock(2, x, 62, z) local oldTerm = nil
-- end if monitor ~= nil then
-- end oldTerm = term.redirect(monitor)
end
local tempChunk = chunk:new()
for x = 0, 15 do
for z = 0, 15 do
tempChunk:setBlock(2, x, 62, z)
end
end
local ws = nil local ws = nil
@ -19,136 +27,185 @@ local playerYaw = 0
local playerPitch = 0 local playerPitch = 0
local onGround = false local onGround = false
term.clear()
if monitor ~= nil then
monitor.setTextScale(2)
end
term.setCursorPos(2, 2)
term.write("Minecraft Server lmao this is dumb")
local function doWebsocket() local function doWebsocket()
ws = http.websocket("ws://localhost:25567") if monitor ~= nil then
ws = http.websocket("wss://ws.eusv.net/mcinmc")
else
ws = http.websocket("ws://localhost:25567")
end
while true do while true do
local b, bv, data, bx = os.pullEvent("websocket_message") local b, bv, data, bx = os.pullEvent("websocket_message")
local reader = bufferStuff.Reader:new(data) local reader = bufferStuff.Reader:new(data)
local packetId = reader:readByte() while reader.offset <= #reader.bufferData do
if packetId == packet.KeepAlive then local packetId = reader:readByte()
ws.send(data, true) print(packet_names[packetId], packetId, reader.offset, #reader.bufferData)
elseif packetId == packet.LoginRequest then if packetId == packet.LoginRequest then
local protocolVersion = reader:readInt() local protocolVersion = reader:readInt()
local username = reader:readString16() local username = reader:readString16()
local mapSeed = reader:readLong() local mapSeed = reader:readLong()
local dimension = reader:readByte() local dimension = reader:readByte()
local writer = bufferStuff.Writer:new() local writer = bufferStuff.Writer:new()
-- Login packet -- Login packet
writer:writeByte(packet.LoginRequest) -- Packet ID writer:writeByte(packet.LoginRequest) -- Packet ID
writer:writeInt(1) -- Entity ID writer:writeInt(1) -- Entity ID
writer:writeString16("") -- Login Token writer:writeString16("") -- Login Token
writer:writeLong(0) -- Seed writer:writeLong(0) -- Seed
writer:writeByte(0) -- Dimension writer:writeByte(0) -- Dimension
ws.send(writer:toBuffer(), true) ws.send(writer:toBuffer(), true)
-- teleport -- teleport
-- local teleportWriter = bufferStuff.Writer:new() local teleportWriter = bufferStuff.Writer:new()
-- teleportWriter:writeByte(packet.PlayerPositionLook) teleportWriter:writeByte(packet.PlayerPosition)
-- teleportWriter:writeDouble(8) teleportWriter:writeDouble(8)
-- teleportWriter:writeDouble(75) teleportWriter:writeDouble(75)
-- teleportWriter:writeDouble(75.62) teleportWriter:writeDouble(75.62)
-- teleportWriter:writeDouble(8) teleportWriter:writeDouble(8)
-- teleportWriter:writeFloat(0) teleportWriter:writeBool(false)
-- teleportWriter:writeFloat(0) ws.send(teleportWriter:toBuffer(), true)
-- teleportWriter:writeBool(true)
-- ws.send(teleportWriter:toBuffer(), true)
-- pre chunk -- pre chunk
local preChunkWriter = bufferStuff.Writer:new() local preChunkWriter = bufferStuff.Writer:new()
preChunkWriter:writeByte(packet.PreChunk) preChunkWriter:writeByte(packet.PreChunk)
preChunkWriter:writeInt(0) preChunkWriter:writeInt(0)
preChunkWriter:writeInt(0) preChunkWriter:writeInt(0)
preChunkWriter:writeBool(true) preChunkWriter:writeBool(true)
ws.send(preChunkWriter:toBuffer(), true) ws.send(preChunkWriter:toBuffer(), true)
local blockWriter = bufferStuff.Writer:new() local blockWriter = bufferStuff.Writer:new()
for x = 0, 15 do for x = 0, 15 do
for z = 0, 15 do for z = 0, 15 do
blockWriter:writeByte(packet.BlockChange) blockWriter:writeByte(packet.BlockChange)
blockWriter:writeInt(x) blockWriter:writeInt(x)
blockWriter:writeByte(62) blockWriter:writeByte(62)
blockWriter:writeInt(z) blockWriter:writeInt(z)
blockWriter:writeByte(2) blockWriter:writeByte(20)
blockWriter:writeByte(0) blockWriter:writeByte(0)
end
end end
ws.send(blockWriter:toBuffer(), true)
-- chunk
-- TODO: Speed this up, it's SLOW as balls
local chunkWriter = bufferStuff.Writer:new()
local chunkData = bufferStuff.Writer:new()
for _, id in ipairs(tempChunk.blocks) do
chunkData:writeUByte(id)
end
for _, id in ipairs(tempChunk.metadata) do
chunkData:writeUByte(id)
end
for _, id in ipairs(tempChunk.blockLight) do
chunkData:writeUByte(id)
end
for _, id in ipairs(tempChunk.skyLight) do
chunkData:writeUByte(id)
end
chunkWriter:writeByte(packet.MapChunk)
chunkWriter:writeInt(0)
chunkWriter:writeShort(0)
chunkWriter:writeInt(0)
chunkWriter:writeByte(15)
chunkWriter:writeByte(127)
chunkWriter:writeByte(15)
local compressedData = libDeflate:CompressZlib(chunkData:toBuffer())
chunkWriter:writeInt(#compressedData)
local chunkPacket = chunkWriter:toBuffer()
ws.send(chunkPacket .. compressedData, true)
elseif packetId == packet.Handshake then
local username = reader:readString16()
local writer = bufferStuff.Writer:new()
writer:writeByte(packet.Handshake)
writer:writeString16("-")
ws.send(writer:toBuffer(), true)
elseif packetId == packet.Player then
onGround = reader:readBool()
elseif packetId == packet.PlayerLook then
playerYaw = reader:readFloat()
playerPitch = reader:readFloat()
onGround = reader:readBool()
elseif packetId == packet.PlayerPosition then
playerX = reader:readDouble()
playerY = reader:readDouble()
reader:readDouble()
playerZ = reader:readDouble()
onGround = reader:readBool()
elseif packetId == packet.PlayerPositionLook then
playerX = reader:readDouble()
playerY = reader:readDouble()
reader:readDouble()
playerZ = reader:readDouble()
playerYaw = reader:readFloat()
playerPitch = reader:readFloat()
onGround = reader:readBool()
end end
ws.send(blockWriter:toBuffer(), true)
-- chunk
-- local chunkWriter = bufferStuff.Writer:new()
-- local chunkData = bufferStuff.Writer:new()
-- for _, id in ipairs(tempChunk.blocks) do
-- chunkData:writeUByte(id)
-- end
-- for _, id in ipairs(tempChunk.metadata) do
-- chunkData:writeUByte(id)
-- end
-- for _, id in ipairs(tempChunk.blockLight) do
-- chunkData:writeUByte(id)
-- end
-- for _, id in ipairs(tempChunk.skyLight) do
-- chunkData:writeUByte(id)
-- end
-- chunkWriter:writeByte(packet.MapChunk)
-- chunkWriter:writeInt(0)
-- chunkWriter:writeShort(0)
-- chunkWriter:writeInt(0)
-- chunkWriter:writeByte(15)
-- chunkWriter:writeByte(127)
-- chunkWriter:writeByte(15)
-- local compressedData = libDeflate:CompressDeflate(chunkData:toBuffer())
-- chunkWriter:writeInt(#compressedData)
-- local chunkPacket = chunkWriter:toBuffer()
-- ws.send(chunkPacket .. compressedData, true)
elseif packetId == packet.Handshake then
local username = reader:readString16()
local writer = bufferStuff.Writer:new()
writer:writeByte(packet.Handshake)
writer:writeString16("-")
ws.send(writer:toBuffer(), true)
elseif packetId == packet.Player then
onGround = reader:readBool()
elseif packetId == packet.PlayerPosition then
playerX = reader:readDouble()
playerY = reader:readDouble()
reader:readDouble()
playerZ = reader:readDouble()
onGround = reader:readBool()
elseif packetId == packet.PlayerPositionLook then
playerX = reader:readDouble()
playerY = reader:readDouble()
reader:readDouble()
playerZ = reader:readDouble()
playerYaw = reader:readFloat()
playerPitch = reader:readFloat()
onGround = reader:readBool()
elseif packet == packet.PlayerLook then
playerYaw = reader:readFloat()
playerPitch = reader:readFloat()
onGround = reader:readBool()
end end
end end
end end
local keepAliveWriter = bufferStuff.Writer:new() local keepalivePacket = bufferStuff.Writer:new()
keepAliveWriter:writeByte(packet.KeepAlive) keepalivePacket:writeByte(packet.KeepAlive)
local keepAlivePacket = keepAliveWriter:toBuffer() keepalivePacket = keepalivePacket:toBuffer()
local d = false
local timeLastFrame = os.clock()
local tickCount = 0 local tickCount = 0
local function serverTickLoop() local function serverTickLoop()
while true do while true do
--print(playerX, playerY, playerZ, playerYaw, playerPitch, onGround) if tickCount % 20 == 0 then
if ws ~= nil then
ws.send(keepalivePacket)
end
end
os.sleep(0.1) os.sleep(0.05)
tickCount = tickCount + 1 tickCount = tickCount + 1
-- Term output
-- term.setCursorPos(2, 4)
-- term.write("X: " .. playerX .. " ")
-- term.setCursorPos(2, 5)
-- term.write("Y: " .. playerY .. " ")
-- term.setCursorPos(2, 6)
-- term.write("Z: " .. playerZ .. " ")
-- term.setCursorPos(2, 8)
-- term.write("Yaw: " .. playerYaw .. " ")
-- term.setCursorPos(2, 9)
-- term.write("Pitch: " .. playerPitch .. " ")
-- term.setCursorPos(2, 11)
-- term.write("On Ground: " .. tostring(onGround) .. " ")
-- term.setCursorPos(2, 13)
-- term.write("Tick Counter: " .. tostring(tickCount) .. " TPS: " .. tostring(math.floor((1 / (os.clock() - timeLastFrame)) + 0.5)))
-- if tickCount % 20 == 0 then
-- d = not d
-- if not d then
-- term.write(" - ")
-- else
-- term.write(" | ")
-- end
-- end
timeLastFrame = os.clock()
end end
end end
parallel.waitForAll(doWebsocket, serverTickLoop) parallel.waitForAll(doWebsocket, serverTickLoop)
if monitor ~= nil then
term.redirect(oldTerm)
end

1
mc/string_pack.lua Normal file

File diff suppressed because one or more lines are too long