-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSupportLibrary.lua
More file actions
754 lines (555 loc) · 18.4 KB
/
SupportLibrary.lua
File metadata and controls
754 lines (555 loc) · 18.4 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
SupportLibrary = {};
function SupportLibrary.FindTableOccurrences(Haystack, Needle)
-- Returns the positions of instances of `needle` in table `haystack`
local Positions = {};
-- Add any indexes from `Haystack` that are `Needle`
for Index, Value in pairs(Haystack) do
if Value == Needle then
table.insert(Positions, Index);
end;
end;
return Positions;
end;
function SupportLibrary.FindTableOccurrence(Haystack, Needle)
-- Returns one occurrence of `Needle` in `Haystack`
-- Search for the first instance of `Needle` found and return it
for Index, Value in pairs(Haystack) do
if Value == Needle then
return Index;
end;
end;
-- If no occurrences exist, return `nil`
return nil;
end;
function SupportLibrary.IsInTable(Haystack, Needle)
-- Returns whether the given `Needle` can be found within table `Haystack`
-- Go through every value in `Haystack` and return whether `Needle` is found
for _, Value in pairs(Haystack) do
if Value == Needle then
return true;
end;
end;
-- If no instances were found, return false
return false;
end;
function SupportLibrary.DoTablesMatch(A, B)
-- Returns whether the values of tables A and B are the same
-- Check B table differences
for Index in pairs(A) do
if A[Index] ~= B[Index] then
return false;
end;
end;
-- Check A table differences
for Index in pairs(B) do
if B[Index] ~= A[Index] then
return false;
end;
end;
-- Return true if no differences
return true;
end;
function SupportLibrary.Round(Number, Places)
-- Returns `Number` rounded to the given number of decimal places (from lua-users)
-- Ensure that `Number` is a number
if type(Number) ~= 'number' then
return;
end;
-- Round the number
local Multiplier = 10 ^ (Places or 0);
local RoundedNumber = math.floor(Number * Multiplier + 0.5) / Multiplier;
-- Return the rounded number
return RoundedNumber;
end;
function SupportLibrary.CloneTable(Table)
-- Returns a copy of `Table`
local ClonedTable = {};
-- Copy all values into `ClonedTable`
for Key, Value in pairs(Table) do
ClonedTable[Key] = Value;
end;
-- Return the clone
return ClonedTable;
end;
function SupportLibrary.GetAllDescendants(Parent)
-- Recursively gets all the descendants of `Parent` and returns them
local Descendants = {};
for _, Child in pairs(Parent:GetChildren()) do
-- Add the direct descendants of `Parent`
table.insert(Descendants, Child);
-- Add the descendants of each child
for _, Subchild in pairs(SupportLibrary.GetAllDescendants(Child)) do
table.insert(Descendants, Subchild);
end;
end;
return Descendants;
end;
function SupportLibrary.GetDescendantCount(Parent)
-- Recursively gets a count of all the descendants of `Parent` and returns them
local Count = 0;
for _, Child in pairs(Parent:GetChildren()) do
-- Count the direct descendants of `Parent`
Count = Count + 1;
-- Count and add the descendants of each child
Count = Count + SupportLibrary.GetDescendantCount(Child);
end;
return Count;
end;
function SupportLibrary.CloneParts(Parts)
-- Returns a table of cloned `Parts`
local Clones = {};
-- Copy the parts into `Clones`
for Index, Part in pairs(Parts) do
Clones[Index] = Part:Clone();
end;
return Clones;
end;
function SupportLibrary.SplitString(String, Delimiter)
-- Returns a table of string `String` split by pattern `Delimiter`
local StringParts = {};
local Pattern = ('([^%s]+)'):format(Delimiter);
-- Capture each separated part
String:gsub(Pattern, function (Part)
table.insert(StringParts, Part);
end);
return StringParts;
end;
function SupportLibrary.GetChildOfClass(Parent, ClassName, Inherit)
-- Returns the first child of `Parent` that is of class `ClassName`
-- or nil if it couldn't find any
-- Look for a child of `Parent` of class `ClassName` and return it
if not Inherit then
for _, Child in pairs(Parent:GetChildren()) do
if Child.ClassName == ClassName then
return Child;
end;
end;
else
for _, Child in pairs(Parent:GetChildren()) do
if Child:IsA(ClassName) then
return Child;
end;
end;
end;
return nil;
end;
function SupportLibrary.GetChildrenOfClass(Parent, ClassName, Inherit)
-- Returns a table containing the children of `Parent` that are
-- of class `ClassName`
local Matches = {};
if not Inherit then
for _, Child in pairs(Parent:GetChildren()) do
if Child.ClassName == ClassName then
table.insert(Matches, Child);
end;
end;
else
for _, Child in pairs(Parent:GetChildren()) do
if Child:IsA(ClassName) then
table.insert(Matches, Child);
end;
end;
end;
return Matches;
end;
function SupportLibrary.HSVToRGB(Hue, Saturation, Value)
-- Returns the RGB equivalent of the given HSV-defined color
-- (adapted from some code found around the web)
-- If it's achromatic, just return the value
if Saturation == 0 then
return Value;
end;
-- Get the hue sector
local HueSector = math.floor(Hue / 60);
local HueSectorOffset = (Hue / 60) - HueSector;
local P = Value * (1 - Saturation);
local Q = Value * (1 - Saturation * HueSectorOffset);
local T = Value * (1 - Saturation * (1 - HueSectorOffset));
if HueSector == 0 then
return Value, T, P;
elseif HueSector == 1 then
return Q, Value, P;
elseif HueSector == 2 then
return P, Value, T;
elseif HueSector == 3 then
return P, Q, Value;
elseif HueSector == 4 then
return T, P, Value;
elseif HueSector == 5 then
return Value, P, Q;
end;
end;
function SupportLibrary.RGBToHSV(Red, Green, Blue)
-- Returns the HSV equivalent of the given RGB-defined color
-- (adapted from some code found around the web)
local Hue, Saturation, Value;
local MinValue = math.min(Red, Green, Blue);
local MaxValue = math.max(Red, Green, Blue);
Value = MaxValue;
local ValueDelta = MaxValue - MinValue;
-- If the color is not black
if MaxValue ~= 0 then
Saturation = ValueDelta / MaxValue;
-- If the color is purely black
else
Saturation = 0;
Hue = -1;
return Hue, Saturation, Value;
end;
if Red == MaxValue then
Hue = (Green - Blue) / ValueDelta;
elseif Green == MaxValue then
Hue = 2 + (Blue - Red) / ValueDelta;
else
Hue = 4 + (Red - Green) / ValueDelta;
end;
Hue = Hue * 60;
if Hue < 0 then
Hue = Hue + 360;
end;
return Hue, Saturation, Value;
end;
function SupportLibrary.IdentifyCommonItem(Items)
-- Returns the common item in table `Items`, or `nil` if
-- they vary
local CommonItem = nil;
for ItemIndex, Item in pairs(Items) do
-- Set the initial item to compare against
if ItemIndex == 1 then
CommonItem = Item;
-- Check if this item is the same as the rest
else
-- If it isn't the same, there is no common item, so just stop right here
if Item ~= CommonItem then
return nil;
end;
end;
end;
-- Return the common item
return CommonItem;
end;
function SupportLibrary.IdentifyCommonProperty(Items, Property)
-- Returns the common `Property` value in the instances given in `Items`
local PropertyVariations = {};
-- Capture all the variations of the property value
for _, Item in pairs(Items) do
table.insert(PropertyVariations, Item[Property]);
end;
-- Return the common property value
return SupportLibrary.IdentifyCommonItem(PropertyVariations);
end;
function SupportLibrary.CreateSignal()
-- Returns a ROBLOX-like signal for connections (RbxUtility's is buggy)
local Signal = {
Connections = {};
-- Provide a function to connect an event handler
Connect = function (Signal, Handler)
-- Register the handler
table.insert(Signal.Connections, Handler);
-- Return a controller for this connection
local ConnectionController = {
-- Include a reference to the connection's handler
Handler = Handler;
-- Provide a way to disconnect this connection
Disconnect = function (Connection)
local ConnectionSearch = SupportLibrary.FindTableOccurrences(Signal.Connections, Connection.Handler);
if #ConnectionSearch > 0 then
local ConnectionIndex = ConnectionSearch[1];
table.remove(Signal.Connections, ConnectionIndex);
end;
end;
};
-- Add compatibility aliases
ConnectionController.disconnect = ConnectionController.Disconnect;
-- Return the connection's controller
return ConnectionController;
end;
-- Provide a function to trigger any connections' handlers
Fire = function (Signal, ...)
for _, Connection in pairs(Signal.Connections) do
Connection(...);
end;
end;
};
-- Add compatibility aliases
Signal.connect = Signal.Connect;
Signal.fire = Signal.Fire;
return Signal;
end;
function SupportLibrary.GetPartCorners(Part)
-- Returns a table of the given part's corners' CFrames
-- Make references to functions called a lot for efficiency
local Insert = table.insert;
local ToWorldSpace = CFrame.new().toWorldSpace;
local NewCFrame = CFrame.new;
-- Get info about the part
local PartCFrame = Part.CFrame;
local SizeX, SizeY, SizeZ = Part.Size.x / 2, Part.Size.y / 2, Part.Size.z / 2;
-- Get each corner
local Corners = {};
Insert(Corners, ToWorldSpace(PartCFrame, NewCFrame(SizeX, SizeY, SizeZ)));
Insert(Corners, ToWorldSpace(PartCFrame, NewCFrame(-SizeX, SizeY, SizeZ)));
Insert(Corners, ToWorldSpace(PartCFrame, NewCFrame(SizeX, -SizeY, SizeZ)));
Insert(Corners, ToWorldSpace(PartCFrame, NewCFrame(SizeX, SizeY, -SizeZ)));
Insert(Corners, ToWorldSpace(PartCFrame, NewCFrame(-SizeX, SizeY, -SizeZ)));
Insert(Corners, ToWorldSpace(PartCFrame, NewCFrame(-SizeX, -SizeY, SizeZ)));
Insert(Corners, ToWorldSpace(PartCFrame, NewCFrame(SizeX, -SizeY, -SizeZ)));
Insert(Corners, ToWorldSpace(PartCFrame, NewCFrame(-SizeX, -SizeY, -SizeZ)));
return Corners;
end;
function SupportLibrary.CreatePart(PartType)
-- Creates and returns new part based on `PartType` with sensible defaults
local NewPart;
if PartType == 'Normal' then
NewPart = Instance.new('Part');
NewPart.Size = Vector3.new(4, 1, 2);
elseif PartType == 'Truss' then
NewPart = Instance.new('TrussPart');
elseif PartType == 'Wedge' then
NewPart = Instance.new('WedgePart');
NewPart.Size = Vector3.new(4, 1, 2);
elseif PartType == 'Corner' then
NewPart = Instance.new('CornerWedgePart');
elseif PartType == 'Cylinder' then
NewPart = Instance.new('Part');
NewPart.Shape = 'Cylinder';
NewPart.TopSurface = Enum.SurfaceType.Smooth;
NewPart.BottomSurface = Enum.SurfaceType.Smooth;
NewPart.Size = Vector3.new(2, 2, 2);
elseif PartType == 'Ball' then
NewPart = Instance.new('Part');
NewPart.Shape = 'Ball';
NewPart.TopSurface = Enum.SurfaceType.Smooth;
NewPart.BottomSurface = Enum.SurfaceType.Smooth;
elseif PartType == 'Seat' then
NewPart = Instance.new('Seat');
NewPart.Size = Vector3.new(4, 1, 2);
elseif PartType == 'Vehicle Seat' then
NewPart = Instance.new('VehicleSeat');
NewPart.Size = Vector3.new(4, 1, 2);
elseif PartType == 'Spawn' then
NewPart = Instance.new('SpawnLocation');
NewPart.Size = Vector3.new(4, 1, 2);
end;
-- Make sure the part is anchored
NewPart.Anchored = true;
return NewPart;
end;
function SupportLibrary.ImportServices()
-- Adds references to common services into the calling environment
-- Get the calling environment
local CallingEnvironment = getfenv(2);
-- Add the services
CallingEnvironment.Workspace = Game:GetService 'Workspace';
CallingEnvironment.Players = Game:GetService 'Players';
CallingEnvironment.MarketplaceService = Game:GetService 'MarketplaceService';
CallingEnvironment.ContentProvider = Game:GetService 'ContentProvider';
CallingEnvironment.SoundService = Game:GetService 'SoundService';
CallingEnvironment.UserInputService = Game:GetService 'UserInputService';
CallingEnvironment.SelectionService = Game:GetService 'Selection';
CallingEnvironment.CoreGui = Game:GetService 'CoreGui';
CallingEnvironment.HttpService = Game:GetService 'HttpService';
CallingEnvironment.ChangeHistoryService = Game:GetService 'ChangeHistoryService';
CallingEnvironment.ReplicatedStorage = Game:GetService 'ReplicatedStorage';
CallingEnvironment.GroupService = Game:GetService 'GroupService';
CallingEnvironment.ServerScriptService = Game:GetService 'ServerScriptService';
CallingEnvironment.StarterGui = Game:GetService 'StarterGui';
CallingEnvironment.RunService = Game:GetService 'RunService';
end;
function SupportLibrary.GetListMembers(List, MemberName)
-- Gets the given member for each object in the given list table
local Members = {};
-- Collect the member values for each item in the list
for _, Item in pairs(List) do
table.insert(Members, Item[MemberName]);
end;
-- Return the members
return Members;
end;
function SupportLibrary.AddUserInputListener(InputState, InputType, CatchAll, Callback)
-- Connects to the given user input event and takes care of standard boilerplate code
-- Turn the given `InputType` string into a proper enum
local InputType = Enum.UserInputType[InputType];
-- Create a UserInputService listener based on the given `InputState`
return Game:GetService('UserInputService')['Input' .. InputState]:connect(function (Input, GameProcessedEvent)
-- Make sure this input was not captured by the client (unless `CatchAll` is enabled)
if GameProcessedEvent and not CatchAll then
return;
end;
-- Make sure this is the right input type
if Input.UserInputType ~= InputType then
return;
end;
-- Make sure any key input did not occur while typing into a UI
if InputType == Enum.UserInputType.Keyboard and Game:GetService('UserInputService'):GetFocusedTextBox() then
return;
end;
-- Call back upon passing all conditions
Callback(Input);
end);
end;
function SupportLibrary.AddGuiInputListener(Gui, InputState, InputType, CatchAll, Callback)
-- Connects to the given GUI user input event and takes care of standard boilerplate code
-- Turn the given `InputType` string into a proper enum
local InputType = Enum.UserInputType[InputType];
-- Create a UserInputService listener based on the given `InputState`
return Gui['Input' .. InputState]:connect(function (Input, GameProcessedEvent)
-- Make sure this input was not captured by the client (unless `CatchAll` is enabled)
if GameProcessedEvent and not CatchAll then
return;
end;
-- Make sure this is the right input type
if Input.UserInputType ~= InputType then
return;
end;
-- Call back upon passing all conditions
Callback(Input);
end);
end;
function SupportLibrary.AreKeysPressed(...)
-- Returns whether the given keys are pressed
local RequestedKeysPressed = 0;
-- Get currently pressed keys
local PressedKeys = SupportLibrary.GetListMembers(Game:GetService('UserInputService'):GetKeysPressed(), 'KeyCode');
-- Go through each requested key
for _, Key in pairs({ ... }) do
-- Count requested keys that are pressed
if SupportLibrary.IsInTable(PressedKeys, Key) then
RequestedKeysPressed = RequestedKeysPressed + 1;
end;
end;
-- Return whether all the requested keys are pressed or not
return RequestedKeysPressed == #{...};
end;
function SupportLibrary.ConcatTable(DestinationTable, SourceTable)
-- Inserts all values of SourceTable into DestinationTable
-- Add each value from `SourceTable` into `DestinationTable`
for _, Value in pairs(SourceTable) do
table.insert(DestinationTable, Value);
end;
-- Return the destination table
return DestinationTable;
end;
function SupportLibrary.ClearTable(Table)
-- Clears out every value in `Table`
-- Clear each index
for Index in pairs(Table) do
Table[Index] = nil;
end;
-- Return the given table
return Table;
end;
function SupportLibrary.Values(Table)
-- Returns all the values in the given table
local Values = {};
-- Go through each key and get each value
for _, Value in pairs(Table) do
table.insert(Values, Value);
end;
-- Return the values
return Values;
end;
function SupportLibrary.Keys(Table)
-- Returns all the keys in the given table
local Keys = {};
-- Go through each key and get each value
for Key in pairs(Table) do
table.insert(Keys, Key);
end;
-- Return the values
return Keys;
end;
function SupportLibrary.Call(Function, ...)
-- Returns a callback to `Function` with the given arguments
local Args = { ... };
return function (...)
Function(unpack(
SupportLibrary.ConcatTable(SupportLibrary.CloneTable(Args), { ... })
));
end;
end;
function SupportLibrary.Trim(String)
-- Returns a trimmed version of `String` (adapted from code from lua-users)
return (String:gsub("^%s*(.-)%s*$", "%1"));
end
function SupportLibrary.ChainCall(...)
-- Returns function that passes arguments through given functions and returns the final result
-- Get the given chain of functions
local Chain = { ... };
-- Return the chaining function
return function (...)
-- Get arguments
local Arguments = { ... };
-- Go through each function and store the returned data to reuse in the next function's arguments
for _, Function in ipairs(Chain) do
Arguments = { Function(unpack(Arguments)) };
end;
-- Return the final returned data
return unpack(Arguments);
end;
end;
function SupportLibrary.CountKeys(Table)
-- Returns the number of keys in `Table`
local Count = 0;
-- Count each key
for _ in pairs(Table) do
Count = Count + 1;
end;
-- Return the count
return Count;
end;
function SupportLibrary.Slice(Table, Start, End)
-- Returns values from `Start` to `End` in `Table`
local Slice = {};
-- Go through the given indices
for Index = Start, End do
table.insert(Slice, Table[Index]);
end;
-- Return the slice
return Slice;
end;
function SupportLibrary.FlipTable(Table)
-- Returns a table with keys and values in `Table` swapped
local FlippedTable = {};
-- Flip each key and value
for Key, Value in pairs(Table) do
FlippedTable[Value] = Key;
end;
-- Return the flipped table
return FlippedTable;
end;
function SupportLibrary.ScheduleRecurringTask(TaskFunction, Interval)
-- Repeats `Task` every `Interval` seconds until stopped
-- Create a task object
local Task = {
-- A switch determining if it's running or not
Running = true;
-- A function to stop this task
Stop = function (Task)
Task.Running = false;
end;
-- References to the task function and set interval
TaskFunction = TaskFunction;
Interval = Interval;
};
coroutine.wrap(function (Task)
-- Repeat the task
while wait(Task.Interval) and Task.Running do
Task.TaskFunction();
end;
end)(Task);
-- Return the task object
return Task;
end;
function SupportLibrary.Clamp(Number, Minimum, Maximum)
-- Returns the given number, clamped according to the provided min/max
-- Clamp the number
if Minimum and Number < Minimum then
Number = Minimum;
elseif Maximum and Number > Maximum then
Number = Maximum;
end;
-- Return the clamped number
return Number;
end;
return SupportLibrary;