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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
local M = {}
local DATA = "@data/"
local FLUSH_BYTES = 32 * 1024
local FRAME_GAP = 250
local MOVE_GAP = 10
local SETTLE = 400
local WINDOW_SETTLE = 250
local MOVE_MIN = 16
local WINDOW_MIN = 1000
local MOVE_QUIET = 1000
local CLICKS_MAX = 8
local CROP = 160
local LABEL_MAX = 200
local MAX_BYTES = 512 * 1024 * 1024
local MACRO_SCRIPT_MAX = 4 * 1024 * 1024
local cfg
local notify
M.live = nil
M.unsaved = {}
local function now()
return rb.system.time()
end
function M.init(config, toast)
cfg = config
notify = toast
end
local function flush(s)
if s.buf_bytes == 0 then
return true
end
local text = table.concat(s.buf)
local ok, err = pcall(rb.file.append, s.dir .. "/events.jsonl", text)
if not ok then
rb.log.warn(
"Dev Spy: the recording's log was not written: " .. tostring(err)
)
s.broken = true
return false
end
table.clear(s.buf)
s.buf_bytes = 0
s.log_bytes += #text
return true
end
local function line(s, fields)
s.seq += 1
fields.seq = s.seq
fields.t = now() - s.started
local text = string.gsub(rb.json.stringify(fields), "\n%s*", "") .. "\n"
table.insert(s.buf, text)
s.buf_bytes += #text
s.events += 1
if s.buf_bytes >= FLUSH_BYTES then
flush(s)
end
end
local function cursor()
local x, y = rb.system.mouse()
return { x = x, y = y }
end
local function displays()
local ok, list = pcall(rb.screen.list)
return ok and list or {}
end
local function motion(s)
local m = s.motion
if m.n == 0 then
return
end
line(s, {
kind = "move",
dx = m.dx,
dy = m.dy,
n = m.n,
cursor = { x = m.x, y = m.y },
})
m.dx, m.dy, m.n, m.at = 0, 0, 0, now()
end
local function window_line(s, w)
line(s, {
kind = "window",
title = w.title,
process = w.process,
x = w.x,
y = w.y,
width = w.width,
height = w.height,
})
end
local function wanted(s, what)
return s ~= nil and (s.mode == "observe" or cfg["macro_" .. what])
end
local function failed(s, file, err, cause)
line(s, {
kind = "missing",
file = file,
cause = cause,
reason = tostring(err),
})
if not s.failing then
s.failing = true
rb.log.warn("Dev Spy: screenshot not saved: " .. tostring(err))
notify(file .. "\n" .. tostring(err), "screenshot not saved")
end
end
local function keep(s, shot, kind, file, cause)
local hash = rb.hash.crc32(shot.image)
local last = s.last[kind]
if last ~= nil and last.hash == hash then
return last.file, true
end
local png = rb.codec.base64_decode(shot.image)
local ok, err = pcall(rb.file.write, s.dir .. "/" .. file, png)
if not ok then
failed(s, file, err, cause)
return nil
end
s.failing = false
s.frame_bytes += #png
s.last[kind] = { hash = hash, file = file }
return file, false
end
local function display_of(s, d)
return {
index = d.index,
x = d.x,
y = d.y,
width = d.width,
height = d.height,
scale = s.scales[d.index],
}
end
local shoot
local function frame(s, cause, started, input)
s.capturing, s.last_frame = true, started
rb.run(function()
local ok, shot = pcall(rb.screen.capture, { max_edge = cfg.frame_edge })
s.capturing = false
if s.closed then
return
end
if s.now_cause ~= nil then
shoot(s)
end
s.frame_seq += 1
local file = string.format("frames/%06d.png", s.frame_seq)
if not ok then
failed(s, file, shot, cause)
return
end
local kept, dup = keep(s, shot, "frame", file, cause)
if kept == nil then
return
end
if not dup then
s.frames += 1
s.last_file = kept
end
line(s, {
kind = "frame",
file = kept,
dup = dup or nil,
cause = cause,
input = input,
at = started - s.started,
display = display_of(s, shot.display),
width = shot.width,
height = shot.height,
cursor = shot.cursor,
ms = now() - started,
})
end)
end
local function due(s, t)
if s.now_cause ~= nil then
return s.now_cause
end
if s.settle_at ~= nil and t >= s.settle_at then
return "settle"
end
if s.window_at ~= nil and t >= s.window_at then
return "window"
end
if cfg.frame_ms > 0 and t - s.last_frame >= cfg.frame_ms * 1000 then
return "timer"
end
return nil
end
function shoot(s)
if s.frames_off or s.capturing then
return
end
local t = now()
local cause = due(s, t)
if cause == nil then
return
end
local now_one = cause == "click" or cause == "key" or cause == "input"
if not now_one and t - s.last_frame < FRAME_GAP then
return
end
local input = nil
if s.now_cause ~= nil then
input = s.now_seq
s.now_cause, s.now_seq = nil, nil
elseif cause == "settle" then
s.settle_at = nil
s.burst = false
for _, click in s.clicks do
local ok, hex = pcall(rb.screen.pixel_hex, click.x, click.y)
if ok then
line(s, { kind = "px_after", click = click.seq, px = hex })
end
end
table.clear(s.clicks)
end
if s.window_at ~= nil and t >= s.window_at then
s.window_at = nil
s.window_shot = t
end
frame(s, cause, t, input)
end
local function settle(s)
s.settle_at = now() + SETTLE
end
local RANK = { start = 1, input = 1, key = 2, click = 2 }
local function now_shot(s, cause, seq)
if s.now_cause == nil or RANK[cause] > RANK[s.now_cause] then
s.now_cause, s.now_seq = cause, seq
end
end
local function act(s, cause, seq)
now_shot(s, cause, seq)
s.burst = true
settle(s)
shoot(s)
end
local function burst(s, seq, opens)
if opens and not s.burst then
now_shot(s, "input", seq)
end
s.burst = s.burst or opens
settle(s)
shoot(s)
end
local function crop(s, seq, x, y)
if s.frames_off then
return
end
if s.cropping then
line(s, {
kind = "missing",
click = seq,
reason = "the crop of the click before was still being taken",
})
return
end
local display
for _, d in displays() do
if x >= d.x and x < d.x + d.width and y >= d.y and y < d.y + d.height then
display = d
end
end
if display == nil then
return
end
local w = math.min(CROP, display.width)
local h = math.min(CROP, display.height)
local rx = math.clamp(math.floor(x - display.x - w / 2), 0, display.width - w)
local ry =
math.clamp(math.floor(y - display.y - h / 2), 0, display.height - h)
local started = now()
s.cropping = true
rb.run(function()
local ok, shot = pcall(rb.screen.capture, {
display = display.index,
region = { x = rx, y = ry, w = w, h = h },
})
s.cropping = false
if s.closed then
return
end
s.crop_seq += 1
local file = string.format("crops/%06d.png", s.crop_seq)
if not ok then
failed(s, file, shot)
return
end
local kept, dup = keep(s, shot, "crop", file)
if kept == nil then
return
end
if not dup then
s.crops += 1
end
local scale = shot.width / w
s.scales[display.index] = scale
line(s, {
kind = "crop",
file = kept,
dup = dup or nil,
click = seq,
at = started - s.started,
display = display_of(s, display),
region = { x = display.x + rx, y = display.y + ry, w = w, h = h },
scale = scale,
ms = now() - started,
})
end)
end
function M.start(mode)
if M.live ~= nil then
return nil, "a session is already running"
end
local stamp = os.date("%Y%m%d-%H%M%S")
local id, n = stamp .. "-" .. mode, 2
while rb.file.exists(DATA .. "sessions/" .. id) do
id = stamp .. "-" .. n .. "-" .. mode
n += 1
end
local s = {
id = id,
dir = DATA .. "sessions/" .. id,
mode = mode,
started = now(),
seq = -1,
buf = {},
buf_bytes = 0,
log_bytes = 0,
frame_bytes = 0,
events = 0,
frames = 0,
frame_seq = 0,
crop_seq = 0,
crops = 0,
marks = 0,
scales = {},
last_frame = -math.huge,
last = {},
path = 0,
path_at = 0,
clicks = {},
window_shot = -math.huge,
motion = { dx = 0, dy = 0, n = 0, x = 0, y = 0, at = now() },
}
local observe = mode == "observe"
for _, dir in observe and { "/frames", "/crops" } or { "" } do
local ok, err = pcall(rb.file.mkdir, s.dir .. dir)
if not ok then
return nil, tostring(err)
end
end
local devices_ok, devices = pcall(rb.input.devices)
s.input = devices_ok and #devices > 0 and "hardware" or "software"
local list = displays()
s.frames_off = not observe or #list == 0
M.live = s
line(s, {
kind = "start",
id = id,
mode = mode,
epoch_ms = s.started,
cursor = cursor(),
goal = cfg.goal ~= "" and cfg.goal or nil,
platform = rb.info.platform,
input = s.input,
displays = list,
frame_edge = observe and cfg.frame_edge or nil,
relay = rb.info.version,
frames_off = s.frames_off,
})
if observe then
window_line(s, rb.system.window())
if #list == 0 then
notify(
"no displays found\nthis observation records input only",
"no screenshots"
)
end
now_shot(s, "start")
shoot(s)
end
flush(s)
return s
end
function M.stop(reason)
local s = M.live
if s == nil then
return nil
end
motion(s)
s.ended = now()
if s.capturing then
line(
s,
{ kind = "missing", reason = "the recording stopped during the capture" }
)
end
if s.cropping then
line(
s,
{ kind = "missing", reason = "the recording stopped during a crop" }
)
end
line(s, {
kind = "end",
reason = reason,
seconds = (s.ended - s.started) / 1000,
events = s.events,
frames = s.frames,
})
flush(s)
s.closed = true
M.live = nil
table.insert(M.unsaved, s)
pcall(rb.file.write_json, s.dir .. "/summary.json", {
mode = s.mode,
started = s.started,
ended = s.ended,
events = s.events,
frames = s.frames,
log_bytes = s.log_bytes,
frame_bytes = s.frame_bytes,
})
return s
end
function M.tick()
local s = M.live
if s == nil then
return nil
end
if s.motion.n > 0 and now() - s.motion.at >= MOVE_GAP then
motion(s)
end
shoot(s)
if s.broken or not flush(s) then
return "its log could not be written"
end
if s.log_bytes + s.frame_bytes >= MAX_BYTES then
return "it reached 512 MB"
end
if now() - s.started >= cfg.max_minutes * 60000 then
return "it reached " .. cfg.max_minutes .. " minutes"
end
return nil
end
local function pad(key)
local head = string.sub(key, 1, 3)
return head == "Pad" or head == "Joy"
end
local COMMIT = { enter = true, escape = true, tab = true }
local MODIFIER = {
lctrl = true,
rctrl = true,
lshift = true,
rshift = true,
lalt = true,
ralt = true,
lwin = true,
rwin = true,
}
local function action_cause(key)
if string.sub(key, 1, 5) == "Mouse" then
return "click"
end
local k = string.lower(key)
if COMMIT[k] then
return "key"
end
local mods = rb.input.modifiers()
local chord = mods.ctrl or mods.alt or mods.win
return chord and not MODIFIER[k] and "key" or nil
end
local function device_seen(s, device)
if device ~= nil and s.input == "software" then
s.input = "hardware"
line(s, { kind = "input", mode = "hardware" })
end
end
function M.key(down, key, held, device)
local s = M.live
if s == nil or (s.mode == "macro" and pad(key) and not cfg.macro_pad) then
return
end
motion(s)
device_seen(s, device)
local at = cursor()
local fields = { kind = down and "down" or "up", key = key, cursor = at }
if not down then
fields.held = held
end
if device ~= nil then
fields.dev = device.id
fields.dev_name = device.nickname or device.name
end
if s.mode == "observe" then
local w = rb.system.window()
fields.wcursor = { x = at.x - w.x, y = at.y - w.y }
fields.pre = s.last_file
if down then
local ok, hex = pcall(rb.screen.pixel_hex, at.x, at.y)
fields.px = ok and hex or nil
end
end
line(s, fields)
if s.mode ~= "observe" then
return
end
local cause = down and action_cause(key) or nil
if cause == "click" then
if #s.clicks < CLICKS_MAX then
table.insert(s.clicks, { seq = s.seq, x = at.x, y = at.y })
end
crop(s, s.seq, at.x, at.y)
end
if cause ~= nil then
act(s, cause, s.seq)
else
burst(s, s.seq, down)
end
end
function M.move(dx, dy)
local s = M.live
if not wanted(s, "moves") then
return
end
local m = s.motion
m.dx += dx
m.dy += dy
m.n += 1
m.x, m.y = rb.system.mouse()
if now() - m.at >= MOVE_GAP then
motion(s)
end
local t = now()
if t - s.path_at > MOVE_QUIET then
s.path = 0
end
s.path_at = t
s.path += math.abs(dx) + math.abs(dy)
if s.mode == "observe" and s.path >= MOVE_MIN then
burst(s, nil, true)
end
end
function M.scroll(delta, device)
local s = M.live
if not wanted(s, "scroll") then
return
end
motion(s)
device_seen(s, device)
line(s, {
kind = "scroll",
delta = delta,
cursor = cursor(),
dev = device and device.id,
dev_name = device and (device.nickname or device.name),
})
if s.mode == "observe" then
burst(s, s.seq, true)
end
end
function M.axis(name, value)
local s = M.live
if wanted(s, "pad") then
line(s, { kind = "axis", axis = name, value = value })
if s.mode == "observe" then
burst(s, s.seq, true)
end
end
end
function M.window(w)
local s = M.live
if s ~= nil and s.mode == "observe" then
window_line(s, w)
s.window_at = s.window_at
or math.max(now() + WINDOW_SETTLE, s.window_shot + WINDOW_MIN)
end
end
function M.display(d)
local s = M.live
if s ~= nil and s.mode == "observe" then
line(s, {
kind = "display",
index = d.index,
x = d.x,
y = d.y,
width = d.width,
height = d.height,
})
end
end
function M.label(text)
text = string.gsub(text or "", "%c", " ")
local cut = utf8.offset(text, LABEL_MAX + 1)
return cut and string.sub(text, 1, cut - 1) or text
end
function M.note(fields)
local s = M.live
if s == nil then
return false
end
motion(s)
line(s, fields)
return true
end
function M.recover()
local ok, names = pcall(rb.file.list, DATA .. "sessions")
if not ok then
return 0
end
table.sort(names)
for _, id in names do
local dir = DATA .. "sessions/" .. id
local summary_ok, summary = pcall(rb.file.read_json, dir .. "/summary.json")
if summary_ok and type(summary) == "table" then
summary.id, summary.dir = id, dir
table.insert(M.unsaved, summary)
elseif rb.file.is_dir(dir) then
local s = { id = id, dir = dir, mode = string.match(id, "%-(%a+)$") }
local log_ok, size = pcall(rb.file.size, dir .. "/events.jsonl")
s.log_bytes = log_ok and size or 0
s.frame_bytes, s.frames = 0, 0
for _, sub in { "frames", "crops" } do
local list_ok, files = pcall(rb.file.list, dir .. "/" .. sub)
for _, name in list_ok and files or {} do
local path = dir .. "/" .. sub .. "/" .. name
local size_ok, bytes = pcall(rb.file.size, path)
s.frames += sub == "frames" and 1 or 0
s.frame_bytes += size_ok and bytes or 0
end
end
table.insert(M.unsaved, s)
end
end
return #M.unsaved
end
local RESERVED = { con = true, prn = true, aux = true, nul = true }
for i = 1, 9 do
RESERVED["com" .. i] = true
RESERVED["lpt" .. i] = true
end
function M.clean(name)
local cleaned = string.gsub(name or "", "[^%w %._%-]", "")
cleaned = string.sub(string.match(cleaned, "^[%s%.]*(.-)$"), 1, 64)
cleaned = string.match(cleaned, "^(.-)[%s%.]*$")
if RESERVED[string.lower(string.match(cleaned, "^[^%.]*"))] then
cleaned ..= "_"
end
return cleaned
end
local function forget(s)
local i = table.find(M.unsaved, s)
if i ~= nil then
table.remove(M.unsaved, i)
end
end
local function write_macro(path, name)
local log = path .. "/events.jsonl"
if rb.file.size(log) > MACRO_SCRIPT_MAX then
return nil, "the log is over 4 MB"
end
local actions, at, open, dropped = {}, nil, {}, {}
local function add(action, t)
action.t = t
table.insert(actions, action)
return action
end
local function move_to(c, t)
if at ~= nil and (c.x ~= at.x or c.y ~= at.y) then
add({ action = "move", dx = c.x - at.x, dy = c.y - at.y }, t)
end
at = c
end
for text in string.gmatch(rb.file.read(log), "[^\n]+") do
local e = rb.json.parse(text)
if e.kind == "start" then
at = e.cursor
elseif e.kind == "move" then
move_to(e.cursor, e.t)
elseif e.kind == "own" then
for _, key in e.keys do
if open[key] ~= nil then
open[key].skip = true
open[key] = nil
dropped[key] = true
end
end
elseif e.kind == "down" or e.kind == "up" then
if string.sub(e.key, 1, 5) == "Mouse" then
move_to(e.cursor, e.t)
end
if e.kind == "up" and dropped[e.key] then
dropped[e.key] = nil
else
local action = add({ action = e.kind, code = e.key }, e.t)
open[e.key] = e.kind == "down" and action or nil
end
elseif e.kind == "scroll" then
add({ action = "scroll", amount = e.delta }, e.t)
end
end
local ended = #actions > 0 and actions[#actions].t or 0
for key, down in open do
if not down.skip then
add({ action = "up", code = key }, ended)
end
end
local kept, last = {}, nil
for _, action in actions do
if not action.skip then
action.delay = last and action.t - last or 0
last = action.t
action.t, action.skip = nil, nil
table.insert(kept, action)
end
end
actions = kept
if #actions == 0 then
return nil, "no keys, buttons or movement to replay"
end
local file = path .. ".luau"
rb.file.write(file, rb.macro.to_script(actions, name))
return file
end
function M.free(target, name)
local base = DATA .. target .. "/" .. name
local path, n = base, 2
while rb.file.exists(path) or rb.file.exists(path .. ".luau") do
path = base .. "-" .. n
n += 1
end
return string.sub(path, #DATA + #target + 2)
end
function M.save(s, target, name)
name = M.clean(name)
if name == "" then
name = s.id
end
local path = DATA .. target .. "/" .. M.free(target, name)
rb.file.mkdir(DATA .. target)
rb.file.move(s.dir, path)
forget(s)
if target ~= "macros" then
return path
end
local ok, file, why = pcall(write_macro, path, name)
if not ok then
return path, nil, tostring(file)
end
return path, file, why
end
function M.discard(s)
forget(s)
local ok, err = pcall(rb.file.rmdir, s.dir)
if not ok then
rb.log.warn("Dev Spy: " .. s.dir .. " not removed: " .. tostring(err))
end
end
return M