@el4cteo/rbx-studio-mcp 0.2.9 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/dist/lib/pluginbuild.js +23 -13
- package/dist/lib/pluginbuild.js.map +1 -1
- package/dist/tools/exec.js +10 -1
- package/dist/tools/exec.js.map +1 -1
- package/dist/tools/perf.js +10 -1
- package/dist/tools/perf.js.map +1 -1
- package/dist/tools/scripts.js +54 -12
- package/dist/tools/scripts.js.map +1 -1
- package/dist/tools/session.js +16 -0
- package/dist/tools/session.js.map +1 -1
- package/package.json +1 -1
- package/plugin/src/Config.luau +1 -1
- package/plugin/src/Paths.luau +357 -255
- package/plugin/src/handlers/Exec.luau +314 -270
- package/plugin/src/handlers/Scripts.luau +461 -387
package/plugin/src/Paths.luau
CHANGED
|
@@ -1,255 +1,357 @@
|
|
|
1
|
-
--!strict
|
|
2
|
-
--[[
|
|
3
|
-
Dot-notation instance paths, e.g. "Workspace.Map.Spawn".
|
|
4
|
-
|
|
5
|
-
Paths are the address format for every tool, so resolution failures are the
|
|
6
|
-
most common error an agent will hit. Each failure says which segment broke
|
|
7
|
-
and what does exist at that level, which is usually enough for the model to
|
|
8
|
-
fix the path itself instead of falling back to a blind tree dump.
|
|
9
|
-
]]
|
|
10
|
-
|
|
11
|
-
local Dispatch = require(script.Parent.Dispatch)
|
|
12
|
-
|
|
13
|
-
local Paths = {}
|
|
14
|
-
|
|
15
|
-
--[[
|
|
16
|
-
Splits a path into segments. A leading "game." is optional and stripped, so
|
|
17
|
-
both "game.Workspace.Part" and "Workspace.Part" resolve identically.
|
|
18
|
-
]]
|
|
19
|
-
function Paths.split(path: string): { string }
|
|
20
|
-
local segments: { string } = {}
|
|
21
|
-
for segment in string.gmatch(path, "[^%.]+") do
|
|
22
|
-
table.insert(segments, segment)
|
|
23
|
-
end
|
|
24
|
-
if segments[1] == "game" then
|
|
25
|
-
table.remove(segments, 1)
|
|
26
|
-
end
|
|
27
|
-
return segments
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
--[[
|
|
31
|
-
Splits "Part[3]" into ("Part", 3), or ("Part", nil) when unindexed.
|
|
32
|
-
|
|
33
|
-
Sibling names are not unique in Roblox -- a Workspace with 77 parts all named
|
|
34
|
-
"Part" is completely ordinary -- so a bare dotted path is ambiguous and
|
|
35
|
-
`FindFirstChild` would silently pick the first match. Every path this module
|
|
36
|
-
emits therefore carries a 1-based index whenever the name is shared, and
|
|
37
|
-
`resolve` honours it.
|
|
38
|
-
]]
|
|
39
|
-
local function parseSegment(segment: string): (string, number?)
|
|
40
|
-
local name, index = string.match(segment, "^(.*)%[(%d+)%]$")
|
|
41
|
-
if name and index then
|
|
42
|
-
return name, tonumber(index)
|
|
43
|
-
end
|
|
44
|
-
return segment, nil
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
export type NameIndex = { [Instance]: { [string]: { Instance } } }
|
|
48
|
-
|
|
49
|
-
--[[
|
|
50
|
-
Groups a parent's children by name, memoised across one request. Without the
|
|
51
|
-
memo, formatting paths for N same-named siblings is O(N^2); a listing of a
|
|
52
|
-
few thousand parts would stall Studio's main thread.
|
|
53
|
-
]]
|
|
54
|
-
local function childrenByName(parent: Instance, memo: NameIndex?): { [string]: { Instance } }
|
|
55
|
-
local cached = if memo then memo[parent] else nil
|
|
56
|
-
if cached then
|
|
57
|
-
return cached
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
local byName: { [string]: { Instance } } = {}
|
|
61
|
-
for _, child in parent:GetChildren() do
|
|
62
|
-
local list = byName[child.Name]
|
|
63
|
-
if not list then
|
|
64
|
-
list = {}
|
|
65
|
-
byName[child.Name] = list
|
|
66
|
-
end
|
|
67
|
-
table.insert(list, child)
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
if memo then
|
|
71
|
-
memo[parent] = byName
|
|
72
|
-
end
|
|
73
|
-
return byName
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
--[[
|
|
77
|
-
1-based position among identically named siblings, or nil when the name is
|
|
78
|
-
already unique and no index is needed.
|
|
79
|
-
]]
|
|
80
|
-
local function siblingIndex(instance: Instance, memo: NameIndex?): number?
|
|
81
|
-
local parent = instance.Parent
|
|
82
|
-
if not parent then
|
|
83
|
-
return nil
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
local list = childrenByName(parent, memo)[instance.Name]
|
|
87
|
-
if not list or #list <= 1 then
|
|
88
|
-
return nil
|
|
89
|
-
end
|
|
90
|
-
for index, child in list do
|
|
91
|
-
if child == instance then
|
|
92
|
-
return index
|
|
93
|
-
end
|
|
94
|
-
end
|
|
95
|
-
return nil
|
|
96
|
-
end
|
|
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
|
-
end
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
1
|
+
--!strict
|
|
2
|
+
--[[
|
|
3
|
+
Dot-notation instance paths, e.g. "Workspace.Map.Spawn".
|
|
4
|
+
|
|
5
|
+
Paths are the address format for every tool, so resolution failures are the
|
|
6
|
+
most common error an agent will hit. Each failure says which segment broke
|
|
7
|
+
and what does exist at that level, which is usually enough for the model to
|
|
8
|
+
fix the path itself instead of falling back to a blind tree dump.
|
|
9
|
+
]]
|
|
10
|
+
|
|
11
|
+
local Dispatch = require(script.Parent.Dispatch)
|
|
12
|
+
|
|
13
|
+
local Paths = {}
|
|
14
|
+
|
|
15
|
+
--[[
|
|
16
|
+
Splits a path into segments. A leading "game." is optional and stripped, so
|
|
17
|
+
both "game.Workspace.Part" and "Workspace.Part" resolve identically.
|
|
18
|
+
]]
|
|
19
|
+
function Paths.split(path: string): { string }
|
|
20
|
+
local segments: { string } = {}
|
|
21
|
+
for segment in string.gmatch(path, "[^%.]+") do
|
|
22
|
+
table.insert(segments, segment)
|
|
23
|
+
end
|
|
24
|
+
if segments[1] == "game" then
|
|
25
|
+
table.remove(segments, 1)
|
|
26
|
+
end
|
|
27
|
+
return segments
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
--[[
|
|
31
|
+
Splits "Part[3]" into ("Part", 3), or ("Part", nil) when unindexed.
|
|
32
|
+
|
|
33
|
+
Sibling names are not unique in Roblox -- a Workspace with 77 parts all named
|
|
34
|
+
"Part" is completely ordinary -- so a bare dotted path is ambiguous and
|
|
35
|
+
`FindFirstChild` would silently pick the first match. Every path this module
|
|
36
|
+
emits therefore carries a 1-based index whenever the name is shared, and
|
|
37
|
+
`resolve` honours it.
|
|
38
|
+
]]
|
|
39
|
+
local function parseSegment(segment: string): (string, number?)
|
|
40
|
+
local name, index = string.match(segment, "^(.*)%[(%d+)%]$")
|
|
41
|
+
if name and index then
|
|
42
|
+
return name, tonumber(index)
|
|
43
|
+
end
|
|
44
|
+
return segment, nil
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
export type NameIndex = { [Instance]: { [string]: { Instance } } }
|
|
48
|
+
|
|
49
|
+
--[[
|
|
50
|
+
Groups a parent's children by name, memoised across one request. Without the
|
|
51
|
+
memo, formatting paths for N same-named siblings is O(N^2); a listing of a
|
|
52
|
+
few thousand parts would stall Studio's main thread.
|
|
53
|
+
]]
|
|
54
|
+
local function childrenByName(parent: Instance, memo: NameIndex?): { [string]: { Instance } }
|
|
55
|
+
local cached = if memo then memo[parent] else nil
|
|
56
|
+
if cached then
|
|
57
|
+
return cached
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
local byName: { [string]: { Instance } } = {}
|
|
61
|
+
for _, child in parent:GetChildren() do
|
|
62
|
+
local list = byName[child.Name]
|
|
63
|
+
if not list then
|
|
64
|
+
list = {}
|
|
65
|
+
byName[child.Name] = list
|
|
66
|
+
end
|
|
67
|
+
table.insert(list, child)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
if memo then
|
|
71
|
+
memo[parent] = byName
|
|
72
|
+
end
|
|
73
|
+
return byName
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
--[[
|
|
77
|
+
1-based position among identically named siblings, or nil when the name is
|
|
78
|
+
already unique and no index is needed.
|
|
79
|
+
]]
|
|
80
|
+
local function siblingIndex(instance: Instance, memo: NameIndex?): number?
|
|
81
|
+
local parent = instance.Parent
|
|
82
|
+
if not parent then
|
|
83
|
+
return nil
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
local list = childrenByName(parent, memo)[instance.Name]
|
|
87
|
+
if not list or #list <= 1 then
|
|
88
|
+
return nil
|
|
89
|
+
end
|
|
90
|
+
for index, child in list do
|
|
91
|
+
if child == instance then
|
|
92
|
+
return index
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
return nil
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
--[[
|
|
99
|
+
Names a parent's children for an error message, collapsing repeats.
|
|
100
|
+
|
|
101
|
+
Same-named siblings are ordinary in Roblox, and listing them one by one spent
|
|
102
|
+
the whole budget saying "Part, Part, Part" 25 times -- which tells a reader
|
|
103
|
+
nothing and hides the names that would have helped. Repeats are counted
|
|
104
|
+
instead, and the count is exactly what says an index is needed.
|
|
105
|
+
]]
|
|
106
|
+
local function childNames(parent: Instance, limit: number): string
|
|
107
|
+
local order: { string } = {}
|
|
108
|
+
local counts: { [string]: number } = {}
|
|
109
|
+
for _, child in parent:GetChildren() do
|
|
110
|
+
if counts[child.Name] == nil then
|
|
111
|
+
counts[child.Name] = 0
|
|
112
|
+
table.insert(order, child.Name)
|
|
113
|
+
end
|
|
114
|
+
counts[child.Name] += 1
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
if #order == 0 then
|
|
118
|
+
return "(no children)"
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
local names: { string } = {}
|
|
122
|
+
for _, name in order do
|
|
123
|
+
if #names >= limit then
|
|
124
|
+
table.insert(names, string.format("... %d more", #order - limit))
|
|
125
|
+
break
|
|
126
|
+
end
|
|
127
|
+
local count = counts[name]
|
|
128
|
+
table.insert(names, if count > 1 then string.format("%s (x%d)", name, count) else name)
|
|
129
|
+
end
|
|
130
|
+
return table.concat(names, ", ")
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
--[[
|
|
134
|
+
Finds one child by a single segment, honouring "Name[2]" and, at the root,
|
|
135
|
+
fetching services by class name -- they are not always present as children
|
|
136
|
+
until first accessed.
|
|
137
|
+
|
|
138
|
+
The second return is a diagnostic for the one miss a caller cannot work out
|
|
139
|
+
from the sibling list: an index past the end of a name that does exist.
|
|
140
|
+
]]
|
|
141
|
+
local function childBySegment(
|
|
142
|
+
parent: Instance,
|
|
143
|
+
segment: string,
|
|
144
|
+
atRoot: boolean
|
|
145
|
+
): (Instance?, string?)
|
|
146
|
+
local name, ordinal = parseSegment(segment)
|
|
147
|
+
|
|
148
|
+
if ordinal then
|
|
149
|
+
local list = childrenByName(parent, nil)[name]
|
|
150
|
+
if list == nil then
|
|
151
|
+
return nil, nil
|
|
152
|
+
end
|
|
153
|
+
local child = list[ordinal]
|
|
154
|
+
if child then
|
|
155
|
+
return child, nil
|
|
156
|
+
end
|
|
157
|
+
return nil,
|
|
158
|
+
string.format(
|
|
159
|
+
'"%s" has %d child%s named "%s", so [%d] is past the end. Indexes are '
|
|
160
|
+
.. "1-based and shift when siblings are added or removed -- read a "
|
|
161
|
+
.. "fresh path from `find` or `tree`.",
|
|
162
|
+
parent:GetFullName(),
|
|
163
|
+
#list,
|
|
164
|
+
if #list == 1 then "" else "ren",
|
|
165
|
+
name,
|
|
166
|
+
ordinal
|
|
167
|
+
)
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
if atRoot then
|
|
171
|
+
local ok, service = pcall(function()
|
|
172
|
+
return game:GetService(name :: any)
|
|
173
|
+
end)
|
|
174
|
+
if ok and service then
|
|
175
|
+
return service, nil
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
return parent:FindFirstChild(name), nil
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
--[[
|
|
182
|
+
Resolves a path to an Instance, or raises a NOT_FOUND with the failing
|
|
183
|
+
segment and its siblings. `FindFirstChild` is used rather than indexing so a
|
|
184
|
+
missing child never throws a bare Luau error.
|
|
185
|
+
|
|
186
|
+
Instance names may contain dots -- "Dr. Simon" is an ordinary name, and so is
|
|
187
|
+
"v1.2 backup" -- while the separator is a dot too, so such a name arrives
|
|
188
|
+
here already split into pieces. Each step therefore tries consuming one
|
|
189
|
+
segment first and rejoins further segments only if that leads nowhere, which
|
|
190
|
+
means the search has to BACK TRACK: with both "Dr" and "Dr. Who" present,
|
|
191
|
+
"Workspace.Dr. Who.Head" matches "Dr", fails to find " Who" under it, and
|
|
192
|
+
must return to try "Dr. Who". A first pass without that backtracking left the
|
|
193
|
+
tools emitting `Workspace.Dr. Who` as a path and then refusing to read it
|
|
194
|
+
back, which is worse than either behaviour on its own.
|
|
195
|
+
|
|
196
|
+
Shortest-first keeps the old precedence: where a short name resolves the
|
|
197
|
+
whole remaining path, it still wins over a longer join.
|
|
198
|
+
]]
|
|
199
|
+
function Paths.resolve(path: string): Instance
|
|
200
|
+
if typeof(path) ~= "string" or path == "" then
|
|
201
|
+
Dispatch.fail(
|
|
202
|
+
"BAD_PATH",
|
|
203
|
+
"An instance path is required.",
|
|
204
|
+
'Paths are dot-separated from the DataModel root, e.g. "Workspace.Map.Spawn". '
|
|
205
|
+
.. "Use `find` or `tree` to discover valid paths."
|
|
206
|
+
)
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
local segments = Paths.split(path)
|
|
210
|
+
if #segments == 0 then
|
|
211
|
+
return game
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
-- The deepest point any branch of the search reached, so a failure still
|
|
215
|
+
-- reports the segment that actually broke rather than the first one tried.
|
|
216
|
+
local deepest = 0
|
|
217
|
+
local deepestParent: Instance = game
|
|
218
|
+
local deepestNote: string? = nil
|
|
219
|
+
-- The segment text the note is about: with a rejoined name it is not
|
|
220
|
+
-- `segments[failedAt]`, and reporting that instead named "Mr" for a path
|
|
221
|
+
-- that asked for "Mr. X[3]".
|
|
222
|
+
local deepestSegment: string? = nil
|
|
223
|
+
|
|
224
|
+
local function walk(current: Instance, index: number): Instance?
|
|
225
|
+
if index > #segments then
|
|
226
|
+
return current
|
|
227
|
+
end
|
|
228
|
+
if index - 1 > deepest then
|
|
229
|
+
deepest = index - 1
|
|
230
|
+
deepestParent = current
|
|
231
|
+
deepestNote = nil
|
|
232
|
+
deepestSegment = nil
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
for last = index, #segments do
|
|
236
|
+
local joined = table.concat(segments, ".", index, last)
|
|
237
|
+
local child, note = childBySegment(current, joined, index == 1)
|
|
238
|
+
if note and index - 1 == deepest then
|
|
239
|
+
deepestNote = note
|
|
240
|
+
deepestSegment = joined
|
|
241
|
+
end
|
|
242
|
+
if child then
|
|
243
|
+
local found = walk(child, last + 1)
|
|
244
|
+
if found then
|
|
245
|
+
return found
|
|
246
|
+
end
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
return nil
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
local resolved = walk(game, 1)
|
|
253
|
+
if resolved then
|
|
254
|
+
return resolved
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
local failedAt = deepest + 1
|
|
258
|
+
local reached = if deepest > 0 then table.concat(segments, ".", 1, deepest) .. "." else ""
|
|
259
|
+
local named = if deepestSegment
|
|
260
|
+
then reached .. deepestSegment
|
|
261
|
+
else table.concat(segments, ".", 1, failedAt)
|
|
262
|
+
return Dispatch.fail(
|
|
263
|
+
"NOT_FOUND",
|
|
264
|
+
string.format('No instance at "%s".', named),
|
|
265
|
+
if deepestNote
|
|
266
|
+
then deepestNote
|
|
267
|
+
else string.format(
|
|
268
|
+
'"%s" has no child named "%s". It does have: %s',
|
|
269
|
+
deepestParent:GetFullName(),
|
|
270
|
+
segments[failedAt],
|
|
271
|
+
childNames(deepestParent, 25)
|
|
272
|
+
)
|
|
273
|
+
)
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
--[[
|
|
277
|
+
Resolves many paths at once, collecting failures instead of stopping at the
|
|
278
|
+
first one. Batch tools report every bad path in a single response so the
|
|
279
|
+
agent can fix them all in one retry rather than one call per mistake.
|
|
280
|
+
]]
|
|
281
|
+
function Paths.resolveMany(paths: { string }): ({ Instance }, { string })
|
|
282
|
+
local resolved: { Instance } = {}
|
|
283
|
+
local failures: { string } = {}
|
|
284
|
+
for _, path in paths do
|
|
285
|
+
local ok, result = pcall(Paths.resolve, path)
|
|
286
|
+
if ok then
|
|
287
|
+
table.insert(resolved, result :: Instance)
|
|
288
|
+
else
|
|
289
|
+
local err = result :: any
|
|
290
|
+
table.insert(
|
|
291
|
+
failures,
|
|
292
|
+
string.format("%s: %s", path, if typeof(err) == "table" then err.message else tostring(err))
|
|
293
|
+
)
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
return resolved, failures
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
--[[
|
|
300
|
+
Formats an instance into a path that resolves back to that exact instance.
|
|
301
|
+
|
|
302
|
+
`GetFullName` is deliberately not used: it emits "Workspace.Part" for every
|
|
303
|
+
one of 77 parts named "Part", so the paths it produces are not addresses at
|
|
304
|
+
all. Here each segment gains a [n] suffix when its name is shared with a
|
|
305
|
+
sibling.
|
|
306
|
+
|
|
307
|
+
Pass a shared `memo` when formatting many instances in one request.
|
|
308
|
+
|
|
309
|
+
The index is positional, so it shifts if same-named siblings are inserted or
|
|
310
|
+
removed between calls. Read a fresh path after any structural change.
|
|
311
|
+
]]
|
|
312
|
+
function Paths.of(instance: Instance, memo: NameIndex?): string
|
|
313
|
+
--[[
|
|
314
|
+
Refuses anything that is not an Instance, rather than duck-typing it.
|
|
315
|
+
|
|
316
|
+
This walks `.Name` and `.Parent`, which a plain table can also have -- so
|
|
317
|
+
handed a table carrying those two fields it used to return a perfectly
|
|
318
|
+
well-formed path for an object that exists nowhere in the data model.
|
|
319
|
+
That happened: `GeometryService:FragmentAsync` returns `{Index, Instance}`
|
|
320
|
+
wrappers, a handler assigned `.Name` and `.Parent` to the wrapper, and
|
|
321
|
+
this function reported eight new parts by full path when none had been
|
|
322
|
+
created. A fabricated path is worse than an error, because everything
|
|
323
|
+
downstream treats it as real.
|
|
324
|
+
]]
|
|
325
|
+
if typeof(instance) ~= "Instance" then
|
|
326
|
+
error(
|
|
327
|
+
string.format(
|
|
328
|
+
"Paths.of expects an Instance, got %s. This is a bug in the caller, "
|
|
329
|
+
.. "not in the request.",
|
|
330
|
+
typeof(instance)
|
|
331
|
+
),
|
|
332
|
+
2
|
|
333
|
+
)
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
if instance == game then
|
|
337
|
+
return "game"
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
local segments: { string } = {}
|
|
341
|
+
local current: Instance? = instance
|
|
342
|
+
|
|
343
|
+
while current ~= nil and current ~= game do
|
|
344
|
+
local node = current :: Instance
|
|
345
|
+
local ordinal = siblingIndex(node, memo)
|
|
346
|
+
table.insert(
|
|
347
|
+
segments,
|
|
348
|
+
1,
|
|
349
|
+
if ordinal then string.format("%s[%d]", node.Name, ordinal) else node.Name
|
|
350
|
+
)
|
|
351
|
+
current = node.Parent
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
return table.concat(segments, ".")
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
return Paths
|