-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtabSerialization.lua
More file actions
527 lines (466 loc) · 16.1 KB
/
tabSerialization.lua
File metadata and controls
527 lines (466 loc) · 16.1 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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
--author cs
--email 04nycs@gmail.com
--
--https://github.com/ThinEureka/tabMachine
--created on Oct 26, 2023
--
--
local tabSerialization = {}
local baseExcludeKeys = {
__tab = true,
__type = true,
__address = true,
}
local raw_pairs = function(x)
return next, x, nil
end
local rawget = rawget
function tabSerialization.createSnapshot(rootContext, detailControl)
local addPath = false
local addStat = nil
local addTableAddress = false
local excludeKeys = baseExcludeKeys
if detailControl ~= nil then
addPath = detailControl.addPath
addTableAddress = detailControl.addTableAddress
local extraExcludeKeys = detailControl.extraExcludeKeys
if extraExcludeKeys ~= nil then
excludeKeys = {}
for k, v in pairs(baseExcludeKeys) do
excludeKeys[k] = v
end
for k, v in pairs(extraExcludeKeys) do
excludeKeys[k] = v
end
end
addStat = detailControl.addStat
end
local addressToTable = {}
local tableToAddress = {}
local addressToImage = {}
local addressToPath = {}
local nextAddress = 1
local visitArray = {}
local visitMap = {}
local function createImage(t, type, path)
local address = tableToAddress[t]
if address ~= nil then
local image = addressToImage[address]
return address, image
end
local address = nextAddress
nextAddress = nextAddress + 1
tableToAddress[t] = address
addressToTable[address] = t
addressToPath[address] = path
local image = {}
if type == "table" then
if addTableAddress then
image.__type = type
image.__address = address
end
else
image.__type = type
image.__address = address
end
if addPath then
image.__path = path
end
addressToImage[address] = image
return address, image
end
--create image of contexts first
local contextArray = {}
table.insert(contextArray, rootContext)
local contextIndex = 1
while contextIndex <= #contextArray do
local c = contextArray[contextIndex]
local address, image = createImage(c, "table", c:_getPath())
local subContexts = c.__subContexts
if subContexts ~= nil then
for _, sc in ipairs(subContexts) do
if not sc.__excludeInSnapshot then
table.insert(contextArray, sc)
end
end
end
contextIndex = contextIndex + 1
end
local function visitTable(t, image, address, path)
for k, v in raw_pairs(t) do
local k_type = type(k)
if k_type == "string" or k_type == "number" then
if excludeKeys[k] == nil then
local v_type = type(v)
if v_type == "number" or v_type == "string" or v_type == "boolean" then
image[k] = v
elseif v_type == "table" then
local v_path = nil
if addPath then
v_path = path .. "@" .. k
end
local v_address, v_image = createImage(v, v_type, v_path)
image[k] = v_image
if not rawget(v, "__excludeInSnapshot") then
if not visitMap[v_address] then
visitMap[v_address] = true
table.insert(visitArray, v_address)
end
else
v_image.__name = rawget(v, "__name")
end
else
if addPath then
v_path = path .. "@" .. k
end
local v_address, v_image = createImage(v, v_type, v_path)
image[k] = v_image
end
end
end
end
end
local rootAddress = tableToAddress[rootContext]
visitMap[rootAddress] = true
table.insert(visitArray, rootAddress)
local visitIndex = 1
while visitIndex <= #visitArray do
local nextA = visitArray[visitIndex]
local nextImage = addressToImage[nextA]
local nextT = addressToTable[nextA]
local nextPath = addressToPath[nextA]
visitTable(nextT, nextImage, nextA, nextPath)
visitIndex = visitIndex + 1
end
local rootImage = addressToImage[rootAddress]
if addStat then
if addStat.statTreeSize then
tabSerialization.statTreeSize(rootImage)
end
end
local snapshot = rootImage
return snapshot
end
function tabSerialization.statTreeSize(context)
local treeSize = 1
local subContexts = context.__subContexts
if subContexts ~= nil then
for _, sc in ipairs(subContexts) do
if not sc.__excludeInSnapshot then
treeSize = treeSize + tabSerialization.statTreeSize(sc)
end
end
end
context.__treeSize = treeSize
return treeSize
end
function tabSerialization.createTabTreeFromSnapshot(snapshot)
do
return snapshot
end
-- local addressToTable = {}
-- local addressToImage = snapshot.addressToImage
--
-- local visitArray = {}
-- local visitMap = {}
--
-- local function createTable(address, type)
-- local t = addressToTable[address]
-- if t ~= nil then
-- return t
-- end
--
-- t = {}
-- addressToTable[address] = t
--
-- if type ~= nil then
-- t["#type"] = type
-- t["#address"] = address
-- end
--
-- return t
-- end
--
-- local function visit(t, image, address)
-- for k, v in pairs(image) do
-- if excludeKeys[k] == nil then
-- local v_type = type(v)
-- if v_type == "number" or v_type == "string" or v_type == "boolean" then
-- t[k] = v
-- elseif v_type == "table" then
-- if v.__type == "table" then
-- local v_address = v.__address
-- t[k] = createTable(v_address)
-- if not visitMap[v_address] then
-- visitMap[v_address] = true
-- table.insert(visitArray, v_address)
-- end
-- else
-- t[k] = createTable(v.__address, v.__type)
-- end
-- end
-- end
-- end
-- end
--
-- local rootAddress = snapshot.rootAddress
-- local rootContext = createTable(rootAddress)
-- local rootImage = addressToImage[rootAddress]
--
-- local nextT = rootContext
-- local nextImage = rootImage
-- local nextA = rootAddress
--
-- visitMap[rootAddress] = true
--
-- local visitIndex = 1
-- while true do
-- visit(nextT, nextImage, nextA)
-- nextA = visitArray[visitIndex]
-- visitIndex = visitIndex + 1
--
-- if nextA then
-- nextT = addressToTable[nextA]
-- nextImage = addressToImage[nextA]
-- else
-- break
-- end
-- end
--
-- return rootContext
end
local function exportstring( s )
return string.format("%q", s)
end
function tabSerialization.saveSnapshotToFile(snapshot, filename)
local charS,charE = " ","\n"
local file,err = io.open( filename, "wb" )
if err then return err end
-- initiate variables for save procedure
local tables,lookup = { snapshot },{ [snapshot] = 1 }
file:write( "return {"..charE )
for idx,t in ipairs( tables ) do
file:write( "-- Table: {"..idx.."}"..charE )
file:write( "{"..charE )
local thandled = {}
for i,v in ipairs( t ) do
thandled[i] = true
local stype = type( v )
-- only handle value
if stype == "table" then
if not lookup[v] then
table.insert( tables, v )
lookup[v] = #tables
end
file:write( charS.."{"..lookup[v].."},"..charE )
elseif stype == "string" then
file:write( charS..exportstring( v )..","..charE )
elseif stype == "number" then
file:write( charS..tostring( v )..","..charE )
elseif stype == "boolean" then
file:write( charS ..(v and "true" or "false") ..","..charE )
end
end
for i,v in pairs( t ) do
-- escape handled values
if (not thandled[i]) then
local str = ""
local stype = type( i )
-- handle index
if stype == "table" then
if not lookup[i] then
table.insert( tables,i )
lookup[i] = #tables
end
str = charS.."[{"..lookup[i].."}]="
elseif stype == "string" then
str = charS.."["..exportstring( i ).."]="
elseif stype == "number" then
str = charS.."["..tostring( i ).."]="
end
if str ~= "" then
stype = type( v )
-- handle value
if stype == "table" then
if not lookup[v] then
table.insert( tables,v )
lookup[v] = #tables
end
file:write( str.."{"..lookup[v].."},"..charE )
elseif stype == "string" then
file:write( str..exportstring( v )..","..charE )
elseif stype == "number" then
file:write( str..tostring( v )..","..charE )
elseif stype == "boolean" then
file:write( str ..(v and "true" or "false") ..","..charE )
end
end
end
end
file:write( "},"..charE )
end
file:write( "}" )
local fileSize = file:seek()
file:close()
return fileSize
end
function tabSerialization.loadSnapshotFromFile(sfile)
local ftables,err = loadfile( sfile )
if err then return _,err end
local tables = ftables()
for idx = 1,#tables do
local tolinki = {}
for i,v in pairs( tables[idx] ) do
if type( v ) == "table" then
tables[idx][i] = tables[v[1]]
end
if type( i ) == "table" and tables[i[1]] then
table.insert( tolinki,{ i,tables[i[1]] } )
end
end
-- link indices
for _,v in ipairs( tolinki ) do
tables[idx][v[2]],tables[idx][v[1]] = tables[idx][v[1]],nil
end
end
return tables[1]
end
function tabSerialization.addSnapshotTabTree(parentContext, tabTree, name)
local container = nil
if not parentContext:hasSub("__snapshots") then
container = parentContext:call(g_t.tabContainer, "__snapshots")
container.__excludeInSnapshot = true
container.__subContexts = {}
else
container = parentContext:getSub("__snapshots")
end
tabTree.__name = name
table.insert(container.__subContexts, tabTree)
end
tabSerialization.tabRecordTabClip = _{
tabName = "recordSnapshots",
s1 = function (c, rootContext, totalFrame, frameInterval)
-- c.__excludeInSnapshot = true
c.rootContext = rootContext
c.totalFrame = totalFrame
c.frameInterval = frameInterval or 1
local clip = {}
clip.snapshots = {}
c.clip = clip
-- c.clip.__excludeInSnapshot = true
c.beginFrame = g_frameIndex
c:s1_update()
end,
s1_update = function(c)
local frameIndex = g_frameIndex
local frameOffset = frameIndex - c.beginFrame
local clip = c.clip
local snapshots = clip.snapshots
if frameOffset % c.frameInterval == 0 then
local snapshot = tabSerialization.createSnapshot(c.rootContext)
snapshot.__excludeInSnapshot = true
snapshot.frameIndex = frameIndex
table.insert(snapshots, snapshot)
end
if c.totalFrame ~= nil then
if #snapshots >= c.totalFrame then
c:stop()
end
end
end,
final = function(c)
c:output(c.clip)
end,
}
function tabSerialization.serializeTable(tbl)
local strRet = ""
local charS,charE = " ","\n"
-- initiate variables for save procedure
local tables,lookup = { tbl },{ [tbl] = 1 }
strRet = "return {"..charE
for idx,t in ipairs( tables ) do
--strRet = strRet .. "-- Table: {"..idx.."}"..charE
strRet = strRet .. "{"..charE
local thandled = {}
for i,v in ipairs( t ) do
thandled[i] = true
local stype = type( v )
-- only handle value
if stype == "table" then
if not lookup[v] then
table.insert( tables, v )
lookup[v] = #tables
end
strRet = strRet .. charS.."{"..lookup[v].."},"..charE
elseif stype == "string" then
strRet = strRet .. charS..exportstring( v )..","..charE
elseif stype == "number" then
strRet = strRet .. charS..string.format("%0.17g", v)..","..charE
elseif stype == "boolean" then
strRet = strRet .. charS..(v and "true" or "false") ..","..charE
end
end
for i,v in pairs( t ) do
-- escape handled values
if (not thandled[i]) then
local str = ""
local stype = type( i )
-- handle index
if stype == "table" then
if not lookup[i] then
table.insert( tables,i )
lookup[i] = #tables
end
str = charS.."[{"..lookup[i].."}]="
elseif stype == "string" then
str = charS.."["..exportstring( i ).."]="
elseif stype == "number" then
str = charS.."["..tostring( i ).."]="
end
if str ~= "" then
stype = type( v )
-- handle value
if stype == "table" then
if not lookup[v] then
table.insert( tables,v )
lookup[v] = #tables
end
strRet = strRet .. str.."{"..lookup[v].."},"..charE
elseif stype == "string" then
strRet = strRet .. str..exportstring( v )..","..charE
elseif stype == "number" then
strRet = strRet .. str..string.format("%0.17g", v)..","..charE
elseif stype == "boolean" then
strRet = strRet .. str..(v and "true" or "false") ..",".. charE
end
end
end
end
strRet = strRet .. "},"..charE
end
strRet = strRet .. "}"
return strRet
end
local empty = {}
function tabSerialization.deserializeTable(str)
local ftables,err = load(str, "tabSerialization", "t", empty)
if err then return _,err end
local tables = ftables()
for idx = 1,#tables do
local tolinki = {}
for i,v in pairs( tables[idx] ) do
if type( v ) == "table" then
tables[idx][i] = tables[v[1]]
end
if type( i ) == "table" and tables[i[1]] then
table.insert( tolinki,{ i,tables[i[1]] } )
end
end
-- link indices
for _,v in ipairs( tolinki ) do
tables[idx][v[2]],tables[idx][v[1]] = tables[idx][v[1]],nil
end
end
return tables[1]
end
return tabSerialization