-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainModule.lua
More file actions
752 lines (572 loc) · 16.9 KB
/
MainModule.lua
File metadata and controls
752 lines (572 loc) · 16.9 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
--[[
Updates:
The metatables for the function LoadCareFor() are now stable.
The table.deleteeall() function is back.
When a table is permanent lock and you call a function using the metatable, it will return a warn in the output and return the table and args..
Added arguments for the :UntrackIndexInstance and the :UntrackIndexPlayer functions.
Erased metatables bug patch.
WorkonClient delete
Added meta_save function (When save, you can load the table one time and after, the saved table will be deleted [Like a temporary table.])
Added comparators for the function TableCareFor(table).
Added SetIndexFunction.
Added UntrackToFunction to untrack a tracked function.
API:
local TableCare = require(game.ReplicatedStorage.TableCare)
Version: 1.0.2
Author:
ZeFreestyle
--]]
local TableCare = {LoadedTables = {}; TrackedEvents = {}; ChangedIdentities = {}; FunctionsSignals = {}; MetaLoadedTables = {}}
TableCare.__index = TableCare
local Signal = require(script.Signal)
local ClientAllowed = script:GetAttribute("WorkOnClient")
TableCare.NewIndexAdded = Signal.new()
local RunService = game:GetService("RunService")
local function _GetTableType(t)
local Number = 0
local Other = 0
for k, v in pairs(t) do
if typeof(k) == "number" then
Number += 1
else
Other += 1
end
end
if Number > Other then
return "Mixed"
else
return "Dictionnary"
end
end
function _ChangeIdentityRetry(tab, id, add_operator)
if not add_operator then add_operator = 0 end
add_operator += 1
if TableCare.ChangedIdentities[id] then
if TableCare.ChangedIdentities[id..add_operator] then
_ChangeIdentityRetry(tab, id, add_operator)
else
TableCare.ChangedIdentities[id..add_operator] = tab
return id..add_operator
end
else
TableCare.ChangedIdentities[id] = tab
return id
end
end
function _SpinNewId(check_table)
local id = math.random(1,10000000)
if check_table[id] then
_SpinNewId()
else
return id
end
end
function TableCare.checksimilar(tab1, tab2)
if not tab1 then error("[TableCare]: Error for the .checksimilar() function, argument #1 can't be find.") end
local common_elements = {}
for k, v in pairs(tab1) do
if table.find(tab2, v) then
table.insert(common_elements, v)
end
end
return common_elements
end
function TableCare.deleteall(tab, element)
if not tab then error("[TableCare]: Error for the .deleteall() function, argument #1 can't be find.") end
local get_event_id = math.random()
repeat
table.remove(tab, table.find(tab, element))
until not table.find(tab, element)
return tab
end
--[[function TableCare.clearwithout(tab, element_one, element_two, element_three)
if not tab then
error("[TableCare]: Error for the .clearwithout() function, argument #1 can't be find.")
end
local index = 0
for i = 1, #tab do
end
return tab
end--]]
function TableCare.kiss(tab1, tab2)
if not tab1 then error("[TableCare]: Error for the .kiss() function, argument #1 can't be find.") end
for k, v in pairs(tab2) do
table.insert(tab1, v)
end
return tab1
end
function TableCare.save_table(tab, id, erase)
if not tab then error("[TableCare]: Error for the .save_table() function, argument #1 can't be find.") end
if not erase then erase = false end
if not id then
local split = string.split(tostring(tab), " ")
table.remove(split, 1)
id = table.concat(split, " ")
end
if erase == false then
if TableCare.LoadedTables[id] then return error("[TableCare]: Id for the table "..tab.." is already used. Try to put a different id.") end
end
TableCare.LoadedTables[id] = {}
local t = _GetTableType(tab)
for k, v in pairs(tab) do
if t == "Dictionnary" then
TableCare.LoadedTables[id][k] = v
else
table.insert(TableCare.LoadedTables[id], v)
end
end
return id
end
function TableCare.meta_save(tab, save_id)
if not tab then error("[TableCare]: Error for the .save_table() function, argument #1 can't be find.") end
if not save_id then
local split = string.split(tostring(tab), " ")
table.remove(split, 1)
save_id = table.concat(split, " ")
end
TableCare.MetaLoadedTables[save_id] = {}
local t = _GetTableType(tab)
for k, v in pairs(tab) do
if t == "Dictionnary" then
TableCare.MetaLoadedTables[save_id][k] = v
else
table.insert(TableCare.MetaLoadedTables[save_id], v)
end
end
return save_id
end
function TableCare.load_table(tab_id, tab)
if not tab_id then error("[TableCare]: Error for the .load_table() function, argument #1 can't be find.") end
if not tab then tab = {} end
if TableCare.LoadedTables[tab_id] then
local t = _GetTableType(TableCare.LoadedTables[tab_id])
for k, v in pairs(TableCare.LoadedTables[tab_id]) do
if t == "Dictionnary" then
tab[k] = v
else
table.insert(tab, v)
end
end
return tab
end
if TableCare.MetaLoadedTables[tab_id] then
local t = _GetTableType(TableCare.MetaLoadedTables[tab_id])
for k, v in pairs(TableCare.MetaLoadedTables[tab_id]) do
if t == "Dictionnary" then
tab[k] = v
else
table.insert(tab, v)
end
end
TableCare.MetaLoadedTables[tab_id] = nil
return tab
end
end
function TableCare.unload_table(tab_id)
if not tab_id then error("[TableCare]: Error for the .unload_table() function, argument #1 can't be find.") end
TableCare.LoadedTables[tab_id] = nil
TableCare.MetaLoadedTables[tab_id] = nil
end
function TableCare.random_id(f)
local get_id
if f == "load" then
get_id = _SpinNewId(TableCare.LoadedTables)
elseif f == "track_event" then
get_id = _SpinNewId(TableCare.TrackedEvents)
else
get_id = math.random(1,10000000000)
end
return get_id
end
function TableCare.get_length(tab)
if not tab then error("[TableCare]: Error for the .get_length() function, argument #1 can't be find.") end
local count = 0
for index, value in pairs(tab) do
count += 1
end
return count
end
function TableCare:GetLoadedTables()
return self.LoadedTables
end
function TableCare:LoadCareFor(tab, automatic_convert_string_to_id, optional_basic_index)
if not tab then error("[TableCare]: Error for the :LoadPlusFor function, argument #1 can't be find.") end
if getmetatable(tab) == "Locked" then warn("The table is locked! No changement can be effectued.") return tab end
local meta = {}
if getmetatable(tab) then
meta = getmetatable(tab)
end
meta.__newindex = function(t, i, v)
self.NewIndexAdded:Fire(t, i, v)
end
meta.__index = function(t, i)
print("No index for the table " .. tostring(t))
return optional_basic_index
end
meta.__add = function(t1, t2)
local firstCount = 0
local secondCount = 0
if typeof(t1) == "table" then
for index, value in pairs(t1) do
firstCount += tonumber(value)
end
else
firstCount += tonumber(t1)
end
if typeof(t2) == "table" then
for index, value in pairs(t2) do
secondCount += tonumber(value)
end
else
secondCount += tonumber(t2)
end
return firstCount + secondCount
end
meta.__sub = function(t1,t2)
local count = 0
local sub_count = 0
for index, value in pairs(t1) do
count += tonumber(value)
end
if typeof(t2) == "table" then
for index, value in pairs(t2) do
sub_count += tonumber(value)
end
else
sub_count += tonumber(t2)
end
return count - sub_count
end
meta.__mult = function(t1,t2)
local first_count = 0
local second_count = 0
if typeof(t1) == "table" then
for index, value in pairs(t1) do
first_count += tonumber(value)
end
else
first_count += tonumber(t1)
end
if typeof(t2) == "table" then
for index, value in pairs(t2) do
second_count += tonumber(value)
end
else
second_count += tonumber(t2)
end
return first_count * second_count
end
meta.__div = function(t1,t2)
local firstCount = 0
local secondCount = 0
if typeof(t1) =="table" then
for index, value in pairs(t1) do
firstCount += tonumber(value)
end
else
firstCount += tonumber(t1)
end
if typeof(t2) =="table" then
for index, value in pairs(t2) do
secondCount += tonumber(value)
end
else
secondCount += tonumber(t2)
end
return firstCount / secondCount
end
meta.__eq = function(t1,t2)
local firstCount = 0
local secondCount = 0
if typeof(t1) == "table" then
for index, value in pairs(t1) do
firstCount += tonumber(value)
end
else
firstCount += tonumber(t1)
end
if typeof(t2) == "table" then
for index, value in pairs(t2) do
secondCount += tonumber(value)
end
else
secondCount += tonumber(t2)
end
if firstCount == secondCount then
return true
else
return false
end
end
meta.__lt = function(t, value)
local value_value = 0
local t_value = 0
if typeof(value) == "table" then
for index, v in pairs(value) do
value_value += tonumber(v)
end
else
value_value += tonumber(value)
end
if typeof(t) == "table" then
for index, v in pairs(value) do
value_value += tonumber(v)
end
else
value_value += tonumber(t)
end
if t_value < value_value then
return true
else
return false
end
end
meta.__le = function(t, value)
local value_value = 0
local t_value = 0
if typeof(value) == "table" then
for index, v in pairs(value) do
value_value += tonumber(v)
end
else
value_value += tonumber(value)
end
if typeof(t) == "table" then
for index, v in pairs(value) do
value_value += tonumber(v)
end
else
value_value += tonumber(t)
end
if t_value <= value_value then
return true
else
return false
end
end
if not automatic_convert_string_to_id == false or not automatic_convert_string_to_id == nil then
meta.__tostring = function(t)
return tostring(automatic_convert_string_to_id)
end;
end
setmetatable(tab, meta)
return tab
end
function TableCare:UnloadCareFor(tab)
if not tab then error("[TableCare]: Error for the :UnloadPlusFor function, argument #1 can't be find.") end
if getmetatable(tab) == "Locked" then warn("The table is locked! No changement can be effectued.") return tab end
setmetatable(tab, {})
return tab
end
function TableCare:TrackIndexPlayer(tab, bind_id, player, erase)
if not tab then error("[TableCare]: Error for the :TrackIndexPlayer function, argument #1 can't be find.") end
if getmetatable(tab) == "Locked" then warn("The table is locked! No changement can be effectued.") return tab end
if typeof(player) == "string" then
if game.Players:FindFirstChild(player) then
player = game.Players:FindFirstChild(player)
else
error("[TableCare]: Player can't be find in the players class.")
end
end
if not bind_id then
bind_id = _SpinNewId(self.TrackedEvents)
end
if self.TrackedEvents[bind_id] then
if erase == true then
self.TrackedEvents[bind_id] = Signal.new()
else
error("[TableCare]: This id is already used for a bind. Try with another id.")
end
else
self.TrackedEvents[bind_id] = Signal.new()
end
local meta = {}
if getmetatable(tab) then
meta = getmetatable(tab)
end
meta.__newindex = function(t, i, v)
local p = player
if v == p then
self.TrackedEvents[bind_id]:Fire(v)
end
end
setmetatable(tab, meta)
return tab, self.TrackedEvents[bind_id], bind_id
end
function TableCare:UntrackIndexPlayer(bind_id, optional_tab_use)
if not bind_id then error("[TableCare]: Error for the :UntrackIndexPlayer function, argument #1 can't be find.") end
local tab = optional_tab_use or {}
local meta = {}
if getmetatable(tab) then
meta = getmetatable(tab)
end
meta.__newindex = function(t,i,v)
end
setmetatable(tab, meta)
if bind_id then
self.TrackedEvents[bind_id]:Disconnect()
self.TrackedEvents[bind_id] = nil
end
return tab
end
function TableCare:TrackIndexInstance(tab, inst, bind_id, erase)
if not tab then error("[TableCare]: Error for the :TrackIndexInstance function, argument #1 can't be find.") end
if not typeof(inst) == "Instance" then
error("[TableCare]: Argument #2 for the function TrackInstance must be an Instance value.")
end
if getmetatable(tab) == "Locked" then warn("The table is locked! No changement can be effectued.") return tab, nil, nil end
if not bind_id then
bind_id = _SpinNewId(self.TrackedEvents)
end
if self.TrackedEvents[bind_id] then
if erase == true then
self.TrackedEvents[bind_id] = Signal.new()
else
error("[TableCare]: This id is already used for a bind. Try with another id.")
end
else
self.TrackedEvents[bind_id] = Signal.new()
end
local meta = {}
if getmetatable(tab) then
meta = getmetatable(tab)
end
meta.__newindex = function(t,i,v)
local a = inst
if typeof(v) == "Instance" then
if v:IsA(a) then
self.TrackedEvents[bind_id]:Fire(v)
end
end
end
setmetatable(tab, meta)
return tab, self.TrackedEvents[bind_id], bind_id
end
function TableCare:UntrackIndexInstance(bind_id, tab1)
if not bind_id then error("[TableCare]: Error for the :UntrackIndexInstance function, argument #2 can't be find.") end
local tab = tab1 or {}
local meta = {}
if getmetatable(tab) then
meta = getmetatable(tab)
end
meta.__newindex = function(t, i, v)
end
setmetatable(tab, meta)
if bind_id then
self.TrackedEvents[bind_id]:Disconnect()
self.TrackedEvents[bind_id] = nil
end
return tab
end
function TableCare:TrackToFunction(tab, f)
if not tab then error("[TableCare]: Error for the :TrackToFunction function, argument #1 can't be find.") end
if getmetatable(tab) == "Locked" then warn("The table is locked! No changement can be effectued.") return tab end
if not f then
f = function()
warn("[TableCare]: Nothing in this function. Try to set a function in the argument #2 for :TrackToFunction")
end
end
if not tab then
error("[TableCare]: Error for the :ToFunction function, argument #1 can't be find.")
end
local meta = {}
if getmetatable(tab) then
meta = getmetatable(tab)
end
meta.__call = f
setmetatable(tab, meta)
return tab
end
function TableCare:UntrackToFunction(tab)
if not tab then error("[TableCare]: Error for the :TrackToFunction function, argument #1 can't be find.") end
if getmetatable(tab) == "Locked" then warn("The table is locked! No changement can be effectued.") return tab end
local meta = {}
if getmetatable(tab) then
meta = getmetatable(tab)
end
meta.__call = function()
error("[TableCare]: A table can not be called like a function.")
end
setmetatable(tab, meta)
return meta
end
function TableCare:ChangeIdentity(tab, id)
if not tab then error("[TableCare]: Error for the :ChangeIdentity function, argument #1 can't be find.") end
if getmetatable(tab) == "Locked" then warn("The table is locked! No changement can be effectued.") return tab end
if not id then
id = math.random(1,10000000)
end
local get_real_id = _ChangeIdentityRetry(tab, id)
local meta = {}
if getmetatable(tab) then
meta = getmetatable(tab)
end
meta.__concat = function(t1, t2)
if self.ChangedIdentities[get_real_id] == t1 then
return get_real_id .. t2
else
return t1 .. get_real_id
end
end
setmetatable(tab, meta)
return tab
end
function TableCare:PermanentLockToAccess(tab)
if not tab then error("[TableCare]: Error for the :PermanentLockToAccess function, argument #1 can't be find.") end
if getmetatable(tab) == "Locked" then warn("The table is locked! No changement can be effectued.") return tab end
local meta = {}
if getmetatable(tab) then
meta = getmetatable(tab)
meta.__metatable = "Locked"
else
meta.__metatable = "Locked"
setmetatable(tab, meta)
end
return tab
end
function TableCare:SetDefaultIndex(tab, value)
if not tab then error("[TableCare]: Error for the :SetDefaultIndex function, argument #1 can't be find.") end
if getmetatable(tab) == "Locked" then warn("The table is locked! No changement can be effectued.") return tab end
local meta = {}
if getmetatable(tab) then
meta = getmetatable(tab)
end
meta.__index = function(t, i)
return value
end
setmetatable(tab, meta)
return tab
end
function TableCare:SetIndexFunction(tab, f)
if not tab then error("[TableCare]: Error for the :SetDefaultIndex function, argument #1 can't be find.") end
if getmetatable(tab) == "Locked" then warn("The table is locked! No changement can be effectued.") return tab end
if not typeof(f) == "function" then warn("[TableCare]: The argument #2 of the function SetIndexFunction must be a function.") return tab end
local meta = {}
if getmetatable(tab) then
meta = getmetatable(tab)
end
meta.__index = f
setmetatable(tab, meta)
return tab
end
function TableCare:GetTrackedEvents()
return self.TrackedEvents
end
function TableCare:ClearLoadedTables()
table.clear(self.LoadedTables)
end
function TableCare:ClearTrackedEvents()
table.clear(self.TrackedEvents)
end
setmetatable(TableCare, {
__index = function(object, value)
error("Attempt to get ".. tostring(value) .. " (not a valid member).")
end,
__newindex = function(object, index, value)
error("Attempt to get " .. tostring(value) .. " (not a valid member).")
end,
__call = function(tab)
error("Attempt to call the " .. tostring(tab).. " (not a valid function).")
end,
})
return TableCare