-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobloxWebhook.lua
More file actions
736 lines (628 loc) · 20.2 KB
/
Copy pathRobloxWebhook.lua
File metadata and controls
736 lines (628 loc) · 20.2 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
local WsUrl = "ws://localhost:9000"
local MaxCompletionItems = 120
local MaxScriptResults = 250
local WebSocketApi = WebSocket
local DecompileFunction = decompile
local BlacklistedPaths = {
workspace,
game:GetService("Players"),
game:GetService("StarterGui"),
game:GetService("CorePackages"),
game:GetService("CoreGui")
}
-- Services excluded from the type tree dump entirely
local TreeDumpSkipServices = {
CoreGui = true, CorePackages = true, RobloxPluginGuiService = true,
PluginGuiService = true, Selection = true, RobloxGoogleAnalyticsConfiguration = true
}
-- Services whose children are not traversed (included as a typed node, children skipped)
local TreeDumpShallowServices = {
Players = true, HttpService = true, TeleportService = true
}
local TreeDumpMaxDepth = 10
local TreeDumpMaxChildrenPerNode = 200
local function BuildTreeNode(Instance, Depth)
if Depth > TreeDumpMaxDepth then
return { className = Instance.ClassName, children = {} }
end
local Node = { className = Instance.ClassName, children = {} }
local Ok, Children = pcall(function() return Instance:GetChildren() end)
if not Ok then return Node end
local Count = 0
for _, Child in ipairs(Children) do
if Count >= TreeDumpMaxChildrenPerNode then break end
local ChildNode = BuildTreeNode(Child, Depth + 1)
table.insert(Node.children, {
name = Child.Name,
className = ChildNode.className,
children = ChildNode.children
})
Count += 1
end
return Node
end
local function BuildGameTree()
local Root = { className = "DataModel", children = {} }
local Ok, Services = pcall(function() return game:GetChildren() end)
if not Ok then return Root end
for _, Service in ipairs(Services) do
if TreeDumpSkipServices[Service.Name] then continue end
if TreeDumpShallowServices[Service.Name] then
table.insert(Root.children, {
name = Service.Name,
className = Service.ClassName,
children = {}
})
else
local Node = BuildTreeNode(Service, 1)
table.insert(Root.children, {
name = Service.Name,
className = Node.className,
children = Node.children
})
end
end
return Root
end
local ScriptIndexById = {}
local ScriptRecords = {}
local NextScriptId = 0
local function ToLower(Value)
return string.lower(Value or "")
end
local function Trim(Value)
return (Value or ""):match("^%s*(.-)%s*$")
end
local function SafeGetFullName(Instance)
local Ok, FullName = pcall(function()
return Instance:GetFullName()
end)
if Ok and FullName then
return FullName
end
return Instance.Name
end
local function IsDescendantOfBlacklistedPath(Instance)
for _, PathRoot in ipairs(BlacklistedPaths) do
local Ok, IsDescendant = pcall(function()
return Instance:IsDescendantOf(PathRoot)
end)
if Ok and IsDescendant then
return true
end
end
return false
end
local function IsAllowedScript(Instance)
if not Instance then
return false
end
if not (Instance:IsA("BaseScript") or Instance:IsA("ModuleScript")) then
return false
end
if IsDescendantOfBlacklistedPath(Instance) then
return false
end
return true
end
local function BuildDotPath(Instance)
local Parts = {}
local Current = Instance
while Current and Current ~= game do
table.insert(Parts, 1, Current.Name)
Current = Current.Parent
end
if #Parts == 0 then
return "game"
end
local Path = "game:GetService('" .. Parts[1] .. "')"
for Index = 2, #Parts do
Path = Path .. "." .. Parts[Index]
end
return Path
end
local function SplitDotPath(Path)
local Parts = {}
if not Path or Path == "" then
return Parts
end
for Part in string.gmatch(Path, "([%w_]+)") do
table.insert(Parts, Part)
end
return Parts
end
local function StartsWithIgnoreCase(Text, Prefix)
if not Prefix or Prefix == "" then
return true
end
return string.lower(string.sub(Text, 1, #Prefix)) == string.lower(Prefix)
end
local function CloneScriptForDecompile(Instance)
local Ok, Clone = pcall(Instance.Clone, Instance)
if not Ok or not Clone then
return nil
end
if Clone:IsA("BaseScript") then
Clone.Enabled = false
end
Clone.Parent = nil
return Clone
end
local function AddScriptToIndex(Instance)
if not IsAllowedScript(Instance) then
return
end
NextScriptId += 1
local ScriptId = tostring(NextScriptId)
local Path = SafeGetFullName(Instance)
local CachedClone = CloneScriptForDecompile(Instance)
local Record = {
Id = ScriptId,
Name = Instance.Name,
NameLower = ToLower(Instance.Name),
ClassName = Instance.ClassName,
ClassLower = ToLower(Instance.ClassName),
Path = Path,
PathLower = ToLower(Path),
LiveScript = Instance,
CachedScript = CachedClone
}
ScriptIndexById[ScriptId] = Record
table.insert(ScriptRecords, Record)
end
local function BuildScriptIndex()
ScriptIndexById = {}
ScriptRecords = {}
NextScriptId = 0
local Ok, Descendants = pcall(function()
return game:GetDescendants()
end)
if not Ok then
return
end
for _, Instance in ipairs(Descendants) do
AddScriptToIndex(Instance)
end
end
local function EnsureScriptIndex()
if #ScriptRecords == 0 then
BuildScriptIndex()
end
end
local function TokenizeQuery(Query)
local Tokens = {}
local Normalized = ToLower(Trim(Query))
for Token in string.gmatch(Normalized, "[^%s]+") do
table.insert(Tokens, Token)
end
return Tokens, Normalized
end
local function MatchesTokens(Record, Tokens)
if #Tokens == 0 then
return true
end
for _, Token in ipairs(Tokens) do
local InName = string.find(Record.NameLower, Token, 1, true)
local InPath = string.find(Record.PathLower, Token, 1, true)
local InClass = string.find(Record.ClassLower, Token, 1, true)
if not InName and not InPath and not InClass then
return false
end
end
return true
end
local function RankRecord(Record, NormalizedQuery)
if NormalizedQuery == "" then
return 1
end
local Score = 0
if string.sub(Record.NameLower, 1, #NormalizedQuery) == NormalizedQuery then
Score += 200
elseif string.find(Record.NameLower, NormalizedQuery, 1, true) then
Score += 120
end
if string.find(Record.PathLower, NormalizedQuery, 1, true) then
Score += 60
end
if string.find(Record.ClassLower, NormalizedQuery, 1, true) then
Score += 20
end
return Score
end
local function SearchScripts(Query, Limit)
EnsureScriptIndex()
local Results = {}
local Tokens, NormalizedQuery = TokenizeQuery(Query)
local MaxResults = math.max(1, math.min(Limit or MaxScriptResults, MaxScriptResults))
for _, Record in ipairs(ScriptRecords) do
if MatchesTokens(Record, Tokens) then
table.insert(Results, {
id = Record.Id,
name = Record.Name,
className = Record.ClassName,
path = Record.Path,
_score = RankRecord(Record, NormalizedQuery)
})
end
end
table.sort(Results, function(Left, Right)
if Left._score ~= Right._score then
return Left._score > Right._score
end
if Left.name ~= Right.name then
return ToLower(Left.name) < ToLower(Right.name)
end
return Left.path < Right.path
end)
if #Results > MaxResults then
for Index = #Results, MaxResults + 1, -1 do
table.remove(Results, Index)
end
end
for _, Item in ipairs(Results) do
Item._score = nil
end
return Results
end
local function ResolveDecompileTarget(Record)
if Record.CachedScript then
return Record.CachedScript
end
return Record.LiveScript
end
local function DecompileScript(ScriptId)
local Record = ScriptIndexById[ScriptId]
if not Record then
return nil, "Script not found in current index. Refresh search and retry."
end
local Target = ResolveDecompileTarget(Record)
if not Target then
return nil, "No script target available for decompile."
end
if DecompileFunction and type(DecompileFunction) == "function" then
local Ok, Decompiled = pcall(function()
return DecompileFunction(Target)
end)
if Ok and type(Decompiled) == "string" and Decompiled ~= "" then
return "-- " .. Record.Path .. "\n" .. Decompiled
end
end
local Ok, Source = pcall(function()
return Target.Source
end)
if Ok and type(Source) == "string" and Source ~= "" then
return "-- " .. Record.Path .. "\n" .. Source
end
return nil, "Decompiler unavailable or returned empty output."
end
local function ExtractNumberField(Raw, Key)
local Pattern = '"' .. Key .. '"%s*:%s*(%d+)'
local Match = Raw:match(Pattern)
if not Match then
return nil
end
return tonumber(Match)
end
local function JsonEncode(Value)
local ValueType = type(Value)
if Value == nil then
return "null"
elseif ValueType == "boolean" then
return tostring(Value)
elseif ValueType == "number" then
return tostring(Value)
elseif ValueType == "string" then
return '"' .. Value
:gsub('\\', '\\\\')
:gsub('"', '\\"')
:gsub('\n', '\\n')
:gsub('\r', '\\r')
:gsub('\t', '\\t') .. '"'
elseif ValueType == "table" then
if #Value > 0 then
local Parts = {}
for _, Item in ipairs(Value) do
table.insert(Parts, JsonEncode(Item))
end
return "[" .. table.concat(Parts, ",") .. "]"
end
local Parts = {}
for Key, Item in pairs(Value) do
table.insert(Parts, JsonEncode(tostring(Key)) .. ":" .. JsonEncode(Item))
end
return "{" .. table.concat(Parts, ",") .. "}"
end
return "null"
end
local function JsonDecodeString(Raw, StartPos)
-- StartPos should point to the opening quote character
if Raw:sub(StartPos, StartPos) ~= '"' then
return nil, StartPos
end
local Result = {}
local Index = StartPos + 1
local Length = #Raw
while Index <= Length do
local Char = Raw:sub(Index, Index)
if Char == '"' then
return table.concat(Result), Index + 1
elseif Char == '\\' then
Index += 1
local Escaped = Raw:sub(Index, Index)
if Escaped == '"' then
table.insert(Result, '"')
elseif Escaped == '\\' then
table.insert(Result, '\\')
elseif Escaped == '/' then
table.insert(Result, '/')
elseif Escaped == 'n' then
table.insert(Result, '\n')
elseif Escaped == 'r' then
table.insert(Result, '\r')
elseif Escaped == 't' then
table.insert(Result, '\t')
elseif Escaped == 'b' then
table.insert(Result, '\b')
elseif Escaped == 'f' then
table.insert(Result, '\f')
elseif Escaped == 'u' then
-- \uXXXX — skip 4 hex digits, approximate as '?'
table.insert(Result, '?')
Index += 4
else
table.insert(Result, Escaped)
end
else
table.insert(Result, Char)
end
Index += 1
end
return table.concat(Result), Index
end
local function ExtractStringField(Raw, Key)
local KeyPattern = '"' .. Key .. '"%s*:%s*'
local KeyStart, KeyEnd = Raw:find(KeyPattern)
if not KeyStart then
return nil
end
local ValueStart = KeyEnd + 1
-- Skip whitespace
while ValueStart <= #Raw and Raw:sub(ValueStart, ValueStart):match('%s') do
ValueStart += 1
end
if Raw:sub(ValueStart, ValueStart) ~= '"' then
return nil
end
local Value = JsonDecodeString(Raw, ValueStart)
return Value
end
local function ExtractType(Raw)
return ExtractStringField(Raw, "type")
end
local function ResolveRoot(Scope, ServiceName, PathParts)
if Scope == "service" then
if not ServiceName or ServiceName == "" then
return nil, PathParts
end
local Ok, Service = pcall(function()
return game:GetService(ServiceName)
end)
if not Ok then
return nil, PathParts
end
return Service, PathParts
end
if Scope == "workspace" then
local Ok, WorkspaceService = pcall(function()
return game:GetService("Workspace")
end)
if not Ok then
return nil, PathParts
end
return WorkspaceService, PathParts
end
if Scope == "game" then
if #PathParts == 0 then
return game, PathParts
end
local First = PathParts[1]
local Ok, Service = pcall(function()
return game:GetService(First)
end)
if Ok and Service then
table.remove(PathParts, 1)
return Service, PathParts
end
local Child = game:FindFirstChild(First)
if Child then
table.remove(PathParts, 1)
return Child, PathParts
end
return game, PathParts
end
return nil, PathParts
end
local function TraverseToTarget(Root, PathParts)
local Current = Root
for _, Segment in ipairs(PathParts) do
if not Current then
return nil
end
Current = Current:FindFirstChild(Segment)
if not Current then
return nil
end
end
return Current
end
local function CollectChildren(Parent, Prefix)
local Results = {}
if not Parent then
return Results
end
local Ok, Children = pcall(function()
return Parent:GetChildren()
end)
if not Ok then
return Results
end
for _, Child in ipairs(Children) do
if StartsWithIgnoreCase(Child.Name, Prefix) then
table.insert(Results, {
label = Child.Name,
detail = Child.ClassName,
path = BuildDotPath(Child)
})
if #Results >= MaxCompletionItems then
break
end
end
end
table.sort(Results, function(Left, Right)
return string.lower(Left.label) < string.lower(Right.label)
end)
return Results
end
local function ComputeCompletions(Scope, ServiceName, Path, Prefix)
local PathParts = SplitDotPath(Path)
local Root, Remaining = ResolveRoot(Scope, ServiceName, PathParts)
if not Root then
return {}
end
local Target = TraverseToTarget(Root, Remaining)
if not Target then
return {}
end
return CollectChildren(Target, Prefix)
end
local function Connect()
local WebSocketConnection = WebSocketApi and WebSocketApi.connect and WebSocketApi.connect(WsUrl)
if not WebSocketConnection then
warn("[Rotellisense] No WebSocket API found in this executor.")
return
end
print("[Rotellisense] Connected to " .. WsUrl)
local function Send(Payload)
local Ok, ErrorMessage = pcall(function()
WebSocketConnection:Send(JsonEncode(Payload))
end)
if not Ok then
warn("[Rotellisense] Send error: " .. tostring(ErrorMessage))
end
end
WebSocketConnection.OnMessage:Connect(function(Raw)
local MessageType = ExtractType(Raw)
if MessageType == "complete" then
local RequestId = ExtractStringField(Raw, "requestId") or ""
local Scope = ExtractStringField(Raw, "scope") or ""
local ServiceName = ExtractStringField(Raw, "serviceName") or ""
local Path = ExtractStringField(Raw, "path") or ""
local Prefix = ExtractStringField(Raw, "prefix") or ""
local Items = ComputeCompletions(Scope, ServiceName, Path, Prefix)
Send({
type = "complete_result",
requestId = RequestId,
items = Items
})
elseif MessageType == "tree_dump" then
local RequestId = ExtractStringField(Raw, "requestId") or ""
local Tree = BuildGameTree()
Send({
type = "tree_dump_result",
requestId = RequestId,
tree = Tree
})
elseif MessageType == "resolve_class" then
local RequestId = ExtractStringField(Raw, "requestId") or ""
local Scope = ExtractStringField(Raw, "scope") or ""
local ServiceName = ExtractStringField(Raw, "serviceName") or ""
local Path = ExtractStringField(Raw, "path") or ""
local PathParts = SplitDotPath(Path)
local Root, Remaining = ResolveRoot(Scope, ServiceName, PathParts)
local Target = Root and TraverseToTarget(Root, Remaining)
if Target then
Send({
type = "class_result",
requestId = RequestId,
className = Target.ClassName
})
else
Send({
type = "error",
requestId = RequestId,
message = "Instance not found"
})
end
elseif MessageType == "script_search" then
local RequestId = ExtractStringField(Raw, "requestId") or ""
local Query = ExtractStringField(Raw, "query") or ""
local Limit = ExtractNumberField(Raw, "limit") or MaxScriptResults
local ScriptItems = SearchScripts(Query, Limit)
Send({
type = "script_search_result",
requestId = RequestId,
scriptItems = ScriptItems
})
elseif MessageType == "script_decompile" then
local RequestId = ExtractStringField(Raw, "requestId") or ""
local ScriptId = ExtractStringField(Raw, "scriptId") or ""
local Source, ErrorMessage = DecompileScript(ScriptId)
if Source then
Send({
type = "decompile_result",
requestId = RequestId,
source = Source
})
else
Send({
type = "error",
requestId = RequestId,
message = ErrorMessage or "Decompile failed"
})
end
elseif MessageType == "execute_script" then
local RequestId = ExtractStringField(Raw, "requestId") or ""
local Source = ExtractStringField(Raw, "source") or ""
local Fn, LoadError = loadstring(Source)
if not Fn then
Send({
type = "execute_result",
requestId = RequestId,
success = false,
message = LoadError or "Failed to load script"
})
return
end
local Ok, RunError = pcall(Fn)
if not Ok then
Send({
type = "execute_result",
requestId = RequestId,
success = false,
message = tostring(RunError)
})
else
Send({
type = "execute_result",
requestId = RequestId,
success = true,
output = ""
})
end
elseif MessageType == "index_scripts" then
BuildScriptIndex()
Send({
type = "index_scripts_result",
indexedCount = #ScriptRecords
})
end
end)
WebSocketConnection.OnClose:Connect(function()
warn("[Rotellisense] Disconnected. Reconnecting in 3s...")
task.wait(3)
Connect()
end)
end
BuildScriptIndex()
game.DescendantAdded:Connect(AddScriptToIndex)
Connect()