-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.lua
More file actions
584 lines (506 loc) · 16.1 KB
/
proxy.lua
File metadata and controls
584 lines (506 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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
local PATH = (...):match("^(.-)[^%.]+$")
local mintmousse = require(PATH .. "conf")
local threadCommand = require(PATH .. "threadCommand")
local componentLogic = require(PATH .. "componentLogic")
local contract = require(PATH .. "contract")
local utilID = require(PATH .. "util.id")
local loggingStack = require(PATH .. "logging.stack")
local log = mintmousse._logger:extend("Proxy")
local proxy = {
proxyComponents = { },
localChildren = { },
}
local protectedStatic = {
-- variables
id = true,
type = true,
parentID = true,
children = true, -- variable used on MM thread
-- functions
parent = true,
back = true,
remove = true,
setChildrenOrder = true,
moveBefore = true,
moveAfter = true,
moveToFront = true,
moveToBack = true,
children = true,
isDead = true,
}
local keyProtectionCache = { }
local isProtectedKey = function(k)
if keyProtectionCache[k] ~= nil then return keyProtectionCache[k] end
local result = false
if type(k) == "number" then result = true
elseif protectedStatic[k] then result = true
elseif type(k) ~= "string" then result = false
elseif k == "new" or k == "add" then result = true
elseif #k >= 4 then
local s = k:sub(1,3):lower()
if s == "new" or s == "add" then
result = true
end
end
keyProtectionCache[k] = result
return result
end
local needsComponentUpdate = function(compType, key)
if compType == "unknown" then return true end
local ct = contract.componentTypes[compType]
if type(ct.updates) == "table" and ct.updates[key] then return true end
if type(key) == "string" and ct.events then
local event = key:match("^onEvent(.+)")
if event and ct.events[event] then return true end
end
return false
end
local needsChildUpdate = function(parentType, key)
if parentType == "unknown" then return true end
local ct = contract.componentTypes[parentType]
return type(ct.childUpdates) == "table" and ct.childUpdates[key]
end
local needsComponentPush = function(compType, key)
if compType == "unknown" then return true end
local ct = contract.componentTypes[compType]
if type(ct.pushes) == "table" and ct.pushes[key] then return true end
return false
end
local funcKeys = {
parent = function(raw)
loggingStack.push()
if not raw.parentID then return nil end
local v = proxy.get(raw.parentID)
loggingStack.pop()
return v
end,
back = function(raw) -- Alias for parent
loggingStack.push()
if not raw.parentID then return nil end
local v = proxy.get(raw.parentID)
loggingStack.pop()
return v
end,
remove = function(_) return proxy.removeSelf end,
setChildrenOrder = function(_) return proxy.setChildrenOrder end,
moveBefore = function(_) return proxy.moveBefore end,
moveAfter = function(_) return proxy.moveAfter end,
moveToFront = function(_) return proxy.moveToFront end,
moveToBack = function(_) return proxy.moveToBack end,
children = function(_) return proxy.childrenIterator end,
type = function(raw) return raw.type or "unknown" end, -- alias for type
isDead = function(_) return false end, -- can't be dead if it has this metafunction
}
local proxyNewIndex = function(proxyTbl, key, value)
if key == "__raw" then return end
loggingStack.push()
if isProtectedKey(key) then
log:error("Cannot change immutable key:", key)
loggingStack.pop()
return
end
local raw = rawget(proxyTbl, "__raw")
if raw[key] == value then
loggingStack.pop()
return
end
local id = raw.id
local compType = raw.type or "unknown"
local parentID = raw.parentID
if needsComponentUpdate(compType, key) then
threadCommand.call("updateComponent", { id = id, index = key, value = value })
elseif needsComponentPush(compType, key) then
threadCommand.call("pushComponent", { id = id, index = key, value = value })
loggingStack.pop()
return
elseif parentID then
local parentType = proxy.get(parentID).type
if needsChildUpdate(parentType, key) then
threadCommand.call("updateComponent", { id = id, index = key, value = value })
end
end
rawset(raw, key, value)
loggingStack.pop()
end
local creationCache = { }
local proxyIndex = function(proxyTbl, key)
if key == "__raw" then return nil end
loggingStack.push()
local raw = rawget(proxyTbl, "__raw")
local handler = funcKeys[key]
local result
if handler then
loggingStack.pop()
return handler(raw)
elseif type(key) == "string" and #key >= 4 then
if not creationCache[key] then
local prefix = key:sub(1, 3):lower()
local typ = key:sub(4)
if prefix == "new" then
creationCache[key] = function(parentProxy, component)
loggingStack.push()
component = component or { }
component.type = typ
local childProxy = proxy._addComponent(component, parentProxy)
loggingStack.pop()
return childProxy
end
elseif prefix == "add" then
creationCache[key] = function(parentProxy, component)
loggingStack.push()
component = component or { }
component.type = typ
proxy._addComponent(component, parentProxy)
loggingStack.pop()
return parentProxy
end
end
end
if creationCache[key] ~= nil then
loggingStack.pop()
return creationCache[key]
end
end
-- else
loggingStack.pop()
return rawget(raw, key)
end
local proxyMetatable = {
__index = proxyIndex,
__newindex = proxyNewIndex,
}
proxy.childrenIterator = function(proxyTbl)
local parentID = proxyTbl.parentID
local list = parentID and proxy.localChildren[parentID] or { }
return ipairs(list)
end
proxy._registerLocalChild = function(parentID, childProxy)
if not proxy.localChildren[parentID] then
proxy.localChildren[parentID] = { }
end
table.insert(proxy.localChildren[parentID], childProxy)
end
proxy._unregisterLocalChild = function(parentID, childID)
if not parentID then
return
end
local list = proxy.localChildren[parentID]
if not list then return end
for i = #list, 1, -1 do
if list[i].id == childID then
table.remove(list, i)
break
end
end
if #list == 0 then
proxy.localChildren[parentID] = nil
end
end
local removedMT = {
__newindex = function(tbl, key, value)
loggingStack.push()
log:warning("Component has been removed (attempted to write key:", key, ")")
loggingStack.pop()
end,
__index = function(tbl, key)
if key == "isDead" then return true end
loggingStack.push()
log:warning("Component has been removed (attempted to read key:", key, ")")
loggingStack.pop()
return nil
end,
}
local removeProxyFromLocal
removeProxyFromLocal = function(component)
local id = component.id
local list = proxy.localChildren[id]
if list then
for _, child in ipairs(list) do
removeProxyFromLocal(child)
end
end
proxy.localChildren[id], proxy.proxyComponents[id] = nil, nil
setmetatable(component, removedMT)
end
proxy.removeSelf = function(tbl)
loggingStack.push()
local raw = rawget(tbl, "__raw")
local id = raw.id
if proxy.proxyComponents[id] then
proxy._unregisterLocalChild(raw.parentID, id)
removeProxyFromLocal(tbl)
end
threadCommand.call("removeComponent", { id = id })
loggingStack.pop()
end
proxy.setChildrenOrder = function(tbl, newOrder)
loggingStack.push()
if type(newOrder) ~= "table" then
log:warning("setChildrenOrder: newOrder must be a table of IDs")
loggingStack.pop()
return tbl
end
local raw = rawget(tbl, "__raw")
if raw.parentID and proxy.localChildren[raw.parentID] then
local list = proxy.localChildren[raw.parentID]
local orderedIDs = { }
local seen = { }
for _, id in ipairs(newOrder) do
table.insert(orderedIDs, id)
seen[id] = true
end
for _, p in ipairs(list) do
if not seen[p.id] then
table.insert(orderedIDs, p.id)
end
end
local newList = { }
for _, id in ipairs(orderedIDs) do
local found = false
for _, p in ipairs(list) do
if p.id == id then
table.insert(newList, p)
found = true
break
end
end
if not found then
-- Assume sibling is of parent, try to find it, otherwise assume it is a component created by another thread
local p = proxy.get(id)
local pRaw = rawget(p, "__raw")
if not pRaw.parentID then
rawset(pRaw, "parentID", raw.parentID)
end
if pRaw.parentID == raw.parentID then
table.insert(newList, p)
else
log:warning("newOrder: Tried to 'reparent' component!",
"Gave ID that wasn't a child of this component. Gave:", id,
". Actual Parent:", pRaw.parentID, ". Tried to assign to:", raw.parentID)
end
end
end
proxy.localChildren[raw.parentID] = newList
end
threadCommand.call("setChildrenOrder", {
id = raw.id,
newOrder = newOrder,
})
loggingStack.pop()
return tbl
end
proxy.moveBefore = function(tbl, siblingID)
loggingStack.push()
if type(siblingID) == "table" then siblingID = siblingID.id end
if type(siblingID) ~= "string" then
log:warning("moveBefore: siblingID must be type string or component proxy")
loggingStack.pop()
return tbl
end
local success, reason = utilID.isValidID(siblingID)
if not success then
log:warning("moveBefore: siblingID has invalid ID:", reason)
loggingStack.pop()
return tbl
end
local raw = rawget(tbl, "__raw")
if raw.id == siblingID then
loggingStack.pop()
return tbl -- trying to move the child to the child?
end
if raw.parentID and proxy.localChildren[raw.parentID] then
local list = proxy.localChildren[raw.parentID]
local childIndex, siblingIndex
for i, p in ipairs(list) do
if p.id == raw.id then childIndex = i end
if p.id == siblingID then siblingIndex = i end
if childIndex and siblingIndex then break end
end
if childIndex and siblingIndex and childIndex ~= siblingIndex then
local childProxy = table.remove(list, childIndex)
if childIndex < siblingIndex then siblingIndex = siblingIndex - 1 end
table.insert(list, siblingIndex, childProxy)
end
end
threadCommand.call("moveBefore", {
id = raw.id,
siblingID = siblingID,
})
loggingStack.pop()
return tbl
end
proxy.moveAfter = function(tbl, siblingID)
loggingStack.push()
if type(siblingID) == "table" then siblingID = siblingID.id end
if type(siblingID) ~= "string" then
log:warning("moveAfter: siblingID must be type string or component proxy")
loggingStack.pop()
return tbl
end
local success, reason = utilID.isValidID(siblingID)
if not success then
log:warning("moveAfter: siblingID has invalid ID:", reason)
loggingStack.pop()
return tbl
end
local raw = rawget(tbl, "__raw")
if raw.id == siblingID then
loggingStack.pop()
return tbl-- trying to move the child to the child?
end
if raw.parentID and proxy.localChildren[raw.parentID] then
local list = proxy.localChildren[raw.parentID]
local childIndex, siblingIndex
for i, p in ipairs(list) do
if p.id == raw.id then childIndex = i end
if p.id == siblingID then siblingIndex = i end
if childIndex and siblingIndex then break end
end
if childIndex and siblingIndex and childIndex ~= siblingIndex then
local childProxy = table.remove(list, childIndex)
if childIndex < siblingIndex then siblingIndex = siblingIndex - 1 end
table.insert(list, siblingIndex + 1, childProxy)
end
end
threadCommand.call("moveAfter", {
id = raw.id,
siblingID = siblingID,
})
loggingStack.pop()
return tbl
end
proxy.moveToFront = function(tbl)
loggingStack.push()
local raw = rawget(tbl, "__raw")
if raw.parentID and proxy.localChildren[raw.parentID] then
local list = proxy.localChildren[raw.parentID]
local childIndex
for i, p in ipairs(list) do
if p.id == raw.id then
childIndex = i
break
end
end
if childIndex and childIndex ~= 1 then
local childProxy = table.remove(list, childIndex)
table.insert(list, 1, childProxy)
end
end
threadCommand.call("moveToFront", { id = raw.id })
loggingStack.pop()
return tbl
end
proxy.moveToBack = function(tbl)
loggingStack.push()
local raw = rawget(tbl, "__raw")
if raw.parentID and proxy.localChildren[raw.parentID] then
local list = proxy.localChildren[raw.parentID]
local childIndex
for i, p in ipairs(list) do
if p.id == raw.id then
childIndex = i
break
end
end
if childIndex and childIndex ~= #list then
local childProxy = table.remove(list, childIndex)
table.insert(list, childProxy)
end
end
threadCommand.call("moveToBack", { id = raw.id })
loggingStack.pop()
return tbl
end
proxy._addComponent = function(component, parentProxy)
loggingStack.push()
if type(component) == "string" then
component = { type = component }
end
log:assert(type(component) == "table", "Component must be type string or table")
if not component.id then
component.id = utilID.generateID()
end
component.id = proxy._autocorrectID(component.id)
component.parentID = rawget(parentProxy, "__raw").id
local success, reason = utilID.isValidID(component.id)
log:assert(success, "Invalid ID:", reason)
log:assert(type(component.type) == "string", "component.type must be string")
log:assert(component.type ~= "unknown", "component.type mustn't be protected keyword: 'unknown'")
log:assert(component.type ~= "Tab", "Use mintmousse.newTab() instead of type 'tab'")
local ct = contract.componentTypes[component.type]
log:assert(ct, "Unknown component type:", component.type)
log:assert(ct.hasCreateFunction, "component.type has no create function:", component.type)
componentLogic.run(component, parentProxy)
threadCommand.call("addComponent", { component = component })
local childProxy = proxy.createProxyTable(component)
proxy._registerLocalChild(component.parentID, childProxy)
loggingStack.pop()
return childProxy
end
proxy._autocorrectID = function(preferredID)
loggingStack.push()
local id = preferredID
if proxy.proxyComponents[id] then
id = utilID.generateID()
log:warning(("ID clash locally. ID '%s' is in use. Changing it to '%s'"):format(preferredID, id))
end
loggingStack.pop()
return id
end
-- Public functions
-- These should be in their own table really.
proxy.get = function(id, typeHint)
local p = proxy.proxyComponents[id]
if p then return p end
if typeHint and not contract.componentTypes[typeHint] then typeHint = nil end
return proxy.createProxyTable({ id = id, type = typeHint })
end
proxy.has = function(id)
return proxy.proxyComponents[id] ~= nil
end
proxy.createProxyTable = function(raw)
setmetatable(raw, nil)
local p = setmetatable({ __raw = raw }, proxyMetatable)
proxy.proxyComponents[raw.id] = p
return p
end
proxy.createTempProxy = function(raw)
setmetatable(raw, nil)
return setmetatable({ __raw = raw }, proxyMetatable)
end
proxy.newTab = function(title, id, index)
loggingStack.push()
id = id or utilID.generateID()
local success, reason = utilID.isValidID(id)
log:assert(success, "Invalid tab ID:", reason)
local component = { id = id, type = "Tab", title = title, parentID = nil }
componentLogic.run(component)
threadCommand.call("newTab", { id = id, title = title, index = index })
local p = proxy.createProxyTable(component)
loggingStack.pop()
return p
end
proxy.getProtectedKeys = function()
local keys = { }
for k in pairs(protectedStatic) do
table.insert(keys, k)
end
return keys
end
proxy.remove = function(id)
loggingStack.push()
local success, reason = utilID.isValidID(id)
if not success then
log:warning("Invalid ID to remove:", reason)
loggingStack.pop()
return
end
local p = proxy.proxyComponents[id]
if p then
proxy._unregisterLocalChild(p.parentID, id)
removeProxyFromLocal(p)
end
threadCommand.call("removeComponent", { id = id })
loggingStack.pop()
end
return proxy