WARNING
This module relies on the new buffer type which is not yet available for use in live places!
Introduction
Hello! I am releasing my compression utility which is inspired by the Source engine’s network data tables. I wrote this module to handle compression & decompression of entity states sent over the network. It has built in support for delta compression, bytes, shorts, integers and vectors.
API
Pack.Types
Contains all supported data types, both signed and unsigned.
- Float
- Short
- Double
- Vector
- String
- Integer
- Boolean
- UnsignedByte
- UnsignedShort
- UnsignedInteger
type BaseValues = number | Vector3 | boolean | string
A type which accepts all values supported by the utility.
function Always (BaseValues) -> (() -> (BaseValues))
Acts as a wrapper for values that will always be included into the packed result regardless of whether they changed or not.
function Pack.new(Layout: {[string]: CompressionType) -> Packer
WARNING
Layout can only contain a maximum of 53 keys
Takes in a dictionary Layout
where each key is a string and each value is one of the types available in Pack.Types
function Packer.Pack(Values: {[string]: BaseValues}, PreviousValues: {[string]: BaseValues}?) -> buffer
Takes in Values
and an optional PreviousValues
in the layout of Layout
and returns a buffer used for unpacking. If no PreviousValues
is provided it will assume that all values have changed.
function Packer.Unpack(Stream: buffer, Destination: {[string]: BaseValues}) -> ()
Takes in the result of Packer.Pack
and unpacks it into the Destination
table, switching old values with the new ones.
Performance
Layout used for benchmarks
local Packer = Pack.new({
Always = Types.UnsignedByte,
String = Types.String,
UnsignedShort = Types.UnsignedShort,
Boolean = Types.Boolean,
Vector = Types.Vector
})
Benchmark | Pack | Pack x100 | Unpack | Unpack x100
--------------------------------------------------------------
Fastest Time | 1 μs | 94 μs | 1.29 μs | 136 μs
Average Time | 1.21 μs | 115 μs | 1.63 μs | 157 μs
50th Percentile | 1.09 μs | 110 μs | 1.39 μs | 155 μs
90th Percentile | 1.59 μs | 144 μs | 2.40 μs | 183 μs
95th Percentile | 4.50 μs | 190 μs | 7.49 μs | 230 μs
Download
Example.rbxl (60.5 KB)
Pack.lua (13.2 KB)
License
License
Copyright 2023 Axen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.