Skip to content

Commit

Permalink
NBT test (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruin0x11 committed Feb 25, 2021
1 parent cf4640d commit af1749a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 26 deletions.
40 changes: 14 additions & 26 deletions src/scratch/2021-02-25_13-40-29.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,66 +13,54 @@ function Thing:serialize_nbt()
return {
x = nbt.newInt(self.x),
y = nbt.newInt(self.y),
t = nbt.newString(self.t),
t = nbt.newArbitraryTable(self.t),
}
end

function Thing:deserialize_nbt(t)
self.x = t["x"]:getInteger()
self.y = t["y"]:getInteger()
self.t = t["t"]:getString()
self.t = t["t"]:getArbitraryTable()
end

function Thing:__tostring()
return ("%d %d! %s"):format(self.x, self.y, inspect(self.t))
end

do
local t = Thing:new(1, 54, { dood = "hey", asd = 42.222222 })
print(inspect(t))
print(t)

local values = t:serialize_nbt()
local compound = nbt.newCompound(values, "thing")

-- ==========

local instance = Thing:new()
instance:deserialize_nbt(compound:getValue())
print(inspect(instance))
print(instance)
end

local Parent = class.class("Parent")
Parent.__serial_id = "Parent"

function Parent:new(p1, p2)
function Parent:init(p1, p2)
self.p1 = p1
self.p2 = p2
end

function Parent:serialize_nbt()
return {
p1 = self.p1:serialize_nbt(),
p2 = self.p2:serialize_nbt()
p1 = nbt.newClassCompound(self.p1),
p2 = nbt.newClassCompound(self.p2)
}
end

function Parent:deserialize_nbt(t)
self.p1 = t["p1"]:getClassCompound()
self.p2 = t["p2"]:getClassCompound()
end

nbt.registerClass(Thing)
nbt.registerClass(Parent)

do
local t = Thing:new(1, 54, { dood = "hey", asd = 42.222222 })
local p = Parent:new(p, p)
local p = Parent:new(t, t)
print(inspect(p))

local values = p:serialize_nbt()
local compound = nbt.newCompound(values, "thing")
local compound = nbt.newClassCompound(p, "parent")

-- ==========

local instance = Thing:new()
instance:deserialize_nbt(compound:getValue())
local instance = compound:getClassCompound()
print(inspect(instance))
print(instance)
print(instance.p1 == instance.p2)
end
58 changes: 58 additions & 0 deletions src/thirdparty/nbt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ local nbt = {
_LICENSE = "zLib"
}

local registry = {}

-- Tag constants
local TAG_END = 0 nbt.TAG_END = 0
local TAG_BYTE = 1 nbt.TAG_BYTE = 1
Expand Down Expand Up @@ -1214,4 +1216,60 @@ function nbt.decode(input, preservemode)

end

function nbt.registerClass(class)
assert(class.__serial_id)
assert(class.serialize_nbt)
assert(class.deserialize_nbt)
print("REG", class.__serial_id)
registry[class.__serial_id] = class
end

function nbt.newClassCompound(value, name)
local mt = assert(getmetatable(value))
assert(mt.__name)
assert(mt.__serial_id)
local reg = registry[mt.__serial_id]

local values = reg.serialize_nbt(value)

assert(values.__serial_id == nil, "__serial_id is a reserved field")
values.__serial_id = mt.__serial_id
return nbt.newCompound(values, name)
end

function TagClass:getClassCompound()
if
self._type == TAG_COMPOUND
then
local serial_id = assert(self:getValue().__serial_id):getString()
local klass = assert(registry[serial_id])
local instance = {__class=klass, __memoized={}}
setmetatable(instance, klass)

instance:deserialize_nbt(self:getValue())

return instance
end

error("attempt to get class compound of invalid type")
end

function nbt.newArbitraryTable(value, name)
local binser = require("thirdparty.binser")
assert(type(value) == "table")
local serialized = binser.serialize(value)
return nbt.newString(serialized, name)
end

function TagClass:getArbitraryTable()
local binser = require("thirdparty.binser")
if
self._type == TAG_STRING
then
return binser.deserialize(self:getValue())
end

error("attempt to get arbitrary table of invalid type")
end

return nbt

0 comments on commit af1749a

Please sign in to comment.