-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcomputer_env.lua
More file actions
3782 lines (2751 loc) · 77.6 KB
/
computer_env.lua
File metadata and controls
3782 lines (2751 loc) · 77.6 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
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
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local lwcomp = ...
local function new_computer_env (computer)
local ENV = { }
ENV.assert = _G.assert
ENV.tostring = _G.tostring
ENV.tonumber = _G.tonumber
-- ENV.rawget = _G.rawget -- omitted
ENV.ipairs = _G.ipairs
ENV.pcall = _G.pcall
-- ENV.rawset = _G.rawset -- omitted
-- ENV.rawequal = _G.rawequal -- omitted
ENV._VERSION = _G._VERSION
ENV.next = _G.next
ENV.type = _G.type
ENV.xpcall = _G.xpcall
-- ENV.setfenv = _G.setfenv -- modify
-- ENV.getmetatable = _G.getmetatable -- modify
ENV.error = _G.error
ENV.pairs = _G.pairs
-- ENV.setmetatable = _G.setmetatable -- modify
ENV.select = _G.select
ENV.unpack = _G.unpack
-- ENV.getfenv = _G.getfenv -- modify
-- ENV.load = _G.load -- modify
-- ENV.loadfile = _G.loadfile -- modify
-- ENV.loadstring = _G.loadstring -- modify
-- ENV.dofile = _G.dofile -- modify
-- ENV.print = _G.print -- modify
-- ENV.require = _G.require -- omitted
-- ENV.debug = _G.debug -- omitted
-- ENV.package = _G.package -- omitted
-- ENV.jit = _G.jit -- omitted
-- ENV.collectgarbage = _G.collectgarbage -- omitted
ENV._G = ENV
ENV.getfenv = function (func)
if type (func) == "number" then
if func == 0 then
return ENV
elseif func > 0 then
-- over this function
func = func + 1
end
end
local env = getfenv (func)
if env and env ~= _G then
return env
end
return nil
end
ENV.setfenv = function (func, tab)
if type (func) == "number" then
if func > 0 then
-- over this function
func = func + 1
end
end
local env = getfenv (func)
if not env or env ~= _G then
if type (tab) ~= "table" then
error ("bad argument #2 to 'setfenv' (table expected, got "..type (tab)..")", 2)
end
setfenv (func, tab)
return func
end
end
ENV.getmetatable = function (object)
if type (object) == "table" then
local mt = getmetatable (object)
if mt ~= getmetatable (ENV.string) and
mt ~= getmetatable (ENV.coroutine) and
mt ~= getmetatable (ENV.table) and
mt ~= getmetatable (ENV.math) and
mt ~= getmetatable (ENV.vector) then
return mt
end
end
return nil
end
ENV.setmetatable = function (object, metatable)
if type (object) == "table" then
if object ~= ENV.string and
object ~= ENV.coroutine and
object ~= ENV.table and
object ~= ENV.math and
object ~= ENV.vector then
setmetatable (object, metatable)
end
end
end
ENV.string = { }
for key, value in pairs (_G.string) do
ENV.string[key] = value
end
setmetatable (ENV.string, getmetatable (_G.string))
-- ENV.string.find modified below
-- ENV.string.rep modified below
ENV.coroutine = { }
for key, value in pairs (_G.coroutine) do
ENV.coroutine[key] = value
end
setmetatable (ENV.coroutine, getmetatable (_G.coroutine))
ENV.table = { }
for key, value in pairs (_G.table) do
ENV.table[key] = value
end
setmetatable (ENV.table, getmetatable (_G.table))
ENV.math = { }
for key, value in pairs (_G.math) do
ENV.math[key] = value
end
setmetatable (ENV.math, getmetatable (_G.math))
ENV.vector = { }
for key, value in pairs (_G.vector) do
ENV.vector[key] = value
end
setmetatable (ENV.vector, getmetatable (_G.vector))
ENV.io = { }
-- ENV.io.input = _G.io.input -- omitted
-- ENV.io.write = _G.io.write -- omitted
-- ENV.io.read = _G.io.read -- omitted
-- ENV.io.output = _G.io.output -- omitted
-- ENV.io.open = _G.io.open -- modify
-- ENV.io.close = _G.io.close -- modify
-- ENV.io.flush = _G.io.flush -- omitted
-- ENV.io.type = _G.io.type -- modify
-- ENV.io.lines = _G.io.lines -- modify
ENV.os = { }
-- ENV.os.clock = _G.os.clock -- modify
-- ENV.os.date = _G.os.date -- modify
ENV.os.difftime = _G.os.difftime
-- ENV.os.getenv = _G.os.getenv -- modify
-- ENV.os.setenv -- added
-- ENV.os.remove = _G.os.remove -- modify
-- ENV.os.rename = _G.os.rename -- modify
-- ENV.os.time = _G.os.time -- modify
-- ENV.os.setlocale = _G.os.setlocale -- omitted
-- ENV.os.tmpname = _G.os.tmpname -- omitted
ENV.os.environs = { }
ENV.fs = { }
ENV.keys = { }
ENV.term = { }
ENV.term.colors = { }
ENV.utils = { }
ENV.wireless = { }
ENV.digilines = { }
ENV.mesecons = { }
ENV.http = { }
ENV.printer = { }
-- io
ENV.io.close = function (file)
if file and file.close then
file:close ()
end
end
ENV.io.lines = function (path)
if path then
local file = computer.filesys:open (path, "r")
if file then
return function ()
local line = file:read ("*l")
if not line then
file:close ()
end
return line
end
end
end
return function ()
return nil
end
end
ENV.io.open = function (path, mode)
return computer.filesys:open (path, mode)
end
ENV.io.type = function (obj)
if obj and obj.safefile_obj then
if obj.status then
return obj.status
else
return io.type (obj.file)
end
end
return io.type (obj)
end
-- os
ENV.os.clock = function ()
return (minetest.get_us_time () - computer.clock_base) / 1000000.0
end
ENV.os.clock_speed = function ()
return lwcomp.settings.running_tick
end
ENV.os.remove = function (path)
return computer.filesys:remove (path)
end
ENV.os.rename = function (oldname, newname)
return computer.filesys:rename (oldname, newname)
end
ENV.os.getenv = function (varname)
if not varname then
return nil
end
return ENV.os.environs[tostring (varname)]
end
ENV.os.setenv = function (varname, value)
if not value then
ENV.os.environs[varname] = nil
else
ENV.os.environs[varname] = tostring (value or "")
end
end
ENV.os.envstr = function (str)
for k, v in pairs (ENV.os.environs) do
str = string.gsub (str, "$"..k, tostring (v))
end
return str
end
ENV.os.time = function (tm)
if type (tm) == "table" then
return os.time (tm)
end
return lwcomp.get_worldtime ()
end
ENV.os.date = function (fmt, tm)
if not tm then
tm = lwcomp.get_worldtime ()
end
if not fmt then
fmt = "%c"
end
return os.date (fmt, tm)
end
ENV.os.get_event = function (event)
return coroutine.yield ("get_event", tostring (event or ""))
end
ENV.os.peek_event = function (event)
return computer.peek_event (tostring (event or ""))
end
ENV.os.remove_event = function (event)
return computer.remove_event (tostring (event or ""))
end
ENV.os.queue_event = function ( ... )
computer.queue_event ( ... )
end
ENV.os.sleep = function (secs)
coroutine.yield ("sleep", secs)
end
ENV.os.reboot = function ()
coroutine.yield ("reboot")
end
ENV.os.shutdown = function ()
coroutine.yield ("shutdown")
end
ENV.os.key_state = function (key)
if key then
if key == ENV.keys.KEY_CTRL then
return computer.ctrl
elseif key == ENV.keys.KEY_ALT then
return computer.alt
elseif key == ENV.keys.KEY_SHIFT then
return computer.shift
elseif key == ENV.keys.KEY_CAPS then
return computer.caps
end
return nil
end
return computer.ctrl, computer.alt, computer.shift, computer.caps
end
ENV.os.computer_id = function ()
return computer.computer_id ()
end
ENV.os.get_name = function ()
return computer.get_name ()
end
ENV.os.set_name = function (name)
return computer.set_name (name)
end
ENV.os.start_timer = function (secs)
return computer.start_timer (secs)
end
ENV.os.kill_timer = function (tid)
computer.kill_timer (tid)
end
ENV.os.chat = function (message, name)
computer.chat (message, name)
end
ENV.os.has_clipboard = function ()
return computer.has_clipboard ()
end
ENV.os.copy_to_clipboard = function (contents)
return computer.set_clipboard_contents (contents)
end
ENV.os.paste_from_clipboard = function ()
return computer.get_clipboard_contends ()
end
ENV.os.to_worldtime = function (secs)
return lwcomp.to_worldtime (secs)
end
ENV.os.to_realtime = function (secs)
return lwcomp.to_realtime (secs)
end
ENV.os.version = function ()
return lwcomputers.version ()
end
-- security
ENV.security = { }
ENV.security.add_access = function (name)
return computer.add_access (name)
end
ENV.security.remove_access = function (name)
return computer.remove_access (name)
end
ENV.security.access_list = function ()
return computer.access_list ()
end
ENV.security.owner = function ()
return computer.owner ()
end
-- string
-- thank you mesecons developers
ENV.string.rep = function (str, n)
if (#str * n) > lwcomp.settings.max_string_rep_size then
error ("string.rep string too long", 2)
end
return string.rep (str, n)
end
-- thank you mesecons developers
ENV.string.find = function (str, pattern , init)
return string.find(str, pattern , init, true)
end
-- fs
ENV.fs.remove = function (path)
return computer.filesys:remove (path)
end
ENV.fs.rename = function (oldname, newname)
return computer.filesys:rename (oldname, newname)
end
ENV.fs.mkdir = function (path)
return computer.filesys:mkdir (path)
end
ENV.fs.ls = function (path, types)
return computer.filesys:ls (path, types)
end
ENV.fs.file_type = function (path)
return computer.filesys:file_type (path)
end
ENV.fs.file_size = function (path)
return computer.filesys:file_size (path)
end
ENV.fs.file_exists = function (path, types)
return computer.filesys:file_exists (path, types)
end
ENV.fs.get_label = function (drivepath)
return computer.filesys:get_label (drivepath)
end
ENV.fs.set_label = function (drivepath, label)
return computer.filesys:set_label (drivepath, label)
end
ENV.fs.get_drive_id = function (drivepath)
return computer.filesys:get_drive_id (drivepath)
end
ENV.fs.copy_file = function (srcpath, destpath)
return computer.filesys:copy_file (srcpath, destpath)
end
ENV.fs.path_folder = function (path)
return computer.filesys:path_folder (path)
end
ENV.fs.path_name = function (path)
return computer.filesys:path_name (path)
end
ENV.fs.path_extension = function (path)
return computer.filesys:path_extension (path)
end
ENV.fs.path_title = function (path)
return computer.filesys:path_title (path)
end
ENV.fs.abs_path = function (basepath, relpath)
return computer.filesys:abs_path (basepath, relpath)
end
ENV.fs.disk_free = function (drivepath)
return computer.filesys:get_disk_free (drivepath)
end
ENV.fs.disk_size = function (drivepath)
return computer.filesys:get_disk_size (drivepath)
end
-- copy keys
for k, v in pairs (lwcomp.keys) do
ENV.keys[k] = v
end
-- term
-- copy colors
for k, v in pairs (lwcomp.colors) do
ENV.term.colors[k] = v
end
ENV.term.get_cursor = function ()
return computer.cursorx, computer.cursory
end
ENV.term.set_cursor = function (x, y)
x = tonumber (x or 0)
y = tonumber (y or 0)
if x < 0 then
x = 0
end
if x >= computer.width then
x = computer.width - 1
end
if y < 0 then
y = 0
end
if y >= computer.height then
y = computer.height - 1
end
computer.cursorx = x
computer.cursory = y
computer.redraw = true
end
ENV.term.set_blink = function (blink)
computer.blink = blink
computer.redraw = true
end
ENV.term.get_blink = function ()
return computer.blink
end
ENV.term.get_resolution = function ()
return computer.width, computer.height
end
-- stores current colors
local forecolor = lwcomp.colors.white
local backcolor = lwcomp.colors.black
ENV.term.get_colors = function ()
return forecolor, backcolor
end
ENV.term.set_colors = function (fg, bg)
if fg then
fg = tonumber (fg or lwcomp.colors.white)
forecolor = fg % 16
end
if bg then
bg = tonumber (bg or lwcomp.colors.black)
backcolor = bg % 16
end
end
ENV.term.set_char = function (x, y, char, fg, bg)
x = tonumber (x or -1)
y = tonumber (y or -1)
if x >= 0 and x < computer.width and y >= 0 and y < computer.height then
local c = computer.display[(y * computer.width) + x + 1]
if char ~= nil then
c.char = char % 256
end
if fg ~= nil then
c.fg = fg % 16
end
if bg ~= nil then
c.bg = bg % 16
end
end
computer.redraw = true
end
ENV.term.get_char = function (x, y)
x = tonumber (x or -1)
y = tonumber (y or -1)
if x >= 0 and x < computer.width and y >= 0 and y < computer.height then
local c = computer.display[(y * computer.width) + x + 1]
return c.char, c.fg, c.bg
end
return nil, nil, nil
end
ENV.term.write = function (str, fg, bg)
local d = computer.display
str = tostring (str or "")
local size = computer.width * computer.height
local base = (computer.cursory * computer.width) + computer.cursorx
if base < 0 then
base = 0
end
if base >= size then
return
end
for p = 1, str:len () do
if (base + p) > size then
return
end
local c = d[base + p]
c.char = str:byte (p) or 0
if fg ~= nil then
c.fg = fg % 16
else
c.fg = forecolor
end
if bg ~= nil then
c.bg = bg % 16
else
c.bg = backcolor
end
end
computer.redraw = true
end
ENV.term.blit = function (buff, x, y, w, h)
local d = computer.display
x = tonumber (x or 0)
y = tonumber (y or 0)
w = tonumber (w or computer.width)
h = tonumber (h or computer.height)
if w < 1 or h < 1 then
return false, "no size"
end
if type (buff) ~= "table" then
return false, "buffer not a table"
end
if #buff < (w * h) then
return false, "buffer too small"
end
for cy = 0, h - 1 do
for cx = 0, w - 1 do
local sx = x + cx
local sy = y + cy
if sx >= 0 and sx < computer.width and sy >= 0 and sy < computer.height then
local c = d[(sy * computer.width) + sx + 1]
local b = buff[(cy * w) + cx + 1]
if type (b) == "table" then
c.char = (b.char or 0) % 256
c.fg = (b.fg or lwcomp.colors.white) % 16
c.bg = (b.bg or lwcomp.colors.black) % 16
end
end
end
end
computer.redraw = true
return true
end
ENV.term.cache = function (x, y, w, h)
local buff = { }
local d = computer.display
x = tonumber (x or 0)
y = tonumber (y or 0)
w = tonumber (w or computer.width)
h = tonumber (h or computer.height)
if w < 1 or h < 1 then
return nil
end
for cy = 0, h - 1 do
for cx = 0, w - 1 do
local sx = x + cx
local sy = y + cy
if sx >= 0 and sx < computer.width and sy >= 0 and sy < computer.height then
local c = d[(sy * computer.width) + sx + 1]
buff[(cy * w) + cx + 1] =
{
char = c.char,
fg = c.fg,
bg = c.bg
}
else
-- if not defined leave transparent
buff[(cy * w) + cx + 1] = 0
end
end
end
return buff
end
ENV.term.clear = function (char, fg, bg, x, y, w, h)
local d = computer.display
char = ((char or 0) % 256)
fg = fg or forecolor
bg = bg or backcolor
x = tonumber (x or 0) or 0
y = tonumber (y or 0) or 0
w = tonumber (w or computer.width) or computer.width
h = tonumber (h or computer.height) or computer.height
for cy = 0, h - 1 do
for cx = 0, w - 1 do
local sx = x + cx
local sy = y + cy
if sx >= 0 and sx < computer.width and sy >= 0 and sy < computer.height then
local c = d[(sy * computer.width) + sx + 1]
c.char = char
c.fg = fg
c.bg = bg
end
end
end
computer.redraw = true
end
ENV.term.scroll = function (lines, x, y, w, h)
local d = computer.display
lines = tonumber (lines or 0) or 0
x = tonumber (x or 0) or 0
y = tonumber (y or 0) or 0
w = tonumber (w or computer.width) or computer.width
h = tonumber (h or computer.height) or computer.height
if x < 0 then
w = w + x
x = 0
end
if y < 0 then
h = h + y
y = 0
end
if (x + w) > computer.width then
w = computer.width - x
end
if (y + h) > computer.height then
h = computer.height - y
end
if w > 0 and h > 0 then
if lines > 0 then
for cy = (h - 1), 0, -1 do
for cx = 0, w - 1 do
local mx = x + cx
local ry = y + cy
local wy = y + cy + lines
if wy < computer.height then
local r = d[(ry * computer.width) + mx + 1]
local wr = d[(wy * computer.width) + mx + 1]
wr.char = r.char
wr.fg = r.fg
wr.bg = r.bg
end
end
end
computer.redraw = true
elseif lines < 0 then
for cy = 0, h - 1 do
for cx = 0, w - 1 do
local mx = x + cx
local ry = y + cy
local wy = y + cy + lines
if wy >= 0 then
local r = d[(ry * computer.width) + mx + 1]
local wr = d[(wy * computer.width) + mx + 1]
wr.char = r.char
wr.fg = r.fg
wr.bg = r.bg
end
end
end
computer.redraw = true
end
end
end
local function print_raw (fmt, ... )
local result, str = pcall (string.format, fmt, ... )
if not result then
error (str, 3)
end
local x = computer.cursorx
local y = computer.cursory
local d = computer.display
if x >= computer.width then
x = 0
y = y + 1
end
while y >= computer.height do
ENV.term.scroll (-1)
y = y - 1
ENV.term.clear (0, lwcomp.colors.white, lwcomp.colors.black,
0, computer.height - 1, computer.width, 1)
end
for p = 1, str:len () do
local char = str:byte (p)
if char == 10 then
x = 0
y = y + 1
elseif char == 13 then
x = 0
else
local c = d[(y * computer.width) + x + 1]
c.char = char
c.fg = forecolor
c.bg = backcolor
x = x + 1
end
if x >= computer.width then
x = 0
y = y + 1
end
while y >= computer.height do
ENV.term.scroll (-1)
y = y - 1
ENV.term.clear (0, lwcomp.colors.white, lwcomp.colors.black,
0, computer.height - 1, computer.width, 1)
end
end
computer.cursorx = x
computer.cursory = y
computer.redraw = true
end
ENV.term.print = function (fmt, ... )
print_raw (fmt, ... )
end
ENV.term.redraw = function (force)
computer.redraw_formspec (force)
end
ENV.term.invalidate = function ()
computer.redraw = true
end
-- utils
ENV.utils.serialize = function (data)
return minetest.serialize (data)
end
ENV.utils.deserialize = function (str)
return minetest.deserialize (str)
end
ENV.utils.parse_json = function (str, nullvalue)
return minetest.parse_json (str, nullvalue)
end
ENV.utils.write_json = function (data, styled)
return minetest.write_json (data, styled)
end
ENV.utils.compress = function (data, method, ... )
return minetest.compress (data, method, ... )
end