-
Notifications
You must be signed in to change notification settings - Fork 18
/
pon.lua
408 lines (340 loc) · 9.95 KB
/
pon.lua
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
--[[
RECOMMENDED VERSION
VERSION 1.4.1
Copyright thelastpenguin™
You may use this for any purpose as long as:
- You don't remove this copyright notice.
- You don't claim this to be your own.
- You properly credit the author, thelastpenguin™, if you publish your work based on (and/or using) this.
If you modify the code for any purpose, the above still applies to the modified code.
The author is not held responsible for any damages incured from the use of pON, you use it at your own risk.
DATA TYPES SUPPORTED:
- tables - k, v - pointers
- strings - k, v - pointers
- numbers - k, v
- booleans- k, v
- Vectors - k, v
- Angles - k, v
- Entities- k, v
- Players - k, v
CHANGE LOG
V 1.1.0
- Added Vehicle, NPC, NextBot, Player, Weapon
V 1.2.0
- Added custom handling for k, v tables without any array component.
V 1.2.1
- fixed deserialization bug.
V 1.3.0
- added custom handling of strings without any escaped characters.
V 1.4.0
- added detection of numbers without requiring 'n' datatype. (10 datatypes one for each num it could start with)
V 1.4.1
- Various fixes and optimizarions, as well as merging stuff from the pON repo.
THANKS TO...
- VERCAS for the inspiration.
]]
local pon = {}
_G.pon = pon
local type, count = type, table.Count
local tonumber = tonumber
local format = string.format
do
local encode = {}
local try_cache
local cache_size = 0
local type, count = type, table.Count
local tonumber = tonumber
local format = string.format
encode['table'] = function(self, tbl, output, cache)
if (cache[tbl]) then
table.insert(output, '('..cache[tbl]..')')
return
else
cache_size = cache_size + 1
cache[tbl] = cache_size
end
-- CALCULATE COMPONENT SIZES
local nSize = #tbl
local kvSize = count(tbl) - nSize
if (nSize == 0 and kvSize > 0) then
table.insert(output, '[')
else
table.insert(output, '{')
if nSize > 0 then
for i = 1, nSize do
local v = tbl[i]
if v == nil then
table.insert(output, '!')
continue
end
local tv = type(v)
-- HANDLE POINTERS
if (tv == 'string') then
local pid = cache[v]
if (pid) then
table.insert(output, '('..pid..')')
else
cache_size = cache_size + 1
cache[v] = cache_size
self.string(self, v, output, cache)
end
else
self[tv](self, v, output, cache)
end
end
end
end
if (kvSize > 0) then
if (nSize > 0) then
table.insert(output, '~')
end
for k, v in next, tbl do
local key_type = type(k)
if key_type != 'number' or k < 1 or k > nSize then
local tk, tv = key_type, type(v)
-- THE KEY
if (tk == 'string') then
local pid = cache[k]
if (pid) then
table.insert(output, '('..pid..')')
else
cache_size = cache_size + 1
cache[k] = cache_size
self.string(self, k, output, cache)
end
else
self[tk](self, k, output, cache)
end
-- THE VALUE
if (tv == 'string') then
local pid = cache[v]
if (pid) then
table.insert(output, '('..pid..')')
else
cache_size = cache_size + 1
cache[v] = cache_size
self.string(self, v, output, cache)
end
else
self[tv](self, v, output, cache)
end
end
end
end
table.insert(output, '}')
end
-- ENCODE STRING
local gsub = string.gsub
encode['string'] = function(self, str, output)
--if try_cache(str, output) then return end
local estr, count = gsub(str, ";", "\\;")
if (count == 0) then
table.insert(output, '\''..str..';')
else
table.insert(output, '"'..estr..'";')
end
end
-- ENCODE NUMBER
encode['number'] = function(self, num, output)
table.insert(output, tonumber(num)..';')
end
-- ENCODE BOOLEAN
encode['boolean'] = function(self, val, output)
table.insert(output, val and 't' or 'f')
end
-- ENCODE VECTOR
encode['Vector'] = function(self, val, output)
table.insert(output, ('v'..val.x..','..val.y)..(','..val.z..';'))
end
-- ENCODE ANGLE
encode['Angle'] = function(self, val, output)
table.insert(output, ('a'..val.p..','..val.y)..(','..val.r..';'))
end
encode['Entity'] = function(self, val, output)
table.insert(output, 'E'..(IsValid(val) and val:EntIndex()..';' or '#'))
end
encode['Player'] = encode['Entity']
encode['Vehicle'] = encode['Entity']
encode['Weapon'] = encode['Entity']
encode['NPC'] = encode['Entity']
encode['NextBot'] = encode['Entity']
encode['PhysObj'] = encode['Entity']
encode['nil'] = function()
table.insert(output, '?')
end
encode.__index = function(key)
ErrorNoHalt('Cannot encode '..tostring(key)..', encoded as nil.')
return encode['nil']
end
do
local empty, concat = table.Empty, table.concat
function pon.encode(tbl)
local output = {}
cache_size = 0
encode['table'](encode, tbl, output, {})
local res = concat(output)
return res
end
end
end
do
local tonumber = tonumber
local find, sub, gsub, Explode = string.find, string.sub, string.gsub, string.Explode
local Vector, Angle, Entity = Vector, Angle, Entity
local decode = {}
decode['{'] = function(self, index, str, cache)
local cur = {}
table.insert(cache, cur)
local k, v, tk, tv = 1, nil, nil, nil
while (true) do
tv = sub(str, index, index)
if (!tv or tv == '~') then
index = index + 1
break
end
if (tv == '}') then
return index + 1, cur
end
-- READ THE VALUE
index = index + 1
index, v = self[tv](self, index, str, cache)
cur[k] = v
k = k + 1
end
while (true) do
tk = sub(str, index, index)
if (!tk or tk == '}') then
index = index + 1
break
end
-- READ THE KEY
index = index + 1
index, k = self[tk](self, index, str, cache)
-- READ THE VALUE
tv = sub(str, index, index)
index = index + 1
index, v = self[tv](self, index, str, cache)
cur[k] = v
end
return index, cur
end
decode['['] = function(self, index, str, cache)
local cur = {}
table.insert(cache, cur)
local k, v, tk, tv = 1, nil, nil, nil
while (true) do
tk = sub(str, index, index)
if (!tk or tk == '}') then
index = index + 1
break
end
-- READ THE KEY
index = index + 1
index, k = self[tk](self, index, str, cache)
if !k then continue end
-- READ THE VALUE
tv = sub(str, index, index)
index = index + 1
if !self[tv] then
print('did not find type: '..tv)
end
index, v = self[tv](self, index, str, cache)
cur[k] = v
end
return index, cur
end
-- STRING
decode['"'] = function(self, index, str, cache)
local finish = find(str, '";', index, true)
local res = gsub(sub(str, index, finish - 1), '\\;', ';')
index = finish + 2
table.insert(cache, res)
return index, res
end
-- STRING NO ESCAPING NEEDED
decode['\''] = function(self, index, str, cache)
local finish = find(str, ';', index, true)
local res = sub(str, index, finish - 1)
index = finish + 1
table.insert(cache, res)
return index, res
end
decode['!'] = function(self, index, str, cache)
return index, nil
end
-- NUMBER
decode['n'] = function(self, index, str, cache)
index = index - 1
local finish = find(str, ';', index, true)
local num = tonumber(sub(str, index, finish - 1))
index = finish + 1
return index, num
end
decode['0'] = decode['n']
decode['1'] = decode['n']
decode['2'] = decode['n']
decode['3'] = decode['n']
decode['4'] = decode['n']
decode['5'] = decode['n']
decode['6'] = decode['n']
decode['7'] = decode['n']
decode['8'] = decode['n']
decode['9'] = decode['n']
decode['-'] = decode['n']
-- POINTER
decode['('] = function(self, index, str, cache)
local finish = find(str, ')', index, true)
local num = tonumber(sub(str, index, finish - 1))
index = finish + 1
return index, cache[num]
end
-- BOOLEAN. ONE DATA TYPE FOR YES, ANOTHER FOR NO.
decode['t'] = function(self, index)
return index, true
end
decode['f'] = function(self, index)
return index, false
end
-- VECTOR
decode['v'] = function(self, index, str, cache)
local finish = find(str, ';', index, true)
local vecStr = sub(str, index, finish - 1)
index = finish + 1; -- update the index.
local segs = Explode(',', vecStr, false)
return index, Vector(tonumber(segs[1]), tonumber(segs[2]), tonumber(segs[3]))
end
-- ANGLE
decode['a'] = function(self, index, str, cache)
local finish = find(str, ';', index, true)
local angStr = sub(str, index, finish - 1)
index = finish + 1; -- update the index.
local segs = Explode(',', angStr, false)
return index, Angle(tonumber(segs[1]), tonumber(segs[2]), tonumber(segs[3]))
end
-- ENTITY
decode['E'] = function(self, index, str, cache)
if (str[index] == '#') then
index = index + 1
return index, NULL
else
local finish = find(str, ';', index, true)
local num = tonumber(sub(str, index, finish - 1))
index = finish + 1
return index, Entity(num)
end
end
-- PLAYER
decode['P'] = function(self, index, str, cache)
local finish = find(str, ';', index, true)
local num = tonumber(sub(str, index, finish - 1))
index = finish + 1
return index, Entity(num) or NULL
end
decode['?'] = function(self, index, str, cache)
return index + 1, nil
end
function pon.decode(data)
local _, res = decode[sub(data, 1, 1)](decode, 2, data, {})
return res
end
end