Skip to content

Commit

Permalink
Use von for gmod
Browse files Browse the repository at this point in the history
It was overriding von modules by other addons(wiremod) and causing
issues.

Fix #101
  • Loading branch information
Xandaros committed Sep 29, 2014
1 parent ca4e52d commit 3d3cda7
Showing 1 changed file with 214 additions and 10 deletions.
224 changes: 214 additions & 10 deletions lua/autorun/server/von.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--[[
Copyright 2012 Alexandru-Mihai Maftei
--[[ vON 1.1.1
Copyright 2012-2013 Alexandru-Mihai Maftei
aka Vercas
You may use this for any purpose as long as:
Expand All @@ -10,7 +11,10 @@
If you modify the code for any purpose, the above obligations still apply.
Instead of copying this code over for sharing, rather use the link:
https://dl.dropbox.com/u/1217587/GMod/Lua/von.lua
https://dl.dropbox.com/u/1217587/GMod/Lua/von%20for%20GMOD.lua
The author may not be held responsible for any damage or losses directly or indirectly caused by
the use of vON.
If you disagree with the above, don't use the code.
Expand All @@ -21,6 +25,7 @@
Suggested an excellent new way of deserializing strings.
Lead me to finding an extreme flaw in string parsing.
- pennerlord Provided some performance tests to help me improve the code.
- Chessnut Reported bug with handling of nil values when deserializing array components.
-----------------------------------------------------------------------------------------------------------------------------
Expand All @@ -30,18 +35,22 @@
- boolean
- string
- nil
- Entity
- Player
- Vector
- Angle
These are the native Lua types one would normally serialize.
+ Some very common GMod Lua types.
-----------------------------------------------------------------------------------------------------------------------------
New in this version:
- Added errors on (de)serialization when passwing the wrong data type.
- Removed two redundant arguments in the serialization functable.
- Fixed problem with handling of nils in array tables.
--]]

local _deserialize, _serialize, _d_meta, _s_meta, d_findVariable, s_anyVariable
local sub, gsub, find, insert, concat, error, tonumber, tostring, type, next = string.sub, string.gsub, string.find, table.insert, table.concat, error, tonumber, tostring, type, next
local sub, gsub, find, insert, concat, error, tonumber, tostring, type, next, getEnt, getPly = string.sub, string.gsub, string.find, table.insert, table.concat, error, tonumber, tostring, type, next, Entity, player.GetByID

-- This is kept away from the table for speed.
function d_findVariable(s, i, len, lastType)
Expand Down Expand Up @@ -88,6 +97,26 @@ function d_findVariable(s, i, len, lastType)
lastType = "table"
typeRead = true

-- n means a number will follow. Base 10... :C
elseif c == "e" then
lastType = "Entity"
typeRead = true

-- n means a number will follow. Base 10... :C
elseif c == "p" then
lastType = "Player"
typeRead = true

-- n means a number will follow. Base 10... :C
elseif c == "v" then
lastType = "Vector"
typeRead = true

-- n means a number will follow. Base 10... :C
elseif c == "a" then
lastType = "Angle"
typeRead = true

-- If no type has been found, attempt to deserialize the last type read.
elseif lastType then
val, i = _deserialize[lastType](s, i, len)
Expand Down Expand Up @@ -126,7 +155,7 @@ _deserialize = {
-- Well, tables are very loose...
-- The first table doesn't have to begin and end with { and }.
["table"] = function(s, i, len, unnecessaryEnd)
local ret, numeric, i, c, lastType, val, ind, expectValue, key = {}, true, i or 1
local ret, numeric, i, c, lastType, val, ind, expectValue, key = {}, true, i or 1, nil, nil, nil, 1
-- Locals, locals, locals, locals, locals, locals, locals, locals and locals.

-- Keep looping.
Expand All @@ -144,7 +173,7 @@ _deserialize = {
end

-- Cache the character.
c = sub(s,i,i)
c = sub(s, i, i)
--print(i, "table char:", c, tostring(unnecessaryEnd))

-- If it's the end of a table definition, return.
Expand All @@ -164,7 +193,9 @@ _deserialize = {
-- Find a variable and it's value
val, i, lastType = d_findVariable(s, i, len, lastType)
-- Add it to the table.
ret[#ret + 1] = val
ret[ind] = val

ind = ind + 1

-- Otherwise, if it's the key:value component...
else
Expand Down Expand Up @@ -259,6 +290,103 @@ _deserialize = {
error("vON: String definition started... Found no end.")
end
end
end,


-- Entities are stored simply by the ID. They're meant to be transfered, not stored anyway.
-- Exactly like a number definition, except it begins with "e".
["Entity"] = function(s, i, len)
local i, a = i or 1
-- Locals, locals, locals, locals

a = find(s, "[;:}~]", i)

if a then
return getEnt(tonumber(sub(s, i, a - 1))), a - 1
end

error("vON: Entity ID definition started... Found no end.")
end,


-- Exactly like a entity definition, except it begins with "p".
["Player"] = function(s, i, len)
local i, a = i or 1
-- Locals, locals, locals, locals

a = find(s, "[;:}~]", i)

if a then
return getEnt(tonumber(sub(s, i, a - 1))), a - 1
end

error("vON: Player ID definition started... Found no end.")
end,


-- A pair of 3 numbers separated by a comma (,).
["Vector"] = function(s, i, len)
local i, a, x, y, z = i or 1
-- Locals, locals, locals, locals

a = find(s, ",", i)

if a then
x = tonumber(sub(s, i, a - 1))
i = a + 1
end

a = find(s, ",", i)

if a then
y = tonumber(sub(s, i, a - 1))
i = a + 1
end

a = find(s, "[;:}~]", i)

if a then
z = tonumber(sub(s, i, a - 1))
end

if x and y and z then
return Vector(x, y, z), a - 1
end

error("vON: Vector definition started... Found no end.")
end,


-- A pair of 3 numbers separated by a comma (,).
["Angle"] = function(s, i, len)
local i, a, p, y, r = i or 1
-- Locals, locals, locals, locals

a = find(s, ",", i)

if a then
p = tonumber(sub(s, i, a - 1))
i = a + 1
end

a = find(s, ",", i)

if a then
y = tonumber(sub(s, i, a - 1))
i = a + 1
end

a = find(s, "[;:}~]", i)

if a then
r = tonumber(sub(s, i, a - 1))
end

if p and y and r then
return Angle(p, y, r), a - 1
end

error("vON: Angle definition started... Found no end.")
end
}

Expand Down Expand Up @@ -375,6 +503,82 @@ _serialize = {
-- Fastest.
["nil"] = function(data, mustInitiate, isNumeric, isKey, isLast)
return "@"
end,


-- Same as numbers, except they start with "e" instead of "n".
["Entity"] = function(data, mustInitiate, isNumeric, isKey, isLast)
data = data:EntIndex()

if mustInitiate then
if isKey or isLast then
return "e"..data
else
return "e"..data..";"
end
end

if isKey or isLast then
return "e"..data
else
return "e"..data..";"
end
end,


-- Same as entities, except they start with "e" instead of "n".
["Player"] = function(data, mustInitiate, isNumeric, isKey, isLast)
data = data:EntIndex()

if mustInitiate then
if isKey or isLast then
return "p"..data
else
return "p"..data..";"
end
end

if isKey or isLast then
return "p"..data
else
return "p"..data..";"
end
end,


-- 3 numbers separated by a comma.
["Vector"] = function(data, mustInitiate, isNumeric, isKey, isLast)
if mustInitiate then
if isKey or isLast then
return "v"..data.x..","..data.y..","..data.z
else
return "v"..data.x..","..data.y..","..data.z..";"
end
end

if isKey or isLast then
return "v"..data.x..","..data.y..","..data.z
else
return "v"..data.x..","..data.y..","..data.z..";"
end
end,


-- 3 numbers separated by a comma.
["Angle"] = function(data, mustInitiate, isNumeric, isKey, isLast)
if mustInitiate then
if isKey or isLast then
return "a"..data.p..","..data.y..","..data.r
else
return "a"..data.p..","..data.y..","..data.r..";"
end
end

if isKey or isLast then
return "a"..data.p..","..data.y..","..data.r
else
return "a"..data.p..","..data.y..","..data.r..";"
end
end
}

Expand All @@ -401,4 +605,4 @@ _s_meta = {
von = {}

von.deserialize = setmetatable(_deserialize,_d_meta)
von.serialize = setmetatable(_serialize,_s_meta)
von.serialize = setmetatable(_serialize,_s_meta)

0 comments on commit 3d3cda7

Please sign in to comment.