yep that's a chunk alright
This commit is contained in:
parent
64cba49392
commit
4f521aad0c
4 changed files with 202 additions and 134 deletions
|
@ -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
9
mc/packet_names.lua
Normal 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
|
169
mc/server.lua
169
mc/server.lua
|
@ -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,17 +27,29 @@ 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()
|
||||||
|
if monitor ~= nil then
|
||||||
|
ws = http.websocket("wss://ws.eusv.net/mcinmc")
|
||||||
|
else
|
||||||
ws = http.websocket("ws://localhost:25567")
|
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)
|
||||||
|
while reader.offset <= #reader.bufferData do
|
||||||
local packetId = reader:readByte()
|
local packetId = reader:readByte()
|
||||||
if packetId == packet.KeepAlive then
|
print(packet_names[packetId], packetId, reader.offset, #reader.bufferData)
|
||||||
ws.send(data, true)
|
if packetId == packet.LoginRequest then
|
||||||
elseif 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()
|
||||||
|
@ -45,16 +65,14 @@ local function doWebsocket()
|
||||||
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()
|
||||||
|
@ -71,40 +89,41 @@ local function doWebsocket()
|
||||||
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
|
end
|
||||||
ws.send(blockWriter:toBuffer(), true)
|
ws.send(blockWriter:toBuffer(), true)
|
||||||
|
|
||||||
-- chunk
|
-- chunk
|
||||||
-- local chunkWriter = bufferStuff.Writer:new()
|
-- TODO: Speed this up, it's SLOW as balls
|
||||||
-- local chunkData = bufferStuff.Writer:new()
|
local chunkWriter = bufferStuff.Writer:new()
|
||||||
-- for _, id in ipairs(tempChunk.blocks) do
|
local chunkData = bufferStuff.Writer:new()
|
||||||
-- chunkData:writeUByte(id)
|
for _, id in ipairs(tempChunk.blocks) do
|
||||||
-- end
|
chunkData:writeUByte(id)
|
||||||
-- for _, id in ipairs(tempChunk.metadata) do
|
end
|
||||||
-- chunkData:writeUByte(id)
|
for _, id in ipairs(tempChunk.metadata) do
|
||||||
-- end
|
chunkData:writeUByte(id)
|
||||||
-- for _, id in ipairs(tempChunk.blockLight) do
|
end
|
||||||
-- chunkData:writeUByte(id)
|
for _, id in ipairs(tempChunk.blockLight) do
|
||||||
-- end
|
chunkData:writeUByte(id)
|
||||||
-- for _, id in ipairs(tempChunk.skyLight) do
|
end
|
||||||
-- chunkData:writeUByte(id)
|
for _, id in ipairs(tempChunk.skyLight) do
|
||||||
-- end
|
chunkData:writeUByte(id)
|
||||||
-- chunkWriter:writeByte(packet.MapChunk)
|
end
|
||||||
-- chunkWriter:writeInt(0)
|
chunkWriter:writeByte(packet.MapChunk)
|
||||||
-- chunkWriter:writeShort(0)
|
chunkWriter:writeInt(0)
|
||||||
-- chunkWriter:writeInt(0)
|
chunkWriter:writeShort(0)
|
||||||
-- chunkWriter:writeByte(15)
|
chunkWriter:writeInt(0)
|
||||||
-- chunkWriter:writeByte(127)
|
chunkWriter:writeByte(15)
|
||||||
-- chunkWriter:writeByte(15)
|
chunkWriter:writeByte(127)
|
||||||
|
chunkWriter:writeByte(15)
|
||||||
|
|
||||||
-- local compressedData = libDeflate:CompressDeflate(chunkData:toBuffer())
|
local compressedData = libDeflate:CompressZlib(chunkData:toBuffer())
|
||||||
|
|
||||||
-- chunkWriter:writeInt(#compressedData)
|
chunkWriter:writeInt(#compressedData)
|
||||||
-- local chunkPacket = chunkWriter:toBuffer()
|
local chunkPacket = chunkWriter:toBuffer()
|
||||||
-- ws.send(chunkPacket .. compressedData, true)
|
ws.send(chunkPacket .. compressedData, true)
|
||||||
|
|
||||||
elseif packetId == packet.Handshake then
|
elseif packetId == packet.Handshake then
|
||||||
local username = reader:readString16()
|
local username = reader:readString16()
|
||||||
|
@ -115,6 +134,10 @@ local function doWebsocket()
|
||||||
ws.send(writer:toBuffer(), true)
|
ws.send(writer:toBuffer(), true)
|
||||||
elseif packetId == packet.Player then
|
elseif packetId == packet.Player then
|
||||||
onGround = reader:readBool()
|
onGround = reader:readBool()
|
||||||
|
elseif packetId == packet.PlayerLook then
|
||||||
|
playerYaw = reader:readFloat()
|
||||||
|
playerPitch = reader:readFloat()
|
||||||
|
onGround = reader:readBool()
|
||||||
elseif packetId == packet.PlayerPosition then
|
elseif packetId == packet.PlayerPosition then
|
||||||
playerX = reader:readDouble()
|
playerX = reader:readDouble()
|
||||||
playerY = reader:readDouble()
|
playerY = reader:readDouble()
|
||||||
|
@ -129,26 +152,60 @@ local function doWebsocket()
|
||||||
playerYaw = reader:readFloat()
|
playerYaw = reader:readFloat()
|
||||||
playerPitch = reader:readFloat()
|
playerPitch = reader:readFloat()
|
||||||
onGround = reader:readBool()
|
onGround = reader:readBool()
|
||||||
elseif packet == packet.PlayerLook then
|
end
|
||||||
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
1
mc/string_pack.lua
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue