For anybody interested, I’ve created a few custom SeraType
s to use.
Read ‘More Info’ to see important behavior
More Info
When using these types, it’s important that you know the following:
Dictionaries are defined as key-value pairs
Arrays are defined like a stack; a list of items with no gaps
Dictionaries store the following:
- Value count (table length)
- Index/Key
- Value
Arrays store the following:
- Value count (table length)
- Value
By default, all of the values are stored at 8-bit unsigned integers, this can be configured by editing the code.
Dictionary
local u8_Ser, u8_Des = module.Uint8.Ser, module.Uint8.Des
--Index of u8, value of u8
--Intended for small, variable-size dictionaries with keys.
--Has a base cost of 1 byte to store value count
--Each value + key takes 2 bytes
module.u8_u8_dict = table.freeze({
Ser = function(b: buffer, offset: number, value: {number}): number
local curr_offset
do --Store the inner value count
local value_count = 0
for _ in value do
value_count += 1
end
--Write the value count
curr_offset = u8_Ser(b, offset, value_count)
end
--Store the inner values
for i, v in value do
curr_offset = u8_Ser(b, curr_offset, i)
curr_offset = u8_Ser(b, curr_offset, v)
end
return curr_offset --Return the final offset after writing all values
end,
Des = function(b: buffer, offset: number): ({number}, number)
local value_count, curr_offset = u8_Des(b, offset)
local t = {}
for count = 1, value_count do
local i, v
i, curr_offset = u8_Des(b, curr_offset)
v, curr_offset = u8_Des(b, curr_offset)
t[i] = v
end
return t, curr_offset --Return the final offset after reading all values
end,
})
Array
--Index of u8, value of u8
--Intended for small, variable-size arrays
--Has a base cost of 1 byte to store value count
--Each value takes 1 byte
module.u8_u8_array = table.freeze({
Ser = function(b: buffer, offset: number, value: {number}): number
local curr_offset
do --Store the inner value count
local value_count = 0
for i in value do
value_count += 1
if value_count ~= i then
error("Invalid array")
end
end
--Write the value count
curr_offset = u8_Ser(b, offset, value_count)
end
--Store the inner values
for _, v in value do
curr_offset = u8_Ser(b, curr_offset, v)
end
return curr_offset --Return the final offset after writing all values
end,
Des = function(b: buffer, offset: number): ({number}, number)
local value_count, curr_offset = u8_Des(b, offset)
local t = {}
for count = 1, value_count do
local v
v, curr_offset = u8_Des(b, curr_offset)
t[count] = v
end
return t, curr_offset --Return the final offset after reading all values
end,
})
Array (Converted to f32 values)
local u8_Ser, u8_Des = module.Uint8.Ser, module.Uint8.Des
local f32_Ser, f32_Des = module.Float32.Ser, module.Float32.Des
--Index of u8, value of f32
--Intended for small, variable-size arrays
--Has a base cost of 1 byte to store value count
--Each value takes 4 bytes
module.u8_f32_array = table.freeze({
Ser = function(b: buffer, offset: number, value: {number}): number
local curr_offset
do --Store the inner value count
local value_count = 0
for i in value do
value_count += 1
if value_count ~= i then
error("Invalid array")
end
end
--Write the value count
curr_offset = u8_Ser(b, offset, value_count)
end
--Store the inner values
for _, v in value do
curr_offset = f32_Ser(b, curr_offset, v)
end
return curr_offset --Return the final offset after writing all values
end,
Des = function(b: buffer, offset: number): ({number}, number)
local value_count, curr_offset = u8_Des(b, offset)
local t = {}
for count = 1, value_count do
local v
v, curr_offset = f32_Des(b, curr_offset)
t[count] = v
end
return t, curr_offset --Return the final offset after reading all values
end,
})