playcanvas-agent-bridge-cli 0.3.0 → 0.4.2
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/README.md +138 -58
- package/README.zh-CN.md +134 -58
- package/dist/cli.js +483 -87
- package/dist/config.js +1 -1
- package/dist/daemon/server.js +133 -6
- package/dist/daemon/target-registry.js +43 -3
- package/dist/shared/protocol.js +3 -1
- package/dist/snippets.js +726 -69
- package/dist/workspace/manager.js +536 -0
- package/extension/icons/concepts/pcbridge-bridge.png +0 -0
- package/extension/icons/concepts/pcbridge-cube.png +0 -0
- package/extension/icons/concepts/pcbridge-p-active.png +0 -0
- package/extension/icons/concepts/pcbridge-p-arch.png +0 -0
- package/extension/icons/concepts/pcbridge-p-bridge.png +0 -0
- package/extension/icons/concepts/pcbridge-p-emblem.png +0 -0
- package/extension/icons/concepts/pcbridge-signal.png +0 -0
- package/extension/icons/icon-128.png +0 -0
- package/extension/icons/icon-16.png +0 -0
- package/extension/icons/icon-32.png +0 -0
- package/extension/icons/icon-48.png +0 -0
- package/extension/isolated.js +90 -15
- package/extension/main.js +383 -24
- package/extension/manifest.json +14 -2
- package/extension/popup.html +179 -56
- package/extension/popup.js +161 -58
- package/extension/service-worker.js +268 -17
- package/package.json +1 -1
- package/skills/claude/playcanvas-agent-bridge-cli/SKILL.md +83 -28
- package/skills/codex/playcanvas-agent-bridge-cli/SKILL.md +137 -57
- package/skills/cursor/playcanvas-agent-bridge-cli.mdc +51 -24
- package/skills/windsurf/playcanvas-agent-bridge-cli.md +50 -25
package/extension/isolated.js
CHANGED
|
@@ -17,6 +17,10 @@
|
|
|
17
17
|
let reconnectTimer = null;
|
|
18
18
|
let heartbeatTimer = null;
|
|
19
19
|
let stopped = false;
|
|
20
|
+
let connecting = false;
|
|
21
|
+
let reconnectAttempt = 0;
|
|
22
|
+
let connectionState = "unknown";
|
|
23
|
+
let lastTargetSignature = null;
|
|
20
24
|
let config = DEFAULT_CONFIG;
|
|
21
25
|
let tabInfo = {};
|
|
22
26
|
let extensionVersion = null;
|
|
@@ -72,10 +76,12 @@
|
|
|
72
76
|
|
|
73
77
|
function serializeError(error) {
|
|
74
78
|
if (error && typeof error === "object" && error.message) {
|
|
75
|
-
|
|
79
|
+
const serialized = {
|
|
76
80
|
code: error.code || "EXTENSION_ERROR",
|
|
77
81
|
message: String(error.message)
|
|
78
82
|
};
|
|
83
|
+
if (error.details !== undefined) serialized.details = error.details;
|
|
84
|
+
return serialized;
|
|
79
85
|
}
|
|
80
86
|
return { code: "EXTENSION_ERROR", message: String(error) };
|
|
81
87
|
}
|
|
@@ -200,13 +206,26 @@
|
|
|
200
206
|
}
|
|
201
207
|
}
|
|
202
208
|
|
|
203
|
-
async function sendTargetUpdate() {
|
|
209
|
+
async function sendTargetUpdate(force = false) {
|
|
204
210
|
if (stopped || !ws || ws.readyState !== WebSocket.OPEN) return;
|
|
205
211
|
try {
|
|
212
|
+
const target = await describeTarget();
|
|
213
|
+
const signature = JSON.stringify(target);
|
|
214
|
+
if (!force && signature === lastTargetSignature) return;
|
|
215
|
+
if (target.projectId) {
|
|
216
|
+
await requestRuntime({
|
|
217
|
+
type: "pcbridge:rememberProjectContext",
|
|
218
|
+
url: target.url,
|
|
219
|
+
projectId: target.projectId,
|
|
220
|
+
sceneId: target.sceneId
|
|
221
|
+
});
|
|
222
|
+
if (stopped) return;
|
|
223
|
+
}
|
|
224
|
+
lastTargetSignature = signature;
|
|
206
225
|
ws.send(
|
|
207
226
|
JSON.stringify({
|
|
208
227
|
type: "target:update",
|
|
209
|
-
target
|
|
228
|
+
target
|
|
210
229
|
})
|
|
211
230
|
);
|
|
212
231
|
} catch (error) {
|
|
@@ -214,21 +233,53 @@
|
|
|
214
233
|
}
|
|
215
234
|
}
|
|
216
235
|
|
|
236
|
+
function setConnectionState(next) {
|
|
237
|
+
if (connectionState === next) return;
|
|
238
|
+
connectionState = next;
|
|
239
|
+
if (next === "connected") {
|
|
240
|
+
console.info("[pcbridge] connected to local daemon");
|
|
241
|
+
} else if (next === "disconnected") {
|
|
242
|
+
console.info("[pcbridge] disconnected from local daemon; waiting to reconnect");
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
async function probeDaemon() {
|
|
247
|
+
const response = await requestRuntime({ type: "pcbridge:probeDaemon" });
|
|
248
|
+
return Boolean(response && response.reachable);
|
|
249
|
+
}
|
|
250
|
+
|
|
217
251
|
function scheduleReconnect() {
|
|
218
252
|
if (stopped) return;
|
|
219
253
|
if (reconnectTimer) return;
|
|
254
|
+
const delays = [1000, 2000, 5000, 10000, 30000];
|
|
255
|
+
const delay = delays[Math.min(reconnectAttempt, delays.length - 1)];
|
|
256
|
+
reconnectAttempt += 1;
|
|
220
257
|
reconnectTimer = setTimeout(() => {
|
|
221
258
|
reconnectTimer = null;
|
|
222
|
-
connect();
|
|
223
|
-
},
|
|
259
|
+
void connect();
|
|
260
|
+
}, delay);
|
|
224
261
|
}
|
|
225
262
|
|
|
226
|
-
function connect() {
|
|
263
|
+
async function connect() {
|
|
227
264
|
if (stopped) return;
|
|
265
|
+
if (connecting) return;
|
|
228
266
|
if (ws && (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING)) {
|
|
229
267
|
return;
|
|
230
268
|
}
|
|
231
269
|
|
|
270
|
+
connecting = true;
|
|
271
|
+
const reachable = await probeDaemon();
|
|
272
|
+
if (stopped) {
|
|
273
|
+
connecting = false;
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
if (!reachable) {
|
|
277
|
+
connecting = false;
|
|
278
|
+
setConnectionState("disconnected");
|
|
279
|
+
scheduleReconnect();
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
|
|
232
283
|
const url =
|
|
233
284
|
`ws://${config.host}:${config.port}/extension?` +
|
|
234
285
|
`token=${encodeURIComponent(config.token || "")}&clientId=${encodeURIComponent(clientId)}`;
|
|
@@ -236,18 +287,22 @@
|
|
|
236
287
|
try {
|
|
237
288
|
ws = new WebSocket(url);
|
|
238
289
|
} catch {
|
|
290
|
+
connecting = false;
|
|
239
291
|
scheduleReconnect();
|
|
240
292
|
return;
|
|
241
293
|
}
|
|
294
|
+
const socket = ws;
|
|
295
|
+
connecting = false;
|
|
242
296
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
297
|
+
socket.addEventListener("open", () => {
|
|
298
|
+
reconnectAttempt = 0;
|
|
299
|
+
setConnectionState("connected");
|
|
300
|
+
sendTargetUpdate(true).catch((error) => {
|
|
246
301
|
if (isExtensionContextInvalidated(error)) stopBridge();
|
|
247
302
|
});
|
|
248
303
|
});
|
|
249
304
|
|
|
250
|
-
|
|
305
|
+
socket.addEventListener("message", async (event) => {
|
|
251
306
|
let message;
|
|
252
307
|
try {
|
|
253
308
|
message = JSON.parse(event.data);
|
|
@@ -258,7 +313,16 @@
|
|
|
258
313
|
if (!message || message.type !== "request" || !message.id) return;
|
|
259
314
|
|
|
260
315
|
try {
|
|
261
|
-
|
|
316
|
+
let response;
|
|
317
|
+
if (message.method === "bridge:focusTarget") {
|
|
318
|
+
const data = await requestRuntime({ type: "pcbridge:focusCurrentTab" });
|
|
319
|
+
if (!data || !data.ok) {
|
|
320
|
+
throw new Error((data && data.error) || "Could not focus the PlayCanvas tab.");
|
|
321
|
+
}
|
|
322
|
+
response = { data, meta: {} };
|
|
323
|
+
} else {
|
|
324
|
+
response = await callMain(message.method, message.params || {}, message.timeoutMs);
|
|
325
|
+
}
|
|
262
326
|
if (!stopped && ws && ws.readyState === WebSocket.OPEN) {
|
|
263
327
|
ws.send(
|
|
264
328
|
JSON.stringify({
|
|
@@ -288,13 +352,16 @@
|
|
|
288
352
|
}
|
|
289
353
|
});
|
|
290
354
|
|
|
291
|
-
|
|
355
|
+
socket.addEventListener("close", () => {
|
|
356
|
+
if (ws === socket) ws = null;
|
|
357
|
+
lastTargetSignature = null;
|
|
358
|
+
setConnectionState("disconnected");
|
|
292
359
|
scheduleReconnect();
|
|
293
360
|
});
|
|
294
361
|
|
|
295
|
-
|
|
362
|
+
socket.addEventListener("error", () => {
|
|
296
363
|
try {
|
|
297
|
-
|
|
364
|
+
socket.close();
|
|
298
365
|
} catch {
|
|
299
366
|
scheduleReconnect();
|
|
300
367
|
}
|
|
@@ -302,13 +369,21 @@
|
|
|
302
369
|
}
|
|
303
370
|
|
|
304
371
|
async function start() {
|
|
372
|
+
const frontendPreference = await requestRuntime({
|
|
373
|
+
type: "pcbridge:applyFrontendPreference",
|
|
374
|
+
url: location.href
|
|
375
|
+
});
|
|
376
|
+
if (frontendPreference && frontendPreference.redirected) {
|
|
377
|
+
stopBridge();
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
305
380
|
config = await loadConfig();
|
|
306
381
|
if (stopped) return;
|
|
307
382
|
extensionVersion = getExtensionVersion();
|
|
308
383
|
if (stopped) return;
|
|
309
384
|
tabInfo = await getTabInfo();
|
|
310
385
|
if (stopped) return;
|
|
311
|
-
connect();
|
|
386
|
+
void connect();
|
|
312
387
|
heartbeatTimer = setInterval(() => {
|
|
313
388
|
sendTargetUpdate().catch((error) => {
|
|
314
389
|
if (isExtensionContextInvalidated(error)) stopBridge();
|
package/extension/main.js
CHANGED
|
@@ -145,11 +145,14 @@
|
|
|
145
145
|
if (editorSummary) return editorSummary;
|
|
146
146
|
|
|
147
147
|
if (current instanceof Error) {
|
|
148
|
-
|
|
148
|
+
const normalized = {
|
|
149
149
|
name: current.name,
|
|
150
150
|
message: current.message,
|
|
151
151
|
stack: current.stack
|
|
152
152
|
};
|
|
153
|
+
if (typeof current.code === "string") normalized.code = current.code;
|
|
154
|
+
if (current.details !== undefined) normalized.details = normalize(current.details, depth + 1);
|
|
155
|
+
return normalized;
|
|
153
156
|
}
|
|
154
157
|
|
|
155
158
|
if (depth >= settings.maxDepth) {
|
|
@@ -188,31 +191,197 @@
|
|
|
188
191
|
return "playcanvas";
|
|
189
192
|
}
|
|
190
193
|
|
|
191
|
-
function
|
|
194
|
+
function getAppCanvas(app) {
|
|
195
|
+
return (
|
|
196
|
+
(app && app.graphicsDevice && app.graphicsDevice.canvas) ||
|
|
197
|
+
(app && app.canvas) ||
|
|
198
|
+
null
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function getPrimaryCanvas(app) {
|
|
203
|
+
return getAppCanvas(app) || document.querySelector("canvas");
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function isVisibleElement(element) {
|
|
207
|
+
if (!element) return false;
|
|
208
|
+
const style = typeof window.getComputedStyle === "function"
|
|
209
|
+
? window.getComputedStyle(element)
|
|
210
|
+
: null;
|
|
211
|
+
if (
|
|
212
|
+
style &&
|
|
213
|
+
(style.display === "none" || style.visibility === "hidden" || Number(style.opacity) === 0)
|
|
214
|
+
) {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
if (typeof element.getBoundingClientRect !== "function") return true;
|
|
218
|
+
const rect = element.getBoundingClientRect();
|
|
219
|
+
return rect.width > 0 && rect.height > 0;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function runtimeRegistryEntries(registry) {
|
|
223
|
+
if (!registry) return [];
|
|
224
|
+
if (typeof registry.entries === "function") {
|
|
225
|
+
try {
|
|
226
|
+
return Array.from(registry.entries());
|
|
227
|
+
} catch {
|
|
228
|
+
// Fall through to object entries.
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
if (Array.isArray(registry)) return registry.map((app, index) => [String(index), app]);
|
|
232
|
+
if (typeof registry === "object") return Object.entries(registry);
|
|
233
|
+
return [];
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function isLiveRuntimeApp(app) {
|
|
237
|
+
return Boolean(app && app.root);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function resolveRuntimeApp() {
|
|
241
|
+
const explicitCandidates = [];
|
|
242
|
+
const fallbackCandidates = [];
|
|
243
|
+
const seen = new Set();
|
|
244
|
+
const add = (list, app, source) => {
|
|
245
|
+
if (!isLiveRuntimeApp(app) || seen.has(app)) return;
|
|
246
|
+
seen.add(app);
|
|
247
|
+
list.push({ app, source, order: list.length });
|
|
248
|
+
};
|
|
249
|
+
const callGetter = (list, owner, id, source) => {
|
|
250
|
+
try {
|
|
251
|
+
if (owner && typeof owner.getApplication === "function") {
|
|
252
|
+
add(list, owner.getApplication(id), source);
|
|
253
|
+
}
|
|
254
|
+
} catch {
|
|
255
|
+
// Continue through the compatibility fallbacks.
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
const pc = window.pc;
|
|
192
260
|
try {
|
|
193
|
-
if (
|
|
194
|
-
window.
|
|
195
|
-
window.pc.Application &&
|
|
196
|
-
typeof window.pc.Application.getApplication === "function"
|
|
197
|
-
) {
|
|
198
|
-
const app = window.pc.Application.getApplication();
|
|
199
|
-
if (app) return app;
|
|
261
|
+
if (window.editor && typeof window.editor.call === "function") {
|
|
262
|
+
add(explicitCandidates, window.editor.call("viewport:app"), "editor.call(viewport:app)");
|
|
200
263
|
}
|
|
201
264
|
} catch {
|
|
202
|
-
//
|
|
265
|
+
// Launch can expose only a partial Editor event bus during startup.
|
|
266
|
+
}
|
|
267
|
+
const canvasIds = [
|
|
268
|
+
"application-canvas",
|
|
269
|
+
...Array.from(document.querySelectorAll("canvas"))
|
|
270
|
+
.map((canvas) => canvas && canvas.id)
|
|
271
|
+
.filter((id) => id && id !== "application-canvas")
|
|
272
|
+
];
|
|
273
|
+
for (const id of canvasIds) {
|
|
274
|
+
callGetter(
|
|
275
|
+
explicitCandidates,
|
|
276
|
+
pc && pc.AppBase,
|
|
277
|
+
id,
|
|
278
|
+
"pc.AppBase.getApplication(" + id + ")"
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
for (const [owner, label] of [
|
|
282
|
+
[pc && pc.AppBase, "pc.AppBase._applications"],
|
|
283
|
+
[pc && pc.Application, "pc.Application._applications"]
|
|
284
|
+
]) {
|
|
285
|
+
for (const id of canvasIds) {
|
|
286
|
+
const match = runtimeRegistryEntries(owner && owner._applications)
|
|
287
|
+
.find(([key]) => String(key) === String(id));
|
|
288
|
+
if (match) add(explicitCandidates, match[1], label + "[" + id + "]");
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
for (const id of canvasIds) {
|
|
292
|
+
callGetter(
|
|
293
|
+
explicitCandidates,
|
|
294
|
+
pc && pc.Application,
|
|
295
|
+
id,
|
|
296
|
+
"pc.Application.getApplication(" + id + ")"
|
|
297
|
+
);
|
|
203
298
|
}
|
|
204
299
|
|
|
205
|
-
if (
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
300
|
+
if (explicitCandidates.length) {
|
|
301
|
+
const selected = explicitCandidates[0];
|
|
302
|
+
return {
|
|
303
|
+
...selected,
|
|
304
|
+
canvas: getAppCanvas(selected.app),
|
|
305
|
+
rootChildCount: selected.app.root && Array.isArray(selected.app.root.children)
|
|
306
|
+
? selected.app.root.children.length
|
|
307
|
+
: 0,
|
|
308
|
+
score: null
|
|
309
|
+
};
|
|
310
|
+
}
|
|
209
311
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
312
|
+
callGetter(fallbackCandidates, pc && pc.AppBase, undefined, "pc.AppBase.getApplication()");
|
|
313
|
+
callGetter(fallbackCandidates, pc && pc.Application, undefined, "pc.Application.getApplication()");
|
|
314
|
+
|
|
315
|
+
for (const [owner, label] of [
|
|
316
|
+
[pc && pc.AppBase, "pc.AppBase._applications"],
|
|
317
|
+
[pc && pc.Application, "pc.Application._applications"]
|
|
318
|
+
]) {
|
|
319
|
+
for (const [key, app] of runtimeRegistryEntries(owner && owner._applications)) {
|
|
320
|
+
add(fallbackCandidates, app, label + "[" + key + "]");
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
add(fallbackCandidates, window.app, "window.app");
|
|
325
|
+
add(fallbackCandidates, pc && pc.app, "pc.app");
|
|
326
|
+
|
|
327
|
+
const ownedMatches = fallbackCandidates.filter((candidate) => {
|
|
328
|
+
const canvas = getAppCanvas(candidate.app);
|
|
329
|
+
return Boolean(canvas && canvas.id && canvasIds.includes(canvas.id));
|
|
330
|
+
});
|
|
331
|
+
const mainCanvasMatches = ownedMatches.filter(
|
|
332
|
+
(candidate) => getAppCanvas(candidate.app).id === "application-canvas"
|
|
215
333
|
);
|
|
334
|
+
const selected = mainCanvasMatches.length === 1
|
|
335
|
+
? mainCanvasMatches[0]
|
|
336
|
+
: ownedMatches.length === 1
|
|
337
|
+
? ownedMatches[0]
|
|
338
|
+
: fallbackCandidates.length === 1
|
|
339
|
+
? fallbackCandidates[0]
|
|
340
|
+
: null;
|
|
341
|
+
if (selected) {
|
|
342
|
+
return {
|
|
343
|
+
...selected,
|
|
344
|
+
canvas: getAppCanvas(selected.app),
|
|
345
|
+
rootChildCount: Array.isArray(selected.app.root.children)
|
|
346
|
+
? selected.app.root.children.length
|
|
347
|
+
: 0,
|
|
348
|
+
ambiguous: false,
|
|
349
|
+
candidateSources: fallbackCandidates.map((candidate) => candidate.source)
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return {
|
|
354
|
+
app: null,
|
|
355
|
+
source: null,
|
|
356
|
+
canvas: null,
|
|
357
|
+
rootChildCount: 0,
|
|
358
|
+
ambiguous: fallbackCandidates.length > 1,
|
|
359
|
+
candidateSources: fallbackCandidates.map((candidate) => candidate.source)
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function getScriptTypeCount(app) {
|
|
364
|
+
try {
|
|
365
|
+
return app && app.scripts && typeof app.scripts.list === "function"
|
|
366
|
+
? app.scripts.list().length
|
|
367
|
+
: null;
|
|
368
|
+
} catch {
|
|
369
|
+
return null;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function getRuntimeApp() {
|
|
374
|
+
return resolveRuntimeApp().app;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function isLaunchSplashVisible() {
|
|
378
|
+
const selectors = [
|
|
379
|
+
"#application-splash-wrapper",
|
|
380
|
+
"#application-splash",
|
|
381
|
+
".application-splash-wrapper",
|
|
382
|
+
".application-splash"
|
|
383
|
+
];
|
|
384
|
+
return selectors.some((selector) => isVisibleElement(document.querySelector(selector)));
|
|
216
385
|
}
|
|
217
386
|
|
|
218
387
|
function getSceneIdFromUrl() {
|
|
@@ -231,10 +400,47 @@
|
|
|
231
400
|
const config = window.config || {};
|
|
232
401
|
const kind = getTargetKind();
|
|
233
402
|
const editorReady = Boolean(window.editor && window.editor.api && window.editor.api.globals);
|
|
234
|
-
const
|
|
403
|
+
const runtime = resolveRuntimeApp();
|
|
404
|
+
const app = runtime.app;
|
|
235
405
|
const canvasCount = document.querySelectorAll("canvas").length;
|
|
236
|
-
const
|
|
237
|
-
const
|
|
406
|
+
const pageReady = document.readyState !== "loading";
|
|
407
|
+
const visibilityState = document.visibilityState || "unknown";
|
|
408
|
+
const runtimeCreated = Boolean(app && app.root);
|
|
409
|
+
const ownedCanvas = getAppCanvas(app);
|
|
410
|
+
const graphicsContextLost = Boolean(app && app.graphicsDevice && app.graphicsDevice.contextLost);
|
|
411
|
+
const graphicsReady = Boolean(
|
|
412
|
+
runtimeCreated && app.graphicsDevice && ownedCanvas && !graphicsContextLost
|
|
413
|
+
);
|
|
414
|
+
const rootChildren = app && app.root && Array.isArray(app.root.children)
|
|
415
|
+
? app.root.children
|
|
416
|
+
: [];
|
|
417
|
+
const sceneRoot = app && app.scene ? app.scene.root : null;
|
|
418
|
+
const sceneLoaded = Boolean(
|
|
419
|
+
graphicsReady && rootChildren.length > 0 && (!sceneRoot || rootChildren.includes(sceneRoot))
|
|
420
|
+
);
|
|
421
|
+
const runtimeFrame = app && Number.isFinite(Number(app.frame)) ? Number(app.frame) : null;
|
|
422
|
+
const runtimeStarted = Boolean(sceneLoaded && runtimeFrame !== null && runtimeFrame > 0);
|
|
423
|
+
const scriptsReady = runtimeStarted;
|
|
424
|
+
const scriptTypeCount = getScriptTypeCount(app);
|
|
425
|
+
const splashVisible = kind === "launch" && isLaunchSplashVisible();
|
|
426
|
+
const lifecycleReady = Boolean(
|
|
427
|
+
pageReady && runtimeCreated && graphicsReady && sceneLoaded && runtimeStarted && !splashVisible
|
|
428
|
+
);
|
|
429
|
+
const readinessBlockers = [];
|
|
430
|
+
if (kind === "launch") {
|
|
431
|
+
if (!pageReady) readinessBlockers.push("page-loading");
|
|
432
|
+
if (visibilityState !== "visible") readinessBlockers.push("tab-hidden");
|
|
433
|
+
if (!runtimeCreated) readinessBlockers.push("runtime-not-created");
|
|
434
|
+
if (runtimeCreated && graphicsContextLost) readinessBlockers.push("graphics-context-lost");
|
|
435
|
+
if (runtimeCreated && !graphicsReady && !graphicsContextLost) {
|
|
436
|
+
readinessBlockers.push("graphics-not-ready");
|
|
437
|
+
}
|
|
438
|
+
if (runtimeCreated && !sceneLoaded) readinessBlockers.push("scene-not-loaded");
|
|
439
|
+
if (sceneLoaded && !runtimeStarted) readinessBlockers.push("runtime-not-started");
|
|
440
|
+
if (splashVisible) readinessBlockers.push("splash-visible");
|
|
441
|
+
}
|
|
442
|
+
const launchReady = kind === "launch" && lifecycleReady && visibilityState === "visible";
|
|
443
|
+
const ready = kind === "launch" ? launchReady : editorReady;
|
|
238
444
|
return {
|
|
239
445
|
kind,
|
|
240
446
|
url: location.href,
|
|
@@ -243,16 +449,47 @@
|
|
|
243
449
|
hasEditor: Boolean(window.editor),
|
|
244
450
|
hasPc: Boolean(window.pc),
|
|
245
451
|
hasRuntimeApp: Boolean(app),
|
|
452
|
+
runtimeAppSource: runtime.source || undefined,
|
|
453
|
+
runtimeAppAmbiguous: kind === "launch" ? Boolean(runtime.ambiguous) : undefined,
|
|
454
|
+
runtimeAppCandidateSources: kind === "launch" ? runtime.candidateSources || [] : undefined,
|
|
455
|
+
runtimeCanvasId: app && runtime.canvas && runtime.canvas.id
|
|
456
|
+
? String(runtime.canvas.id)
|
|
457
|
+
: undefined,
|
|
458
|
+
engineVersion: kind === "launch" && window.pc && window.pc.version
|
|
459
|
+
? String(window.pc.version)
|
|
460
|
+
: undefined,
|
|
461
|
+
readinessMode: kind === "launch" ? "heuristic" : undefined,
|
|
462
|
+
pageReady: kind === "launch" ? pageReady : undefined,
|
|
463
|
+
visibilityState: kind === "launch" ? visibilityState : undefined,
|
|
464
|
+
lifecycleReady: kind === "launch" ? lifecycleReady : undefined,
|
|
465
|
+
runtimeCreated: kind === "launch" ? runtimeCreated : undefined,
|
|
466
|
+
graphicsReady: kind === "launch" ? graphicsReady : undefined,
|
|
467
|
+
graphicsContextLost: kind === "launch" ? graphicsContextLost : undefined,
|
|
468
|
+
runtimeStarted: kind === "launch" ? runtimeStarted : undefined,
|
|
469
|
+
runtimeFrame: kind === "launch" ? runtimeFrame : undefined,
|
|
470
|
+
sceneLoaded: kind === "launch" ? sceneLoaded : undefined,
|
|
471
|
+
scriptsReady: kind === "launch" ? scriptsReady : undefined,
|
|
472
|
+
scriptTypeCount: kind === "launch" ? scriptTypeCount : undefined,
|
|
473
|
+
splashVisible: kind === "launch" ? splashVisible : undefined,
|
|
474
|
+
rootChildCount: kind === "launch" ? runtime.rootChildCount : undefined,
|
|
475
|
+
readinessBlockers: kind === "launch" ? readinessBlockers : undefined,
|
|
246
476
|
canvasCount,
|
|
247
477
|
projectId: config.project && config.project.id ? String(config.project.id) : undefined,
|
|
478
|
+
projectName: config.project && config.project.name ? String(config.project.name) : undefined,
|
|
248
479
|
sceneId:
|
|
249
480
|
(config.scene && config.scene.id ? String(config.scene.id) : undefined) ||
|
|
250
481
|
getSceneIdFromUrl(),
|
|
482
|
+
sceneName: config.scene && config.scene.name ? String(config.scene.name) : undefined,
|
|
251
483
|
branchId:
|
|
252
484
|
(config.self && config.self.branch && config.self.branch.id
|
|
253
485
|
? String(config.self.branch.id)
|
|
254
486
|
: undefined) ||
|
|
255
|
-
(config.branch && config.branch.id ? String(config.branch.id) : undefined)
|
|
487
|
+
(config.branch && config.branch.id ? String(config.branch.id) : undefined),
|
|
488
|
+
branchName:
|
|
489
|
+
(config.self && config.self.branch && config.self.branch.name
|
|
490
|
+
? String(config.self.branch.name)
|
|
491
|
+
: undefined) ||
|
|
492
|
+
(config.branch && config.branch.name ? String(config.branch.name) : undefined)
|
|
256
493
|
};
|
|
257
494
|
}
|
|
258
495
|
|
|
@@ -328,6 +565,122 @@
|
|
|
328
565
|
return bytes;
|
|
329
566
|
}
|
|
330
567
|
|
|
568
|
+
function base64FromBytes(bytes) {
|
|
569
|
+
const chunkSize = 0x8000;
|
|
570
|
+
let binary = "";
|
|
571
|
+
for (let offset = 0; offset < bytes.length; offset += chunkSize) {
|
|
572
|
+
binary += String.fromCharCode(...bytes.subarray(offset, offset + chunkSize));
|
|
573
|
+
}
|
|
574
|
+
return btoa(binary);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
function requireEditorAsset(assetId) {
|
|
578
|
+
if (!window.editor || !window.editor.api || !window.editor.api.globals) {
|
|
579
|
+
throw new Error("Workspace operations require a ready PlayCanvas Editor target.");
|
|
580
|
+
}
|
|
581
|
+
const asset = window.editor.api.globals.assets.get(Number(assetId));
|
|
582
|
+
if (!asset) throw new Error("Asset not found: " + assetId);
|
|
583
|
+
return asset;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
async function fetchAssetFile(asset) {
|
|
587
|
+
const filename = asset.get("file.filename") || asset.get("name");
|
|
588
|
+
if (!filename) throw new Error("Asset has no downloadable file: " + asset.get("id"));
|
|
589
|
+
const branchId =
|
|
590
|
+
(window.config && window.config.self && window.config.self.branch && window.config.self.branch.id) ||
|
|
591
|
+
(window.config && window.config.branch && window.config.branch.id);
|
|
592
|
+
const query = branchId ? "?branchId=" + encodeURIComponent(String(branchId)) : "";
|
|
593
|
+
const headers = {};
|
|
594
|
+
const accessToken = window.editor.api.globals.accessToken;
|
|
595
|
+
if (accessToken) headers.Authorization = "Bearer " + accessToken;
|
|
596
|
+
const response = await fetch(
|
|
597
|
+
"/api/assets/" + encodeURIComponent(String(asset.get("id"))) +
|
|
598
|
+
"/file/" + encodeURIComponent(String(filename)) + query,
|
|
599
|
+
{ headers }
|
|
600
|
+
);
|
|
601
|
+
if (!response.ok) {
|
|
602
|
+
const body = await response.text().catch(() => "");
|
|
603
|
+
throw new Error(body || "Failed to download asset " + asset.get("id") + ".");
|
|
604
|
+
}
|
|
605
|
+
return { response, filename };
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
function workspaceSnapshot() {
|
|
609
|
+
if (!window.editor || !window.editor.api || !window.editor.api.globals) {
|
|
610
|
+
throw new Error("Workspace operations require a ready PlayCanvas Editor target.");
|
|
611
|
+
}
|
|
612
|
+
return {
|
|
613
|
+
assets: window.editor.api.globals.assets.list().map(readAsset)
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
async function readAssetText(params) {
|
|
618
|
+
const asset = requireEditorAsset(params.assetId);
|
|
619
|
+
if (asset.get("type") !== "script") {
|
|
620
|
+
throw new Error("Asset is not a script: " + params.assetId);
|
|
621
|
+
}
|
|
622
|
+
const { response, filename } = await fetchAssetFile(asset);
|
|
623
|
+
return {
|
|
624
|
+
assetId: String(asset.get("id")),
|
|
625
|
+
filename,
|
|
626
|
+
text: await response.text()
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
async function readAssetFile(params) {
|
|
631
|
+
const asset = requireEditorAsset(params.assetId);
|
|
632
|
+
const { response, filename } = await fetchAssetFile(asset);
|
|
633
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
634
|
+
return {
|
|
635
|
+
assetId: String(asset.get("id")),
|
|
636
|
+
filename,
|
|
637
|
+
mime: response.headers.get("content-type") || "application/octet-stream",
|
|
638
|
+
size: bytes.length,
|
|
639
|
+
base64: base64FromBytes(bytes)
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
async function writeScriptText(params) {
|
|
644
|
+
const asset = requireEditorAsset(params.assetId);
|
|
645
|
+
if (asset.get("type") !== "script") {
|
|
646
|
+
throw new Error("Asset is not a script: " + params.assetId);
|
|
647
|
+
}
|
|
648
|
+
const filename = asset.get("file.filename") || asset.get("name");
|
|
649
|
+
const form = new FormData();
|
|
650
|
+
form.append("filename", filename);
|
|
651
|
+
form.append("file", new Blob([String(params.text || "")], { type: "text/javascript" }), filename);
|
|
652
|
+
const branchId =
|
|
653
|
+
(window.config && window.config.self && window.config.self.branch && window.config.self.branch.id) ||
|
|
654
|
+
(window.config && window.config.branch && window.config.branch.id);
|
|
655
|
+
if (branchId) form.append("branchId", String(branchId));
|
|
656
|
+
const headers = {};
|
|
657
|
+
const accessToken = window.editor.api.globals.accessToken;
|
|
658
|
+
if (accessToken) headers.Authorization = "Bearer " + accessToken;
|
|
659
|
+
const response = await fetch("/api/assets/" + encodeURIComponent(String(asset.get("id"))), {
|
|
660
|
+
method: "PUT",
|
|
661
|
+
headers,
|
|
662
|
+
body: form
|
|
663
|
+
});
|
|
664
|
+
const body = await response.json().catch(() => ({}));
|
|
665
|
+
if (!response.ok || body.error) {
|
|
666
|
+
throw new Error(body.error || "Failed to update script asset.");
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
const parseResult = await new Promise((resolve, reject) => {
|
|
670
|
+
window.editor.call("scripts:parse", asset.observer, (error, data) => {
|
|
671
|
+
if (error) reject(new Error(String(error)));
|
|
672
|
+
else resolve(data || {});
|
|
673
|
+
});
|
|
674
|
+
});
|
|
675
|
+
return {
|
|
676
|
+
assetId: String(asset.get("id")),
|
|
677
|
+
filename,
|
|
678
|
+
parsed: true,
|
|
679
|
+
scripts: parseResult.scripts || {},
|
|
680
|
+
response: body
|
|
681
|
+
};
|
|
682
|
+
}
|
|
683
|
+
|
|
331
684
|
function withTimeout(promise, timeoutMs) {
|
|
332
685
|
return new Promise((resolve, reject) => {
|
|
333
686
|
const timer = setTimeout(() => reject(new Error(`Timed out after ${timeoutMs}ms.`)), timeoutMs);
|
|
@@ -361,6 +714,7 @@
|
|
|
361
714
|
editor: window.editor,
|
|
362
715
|
pc: window.pc,
|
|
363
716
|
pcui: window.pcui,
|
|
717
|
+
runtimeApp: getRuntimeApp(),
|
|
364
718
|
command,
|
|
365
719
|
serialize
|
|
366
720
|
};
|
|
@@ -374,6 +728,7 @@
|
|
|
374
728
|
const editor = ctx.editor;
|
|
375
729
|
const pc = ctx.pc;
|
|
376
730
|
const pcui = ctx.pcui;
|
|
731
|
+
const runtimeApp = ctx.runtimeApp;
|
|
377
732
|
const command = ctx.command;
|
|
378
733
|
const serialize = ctx.serialize;
|
|
379
734
|
return (async () => {
|
|
@@ -382,7 +737,7 @@
|
|
|
382
737
|
`
|
|
383
738
|
);
|
|
384
739
|
|
|
385
|
-
return serialize(await withTimeout(fn(context), timeoutMs));
|
|
740
|
+
return serialize(await withTimeout(fn(context), timeoutMs), params.serializeOptions);
|
|
386
741
|
}
|
|
387
742
|
|
|
388
743
|
function encodeCanvas(canvas, params, source) {
|
|
@@ -629,6 +984,10 @@
|
|
|
629
984
|
if (method === "bridge:clearLogs") return clearLogs();
|
|
630
985
|
if (method === "bridge:uploadAsset") return uploadAsset(params || {});
|
|
631
986
|
if (method === "bridge:focusViewport") return focusViewport(params || {});
|
|
987
|
+
if (method === "bridge:workspaceSnapshot") return workspaceSnapshot();
|
|
988
|
+
if (method === "bridge:readAssetText") return readAssetText(params || {});
|
|
989
|
+
if (method === "bridge:readAssetFile") return readAssetFile(params || {});
|
|
990
|
+
if (method === "bridge:writeScriptText") return writeScriptText(params || {});
|
|
632
991
|
throw new Error(`Unknown bridge method: ${method}`);
|
|
633
992
|
}
|
|
634
993
|
|