Guest User

Untitled

a guest
Jan 11th, 2013
6,231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.82 KB | None | 0 0
  1. -- GLON: Garry's Mod Lua Object Notation
  2. -- A extension of LON: Lua Object Notation
  3. -- Made entirely by Deco Da Man
  4. -- Types:
  5.     -- 2: table
  6.     -- 3: array
  7.     -- 4: fasle boolean
  8.     -- 5: true boolean
  9.     -- 6: number (NOT COMPRESSED, it isn't worth it)
  10.     -- 7: string
  11.     ---- non-LON types start here!
  12.     -- 8: Vector (NOT COMPRESSED, it isn't worth it)
  13.     -- 9: Angle (NOT COMPRESSED, it isn't worth it)
  14.     -- 10: Entity (Can do players, vehicles, npcs, weapons and any other type of entity (-1 for null entity))
  15.     -- 11: Player (By UserID)
  16.     -- 12: CEffectData
  17.     -- 13: ConVar (Not ClientConVar)
  18.     -- 15: Color
  19.     -- 254: The number equal to -math.huge (tostring(math.huge) == "-1.#INF")
  20.     -- 254: The number equal to math.huge (tostring(math.huge) == "1.#INF")
  21.     -- 255: reference (Sends the ID of the table to use (for "local t = {} t.a=t"))
  22. local pairs = pairs
  23. local type = type
  24. local string = string
  25. local math = math
  26. local tostring = tostring
  27. local IsValid = IsValid
  28. local error = error
  29. local print = print
  30. local setmetatable = setmetatable
  31. local Vector = Vector
  32. local Angle = Angle
  33. local Entity = Entity
  34. local EffectData = EffectData
  35. local GetConVar = GetConVar
  36. local tonumber = tonumber
  37. local player = player
  38. local IsValid = IsValid
  39. module("glon")
  40. local encode_types
  41. local decode_types
  42. local function InDataEscape(s)
  43.     s = string.gsub(s, "([\1\2])", "\2%1")
  44.     s = string.gsub(s, "%z", "\2\3")
  45.     s = string.gsub(s, "\"", "\4") -- escape the " character to simplify client commands
  46.     return s
  47. end
  48. encode_types = {
  49.     ["nil"] = {nil, function()
  50.         return "", nil
  51.     end},
  52.     table = {2, function(o, rtabs)
  53.         for k,v in pairs(rtabs) do
  54.             if v == o then
  55.                 return tostring(k).."\1", 255
  56.             end
  57.         end
  58.         rtabs[#rtabs+1] = o
  59.         local is_array = true
  60.         local i = 0
  61.         for k,v in pairs(o) do
  62.             i = i + 1
  63.             if k ~= i or type(k) ~= "number" or math.floor(k) ~= k then
  64.                 is_array = false
  65.             break end
  66.         end
  67.         local s = ""
  68.         for k,v in pairs(o) do
  69.        
  70.             if ( !encode_types[type(v)] ) then continue end
  71.        
  72.             if not is_array then
  73.                 s = s..Write(k, rtabs)
  74.             end
  75.             s = s..Write(v, rtabs)
  76.         end
  77.         return s.."\1", is_array and 3
  78.     end},
  79.     boolean = {4, function(o)
  80.         return "", o and 5
  81.     end},
  82.     number = {6, function(o)
  83.         o = o == 0 and "" or o
  84.         return o == ((o == math.huge or o == -math.huge) and "") or tostring(o).."\1", (o == math.huge and 254) or (o == -math.huge and 253)
  85.     end},
  86.     string = {7, function(o)
  87.         return InDataEscape(o).."\1"
  88.     end},
  89.     -- non-LON types start here!
  90.     Vector = {8, function(o)
  91.         return o.x.."\1"..o.y.."\1"..o.z.."\1"
  92.     end},
  93.     Angle = {9, function(o)
  94.         return o.p.."\1"..o.y.."\1"..o.r.."\1"
  95.     end},
  96.     Entity = {10, function(o)
  97.         return (IsValid(o) and o:EntIndex() or -1).."\1"
  98.     end},
  99.     NPC = {10, function(o)
  100.         return (IsValid(o) and o:EntIndex() or -1).."\1"
  101.     end},
  102.     Weapon = {10, function(o)
  103.         return (IsValid(o) and o:EntIndex() or -1).."\1"
  104.     end},
  105.     Player = {11, function(o)
  106.         return o:EntIndex().."\1"
  107.     end},
  108.     CEffectData = {12, function(o, rtabs)
  109.         local t = {}
  110.         if o:GetAngles() ~= Angle(0,0,0) then
  111.             t.a = o:GetAngles()
  112.         end
  113.         if o:GetAttachment() ~= 0 then
  114.             t.h = o:GetAttachment()
  115.         end
  116.         if o:GetEntity():IsValid() then
  117.             t.e = o:GetEntity()
  118.         end
  119.         if o:GetMagnitude() ~= 0 then
  120.             t.m = o:GetMagnitude()
  121.         end
  122.         if o:GetNormal() ~= Vector(0,0,0) then
  123.             t.n = o:GetNormal()
  124.         end
  125.         if o:GetOrigin() ~= Vector(0,0,0) then
  126.             t.o = o:GetOrigin()
  127.         end
  128.         if o:GetRadius() ~= 0 then
  129.             t.r = o:GetRadius()
  130.         end
  131.         if o:GetScale() ~= 0 then
  132.             t.c = o:GetScale()
  133.         end
  134.         if o:GetStart() ~= 0 then
  135.             t.s = o:GetStart()
  136.         end
  137.         if o:GetSurfaceProp() ~= 0 then
  138.             t.p = o:GetSurfaceProp()
  139.         end
  140.         return encode_types.table[2](t, rtabs)
  141.     end},
  142.     ConVar = {13, function(o)
  143.         return InDataEscape(o:GetName()).."\1"
  144.     end},
  145.     PhysObj = {14, function(o)
  146.         local parent, obj, id = o:GetEntity()
  147.         for i = 1, parent:GetPhysicsObjectCount() do
  148.             obj = parent:GetPhysicsObjectNum()
  149.             if obj == o then
  150.                 id = i
  151.             break end
  152.         end
  153.         return parent:EntIndex().."\1"..id.."\1"
  154.     end},
  155.     Color = {15, function(o)
  156.         return o.r.."\1"..o.g.."\1"..o.b.."\1"..o.a.."\1"
  157.     end},
  158. }
  159. function Write(data, rtabs)
  160.     local t = encode_types[type(data)]
  161.     if t then
  162.         local data, id_override = t[2](data, rtabs)
  163.         local char = id_override or t[1] or ""
  164.         if char ~= "" then char = string.char(char) end
  165.         return char..(data or "")
  166.     else
  167.         error(string.format("Tried to write unwriteable type: %s",
  168.             type(data)))
  169.     end
  170. end
  171. local CEffectDataTranslation = {
  172.     a = "Angle",
  173.     h = "Attachment",
  174.     e = "Entity",
  175.     m = "Magnitude",
  176.     n = "Normal",
  177.     o = "Origin",
  178.     r = "Radius",
  179.     c = "Scale",
  180.     s = "Start",
  181.     p = "SurfaceProp",
  182. }
  183. decode_types = {
  184.     -- \2\6omg\1\6omgavalue\1\1
  185.     [2  ] = function(reader, rtabs) -- table
  186.         local t, c, pos = {}, reader:Next()
  187.         rtabs[#rtabs+1] = t
  188.         local stage = false
  189.         local key
  190.         while true do
  191.             c, pos = reader:Peek()
  192.             if c == "\1" then
  193.                 if stage then
  194.                     error(string.format("Expected value to match key at %s! (Got EO Table)",
  195.                         pos))
  196.                 else
  197.                     reader:Next()
  198.                     return t
  199.                 end
  200.             else
  201.                 if stage then
  202.                     t[key] = Read(reader, rtabs)
  203.                 else
  204.                     key = Read(reader, rtabs)
  205.                 end
  206.                 stage = not stage
  207.             end
  208.         end
  209.     end,
  210.     [3  ] = function(reader, rtabs) -- array
  211.         local t, i, c, pos = {}, 1, reader:Next()
  212.         rtabs[#rtabs+1] = t
  213.         while true do
  214.             c, pos = reader:Peek()
  215.             if c == "\1" then
  216.                 reader:Next()
  217.                 return t
  218.             else
  219.                 t[i] = Read(reader, rtabs)
  220.                 i = i+1
  221.             end
  222.         end
  223.     end,
  224.     [4  ] = function(reader) -- false boolean
  225.         reader:Next()
  226.         return false
  227.     end,
  228.     [5  ] = function(reader) -- true boolean
  229.         reader:Next()
  230.         return true
  231.     end,
  232.     [6  ] = function(reader) -- number
  233.         local s, c, pos, e = "", reader:Next()
  234.         while true do
  235.             c = reader:Next()
  236.             if not c then
  237.                 error(string.format("Expected \1 to end number at %s! (Got EOF!)",
  238.                     pos))
  239.             elseif c == "\1" then
  240.                 break
  241.             else
  242.                 s = s..c
  243.             end
  244.         end
  245.         if s == "" then s = "0" end
  246.         local n = tonumber(s)
  247.         if not n then
  248.             error(string.format("Invalid number at %s! (%q)",
  249.                 pos, s))
  250.         end
  251.         return n
  252.     end,
  253.     [7  ] = function(reader) -- string
  254.         local s, c, pos, e = "", reader:Next()
  255.         while true do
  256.             c = reader:Next()
  257.             if not c then
  258.                 error(string.format("Expected unescaped \1 to end string at position %s! (Got EOF)",
  259.                     pos))
  260.             elseif e then
  261.                 if c == "\3" then
  262.                     s = s.."\0"
  263.                 else
  264.                     s = s..c
  265.                 end
  266.                 e = false
  267.             elseif c == "\2" then
  268.                 e = true
  269.             elseif c == "\1" then
  270.                 s = string.gsub(s, "\4", "\"") -- unescape quotes
  271.                 return s
  272.             else
  273.                 s = s..c
  274.             end
  275.         end
  276.     end,
  277.     [8  ] = function(reader) -- Vector
  278.         local x = decode_types[6](reader)
  279.         reader:StepBack()
  280.         local y = decode_types[6](reader)
  281.         reader:StepBack()
  282.         local z = decode_types[6](reader)
  283.         return Vector(x, y, z)
  284.     end,
  285.     [9  ] = function(reader) -- Angle
  286.         local p = decode_types[6](reader)
  287.         reader:StepBack()
  288.         local y = decode_types[6](reader)
  289.         reader:StepBack()
  290.         local r = decode_types[6](reader)
  291.         return Angle(p, y, r)
  292.     end,
  293.     [10 ] = function(reader) -- Entity
  294.         return Entity(decode_types[6](reader))
  295.     end,
  296.     [11 ] = function(reader) -- Player
  297.         local num = decode_types[6](reader)
  298.         return player.GetByID(num)
  299.     end,
  300.     [12 ] = function(reader, rtabs) -- CEffectData
  301.         local t = decode_types[2](reader, rtabs)
  302.         local d = EffectData()
  303.         for k,v in pairs(t) do
  304.             d["Set"..CEffectDataTranslation[k]](d, v)
  305.         end
  306.         return d
  307.     end,
  308.     [13 ] = function(reader) -- ConVar
  309.         return GetConVar(decode_types[7](reader))
  310.     end,
  311.     [14 ] = function(reader) -- PhysicsObject
  312.    
  313.         local ent = Entity(decode_types[6](reader))
  314.         local bone = decode_types[6](reader)
  315.        
  316.         if ( !IsValid( ent ) ) then return nil end;
  317.         return ent:GetPhysicsObjectNum( bone )
  318.     end,
  319.     [15 ] = function(reader) -- Color
  320.         local r = decode_types[6](reader)
  321.         reader:StepBack()
  322.         local g = decode_types[6](reader)
  323.         reader:StepBack()
  324.         local b = decode_types[6](reader)
  325.         reader:StepBack()
  326.         local a = decode_types[6](reader)
  327.         return Color(r, g, b, a)
  328.     end,
  329.     [253] = function(reader) -- -math.huge
  330.         reader:Next()
  331.         return -math.huge
  332.     end,
  333.     [254] = function(reader) -- math.huge
  334.         reader:Next()
  335.         return math.huge
  336.     end,
  337.     [255] = function(reader, rtabs) -- Reference
  338.         return rtabs[decode_types[6](reader) - 1]
  339.     end,
  340. }
  341. function Read(reader, rtabs)
  342.     local t, pos = reader:Peek()
  343.     if not t then
  344.         error(string.format("Expected type ID at %s! (Got EOF)",
  345.             pos))
  346.     else
  347.         local dt = decode_types[string.byte(t)]
  348.         if not dt then
  349.             error(string.format("Unknown type ID, %s!",
  350.                 string.byte(t)))
  351.         else
  352.             return dt(reader, rtabs or {0})
  353.         end
  354.     end
  355. end
  356. local reader_meta = {}
  357. reader_meta.__index = reader_meta
  358. function reader_meta:Next()
  359.     self.i = self.i+1
  360.     self.c = string.sub(self.s, self.i, self.i)
  361.     if self.c == "" then self.c = nil end
  362.     self.p = string.sub(self.s, self.i+1, self.i+1)
  363.     if self.p == "" then self.p = nil end
  364.     return self.c, self.i
  365. end
  366. function reader_meta:StepBack()
  367.     self.i = self.i-1
  368.     self.c = string.sub(self.s, self.i, self.i)
  369.     if self.c == "" then self.c = nil end
  370.     self.p = string.sub(self.s, self.i+1, self.i+1)
  371.     if self.p == "" then self.p = nil end
  372.     return self.c, self.i
  373. end
  374. function reader_meta:Peek()
  375.     return self.p, self.i+1
  376. end
  377. function decode(data)
  378.     if type(data) == "nil" then
  379.         return nil
  380.     elseif type(data) ~= "string" then
  381.         error(string.format("Expected string to decode! (Got type %s)",
  382.             type(data)
  383.         ))
  384.     elseif data:len() == 0 then
  385.         return nil
  386.     end
  387.    
  388.    
  389.     return Read(setmetatable({
  390.         s = data,
  391.         i = 0,
  392.         c = string.sub(data, 0, 0),
  393.         p = string.sub(data, 1, 1),
  394.     }, reader_meta), {})
  395. end
  396. function encode(data)
  397.     return Write(data, {0}) -- to use the first key, to prevent it from interfereing with \1s. You can have up to 254 unique tables (including arrays)
  398. end
Advertisement
Add Comment
Please, Sign In to add comment