Glon
From GMod Wiki
GLON (Gmod Lua Object Notation) is a serialising module designed for compression and speed. It can serialise many types and data structures.
Library Functions
glon.encode
glon.decode
glon.Read
glon.Write
Info
GLON (An extension of "Lua Object Notation") is a serialisation module for Garry's Mod Lua allowing Lua scripters to create very compact storable strings. However, the resulting strings are no longer human-readable.
GLON can serialise the following LON types:
- 2: tables
- 3: arrays
- 4: false booleans
- 5: true booleans
- 6: numbers (NC)
- 7: strings
As well as the following non-LON types:
- 8: Vector (NC)
- 9: Angle (NC)
- 10: Entity (Can do players, vehicles, npcs, weapons and any other type of entity (-1 for null entity))
- 11: Player (By UserID)
- 12: CEffectData
- 13: ConVar (Not ClientConVar)
- 15: Color
- 254: A constant representing math.huge ("1.#INF")
- 255: References (ID of the table to use (for "local t = {} t.a=t"))
(NC) represents a type that is not compressed as it does not produce a significant different in network traffic.
Handling of references
GLON can serialise this data:
local t = {} t.a = t whatever = glon.encode(t)
And do this when decoding it:
local t = glon.decode(whatever) t.a.b = 5 print(t.b) -- prints 5!
by noticing that t.a and t.b refer to the same table. In this case, t.a is written as a reference to t, and the data will deserialise properly.
You can send any combination of data, as long as it contains only the types listed above. You can also serialize tables as keys.
Tables using only numerical keys do not have their keys serialised.
Notes
- The encode and decode functions should be called using pcall, as errors are not handled within.
"Infinite Loop Detected!" error
This error is due to Garry's anti-infinite-loop detection code. For performance reasons, recursion and looping is used extensively within GLON and it can conflict with Garry's code.
To fix it, put this code around your glon.encode and/or glon.decode calls:
local h_a, h_b, h_c = debug.gethook() debug.sethook() -- glon call here debug.sethook(h_a, h_b, h_c)
Online Viewable Source
http://codepad.org/wodTxqml
http://codepad.org/SIO9mmjB - Added handlers for NPC, Vehicle and Weapon.
Note: While GLON is included in GMod, you will have to use the require function to enable it.
Example Output
require"glon" local t = { ["zomg\1!"] = LocalPlayer(), { "An", "Array!", }, [666] = GetConVar("something"), } local s, sa, c, b = glon.encode(t), "" for i = 1,s:len() do c = string.sub(s, i, i) b = string.byte(c) if b < 33 or b > 125 then sa = sa.."[\\"..b.."]" else sa = sa..c end end print(sa) -- Output: -- ([\NUMBER] means the special char with NUMBER's ID) -- [\2][\7]zomg[\1][\11]2[\1][\6]1[\1][\3][\7]An[\1][\7]Array![\1][\1][\6]666[\1][\13]something[\1][\1] -- Dissection: -- \2 Start of table -- \7 Start of string (key) -- z -- o -- m -- g -- \1 End of string -- \11 Start of player (value) -- 2 My Entity Index -- \1 End of player -- \6 Start of number (key) -- 1 -- \1 End of number -- \3 Start of array (value) -- \7 Start of string -- A -- n -- \1 End of string -- \7 Start of string -- A -- r -- r -- a -- y -- ! -- \1 End of string -- \1 End of array -- \6 Start of number (key) -- 6 -- 6 -- 6 -- \1 -- \13 Start of ConVar (value) -- s -- o -- m -- e -- t -- h -- i -- n -- g -- \1 End on ConVar -- \1 End of table