-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialize.lua
More file actions
182 lines (171 loc) · 4.84 KB
/
serialize.lua
File metadata and controls
182 lines (171 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
function indextostring(index)
if type(index) == "number" then
return string.format("%q",tostring(index))
else
return string.format("%q",index)
end
end
function process(name,data,saved,out)
out = out or {}
--Numbers are tostring'd
if type(data) == "number" then
local str = "n~"..name.."~"..data.."|"
table.insert(out,str)
elseif type(data) == "string" then
--Strings are store as formated quoted strings
local str = "s~"..name.."~"..string.format("%q",data).."|"
table.insert(out,str)
elseif type(data) == "table" then
--Look for this table in our saved tables
local found = false
for k,v in pairs(saved) do
if v == data then
--This table is already defined
------print("This table is already defined, storing reference name")
local str = "t~"..name..":"..k.."~|"
table.insert(out,str)
found = true
end
end
if not found then
--Define table
saved[name] = data
local str = "t~"..name.."~{|"
local this = {}
for k,v in pairs(data) do
--Serialize each internal element
table.insert(this,stage(v,indextostring(k),saved))
end
str = str .. table.concat(this) .. "}|"
table.insert(out,str)
end
end
return out
end
--Given a number,string or table, returns a string for rebuilding it
-- Supports numbers, strings, tables and nested tables, but doesn't support
-- functions
function stage(data,name,saved)
saved = saved or {}
name = name or indextostring("data")
local out = process(name,data,saved)
str = table.concat(out)
----print(out)
return str
end
function serialize(data,name,saved)
local str = stage(data,name,saved)
return string.sub(str,1,-2)..".|"
end
function getWords(str)
local out = {}
local t = {}
local i = 0
while true do
i = string.find(str, "|", i+1) -- find spaces
if i == nil then break end
table.insert(t, i)
end
local j = 0
for k,v in pairs(t) do
table.insert(out,string.sub(str,j,v))
j=v+1
end
return out
end
function analizeWords(data)
curtable = {}
saved = {}
curtable.tab = {}
curtable.prev = false
define = false
--for k,v in pairs(data) do --print(k,v) end
--print("Begin deserialize")
for k,v in pairs(data) do
if v == "}|" then
--print("Moving from table " .. tostring(curtable) .. " to parent " .. tostring(curtable.prev))
curtable = curtable.prev
--print("Reached end of table")
elseif v=="}.|" then
--print("Ended")
return curtable.tab
elseif string.sub(v,1,2) == "t~" then
----print(v)
local e,_ = string.find(v,"~",3)
local name = string.sub(v,4,e-2)
if name ~= "" then
local number = tonumber(name)
if number then
name=number
end
else
name = #curtable.tab
end
----print("Table found: " .. name)
local image
local c,_ = string.find(v,":")
if c then
define = false
image = string.sub(name,c-1,-1)
name = string.sub(name,1,c-5)
----print("Table " .. name .. " claims to be defined as " .. image)
for k,v in pairs(saved) do
----print(k,v)
end
----print("This table holds " .. #saved[image] .. " elements")
curtable.tab[name] = saved[image]
else
define = true
--Create a new table entry
saved[name] = {}
--print("New table defined: " .. name)
--Create a new current table
newtable = {}
--Set this current table property to the new saved table
newtable.tab = saved[name]
--Insert this new table in the current table
--table.insert(current.table,name,newtable.table)
curtable.tab[name] = newtable.tab
--Asign the new curtable previous property to the previous table
newtable.prev = curtable
--Move on to the new current table
--print("Moving from parent " .. tostring(curtable) .. " to sub-table " .. tostring(newtable))
curtable = newtable
end
elseif string.sub(v,1,2) == "n~" then
local e,_ = string.find(v,"~",3)
local name = string.sub(v,4,e-2)
local value = string.sub(v,e+1,-2)
local number = tonumber(name)
if number then
name = number
end
curtable.tab[name] = value
--print("curtable.table["..name.."] = "..curtable.tab[name])
elseif string.sub(v,1,2) == "s~" then
local e,_ = string.find(v,"~",3)
local name = string.sub(v,4,e-2)
local value = string.sub(v,e+2,-3)
--print("String found " .. name .. " = " .. value)
if name ~= "" then
local number = tonumber(name)
if number then
curtable.tab[number] = value
else
curtable.tab[name] = value
end
else
table.insert(curtable.tab,value)
end
end
--print("Current state of curtable: ")
for k,v in pairs(curtable.tab) do
--print(k,v)
end
end
--print("Something weird happened, returning nil")
return nil
end
function deserialize(str)
return analizeWords(getWords(str))
end