-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathradon.lua
584 lines (532 loc) · 22.5 KB
/
radon.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
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 version = "1.3.33"
local configHelpers = require "util.configHelpers"
local schemas = require "core.schemas"
local ScanInventory = require("core.inventory.ScanInventory")
local oldPullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
local oldPrint = print
local logs = {}
local maxLogs = 100
function print(...)
local args = {...}
local str = ""
for i = 1, #args do
str = str .. tostring(args[i])
if i ~= #args then
str = str .. " "
end
end
table.insert(logs, 1, {time = os.time("utc"), text = str})
if #logs > maxLogs then
table.remove(logs, #logs)
end
end
--- Imports
local _ = require("util.score")
local sound = require("util.sound")
local eventHook = require("util.eventHook")
local Display = require("modules.display")
local Solyd = require("modules.solyd")
local hooks = require("modules.hooks")
local useCanvas = hooks.useCanvas
local Button = require("components.Button")
local SmolButton = require("components.SmolButton")
local BasicButton = require("components.BasicButton")
local BigText = require("components.BigText")
local Modal = require("components.Modal")
local Logs = require("components.Logs")
local ConfigEditor = require("components.ConfigEditor")
local bigFont = require("fonts.bigfont")
local SmolText = require("components.SmolText")
local smolFont = require("fonts.smolfont")
local BasicText = require("components.BasicText")
local Rect = require("components.Rect")
local RenderCanvas = require("components.RenderCanvas")
local Core = require("core.ShopState")
local Pricing = require("core.Pricing")
local ShopRunner = require("core.ShopRunner")
local ConfigValidator = require("core.ConfigValidator")
local defaultLayout = require("DefaultLayout")
local loadRIF = require("modules.rif")
local configDefaults = require("configDefaults")
local config = require("config")
local products = require("products")
local eventHooks = {}
if fs.exists(fs.combine(fs.getDir(shell.getRunningProgram()), "eventHooks.lua")) then
eventHooks = require("eventHooks")
end
--- End Imports
configHelpers.loadDefaults(config, configDefaults)
local configErrors = ConfigValidator.validateConfig(config)
local productsErrors = ConfigValidator.validateProducts(products)
if (configErrors and #configErrors > 0) or (productsErrors and #productsErrors > 0) then
config.ready = false
else
config.ready = true
end
local peripherals = {}
configHelpers.getPeripherals(config, peripherals)
-- if config.shopSync and config.shopSync.enabled and not config.shopSync.force then
-- error("ShopSync is not yet finalized, please update Radon to use this feature, or set config.shopSync.force to true to use current ShopSync spec")
-- end
local display = Display.new({theme=config.theme, monitor=config.peripherals.monitor})
local terminal = Display.new({theme=config.terminalTheme, monitor=term})
local layout = nil
local layoutFile = nil
local layoutRenderer = nil
local configState = {
config = config,
products = products,
eventHooks = eventHooks,
}
local Main = Solyd.wrapComponent("Main", function(props)
local canvas = useCanvas(display)
local flatCanvas = {}
if props.configState.config.ready and props.shopState.kryptonReady then
local theme = props.configState.config.theme
if theme.formatting.layout ~= "custom" or not theme.formatting.layoutFile then
local addBg = false
if theme.formatting.layout ~= layout then
layout = theme.formatting.layout
if theme.palette then
require("util.setPalette")(display.mon, theme.palette)
end
if theme.colors and theme.colors.bgColor then
display.ccCanvas.clear = theme.colors.bgColor
addBg = true
end
display.ccCanvas:outputFlush(display.mon)
end
flatCanvas = defaultLayout(canvas, display, props, theme, version)
if addBg then
table.insert(flatCanvas, Rect {
display = display,
x = 1,
y = 1,
width = display.bgCanvas.width,
height = display.bgCanvas.height,
color = theme.colors.bgColor,
})
end
else
local addBg = false
if theme.formatting.layoutFile ~= layoutFile or theme.formatting.layout ~= layout then
layout = "custom"
layoutFile = theme.formatting.layoutFile
local f = fs.open(layoutFile, "r")
if not f then
error("Could not open layout file: " .. layoutFile)
end
layoutRendererString = f.readAll()
f.close()
local loadedString, err = load(layoutRendererString, layoutFile, "t", setmetatable({ require = require }, {__index = _ENV}))
if not loadedString then
error("Could not load layout file: " .. err)
end
layoutRenderer = loadedString()
if theme.layouts[layoutFile] and theme.layouts[layoutFile].palette then
require("util.setPalette")(display.mon, theme.layouts[layoutFile].palette)
end
if theme.layouts[layoutFile] and theme.layouts[layoutFile].colors and theme.layouts[layoutFile].colors.bgColor then
display.ccCanvas.clear = theme.layouts[layoutFile].colors.bgColor
addBg = true
end
display.ccCanvas:outputFlush(display.mon)
end
flatCanvas = layoutRenderer(canvas, display, props, theme, version)
if addBg then
table.insert(flatCanvas, 1, Rect {
display = display,
x = 1,
y = 1,
width = display.bgCanvas.width,
height = display.bgCanvas.height,
color = theme.layouts[layoutFile].colors.bgColor,
})
end
end
elseif not props.configState.config.ready then
flatCanvas = {
BasicText({
display = display,
text = "Waiting on config...",
x = 1,
y = 1,
color = colors.white,
bgColor = colors.black,
})
}
else
flatCanvas = {
BasicText({
display = display,
text = "Connecting...",
x = 1,
y = 1,
color = colors.white,
bgColor = colors.black,
})
}
end
return _.flat({ _.flat(flatCanvas) }), {
canvas = {canvas, 1, 1},
config = props.configState.config or {},
shopState = props.shopState or {},
products = props.shopState.products,
peripherals = props.peripherals or {},
}
end)
local terminalState = {
prevCatagory = "logs",
activeCatagory = "logs",
configErrors = configErrors,
productsErrors = productsErrors,
scroll = 0,
maxScroll = 0,
}
--local mbsMode = settings.get("mbs.shell.enabled")
local Terminal = Solyd.wrapComponent("Terminal", function(props)
local canvas = useCanvas(terminal)
local theme = props.configState.config.terminalTheme
local flatCanvas = {}
local versionString = "Radon " .. version
local terminalCatagories = { "logs", "config", "products" }
local bodyHeight = math.floor(terminal.bgCanvas.height / 3) - 1
local bodyWidth = math.floor(terminal.bgCanvas.width / 2)
table.insert(flatCanvas, Rect {
key = "header",
display = terminal,
x = 1,
y = 1,
width = terminal.bgCanvas.width,
height = 3,
color = theme.colors.titleBgColor,
})
table.insert(flatCanvas, BasicText {
key = "title",
display = terminal,
align = "left",
text = versionString,
x = 1,
y = 1,
color = theme.colors.titleTextColor,
bg = theme.colors.titleBgColor,
})
local catagoriesX = 1 + #versionString
for i = 1, #terminalCatagories do
local bgColor = theme.colors.catagoryBgColor
if props.terminalState.activeCatagory == terminalCatagories[i] then
bgColor = theme.colors.activeCatagoryBgColor
end
table.insert(flatCanvas, BasicButton {
key = "catagory-" .. terminalCatagories[i],
display = terminal,
align = "center",
text = " " .. terminalCatagories[i] .. " ",
x = 2 + catagoriesX,
y = 1,
color = theme.colors.catagoryTextColor,
bg = bgColor,
onClick = function()
props.terminalState.activeCatagory = terminalCatagories[i]
end
})
catagoriesX = catagoriesX + 3 + #terminalCatagories[i]
end
if (props.terminalState.configErrors and #props.terminalState.configErrors > 0) or terminalState.activeCatagory == "config" then
if terminalState.prevCatagory ~= "config" then
terminalState.prevCatagory = "config"
terminalState.configPath = ""
terminalState.scroll = 0
end
table.insert(flatCanvas, ConfigEditor {
key = "config-editor",
display = terminal,
x = 1,
y = 2,
width = bodyWidth,
height = bodyHeight,
config = props.configState.config,
schema = schemas.configSchema,
errors = props.terminalState.configErrors,
errorPrefix = "config",
terminalState = props.terminalState,
theme = theme.colors.configEditor,
onSave = function(newConfig)
props.shopState.oldConfig = props.configState.config
props.shopState.config = newConfig
props.configState.config = newConfig
props.terminalState.configErrors = ConfigValidator.validateConfig(newConfig)
if (not props.terminalState.configErrors or #props.terminalState.configErrors == 0) and (not props.terminalState.productsErrors or #props.terminalState.productsErrors == 0) then
newConfig.ready = true
end
configHelpers.getPeripherals(newConfig, peripherals)
-- TODO: Detect if we actually need to update currencies
props.shopState.changedCurrencies = true
local f = fs.open("config.lua", "w")
f.write("return " .. textutils.serialize(newConfig))
f.close()
print("Configs updated!")
if props.configState.eventHooks and props.configState.eventHooks.configSaved then
props.configState.eventHooks.configSaved(newConfig)
end
end
})
elseif (props.terminalState.productsErrors and #props.terminalState.productsErrors > 0) or terminalState.activeCatagory == "products" then
if terminalState.prevCatagory ~= "products" then
terminalState.prevCatagory = "products"
terminalState.configPath = ""
terminalState.scroll = 0
end
table.insert(flatCanvas, ConfigEditor {
key = "products-editor",
display = terminal,
x = 1,
y = 2,
width = bodyWidth,
height = bodyHeight,
config = props.shopState.products,
schema = schemas.productsSchema,
errors = props.terminalState.productsErrors,
errorPrefix = "products",
terminalState = props.terminalState,
theme = theme.colors.productsEditor,
onSave = function(newConfig)
props.shopState.products = newConfig
props.configState.products = newConfig
props.terminalState.productsErrors = ConfigValidator.validateProducts(products)
if (not props.terminalState.configErrors or #props.terminalState.configErrors == 0) and (not props.terminalState.productsErrors or #props.terminalState.productsErrors == 0) then
props.configState.config.ready = true
end
ScanInventory.clearNbtCache()
local f = fs.open("products.lua", "w")
f.write("return " .. textutils.serialize(newConfig))
f.close()
print("Products updated!")
if props.configState.eventHooks and props.configState.eventHooks.productsSaved then
props.configState.eventHooks.productsSaved(newConfig)
end
end
})
elseif terminalState.activeCatagory == "logs" then
table.insert(flatCanvas, Logs {
key = "logs",
display = terminal,
x = 1,
y = 2,
width = bodyWidth,
height = bodyHeight,
logs = props.logs,
color = theme.colors.logTextColor,
bg = theme.colors.bgColor,
})
end
table.insert(flatCanvas, Modal { key="modal" })
return _.flat({ _.flat(flatCanvas) }), {
canvas = {canvas, 1, 1},
config = props.configState.config or {},
shopState = props.shopState or {},
products = props.shopState.products,
terminalState = props.terminalState,
peripherals = props.peripherals or {},
modal = props.modal
}
end)
local t = 0
local tree = nil
local terminalTree = nil
local lastClock = os.epoch("utc")
local lastCanvases = {
{ stack = {}, hash = {} },
{ stack = {}, hash = {} },
}
local function diffCanvasStack(diffDisplay, newStack, lastCanvas)
-- Find any canvases that were removed
local removed = {}
local kept, newCanvasHash = {}, {}
for i = 1, #lastCanvas.stack do
removed[lastCanvas.stack[i][1]] = lastCanvas.stack[i]
end
for i = 1, #newStack do
if removed[newStack[i][1]] then
kept[#kept+1] = newStack[i]
removed[newStack[i][1]] = nil
newStack[i][1].allDirty = false
else -- New
newStack[i][1].allDirty = true
end
newCanvasHash[newStack[i][1]] = newStack[i]
end
-- Mark rectangle of removed canvases on bgCanvas (TODO: using bgCanvas is a hack)
for _, canvas in pairs(removed) do
if canvas[1].brand == "TextCanvas" then
diffDisplay.bgCanvas:dirtyRect(canvas[2], canvas[3], canvas[1].width*2, canvas[1].height*3)
else
diffDisplay.bgCanvas:dirtyRect(canvas[2], canvas[3], canvas[1].width, canvas[1].height)
end
end
-- For each kept canvas, mark the bounds if the new bounds are different
for i = 1, #kept do
local newCanvas = kept[i]
local oldCanvas = lastCanvas.hash[newCanvas[1]]
if oldCanvas then
if oldCanvas[2] ~= newCanvas[2] or oldCanvas[3] ~= newCanvas[3] then
-- TODO: Optimize this?
if oldCanvas[1].brand == "TextCanvas" then
diffDisplay.bgCanvas:dirtyRect(oldCanvas[2], oldCanvas[3], oldCanvas[1].width*2, oldCanvas[1].height*3)
diffDisplay.bgCanvas:dirtyRect(newCanvas[2], newCanvas[3], newCanvas[1].width*2, newCanvas[1].height*3)
else
diffDisplay.bgCanvas:dirtyRect(oldCanvas[2], oldCanvas[3], oldCanvas[1].width, oldCanvas[1].height)
diffDisplay.bgCanvas:dirtyRect(newCanvas[2], newCanvas[3], newCanvas[1].width, newCanvas[1].height)
end
end
end
end
lastCanvas.stack = newStack
lastCanvas.hash = newCanvasHash
end
local shopState = Core.ShopState.new(config, products, peripherals, version, logs, eventHooks)
--local Profiler = require("profile")
local deltaTimer = os.startTimer(0)
local success, err = pcall(function() ShopRunner.launchShop(shopState, function()
--Profiler:activate()
print("Radon " .. version .. " started")
if eventHooks and eventHooks.start then
eventHook.execute(eventHooks.start, version, config, products, shopState)
end
while true do
-- add t = t if we need animations
tree = Solyd.render(tree, Main { configState = configState, shopState = shopState, peripherals = peripherals})
local context = Solyd.getTopologicalContext(tree, { "canvas", "aabb" })
diffCanvasStack(display, context.canvas, lastCanvases[1])
local t1 = os.epoch("utc")
local cstack = { {display.bgCanvas, 1, 1}, unpack(context.canvas) }
-- cstack[#cstack+1] = {display.textCanvas, 1, 1}
display.ccCanvas:composite(unpack(cstack))
display.ccCanvas:outputDirty(display.mon)
local t2 = os.epoch("utc")
--print("Render time: " .. (t2-t1) .. "ms")
terminalTree = Solyd.render(terminalTree, Terminal { configState = configState, products = products, shopState = shopState, peripherals = peripherals, logs = logs, terminalState = terminalState, modal = {}})
local terminalContext = Solyd.getTopologicalContext(terminalTree, { "canvas", "aabb", "input" })
diffCanvasStack(terminal, terminalContext.canvas, lastCanvases[2])
t1 = os.epoch("utc")
local cstackT = { {terminal.bgCanvas, 1, 1}, unpack(terminalContext.canvas) }
-- cstack[#cstack+1] = {display.textCanvas, 1, 1}
terminal.ccCanvas:composite(unpack(cstackT))
terminal.ccCanvas:outputDirty(terminal.mon)
t2 = os.epoch("utc")
-- print("Render time: " .. (t2-t1) .. "ms")
local activeNode = hooks.findActiveInput(terminalContext.input)
if activeNode then
if activeNode.inputState.cursorX and activeNode.inputState.cursorY then
terminal.mon.setCursorPos(activeNode.inputState.cursorX, activeNode.inputState.cursorY)
else
terminal.mon.setCursorPos(activeNode.x, activeNode.y)
end
terminal.mon.setTextColor(colors.black)
terminal.mon.setCursorBlink(true)
else
terminal.mon.setCursorBlink(false)
end
local receivedEvent = false
local terminate = false
while not receivedEvent do
local e = { os.pullEvent() }
receivedEvent = true
local name = e[1]
if name == "timer" and e[2] == deltaTimer then
-- local clock = os.epoch("utc")
-- local dt = (clock - lastClock)/1000
-- t = t + dt
-- lastClock = clock
-- deltaTimer = os.startTimer(0)
-- hooks.tickAnimations(dt)
elseif name == "timer" then
receivedEvent = false
elseif name == "monitor_touch" and e[2] == peripheral.getName(display.mon) then
local x, y = e[3], e[4]
local node = hooks.findNodeAt(context.aabb, x, y)
if node then
node.onClick()
end
elseif name == "term_resize" then
terminal.ccCanvas:outputFlush(terminal.mon)
elseif name == "mouse_click" then
local x, y = e[3], e[4]
local clearedInput = hooks.clearActiveInput(terminalContext.input, x, y)
if clearedInput and clearedInput.onBlur then
clearedInput.onBlur()
end
local node = hooks.findNodeAt(terminalContext.aabb, x, y)
if node then
node.onClick()
end
elseif name == "mouse_scroll" then
local dir = e[2]
local x, y = e[3], e[4]
local node = hooks.findNodeAt(terminalContext.aabb, x, y)
local cancelScroll = false
if node and node.onScroll then
if node.onScroll(dir) then
cancelScroll = true
end
end
if not cancelScroll then
if dir >= 1 and terminalState.scroll < terminalState.maxScroll then
terminalState.scroll = math.min(terminalState.scroll + dir, terminalState.maxScroll)
elseif dir <= -1 and terminalState.scroll > 0 then
terminalState.scroll = math.max(terminalState.scroll + dir, 0)
end
end
elseif name == "char" then
local char = e[2]
local node = hooks.findActiveInput(terminalContext.input)
if node then
node.onChar(char)
end
elseif name == "key" then
--if e[2] == keys.q then
-- break
--end
local key, held = e[2] or 0, e[3] or false
local node = hooks.findActiveInput(terminalContext.input)
if node then
node.onKey(key, held)
end
elseif name == "paste" then
local contents = e[2]
local node = hooks.findActiveInput(terminalContext.input)
if node and node.onPaste then
node.onPaste(contents)
end
elseif name == "terminate" then
terminate = true
break
end
end
if terminate then
break
end
end
---Profiler:deactivate()
end) end)
display.mon.clear()
terminal.mon.setBackgroundColor(colors.black)
terminal.mon.setTextColor(colors.white)
terminal.mon.clear()
terminal.mon.setCursorPos(1,1)
for i = 1, #shopState.config.currencies do
local currency = shopState.config.currencies[i]
if (currency.krypton and currency.krypton.ws) then
currency.krypton.ws:disconnect()
end
end
os.pullEvent = oldPullEvent
if not success then
if eventHooks and eventHooks.programError then
eventHook.execute(eventHooks.programError, err)
end
error(err)
end
print("Radon terminated, goodbye!")
--Profiler:write_results(nil, "profile.txt")