-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.lua
More file actions
746 lines (546 loc) · 13.6 KB
/
utils.lua
File metadata and controls
746 lines (546 loc) · 13.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
local lwcomp, mod_storage, http_api = ...
if minetest.get_translator and minetest.get_translator ("lwcomputers") then
lwcomp.S = minetest.get_translator ("lwcomputers")
elseif minetest.global_exists ("intllib") then
if intllib.make_gettext_pair then
lwcomp.S = intllib.make_gettext_pair ()
else
lwcomp.S = intllib.Getter ()
end
else
lwcomp.S = function (s) return s end
end
local function get_worldpath ()
local path = minetest.get_worldpath ()
local tokens = { }
local complete = { }
if path:sub (1, 1) == "/" then
tokens[1] = ""
end
for t in string.gmatch (path, "[^/]+") do
tokens[#tokens + 1] = t
end
for i = 1, #tokens do
if tokens[i] == ".." then
if #complete < 1 then
return nil, "invalid path"
end
table.remove (complete, #complete)
else
complete[#complete + 1] = tokens[i]
end
end
return table.concat (complete, "/")
end
lwcomp.modpath = minetest.get_modpath("lwcomputers")
lwcomp.worldpath = get_worldpath()
lwcomp.computer_data = { }
lwcomp.computer_list = minetest.deserialize (mod_storage:get_string ("computer_list") or "")
if type (lwcomp.computer_list) ~= "table" then
lwcomp.computer_list = { }
end
function lwcomp.store_computer_list ()
mod_storage:set_string ("computer_list",
minetest.serialize (lwcomp.computer_list))
end
-- check for mesecon
if minetest.global_exists ("mesecon") then
lwcomp.mesecon_supported = true
lwcomp.mesecon_state_on = mesecon.state.on
lwcomp.mesecon_state_off = mesecon.state.off
lwcomp.mesecon_receptor_on = mesecon.receptor_on
lwcomp.mesecon_receptor_off = mesecon.receptor_off
lwcomp.mesecon_default_rules = mesecon.rules.default
else
lwcomp.mesecon_supported = false
lwcomp.mesecon_state_on = "on"
lwcomp.mesecon_state_off = "off"
lwcomp.mesecon_default_rules = { }
-- dummies
lwcomp.mesecon_receptor_on = function (pos, rules)
end
lwcomp.mesecon_receptor_off = function (pos, rules)
end
end
-- check for digilines
if minetest.global_exists ("digilines") then
lwcomp.digilines_supported = true
lwcomp.digilines_receptor_send = digilines.receptor_send
else
lwcomp.digilines_supported = false
-- dummy
lwcomp.digilines_receptor_send = function (pos, rules, channel, msg)
end
end
-- check for lwwires
if minetest.global_exists ("lwwires") then
lwcomp.wires_supported = true
else
lwcomp.wires_supported = false
end
lwcomp.colors =
{
black = 0,
orange = 1,
magenta = 2,
sky = 3,
yellow = 4,
pink = 5,
cyan = 6,
gray = 7,
silver = 8,
red = 9,
green = 10,
blue = 11,
brown = 12,
lime = 13,
purple = 14,
white = 15
}
lwcomp.keys =
{
KEY_BACKSPACE = 8,
KEY_TAB = 9,
KEY_LINE = 10,
KEY_ENTER = 13,
KEY_ESC = 27, -- depreciated
KEY_ESCAPE = 27,
KEY_SPACE = 32,
KEY_EXCLAIM = 33,
KEY_QUOTE = 34,
KEY_HASH = 35,
KEY_CURRENCY = 36,
KEY_PERCENT = 37,
KEY_AMP = 38,
KEY_APOSTROPHE = 39,
KEY_OPENPAREN = 40,
KEY_CLOSEPAREN = 41,
KEY_MULTIPLY = 42,
KEY_ADD = 43,
KEY_COMMA = 44,
KEY_SUBTRACT = 45,
KEY_DOT = 46,
KEY_DIVIDE = 47,
KEY_0 = 48,
KEY_1 = 49,
KEY_2 = 50,
KEY_3 = 51,
KEY_4 = 52,
KEY_5 = 53,
KEY_6 = 54,
KEY_7 = 55,
KEY_8 = 56,
KEY_9 = 57,
KEY_COLON = 58,
KEY_SEMICOLON = 59,
KEY_LESS = 60,
KEY_EQUAL = 61,
KEY_GREATER = 62,
KEY_QUESTION = 63,
KEY_AT = 64,
KEY_A = 65,
KEY_B = 66,
KEY_C = 67,
KEY_D = 68,
KEY_E = 69,
KEY_F = 70,
KEY_G = 71,
KEY_H = 72,
KEY_I = 73,
KEY_J = 74,
KEY_K = 75,
KEY_L = 76,
KEY_M = 77,
KEY_N = 78,
KEY_O = 79,
KEY_P = 80,
KEY_Q = 81,
KEY_R = 82,
KEY_S = 83,
KEY_T = 84,
KEY_U = 85,
KEY_V = 86,
KEY_W = 87,
KEY_X = 88,
KEY_Y = 89,
KEY_Z = 90,
KEY_OPENSQUARE = 91,
KEY_SLASH = 92,
KEY_CLOSESQUARE = 93,
KEY_CARET = 94,
KEY_UNDERSCORE = 95,
KEY_TICK = 96,
KEY_OPENBRACE = 123,
KEY_BAR = 124,
KEY_CLOSEBRACE = 125,
KEY_TILDE = 126,
KEY_DELETE = 127,
KEY_INSERT = 128,
KEY_HOME = 129,
KEY_END = 130,
KEY_PAGEUP = 131,
KEY_PAGEDOWN = 132,
KEY_SHIFT = 133,
KEY_CAPS = 134,
KEY_CTRL = 135,
KEY_ALT = 136,
KEY_UP = 137,
KEY_DOWN = 138,
KEY_LEFT = 139,
KEY_RIGHT = 140,
KEY_F1 = 141,
KEY_F2 = 142,
KEY_F3 = 143,
KEY_F4 = 144,
KEY_F5 = 145,
KEY_F6 = 146,
KEY_F7 = 147,
KEY_F8 = 148,
KEY_F9 = 149,
KEY_F10 = 150,
KEY_F11 = 151,
KEY_F12 = 152
}
lwcomp.shift_keys =
{
KEY_TICK = lwcomp.keys.KEY_TILDE,
KEY_0 = lwcomp.keys.KEY_CLOSEPAREN,
KEY_1 = lwcomp.keys.KEY_EXCLAIM,
KEY_2 = lwcomp.keys.KEY_AT,
KEY_3 = lwcomp.keys.KEY_HASH,
KEY_4 = lwcomp.keys.KEY_CURRENCY,
KEY_5 = lwcomp.keys.KEY_PERCENT,
KEY_6 = lwcomp.keys.KEY_CARET,
KEY_7 = lwcomp.keys.KEY_AMP,
KEY_8 = lwcomp.keys.KEY_MULTIPLY,
KEY_9 = lwcomp.keys.KEY_OPENPAREN,
KEY_SUBTRACT = lwcomp.keys.KEY_UNDERSCORE,
KEY_EQUAL = lwcomp.keys.KEY_ADD,
KEY_OPENSQUARE = lwcomp.keys.KEY_OPENBRACE,
KEY_SLASH = lwcomp.keys.KEY_BAR,
KEY_CLOSESQUARE = lwcomp.keys.KEY_CLOSEBRACE,
KEY_SEMICOLON = lwcomp.keys.KEY_COLON,
KEY_APOSTROPHE = lwcomp.keys.KEY_QUOTE,
KEY_COMMA = lwcomp.keys.KEY_LESS,
KEY_DOT = lwcomp.keys.KEY_GREATER,
KEY_DIVIDE = lwcomp.keys.KEY_QUESTION
}
function lwcomp.http_fetch (request, computer)
if http_api and computer.id then
if not request then
return nil, "no request"
end
if type (request.url) ~= "string" then
return nil, "no url"
end
if request.url:len () < 1 then
return nil, "no url"
end
-- check white list
local found = false
for l = 1, #lwcomp.settings.http_white_list do
if string.match (request.url, lwcomp.settings.http_white_list[l]) then
found = true
end
end
if not found then
return nil, "denied"
end
if request.timeout then
if request.timeout > 30 then
request.timeout = 30
end
end
computer.timed_out = false
http_api.fetch (request, computer.http_callback)
computer.resumed_at = minetest.get_us_time ()
local result, msg = coroutine.yield ("http_fetch")
if result then
return computer.http_result
end
return nil, msg
end
return nil, "no http"
end
lwcomp.place_substitute = dofile (lwcomp.modpath.."/place_substitute.lua")
lwcomp.crafting_mods = dofile (lwcomp.modpath.."/crafting_mods.lua")
function lwcomp.get_place_substitute (item, dir)
local subst = lwcomp.place_substitute[item]
if subst then
if type (subst) == "table" then
if dir and type (subst[dir]) == "string" then
return subst[dir]
elseif type (subst[1]) == "string" then
return subst[1]
end
elseif type (subst) == "string" then
return subst
end
end
return item
end
function lwcomp.get_crafting_mods (item)
return lwcomp.crafting_mods[item]
end
function lwcomp.get_computer_data (id, pos)
local name = tostring (id)
local data = lwcomp.computer_data[name]
if not data then
if pos then
local meta = minetest.get_meta (pos)
data = lwcomp.new_computer (pos,
id,
meta:get_int ("persists") == 1,
meta:get_int ("robot") == 1)
lwcomp.computer_data[name] = data
lwcomp.computer_list[name] =
{
pos = { x = pos.x, y = pos.y, z = pos.z },
}
lwcomp.store_computer_list ()
end
elseif pos then
if data.pos.x ~= pos.x or data.pos.y ~= pos.y or data.pos.z ~= pos.z then
data.pos = { x = pos.x, y = pos.y, z = pos.z }
lwcomp.computer_list[name] =
{
pos = { x = pos.x, y = pos.y, z = pos.z },
}
if data.filesys then
data.filesys.pos = { x = pos.x, y = pos.y, z = pos.z }
end
lwcomp.store_computer_list ()
end
end
return data
end
function lwcomp.reset_computer_data (id, pos)
local name = tostring (id)
local meta = minetest.get_meta (pos)
local data = lwcomp.new_computer (pos,
id,
meta:get_int ("persists") == 1,
meta:get_int ("robot") == 1)
lwcomp.computer_data[name] = data
lwcomp.computer_list[name] =
{
pos = { x = pos.x, y = pos.y, z = pos.z },
}
lwcomp.store_computer_list ()
return data
end
function lwcomp.remove_computer_data (id)
local name = tostring (id)
lwcomp.computer_data[name] = nil
lwcomp.computer_list[name] = nil
lwcomp.store_computer_list ()
end
function lwcomp.send_message (sender_id, msg, target_id)
target_id = tonumber (target_id or 0)
msg = tostring (msg or "")
if target_id > 0 then
local target = tostring (target_id)
local stats = lwcomp.computer_list[target]
if stats then
local meta = minetest.get_meta (stats.pos)
if meta then
local id = meta:get_int ("lwcomputer_id")
if id == 0 or id ~= target_id then
-- no longer there
lwcomp.remove_computer_data (target_id)
elseif id == target_id and target_id ~= sender_id then
local data = lwcomp.get_computer_data (target_id, stats.pos)
if data then
data.queue_event ("wireless", msg, sender_id, target_id)
return true
end
end
end
-- else doesn't exist
end
else
-- broadcast
local remove_list = { }
for target, stats in pairs (lwcomp.computer_list) do
local meta = minetest.get_meta (stats.pos)
if meta then
local id = meta:get_int ("lwcomputer_id")
target_id = tonumber (target)
if id == 0 or id ~= target_id then
-- no longer there
remove_list[#remove_list + 1] = target_id
else
if target_id ~= sender_id then
local data = lwcomp.get_computer_data (target_id, stats.pos)
if data then
data.queue_event ("wireless", msg, sender_id, nil)
end
end
end
-- else doesn't exist
end
end
-- remove redundant machines
for c = 1, #remove_list do
lwcomp.remove_computer_data (remove_list[c])
end
return true
end
return false
end
function lwcomp.name_from_id (id)
id = tonumber (id or 0)
local name = nil
if id > 0 then
local stats = lwcomp.computer_list[tostring (id)]
if stats then
local meta = minetest.get_meta (stats.pos)
if meta then
if meta:get_int ("lwcomputer_id") == id then
name = meta:get_string ("name")
else
-- no longer there
lwcomp.remove_computer_data (id)
end
end
end
end
return name
end
function lwcomp.id_from_name (name)
name = tostring (name or "")
local id = nil
local remove_list = { }
for target, stats in pairs (lwcomp.computer_list) do
local meta = minetest.get_meta (stats.pos)
if meta then
local target_id = tonumber (target)
local test_id = meta:get_int ("lwcomputer_id")
if test_id == 0 or test_id ~= target_id then
-- no longer there
remove_list[#remove_list + 1] = target_id
else
if name == meta:get_string ("name") then
id = target_id
end
end
-- else out of range or doesn't exist
end
end
-- remove redundant machines
for c = 1, #remove_list do
lwcomp.remove_computer_data (remove_list[c])
end
return id
end
function lwcomp.get_worldtime ()
return ((minetest.get_timeofday () + minetest.get_day_count ()) * 86400) + lwcomp.settings.epoch_offset
end
function lwcomp.to_worldtime (secs)
if lwcomp.settings.time_scale > 0 then
return secs * lwcomp.settings.time_scale
end
return secs
end
function lwcomp.to_realtime (secs)
if lwcomp.settings.time_scale > 0 then
return secs / lwcomp.settings.time_scale
end
return secs
end
lwcomp.floppy_disk = { }
function lwcomp.is_floppy_disk (item)
return lwcomp.floppy_disk[item]
end
lwcomp.clipboards = { }
function lwcomp.is_clipboard (item)
return lwcomp.clipboards[item]
end
function lwcomp.can_interact_with_node (pos, player)
if not player or not player:is_player () then
return false
end
if minetest.check_player_privs (player, "protection_bypass") then
return true
end
local meta = minetest.get_meta (pos)
if meta then
local owner = meta:get_string ("owner")
local name = player:get_player_name ()
if not owner or owner == "" or owner == name then
return true
end
local access = meta:get_string ("access_by")
if access:len () > 0 then
local list = minetest.deserialize (access)
if list[name] then
return true
end
end
end
return false
end
function lwcomp.find_item_def (name)
local def = minetest.registered_items[name]
if not def then
def = minetest.registered_craftitems[name]
end
if not def then
def = minetest.registered_nodes[name]
end
if not def then
def = minetest.registered_tools[name]
end
return def
end
function lwcomp.to_hex (str)
local hex = ""
str = tostring (str) or ""
for i = 1, str:len () do
hex = hex..string.format ("%02X", str:byte (i))
end
return hex
end
function lwcomp.from_hex (hex)
local str = ""
hex = tostring (hex) or ""
for i = 1, hex:len (), 2 do
str = str..string.char (tonumber (hex:sub (i, i + 1), 16) or 0)
end
return str
end
function lwcomp.on_destroy (itemstack)
local stack = ItemStack (itemstack)
if stack and stack:get_count () > 0 then
local def = lwcomp.find_item_def (stack:get_name ())
if def and def.on_destroy then
def.on_destroy (stack)
end
end
end
function lwcomp.item_drop (itemstack, dropper, pos)
if itemstack then
local def = lwcomp.find_item_def (itemstack:get_name ())
if def and def.on_drop then
return def.on_drop (itemstack, dropper, pos)
end
end
return minetest.item_drop (itemstack, dropper, pos)
end
local minetest_version
do
local parts = { }
for n in string.gmatch (minetest.get_version ().string or "", "%d+") do
parts[#parts + 1] = n
end
minetest_version =
{
major = (tonumber (parts[1]) or 0),
minor = (tonumber (parts[2]) or 0),
patch = (tonumber (parts[3]) or 0)
}
end
function lwcomp.get_minetest_version ()
return minetest_version
end
--