-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariables.lua
379 lines (294 loc) · 8.09 KB
/
Variables.lua
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
--!strict
type VariableData = {
Name: string?,
Value: any,
Order: number,
Lookup: any?,
Class: string?,
Comment: string?
}
type Table = {
[any]: any
}
type VariablesDict = {
[any]: VariableData
}
export type ClassDict = {
VariableCount: number,
Variables: VariablesDict
}
type Module = {
VariablesDict: Table,
VariableLookup: Table,
InstanceQueue: Table,
NoNameCount: number,
VariableBase: string
}
local Globals = getfenv(1)
--// Variable pre-render functions
local RenderFuncs = {
["Instance"] = function(self, Items: Table)
local Parser = self.Parser
local Formatter = self.Formatter
local AllParents, ObjectParents = self:BulkCollectParents(Items)
local Duplicates = self:FindDuplicates(AllParents)
--// Make duplicates into variables
for _, Object: Instance in next, Duplicates do
local Path, ParentsCount = Parser:MakePathString({
Object = Object
})
--// Check the parent count to prevent single paths
if ParentsCount < 3 then continue end
local Name = Formatter:MakeName(Object)
--// Make variable
self:MakeVariable({
Lookup = Object,
Name = Name,
--Comment = "Compressed duplicate",
Value = Path
})
end
end,
}
local Module = {
VariableBase = "Jit"
}
Module.__index = Module
local function MultiInsert(Table: Table, ToInsert: Table)
for _, Value in next, ToInsert do
table.insert(Table, Value)
end
end
function Module.new(Values)
local Class = {
VariablesDict = {},
VariableLookup = {},
InstanceQueue = {},
VariableNames = {},
NoNameCount = 0
}
return setmetatable(Class, Module) :: Module
end
function Module:GetNoNameCount(): number
return self.NoNameCount
end
function Module:AddVariableToClass(ClassDict: ClassDict, Data: VariableData)
--// Variable data
local Value = Data.Value
local Lookup = Data.Lookup or Value
ClassDict.VariableCount += 1
--// Class data
local Position = ClassDict.VariableCount
local Variables = ClassDict.Variables
Data.Order = Position
Variables[Lookup] = Data
end
function Module:GetClassDict(Class: string): ClassDict
local Variables = self.VariablesDict
local ClassDict = Variables[Class]
--// Return existing
if ClassDict then return ClassDict end
--// Create class dict
ClassDict = {
VariableCount = 0,
Variables = {}
}
Variables[Class] = ClassDict
return ClassDict
end
function Module:IsGlobal(Value: (string|Instance)): boolean
local IndexFunc = self.IndexFunc
--// Check based on instance name
if typeof(Value) == "Instance" then
local Name = IndexFunc(Value, "Name")
return Globals[Name] == Value
end
return Globals[Value] and Value or false
end
function Module:IsService(Object: Instance): boolean
local IndexFunc = self.IndexFunc
local ClassName = IndexFunc(Object, "ClassName")
--// Check if object is a service based on the ClassName
local Success = pcall(function()
return game:GetService(ClassName)
end)
return Success and ClassName or false
end
function Module:IncreaseNameUseCount(Name: string): number
if not Name then return 0 end
local VariableNames = self.VariableNames
local NameUseCount = VariableNames[Name]
--// Create missing dict
if not NameUseCount then
NameUseCount = 0
VariableNames[Name] = NameUseCount
end
VariableNames[Name] += 1
return NameUseCount
end
function Module:IncreaseNoNameCount(): number
self.NoNameCount += 1
return self.NoNameCount
end
function Module:CheckName(Data): string
local Name = Data.Name
local NameUseCount = self:IncreaseNameUseCount(Name)
--// Check if the variable already has defined name
if Name then
if NameUseCount <= 0 then
return Name
else
return `{Name}{NameUseCount}`
end
end
--// Create a default variable name
local NoNameCount = self:IncreaseNoNameCount()
--// Format VariableBase string
local Base = self.VariableBase
return Base:format(NoNameCount)
end
function Module:GetVariable(Value): VariableData?
local VariableLookup = self.VariableLookup
return VariableLookup[Value]
end
function Module:OrderVariables(Variables: VariablesDict): Table
local Ordered = {}
for Lookup, Data in next, Variables do
local Order = Data.Order
table.insert(Ordered, Order, Data)
end
return Ordered
end
function Module:MakeVariable(Data: VariableData): string
local VariableLookup = self.VariableLookup
local InstanceQueue = self.InstanceQueue
local Value = Data.Value
local Lookup = Data.Lookup or Value
local Class = Data.Class or "Variables"
--// Return existing variable
local Existing = self:GetVariable(Lookup)
if Existing then
return Existing.Name
end
--// Check if the value is a global
local Global = self:IsGlobal(Value)
if Global then
return Global
end
--// Check if value is an instance
if not Data.Name and typeof(Value) == "Instance" then
InstanceQueue[Value] = Data
end
--// Generate variable name
local Name = self:CheckName(Data)
Data.Name = Name
--// Check variable class dict
local ClassDict = self:GetClassDict(Class)
self:AddVariableToClass(ClassDict, Data)
VariableLookup[Lookup] = Data
return Name
end
function Module:CollectTableItems(Table: Table, Callback: (Value: any)->nil)
local function Process(Value)
local Type = typeof(Value)
--// Recursive search
if Type == "table" then
self:CollectTableItems(Value, Callback)
return
end
Callback(Value)
end
--// Process each item in table
for A, B in next, Table do
Process(A)
Process(B)
end
end
function Module:FindDuplicates(Table: Table): Table
local Duplicates = {}
local IndexStates = {}
for Index, Value in next, Table do
local State = IndexStates[Value]
--// Check if the value has already been indexed
if State == 1 then
IndexStates[Value] = 2
table.insert(Duplicates, Value)
continue
end
IndexStates[Value] = 1
end
--// Clear index states in memory
table.clear(IndexStates)
return Duplicates
end
function Module:CollectTableTypes(Table: Table, Types: Table): Table
local Collections = {}
local function Process(Value)
local Type = typeof(Value)
--// Check if type should be collected
if not table.find(Types, Type) then return end
local Collected = Collections[Type]
if not Collected then
Collected = {}
Collections[Type] = Collected
end
table.insert(Collected, Value)
end
--// Collect all table items
self:CollectTableItems(Table, Process)
return Collections
end
function Module:MakeParentsTable(Object: Instance): Table
local IndexFunc = self.IndexFunc
local Swaps = self.Swaps
local Variables = self.Variables
local Parents = {}
local NextParent = Object
while true do
local Current = NextParent
NextParent = IndexFunc(NextParent, "Parent")
--// Global check
if NextParent == game and self:IsGlobal(Current) then
NextParent = nil
end
--// Check for swaps
if Swaps then
local Swap = Swaps[Current]
if Swap and Swap.NextParent then
NextParent = Swap.NextParent
end
end
--// Check for a variable with the path
local Variable = Variables:GetVariable(Current)
if Variable and NextParent then
NextParent = nil
end
table.insert(Parents, 1, Current)
--// Break if no parent
if not NextParent then break end
end
return Parents
end
function Module:BulkCollectParents(Objects: Table): (Table, Table)
local AllParents = {}
local ObjectParents = {}
--// Collect all parents
for _, Object in next, Objects do
if typeof(Object) ~= "Instance" then continue end
local Parents = self:MakeParentsTable(Object)
MultiInsert(AllParents, Parents)
ObjectParents[Object] = Parents
end
return AllParents, ObjectParents
end
function Module:PrerenderVariables(Table: Table, Types: Table)
local Collections = self:CollectTableTypes(Table, Types)
--// Instances
for Type, Items in next, Collections do
local Render = RenderFuncs[Type]
if Render then
Render(self, Items)
end
end
end
return Module