@chrrxs/robloxstudio-mcp-inspector 2.8.0

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.
Files changed (30) hide show
  1. package/dist/index.js +4483 -0
  2. package/package.json +50 -0
  3. package/studio-plugin/INSTALLATION.md +150 -0
  4. package/studio-plugin/MCPInspectorPlugin.rbxmx +9074 -0
  5. package/studio-plugin/MCPPlugin.rbxmx +9074 -0
  6. package/studio-plugin/default.project.json +19 -0
  7. package/studio-plugin/dev.project.json +23 -0
  8. package/studio-plugin/inspector-icon.png +0 -0
  9. package/studio-plugin/package-lock.json +706 -0
  10. package/studio-plugin/package.json +19 -0
  11. package/studio-plugin/plugin.json +10 -0
  12. package/studio-plugin/src/modules/ClientBroker.ts +221 -0
  13. package/studio-plugin/src/modules/Communication.ts +399 -0
  14. package/studio-plugin/src/modules/Recording.ts +28 -0
  15. package/studio-plugin/src/modules/State.ts +94 -0
  16. package/studio-plugin/src/modules/UI.ts +725 -0
  17. package/studio-plugin/src/modules/Utils.ts +318 -0
  18. package/studio-plugin/src/modules/handlers/AssetHandlers.ts +241 -0
  19. package/studio-plugin/src/modules/handlers/BuildHandlers.ts +481 -0
  20. package/studio-plugin/src/modules/handlers/CaptureHandlers.ts +128 -0
  21. package/studio-plugin/src/modules/handlers/InputHandlers.ts +102 -0
  22. package/studio-plugin/src/modules/handlers/InstanceHandlers.ts +380 -0
  23. package/studio-plugin/src/modules/handlers/MetadataHandlers.ts +391 -0
  24. package/studio-plugin/src/modules/handlers/PropertyHandlers.ts +191 -0
  25. package/studio-plugin/src/modules/handlers/QueryHandlers.ts +827 -0
  26. package/studio-plugin/src/modules/handlers/ScriptHandlers.ts +530 -0
  27. package/studio-plugin/src/modules/handlers/TestHandlers.ts +277 -0
  28. package/studio-plugin/src/server/index.server.ts +63 -0
  29. package/studio-plugin/src/types/index.d.ts +44 -0
  30. package/studio-plugin/tsconfig.json +20 -0
@@ -0,0 +1,481 @@
1
+ import Utils from "../Utils";
2
+ import Recording from "../Recording";
3
+
4
+ const MaterialService = game.GetService("MaterialService");
5
+
6
+ const { getInstancePath, getInstanceByPath } = Utils;
7
+ const { beginRecording, finishRecording } = Recording;
8
+
9
+ const MATERIAL_BY_NAME = new Map<string, Enum.Material>();
10
+ for (const enumItem of Enum.Material.GetEnumItems()) {
11
+ MATERIAL_BY_NAME.set(enumItem.Name, enumItem as unknown as Enum.Material);
12
+ }
13
+
14
+ // Shape class mapping
15
+ const SHAPE_CLASSES: Record<string, string> = {
16
+ Block: "Part",
17
+ Wedge: "WedgePart",
18
+ Cylinder: "Part",
19
+ Ball: "Part",
20
+ CornerWedge: "CornerWedgePart",
21
+ };
22
+
23
+ const PALETTE_KEYS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
24
+
25
+ function roundTo(n: number, decimals: number): number {
26
+ const mult = 10 ** decimals;
27
+ return math.round(n * mult) / mult;
28
+ }
29
+
30
+ function encodePaletteKey(index: number): string {
31
+ const base = PALETTE_KEYS.size();
32
+ let value = math.floor(index) + 1;
33
+ let encoded = "";
34
+ while (value > 0) {
35
+ value -= 1;
36
+ const digit = value % base;
37
+ encoded = PALETTE_KEYS.sub(digit + 1, digit + 1) + encoded;
38
+ value = math.floor(value / base);
39
+ }
40
+ return encoded;
41
+ }
42
+
43
+ function getVariantName(part: BasePart): string {
44
+ let variantName = part.MaterialVariant;
45
+ if (variantName === "") {
46
+ const [ok, variantAttr] = pcall(() => part.GetAttribute("MaterialVariant"));
47
+ if (ok && type(variantAttr) === "string") {
48
+ variantName = variantAttr as string;
49
+ }
50
+ }
51
+ return variantName;
52
+ }
53
+
54
+ function exportBuild(requestData: Record<string, unknown>) {
55
+ const instancePath = requestData.instancePath as string;
56
+ const outputId = requestData.outputId as string;
57
+ const style = (requestData.style as string) ?? "misc";
58
+
59
+ if (!instancePath) {
60
+ return { error: "Instance path is required" };
61
+ }
62
+
63
+ const instance = getInstanceByPath(instancePath);
64
+ if (!instance) return { error: `Instance not found: ${instancePath}` };
65
+
66
+ if (!instance.IsA("Model") && !instance.IsA("Folder")) {
67
+ return { error: "Instance must be a Model or Folder" };
68
+ }
69
+
70
+ const [success, result] = pcall(() => {
71
+ const descendants = instance.GetDescendants();
72
+ const baseParts: BasePart[] = [];
73
+ for (const desc of descendants) {
74
+ if (desc.IsA("BasePart")) {
75
+ baseParts.push(desc);
76
+ }
77
+ }
78
+
79
+ if (baseParts.size() === 0) {
80
+ return { error: "No BaseParts found in instance" };
81
+ }
82
+
83
+ // Compute bounding box center
84
+ let minX = math.huge;
85
+ let minY = math.huge;
86
+ let minZ = math.huge;
87
+ let maxX = -math.huge;
88
+ let maxY = -math.huge;
89
+ let maxZ = -math.huge;
90
+
91
+ for (const part of baseParts) {
92
+ const pos = part.Position;
93
+ const sz = part.Size;
94
+ const halfX = sz.X / 2;
95
+ const halfY = sz.Y / 2;
96
+ const halfZ = sz.Z / 2;
97
+ minX = math.min(minX, pos.X - halfX);
98
+ minY = math.min(minY, pos.Y - halfY);
99
+ minZ = math.min(minZ, pos.Z - halfZ);
100
+ maxX = math.max(maxX, pos.X + halfX);
101
+ maxY = math.max(maxY, pos.Y + halfY);
102
+ maxZ = math.max(maxZ, pos.Z + halfZ);
103
+ }
104
+
105
+ const centerX = (minX + maxX) / 2;
106
+ const centerY = minY; // Use bottom as Y origin
107
+ const centerZ = (minZ + maxZ) / 2;
108
+ const boundsX = roundTo(maxX - minX, 1);
109
+ const boundsY = roundTo(maxY - minY, 1);
110
+ const boundsZ = roundTo(maxZ - minZ, 1);
111
+
112
+ // Build palette from unique (BrickColor, Material, MaterialVariant?) combos
113
+ const paletteMap = new Map<string, string>();
114
+ const palette: Record<string, [string, string] | [string, string, string]> = {};
115
+ let keyIndex = 0;
116
+
117
+ for (const part of baseParts) {
118
+ const colorName = part.BrickColor.Name;
119
+ const materialName = part.Material.Name;
120
+ const variantName = getVariantName(part);
121
+ const combo = variantName !== "" ? `${colorName}|${materialName}|${variantName}` : `${colorName}|${materialName}`;
122
+
123
+ if (!paletteMap.has(combo)) {
124
+ const key = encodePaletteKey(keyIndex);
125
+ keyIndex++;
126
+ paletteMap.set(combo, key);
127
+ if (variantName !== "") {
128
+ palette[key] = [colorName, materialName, variantName];
129
+ } else {
130
+ palette[key] = [colorName, materialName];
131
+ }
132
+ }
133
+ }
134
+
135
+ // Build compact part arrays
136
+ const parts: unknown[][] = [];
137
+ for (const part of baseParts) {
138
+ const pos = part.Position;
139
+ const orient = part.Orientation;
140
+ const sz = part.Size;
141
+ const colorName = part.BrickColor.Name;
142
+ const materialName = part.Material.Name;
143
+ const variantName = getVariantName(part);
144
+ const combo = variantName !== "" ? `${colorName}|${materialName}|${variantName}` : `${colorName}|${materialName}`;
145
+ const paletteKey = paletteMap.get(combo) ?? "a";
146
+
147
+ // Relative position to center
148
+ const relX = roundTo(pos.X - centerX, 1);
149
+ const relY = roundTo(pos.Y - centerY, 1);
150
+ const relZ = roundTo(pos.Z - centerZ, 1);
151
+ const sizeX = roundTo(sz.X, 1);
152
+ const sizeY = roundTo(sz.Y, 1);
153
+ const sizeZ = roundTo(sz.Z, 1);
154
+ const rotX = roundTo(orient.X, 1);
155
+ const rotY = roundTo(orient.Y, 1);
156
+ const rotZ = roundTo(orient.Z, 1);
157
+
158
+ // Determine shape
159
+ let shape = "Block";
160
+ if (part.IsA("WedgePart")) {
161
+ shape = "Wedge";
162
+ } else if (part.IsA("CornerWedgePart")) {
163
+ shape = "CornerWedge";
164
+ } else if (part.IsA("Part")) {
165
+ const p = part as Part;
166
+ if (p.Shape === Enum.PartType.Cylinder) {
167
+ shape = "Cylinder";
168
+ } else if (p.Shape === Enum.PartType.Ball) {
169
+ shape = "Ball";
170
+ }
171
+ }
172
+
173
+ // Build part array with optional trailing fields
174
+ const hasTransparency = part.Transparency > 0;
175
+ const hasShape = shape !== "Block";
176
+
177
+ let partArr: defined[];
178
+ if (hasTransparency) {
179
+ partArr = [relX, relY, relZ, sizeX, sizeY, sizeZ, rotX, rotY, rotZ, paletteKey, hasShape ? shape : "Block", roundTo(part.Transparency, 2)];
180
+ } else if (hasShape) {
181
+ partArr = [relX, relY, relZ, sizeX, sizeY, sizeZ, rotX, rotY, rotZ, paletteKey, shape];
182
+ } else {
183
+ partArr = [relX, relY, relZ, sizeX, sizeY, sizeZ, rotX, rotY, rotZ, paletteKey];
184
+ }
185
+
186
+ parts.push(partArr);
187
+ }
188
+
189
+ const buildId = outputId ?? `${style}/${instance.Name.lower().gsub(" ", "_")[0]}`;
190
+
191
+ return {
192
+ success: true,
193
+ buildData: {
194
+ id: buildId,
195
+ style: style,
196
+ bounds: [boundsX, boundsY, boundsZ],
197
+ palette: palette,
198
+ parts: parts,
199
+ },
200
+ };
201
+ });
202
+
203
+ if (success && result) {
204
+ return result;
205
+ } else {
206
+ return { error: `Failed to export build: ${result}` };
207
+ }
208
+ }
209
+
210
+ function importBuild(requestData: Record<string, unknown>) {
211
+ const buildData = requestData.buildData as Record<string, unknown>;
212
+ const targetPath = requestData.targetPath as string;
213
+ const positionOffset = (requestData.position as number[]) ?? [0, 0, 0];
214
+
215
+ if (!buildData || !targetPath) {
216
+ return { error: "buildData and targetPath are required" };
217
+ }
218
+
219
+ const parentInstance = getInstanceByPath(targetPath);
220
+ if (!parentInstance) return { error: `Target not found: ${targetPath}` };
221
+ const recordingId = beginRecording("Import build");
222
+
223
+ const [success, result] = pcall(() => {
224
+ const palette = buildData.palette as Record<string, [string, string, string?]>;
225
+ const parts = buildData.parts as unknown[][];
226
+ const buildId = (buildData.id as string) ?? "imported_build";
227
+
228
+ // Create model container
229
+ const model = new Instance("Model");
230
+ model.Name = buildId.match("[^/]+$")[0] as string ?? buildId;
231
+
232
+ let partCount = 0;
233
+
234
+ for (const partArr of parts) {
235
+ const posX = (partArr[0] as number) + (positionOffset[0] ?? 0);
236
+ const posY = (partArr[1] as number) + (positionOffset[1] ?? 0);
237
+ const posZ = (partArr[2] as number) + (positionOffset[2] ?? 0);
238
+ const sizeX = partArr[3] as number;
239
+ const sizeY = partArr[4] as number;
240
+ const sizeZ = partArr[5] as number;
241
+ const rotX = partArr[6] as number;
242
+ const rotY = partArr[7] as number;
243
+ const rotZ = partArr[8] as number;
244
+ const paletteKey = partArr[9] as string;
245
+ const shape = (partArr[10] as string) ?? "Block";
246
+ const transparency = (partArr[11] as number) ?? 0;
247
+
248
+ // Determine class from shape
249
+ const className = SHAPE_CLASSES[shape] ?? "Part";
250
+ const part = new Instance(className as keyof CreatableInstances) as BasePart;
251
+
252
+ // Set shape for Part instances with non-Block shapes
253
+ if (className === "Part" && shape !== "Block") {
254
+ if (shape === "Cylinder") {
255
+ (part as Part).Shape = Enum.PartType.Cylinder;
256
+ } else if (shape === "Ball") {
257
+ (part as Part).Shape = Enum.PartType.Ball;
258
+ }
259
+ }
260
+
261
+ part.Size = new Vector3(sizeX, sizeY, sizeZ);
262
+ part.Position = new Vector3(posX, posY, posZ);
263
+ part.Orientation = new Vector3(rotX, rotY, rotZ);
264
+ part.Anchored = true;
265
+
266
+ if (transparency > 0) {
267
+ part.Transparency = transparency;
268
+ }
269
+
270
+ // Apply palette
271
+ const paletteEntry = palette[paletteKey];
272
+ if (paletteEntry) {
273
+ const [colorName, materialName, variantName] = paletteEntry;
274
+ pcall(() => {
275
+ part.BrickColor = new BrickColor(colorName as unknown as number);
276
+ });
277
+ pcall(() => {
278
+ const mat = MATERIAL_BY_NAME.get(materialName);
279
+ if (mat !== undefined) {
280
+ part.Material = mat;
281
+ }
282
+ });
283
+ // Apply MaterialVariant if specified
284
+ if (variantName !== undefined && variantName !== "") {
285
+ pcall(() => {
286
+ part.MaterialVariant = variantName;
287
+ });
288
+ }
289
+ }
290
+
291
+ part.Parent = model;
292
+ partCount++;
293
+ }
294
+
295
+ model.Parent = parentInstance;
296
+
297
+ return {
298
+ success: true,
299
+ partCount: partCount,
300
+ modelPath: getInstancePath(model),
301
+ };
302
+ });
303
+
304
+ if (success && result) {
305
+ finishRecording(recordingId, true);
306
+ return result;
307
+ } else {
308
+ finishRecording(recordingId, false);
309
+ return { error: `Failed to import build: ${result}` };
310
+ }
311
+ }
312
+
313
+ function importScene(requestData: Record<string, unknown>) {
314
+ const expandedBuilds = requestData.expandedBuilds as Record<string, unknown>[];
315
+ const targetPath = (requestData.targetPath as string) ?? "game.Workspace";
316
+
317
+ if (!expandedBuilds || !typeIs(expandedBuilds, "table") || (expandedBuilds as defined[]).size() === 0) {
318
+ return { error: "expandedBuilds array is required" };
319
+ }
320
+
321
+ const parentInstance = getInstanceByPath(targetPath);
322
+ if (!parentInstance) return { error: `Target not found: ${targetPath}` };
323
+ const recordingId = beginRecording("Import scene");
324
+
325
+ const [success, result] = pcall(() => {
326
+ let modelCount = 0;
327
+ let totalParts = 0;
328
+ const models: Record<string, unknown>[] = [];
329
+
330
+ for (const entry of expandedBuilds) {
331
+ const buildData = entry.buildData as Record<string, unknown>;
332
+ const position = (entry.position as number[]) ?? [0, 0, 0];
333
+ const rotation = (entry.rotation as number[]) ?? [0, 0, 0];
334
+ const name = (entry.name as string) ?? "SceneModel";
335
+
336
+ const palette = buildData.palette as Record<string, [string, string, string?]>;
337
+ const parts = buildData.parts as unknown[][];
338
+
339
+ const model = new Instance("Model");
340
+ model.Name = name;
341
+
342
+ const rotCF = CFrame.Angles(
343
+ math.rad(rotation[0] ?? 0),
344
+ math.rad(rotation[1] ?? 0),
345
+ math.rad(rotation[2] ?? 0),
346
+ );
347
+ const originCF = new CFrame(position[0] ?? 0, position[1] ?? 0, position[2] ?? 0).mul(rotCF);
348
+
349
+ let partCount = 0;
350
+
351
+ for (const partArr of parts) {
352
+ const localX = partArr[0] as number;
353
+ const localY = partArr[1] as number;
354
+ const localZ = partArr[2] as number;
355
+ const sizeX = partArr[3] as number;
356
+ const sizeY = partArr[4] as number;
357
+ const sizeZ = partArr[5] as number;
358
+ const rotX = partArr[6] as number;
359
+ const rotY = partArr[7] as number;
360
+ const rotZ = partArr[8] as number;
361
+ const paletteKey = partArr[9] as string;
362
+ const shape = (partArr[10] as string) ?? "Block";
363
+ const transparency = (partArr[11] as number) ?? 0;
364
+
365
+ const className = SHAPE_CLASSES[shape] ?? "Part";
366
+ const part = new Instance(className as keyof CreatableInstances) as BasePart;
367
+
368
+ if (className === "Part" && shape !== "Block") {
369
+ if (shape === "Cylinder") {
370
+ (part as Part).Shape = Enum.PartType.Cylinder;
371
+ } else if (shape === "Ball") {
372
+ (part as Part).Shape = Enum.PartType.Ball;
373
+ }
374
+ }
375
+
376
+ part.Size = new Vector3(sizeX, sizeY, sizeZ);
377
+
378
+ // Apply local rotation then world transform
379
+ const localRotCF = CFrame.Angles(math.rad(rotX), math.rad(rotY), math.rad(rotZ));
380
+ const localPosCF = new CFrame(localX, localY, localZ).mul(localRotCF);
381
+ const worldCF = originCF.mul(localPosCF);
382
+
383
+ part.CFrame = worldCF;
384
+ part.Anchored = true;
385
+
386
+ if (transparency > 0) {
387
+ part.Transparency = transparency;
388
+ }
389
+
390
+ const paletteEntry = palette[paletteKey];
391
+ if (paletteEntry) {
392
+ const [colorName, materialName, variantName] = paletteEntry;
393
+ pcall(() => {
394
+ part.BrickColor = new BrickColor(colorName as unknown as number);
395
+ });
396
+ pcall(() => {
397
+ const mat = MATERIAL_BY_NAME.get(materialName);
398
+ if (mat !== undefined) {
399
+ part.Material = mat;
400
+ }
401
+ });
402
+ if (variantName !== undefined && variantName !== "") {
403
+ pcall(() => {
404
+ part.MaterialVariant = variantName;
405
+ });
406
+ }
407
+ }
408
+
409
+ part.Parent = model;
410
+ partCount++;
411
+ }
412
+
413
+ model.Parent = parentInstance;
414
+ modelCount++;
415
+ totalParts += partCount;
416
+ models.push({
417
+ name: name,
418
+ partCount: partCount,
419
+ modelPath: getInstancePath(model),
420
+ });
421
+ }
422
+
423
+ return {
424
+ success: true,
425
+ modelCount: modelCount,
426
+ totalParts: totalParts,
427
+ models: models,
428
+ };
429
+ });
430
+
431
+ if (success && result) {
432
+ finishRecording(recordingId, true);
433
+ return result;
434
+ } else {
435
+ finishRecording(recordingId, false);
436
+ return { error: `Failed to import scene: ${result}` };
437
+ }
438
+ }
439
+
440
+ function searchMaterials(requestData: Record<string, unknown>) {
441
+ const query = ((requestData.query as string) ?? "").lower();
442
+ const maxResults = (requestData.maxResults as number) ?? 50;
443
+
444
+ const [success, result] = pcall(() => {
445
+ const children = MaterialService.GetChildren();
446
+ const materials: Record<string, unknown>[] = [];
447
+
448
+ for (const child of children) {
449
+ if (!child.IsA("MaterialVariant")) continue;
450
+
451
+ const nameMatch = query === "" || child.Name.lower().find(query)[0] !== undefined;
452
+ if (!nameMatch) continue;
453
+
454
+ materials.push({
455
+ name: child.Name,
456
+ baseMaterial: child.BaseMaterial.Name,
457
+ });
458
+
459
+ if (materials.size() >= maxResults) break;
460
+ }
461
+
462
+ return {
463
+ success: true,
464
+ materials: materials,
465
+ total: materials.size(),
466
+ };
467
+ });
468
+
469
+ if (success && result) {
470
+ return result;
471
+ } else {
472
+ return { error: `Failed to search materials: ${result}` };
473
+ }
474
+ }
475
+
476
+ export = {
477
+ exportBuild,
478
+ importBuild,
479
+ importScene,
480
+ searchMaterials,
481
+ };
@@ -0,0 +1,128 @@
1
+ const CaptureService = game.GetService("CaptureService");
2
+ const AssetService = game.GetService("AssetService");
3
+
4
+ const MAX_TILE_SIZE = 1024;
5
+ const BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
6
+ const PAD_BYTE = string.byte("=")[0];
7
+
8
+ const B64: number[] = [];
9
+ for (let i = 0; i < 64; i++) {
10
+ B64[i] = string.byte(BASE64_CHARS, i + 1)[0];
11
+ }
12
+
13
+ function encodeBase64(buf: buffer): string {
14
+ const len = buffer.len(buf);
15
+ const fullTriples = math.floor(len / 3);
16
+ const remaining = len - fullTriples * 3;
17
+ const outLen = (fullTriples + (remaining > 0 ? 1 : 0)) * 4;
18
+ const out = buffer.create(outLen);
19
+
20
+ let si = 0;
21
+ let di = 0;
22
+
23
+ for (let t = 0; t < fullTriples; t++) {
24
+ const b0 = buffer.readu8(buf, si);
25
+ const b1 = buffer.readu8(buf, si + 1);
26
+ const b2 = buffer.readu8(buf, si + 2);
27
+
28
+ buffer.writeu8(out, di, B64[bit32.rshift(b0, 2)]);
29
+ buffer.writeu8(out, di + 1, B64[bit32.bor(bit32.lshift(bit32.band(b0, 3), 4), bit32.rshift(b1, 4))]);
30
+ buffer.writeu8(out, di + 2, B64[bit32.bor(bit32.lshift(bit32.band(b1, 15), 2), bit32.rshift(b2, 6))]);
31
+ buffer.writeu8(out, di + 3, B64[bit32.band(b2, 63)]);
32
+
33
+ si += 3;
34
+ di += 4;
35
+ }
36
+
37
+ if (remaining === 2) {
38
+ const b0 = buffer.readu8(buf, si);
39
+ const b1 = buffer.readu8(buf, si + 1);
40
+ buffer.writeu8(out, di, B64[bit32.rshift(b0, 2)]);
41
+ buffer.writeu8(out, di + 1, B64[bit32.bor(bit32.lshift(bit32.band(b0, 3), 4), bit32.rshift(b1, 4))]);
42
+ buffer.writeu8(out, di + 2, B64[bit32.lshift(bit32.band(b1, 15), 2)]);
43
+ buffer.writeu8(out, di + 3, PAD_BYTE);
44
+ } else if (remaining === 1) {
45
+ const b0 = buffer.readu8(buf, si);
46
+ buffer.writeu8(out, di, B64[bit32.rshift(b0, 2)]);
47
+ buffer.writeu8(out, di + 1, B64[bit32.lshift(bit32.band(b0, 3), 4)]);
48
+ buffer.writeu8(out, di + 2, PAD_BYTE);
49
+ buffer.writeu8(out, di + 3, PAD_BYTE);
50
+ }
51
+
52
+ return buffer.tostring(out);
53
+ }
54
+
55
+ function readPixelsTiled(img: EditableImage, w: number, h: number): buffer {
56
+ const BYTES_PER_PIXEL = 4;
57
+ const fullBuf = buffer.create(w * h * BYTES_PER_PIXEL);
58
+ const fullRowBytes = w * BYTES_PER_PIXEL;
59
+
60
+ for (let ty = 0; ty < h; ty += MAX_TILE_SIZE) {
61
+ const tileH = math.min(MAX_TILE_SIZE, h - ty);
62
+ for (let tx = 0; tx < w; tx += MAX_TILE_SIZE) {
63
+ const tileW = math.min(MAX_TILE_SIZE, w - tx);
64
+ const tileBuf = img.ReadPixelsBuffer(new Vector2(tx, ty), new Vector2(tileW, tileH));
65
+ const tileRowBytes = tileW * BYTES_PER_PIXEL;
66
+ for (let row = 0; row < tileH; row++) {
67
+ buffer.copy(fullBuf, (ty + row) * fullRowBytes + tx * BYTES_PER_PIXEL, tileBuf, row * tileRowBytes, tileRowBytes);
68
+ }
69
+ }
70
+ }
71
+ return fullBuf;
72
+ }
73
+
74
+ function captureScreenshotData(): unknown {
75
+ let contentId: string | undefined;
76
+
77
+ CaptureService.CaptureScreenshot((id: string) => {
78
+ contentId = id;
79
+ });
80
+
81
+ const startTime = tick();
82
+ while (contentId === undefined) {
83
+ if (tick() - startTime > 10) {
84
+ return {
85
+ error: "Screenshot capture timed out. Ensure the Studio viewport is visible and you are in Edit mode (not Play mode). Known Roblox bug: capture may fail if viewport renders a solid color.",
86
+ };
87
+ }
88
+ task.wait(0.1);
89
+ }
90
+
91
+ const [editableOk, editableResult] = pcall(() => {
92
+ return AssetService.CreateEditableImageAsync(Content.fromUri(contentId!));
93
+ });
94
+
95
+ if (!editableOk) {
96
+ return {
97
+ error: `Failed to create EditableImage from screenshot. Enable EditableImage API: Game Settings > Security > 'Allow Mesh / Image APIs'. (${tostring(editableResult)})`,
98
+ };
99
+ }
100
+
101
+ const editableImage = editableResult as EditableImage;
102
+ const imgSize = editableImage.Size;
103
+ const w = math.floor(imgSize.X);
104
+ const h = math.floor(imgSize.Y);
105
+
106
+ const [readOk, pixelBuffer] = pcall(() => {
107
+ return readPixelsTiled(editableImage, w, h);
108
+ });
109
+
110
+ editableImage.Destroy();
111
+
112
+ if (!readOk) {
113
+ return { error: `Failed to read pixel data: ${tostring(pixelBuffer)}` };
114
+ }
115
+
116
+ const base64Data = encodeBase64(pixelBuffer as buffer);
117
+
118
+ return { success: true, width: w, height: h, data: base64Data };
119
+ }
120
+
121
+ function captureScreenshot(): unknown {
122
+ return captureScreenshotData();
123
+ }
124
+
125
+ export = {
126
+ captureScreenshotData,
127
+ captureScreenshot,
128
+ };
@@ -0,0 +1,102 @@
1
+ interface VIMethods {
2
+ SendMouseButtonEvent(x: number, y: number, button: number, isDown: boolean): void;
3
+ SendMouseMoveEvent(x: number, y: number): void;
4
+ SendMouseWheelEvent(x: number, y: number, isForward: boolean): void;
5
+ SendKeyEvent(isPressed: boolean, keyCode: Enum.KeyCode, isRepeatedKey: boolean): void;
6
+ }
7
+
8
+ function getVIM(): VIMethods | undefined {
9
+ const [ok, result] = pcall(() => {
10
+ return (game as unknown as { GetService(name: string): Instance }).GetService("VirtualInputManager");
11
+ });
12
+ if (ok && result) return result as unknown as VIMethods;
13
+ return undefined;
14
+ }
15
+
16
+ const BUTTON_MAP: Record<string, number> = { Left: 0, Right: 1, Middle: 2 };
17
+
18
+ function simulateMouseInput(requestData: Record<string, unknown>) {
19
+ const action = requestData.action as string;
20
+ const x = requestData.x as number | undefined;
21
+ const y = requestData.y as number | undefined;
22
+ const button = (requestData.button as string) ?? "Left";
23
+ const scrollDirection = requestData.scrollDirection as string | undefined;
24
+
25
+ if (!action) return { error: "action is required" };
26
+
27
+ const vim = getVIM();
28
+ if (!vim) return { error: "VirtualInputManager is not available in this context" };
29
+
30
+ const buttonNum = BUTTON_MAP[button] ?? 0;
31
+
32
+ const [success, err] = pcall(() => {
33
+ if (action === "click") {
34
+ if (x === undefined || y === undefined) error("x and y are required for click");
35
+ vim.SendMouseButtonEvent(x, y, buttonNum, true);
36
+ task.wait(0.05);
37
+ vim.SendMouseButtonEvent(x, y, buttonNum, false);
38
+ } else if (action === "mouseDown") {
39
+ if (x === undefined || y === undefined) error("x and y are required for mouseDown");
40
+ vim.SendMouseButtonEvent(x, y, buttonNum, true);
41
+ } else if (action === "mouseUp") {
42
+ if (x === undefined || y === undefined) error("x and y are required for mouseUp");
43
+ vim.SendMouseButtonEvent(x, y, buttonNum, false);
44
+ } else if (action === "move") {
45
+ if (x === undefined || y === undefined) error("x and y are required for move");
46
+ vim.SendMouseMoveEvent(x, y);
47
+ } else if (action === "scroll") {
48
+ if (x === undefined || y === undefined) error("x and y are required for scroll");
49
+ if (!scrollDirection) error("scrollDirection is required for scroll");
50
+ vim.SendMouseWheelEvent(x, y, scrollDirection === "up");
51
+ } else {
52
+ error(`Unknown action: ${action}`);
53
+ }
54
+ });
55
+
56
+ if (success) {
57
+ return { success: true, action, x, y, button };
58
+ }
59
+ return { error: `Failed to simulate mouse input: ${err}` };
60
+ }
61
+
62
+ function simulateKeyboardInput(requestData: Record<string, unknown>) {
63
+ const keyCodeName = requestData.keyCode as string;
64
+ const action = (requestData.action as string) ?? "tap";
65
+ const duration = (requestData.duration as number) ?? 0.1;
66
+
67
+ if (!keyCodeName) return { error: "keyCode is required" };
68
+
69
+ const vim = getVIM();
70
+ if (!vim) return { error: "VirtualInputManager is not available in this context" };
71
+
72
+ const [enumOk, keyCode] = pcall(() => {
73
+ return (Enum.KeyCode as unknown as Record<string, Enum.KeyCode>)[keyCodeName];
74
+ });
75
+ if (!enumOk || !keyCode) {
76
+ return { error: `Unknown keyCode: ${keyCodeName}. Use Enum.KeyCode names like "W", "Space", "E", "LeftShift", etc.` };
77
+ }
78
+
79
+ const [success, err] = pcall(() => {
80
+ if (action === "press") {
81
+ vim.SendKeyEvent(true, keyCode, false);
82
+ } else if (action === "release") {
83
+ vim.SendKeyEvent(false, keyCode, false);
84
+ } else if (action === "tap") {
85
+ vim.SendKeyEvent(true, keyCode, false);
86
+ task.wait(duration);
87
+ vim.SendKeyEvent(false, keyCode, false);
88
+ } else {
89
+ error(`Unknown action: ${action}`);
90
+ }
91
+ });
92
+
93
+ if (success) {
94
+ return { success: true, keyCode: keyCodeName, action };
95
+ }
96
+ return { error: `Failed to simulate keyboard input: ${err}` };
97
+ }
98
+
99
+ export = {
100
+ simulateMouseInput,
101
+ simulateKeyboardInput,
102
+ };