BufferConverter | An easy way to turn anything into a buffer (and back!)

BufferConverter is a simple module to convert regular datatypes into buffers.

Documentation:

SerializedBuffer<T> Type

All this type does is store the datatype that its storing. Very simple.

ReadOptions & WriteOptions

The ReadOptions and WriteOptions type both determine how BufferConverter will serialize or deserialize certain datatypes.

Here are what each option does:

  1. errorOnException :: boolean?

    • This option determines wether or not the module with throw an error or a warn if it encounters something wrong.
      Useful if you don’t want the entire thing to stop

  2. keys :: boolean?

    • This option determines wether or not keys will be written/read, depending on wether or not youre using this in a WriteOptions or a ReadOptions object.
      Useful if you want to save space by not serializing keys, and just want to write arrays.

  3. write/readInstanceAsCopy :: boolean?

    • This option determines wether or not Instances will be fully serialized with all of their properties and children, or just their unique id reference.
      Useful if you want to use BufferConverter for, say, a base-saving system instead of a networking system.

  4. numbersAs :: "(u8"|"i8"|"u16"|"i16"|"u32"|"i32"|"f32"|"f64")?

    • This option determines how numbers will be written and read as.
      Useful if you don’t wanna waste space storing a few numbers.
      Note that this option applies to ALL numbers written and read, use carefully!

Serializable Datatypes & Their Respective Sizes
  1. Table : Size = (amount of keys and values (or just the amount of values if you’ve set keys to false) + sum of all the sizes of each key and value (or just the sum of all the values if you’ve set keys to false) + 1 (null terminator)) bytes

  2. Number : Size = (4 bytes default (as i32), variable if using the numbersAs option) bytes

  3. String : Size = (amount of letters + 1 (null terminator)) bytes

  4. Boolean : Size = 1 bytes

  5. CFrame : Size = 22 bytes

  6. Vector3 : Size = 12 bytes

  7. Vector3int16 : Size = 6 bytes

  8. Vector2 : Size = 8 bytes

  9. Vector2int16 : Size = 4 bytes

  10. Color3 : Size = 3 bytes

  11. EnumItem: Size = 3 bytes

  12. Instance : Size = (if not writeInstanceAsCopy then 2 else too long, read the code here:)

Functions

BufferConverter has 2 functions, One for serializing, one for deserializing.

  1. BufferConverter.Serialize<T>(item: T, options: WriteOptions): SerializedBuffer<T>

    • This function takes in any serializable datatype T (see serializable datatypes), and returns it as a buffer (as a SerializedBuffer to store the datatype so that it can be returned later by BufferConverter.Deserialize()).

  2. BufferConverter.Deserialize<T>(packet: SerializedBuffer<T>, options: ReadOptions): T

    • This function takes in a SerializedBuffer, and returns the datatype it’s storing, T.

And that’s basically it. No need to get complicated here!

Example Usage
local Converter = require(path_to_converter)

local tbl = {
Hi = 10,
Lol = "Foo"
}

local packet = Converter.Serialize(tbl)

print(Converter.Deserialize(packet)) -- the table earlier

Serializing parts and children:

local Converter = require(game.ReplicatedStorage.Modules.Utility.Converter)

local target = workspace.Target

-- Should clone 3 times
local buf = Converter.Serialize({
	target,
	target,
	target
}, {writeInstanceAsCopy = true})

local new = Converter.Deserialize(buf, {readInstanceAsCopy = true})

for _, v in new do
	v.Parent = workspace
end

Result (spread out so its easier to see that Target has been cloned 3 times with all of its children):

Serializing More Instances

For reasons of being lazy :stuck_out_tongue:, I didn’t finish the Instance full copy serializer. You can add your own by editing the Properties module here:

In the getProperties function, just simply add more elseif statements. If you need custom applier functions, simply prefix the key with METHOD_ and it will attempt to find the method name in the methods table, and call it passing the instance and the value.

Download BufferConverter here! →
Converter.rbxm (9.6 KB)

If you find any bugs/errors, let me know!

And if you haven’t already, check out my other module SimpleZone! →
SimpleZone: A simpler and newer alternative to ZonePlus - #14 by athar_adv

9 Likes

Currently working on some bugs I found, please do not download the module before I fix them :sweat_smile:

Fixed! You can use the module now :slight_smile:

Very useful, easy, and lightweight. I used it for my Network module.

2 Likes