Re: Blob.lua - binary serialization library
Posted: Wed Nov 28, 2018 8:40 am
MissDanish wrote: ↑Wed Nov 28, 2018 6:12 amCode: Select all
function saves_load() if love.filesystem.getInfo(saveListPath) ~= nil then saves = BlobReader(love.filesystem.read(saveListPath)) end end
If that's the issue, then just adding parentheses suffices:
Code: Select all
function saves_load()
if love.filesystem.getInfo(saveListPath) ~= nil then
saves = BlobReader((love.filesystem.read(saveListPath)))
end
end
Right, thanks.
Code: Select all
local BlobReader = require('BlobReader')
local r = BlobReader(love.filesystem.read('main.lua'))
print(r:raw(r:size()))
Not fully.yetneverdone wrote: ↑Fri Jan 25, 2019 1:50 am Hi, it says support for c data type. Would this be compatible for https://github.com/novemberisms/brinevector
?
Code: Select all
local v = Vector(23, 42)
-- serialize
local w = BlobWriter()
w:number(v.x):number(v.y)
-- deserialize
local r = BlobReader(w:tostring())
v = Vector(r:number(), r:number())
Code: Select all
local t = { Vector(23, 42) }
BlobWriter():write(t) -- error, can't serialize cdata in tables
Code: Select all
local v = Vector(23, 42)
local w = BlobWriter()
w:table(v) -- works
Code: Select all
local r = BlobReader(w:tostring())
local v = r:table()
print(v.x, v.y) -- works
print(v.length) -- does not work, v is a plain table, not a Vector
v = Vector(v.x, v.y) -- now v is a Vector again
print(v.length) -- works
Code: Select all
local v = Vector(23, 42)
local w = BlobWriter()
w:raw(v, ffi.sizeof(v))
local r = BlobReader(w:tostring())
local v2 = ffi.cast('brinevector*', r:raw(ffi.sizeof('brinevector')))
print(v2.x, v2.y, v2.length)
Thanks! Yeah, vector support is really needed since serialization is used for saving objects.grump wrote: ↑Fri Jan 25, 2019 8:50 amNot fully.yetneverdone wrote: ↑Fri Jan 25, 2019 1:50 am Hi, it says support for c data type. Would this be compatible for https://github.com/novemberisms/brinevector
?
This works:Serializing tables that contain Vectors is not supported. (I'm thinking about adding support for this)Code: Select all
local v = Vector(23, 42) -- serialize local w = BlobWriter() w:number(v.x):number(v.y) -- deserialize local r = BlobReader(w:tostring()) v = Vector(r:number(), r:number())
Serializing them directly with BlobWriter:table works, but that has drawbacks. BlobReader can't automatically recreate the Vector object.Code: Select all
local t = { Vector(23, 42) } BlobWriter():write(t) -- error, can't serialize cdata in tables
When you read this back, it becomes a plain table:Code: Select all
local v = Vector(23, 42) local w = BlobWriter() w:table(v) -- works
EDIT: this will also work:Code: Select all
local r = BlobReader(w:tostring()) local v = r:table() print(v.x, v.y) -- works print(v.length) -- does not work, v is a plain table, not a Vector v = Vector(v.x, v.y) -- now v is a Vector again print(v.length) -- works
I will add some code to simplify this a bit.Code: Select all
local v = Vector(23, 42) local w = BlobWriter() w:raw(v, ffi.sizeof(v)) local r = BlobReader(w:tostring()) local v2 = ffi.cast('brinevector*', r:raw(ffi.sizeof('brinevector'))) print(v2.x, v2.y, v2.length)
You can implement object saving without explicit ctype support if you use one of the methods I have shown in the previous comment.yetneverdone wrote: ↑Wed Jan 30, 2019 4:59 am Thanks! Yeah, vector support is really needed since serialization is used for saving objects.
cdata is supported. :raw can read and write cdata just fine.
Yeah but that means I have to manually iterate over my tables doing hacky things instead of just this as per the example:
Code: Select all
local blob = BlobWriter()
blob:write(data)
Can I pass the ctype information to the library somehow? I have the string definition, after all.