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 sp = require("string_pack")
-- quick and dirty BIG ENDIAN ONLY port of bufferstuff to lua
@ -24,7 +25,7 @@ function bufferStuff.Writer:new()
end
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
function bufferStuff.Writer:writeByte(value)
@ -132,30 +133,30 @@ function bufferStuff.Reader:new(data)
end
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
return value
end
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
return value >= 1
end
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
return value
end
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
local text = ""
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
end
@ -163,60 +164,60 @@ function bufferStuff.Reader:readShortString()
end
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
return value
end
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
return value
end
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
return value
end
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
return value
end
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
return value
end
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
return value
end
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
return value
end
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
return value
end
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
local text = ""
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
end
@ -224,12 +225,12 @@ function bufferStuff.Reader:readString()
end
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
local text = ""
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
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_names = require("packet_names")
local bufferStuff = require("bufferstuff")
--local libDeflate = require("LibDeflate")
-- local tempChunk = chunk:new()
-- for x = 0, 15 do
-- for z = 0, 15 do
-- tempChunk:setBlock(2, x, 62, z)
-- end
-- end
local libDeflate = require("LibDeflate")
local monitor = peripheral.wrap("back")
local oldTerm = nil
if monitor ~= nil then
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
@ -19,136 +27,185 @@ local playerYaw = 0
local playerPitch = 0
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()
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
local b, bv, data, bx = os.pullEvent("websocket_message")
local reader = bufferStuff.Reader:new(data)
local packetId = reader:readByte()
if packetId == packet.KeepAlive then
ws.send(data, true)
elseif packetId == packet.LoginRequest then
local protocolVersion = reader:readInt()
local username = reader:readString16()
local mapSeed = reader:readLong()
local dimension = reader:readByte()
while reader.offset <= #reader.bufferData do
local packetId = reader:readByte()
print(packet_names[packetId], packetId, reader.offset, #reader.bufferData)
if packetId == packet.LoginRequest then
local protocolVersion = reader:readInt()
local username = reader:readString16()
local mapSeed = reader:readLong()
local dimension = reader:readByte()
local writer = bufferStuff.Writer:new()
-- Login packet
writer:writeByte(packet.LoginRequest) -- Packet ID
writer:writeInt(1) -- Entity ID
writer:writeString16("") -- Login Token
writer:writeLong(0) -- Seed
writer:writeByte(0) -- Dimension
ws.send(writer:toBuffer(), true)
local writer = bufferStuff.Writer:new()
-- Login packet
writer:writeByte(packet.LoginRequest) -- Packet ID
writer:writeInt(1) -- Entity ID
writer:writeString16("") -- Login Token
writer:writeLong(0) -- Seed
writer:writeByte(0) -- Dimension
ws.send(writer:toBuffer(), true)
-- teleport
-- local teleportWriter = bufferStuff.Writer:new()
-- teleportWriter:writeByte(packet.PlayerPositionLook)
-- teleportWriter:writeDouble(8)
-- teleportWriter:writeDouble(75)
-- teleportWriter:writeDouble(75.62)
-- teleportWriter:writeDouble(8)
-- teleportWriter:writeFloat(0)
-- teleportWriter:writeFloat(0)
-- teleportWriter:writeBool(true)
-- ws.send(teleportWriter:toBuffer(), true)
-- teleport
local teleportWriter = bufferStuff.Writer:new()
teleportWriter:writeByte(packet.PlayerPosition)
teleportWriter:writeDouble(8)
teleportWriter:writeDouble(75)
teleportWriter:writeDouble(75.62)
teleportWriter:writeDouble(8)
teleportWriter:writeBool(false)
ws.send(teleportWriter:toBuffer(), true)
-- pre chunk
local preChunkWriter = bufferStuff.Writer:new()
preChunkWriter:writeByte(packet.PreChunk)
preChunkWriter:writeInt(0)
preChunkWriter:writeInt(0)
preChunkWriter:writeBool(true)
ws.send(preChunkWriter:toBuffer(), true)
-- pre chunk
local preChunkWriter = bufferStuff.Writer:new()
preChunkWriter:writeByte(packet.PreChunk)
preChunkWriter:writeInt(0)
preChunkWriter:writeInt(0)
preChunkWriter:writeBool(true)
ws.send(preChunkWriter:toBuffer(), true)
local blockWriter = bufferStuff.Writer:new()
for x = 0, 15 do
for z = 0, 15 do
blockWriter:writeByte(packet.BlockChange)
blockWriter:writeInt(x)
blockWriter:writeByte(62)
blockWriter:writeInt(z)
blockWriter:writeByte(2)
blockWriter:writeByte(0)
local blockWriter = bufferStuff.Writer:new()
for x = 0, 15 do
for z = 0, 15 do
blockWriter:writeByte(packet.BlockChange)
blockWriter:writeInt(x)
blockWriter:writeByte(62)
blockWriter:writeInt(z)
blockWriter:writeByte(20)
blockWriter:writeByte(0)
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
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
local keepAliveWriter = bufferStuff.Writer:new()
keepAliveWriter:writeByte(packet.KeepAlive)
local keepAlivePacket = keepAliveWriter:toBuffer()
local keepalivePacket = bufferStuff.Writer:new()
keepalivePacket:writeByte(packet.KeepAlive)
keepalivePacket = keepalivePacket:toBuffer()
local d = false
local timeLastFrame = os.clock()
local tickCount = 0
local function serverTickLoop()
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
-- 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
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