playcanvas-agent-bridge-cli 0.6.1 → 0.7.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.
- package/README.md +148 -31
- package/README.zh-CN.md +137 -33
- package/dist/cli.js +514 -95
- package/dist/config.js +23 -1
- package/dist/daemon/server.js +336 -28
- package/dist/daemon/target-registry.js +3 -0
- package/dist/plugins/host.js +260 -0
- package/dist/plugins/store.js +567 -0
- package/dist/processing/manager.js +265 -0
- package/dist/processing/tools.js +97 -0
- package/dist/snippets.js +27 -0
- package/dist/updates.js +475 -0
- package/dist/workspace/json-sync.js +821 -0
- package/dist/workspace/manager.js +1973 -326
- package/extension/icons/icon-128.png +0 -0
- package/extension/icons/icon-16.png +0 -0
- package/extension/icons/icon-256.png +0 -0
- package/extension/icons/icon-32.png +0 -0
- package/extension/icons/icon-48.png +0 -0
- package/extension/isolated.js +59 -2
- package/extension/main.js +770 -275
- package/extension/manifest.json +60 -10
- package/extension/popup.html +115 -3
- package/extension/popup.js +398 -14
- package/extension/service-worker.js +221 -4
- package/package.json +18 -18
- package/plugins/index.json +14 -0
- package/plugins/tinyapp-builder/node/builder.js +589 -0
- package/plugins/tinyapp-builder/node/index.js +30 -0
- package/plugins/tinyapp-builder/package.json +13 -0
- package/plugins/tinyapp-builder/page/editor.js +249 -0
- package/plugins/tinyapp-builder/pcbridge-plugin.json +28 -0
- package/skills/claude/pcbridge-plugin-authoring/SKILL.md +220 -0
- package/skills/claude/playcanvas-agent-bridge-cli/SKILL.md +99 -43
- package/skills/codex/pcbridge-plugin-authoring/SKILL.md +220 -0
- package/skills/codex/pcbridge-plugin-authoring/agents/openai.yaml +4 -0
- package/skills/codex/playcanvas-agent-bridge-cli/SKILL.md +89 -62
- package/skills/cursor/pcbridge-plugin-authoring.mdc +221 -0
- package/skills/cursor/playcanvas-agent-bridge-cli.mdc +69 -33
- package/skills/windsurf/pcbridge-plugin-authoring.md +215 -0
- package/skills/windsurf/playcanvas-agent-bridge-cli.md +68 -33
- package/dist/builder/manager.js +0 -564
package/extension/main.js
CHANGED
|
@@ -9,12 +9,27 @@
|
|
|
9
9
|
const capturedLogs = [];
|
|
10
10
|
const daemonWaiters = new Map();
|
|
11
11
|
let nextLogSeq = 1;
|
|
12
|
-
|
|
13
|
-
let
|
|
14
|
-
let selectedTemplateId = null;
|
|
15
|
-
let selectedTemplateAsset = null;
|
|
12
|
+
const loadedPlugins = new Map();
|
|
13
|
+
let pluginSyncQueue = Promise.resolve();
|
|
16
14
|
let workspaceEventsInstalled = false;
|
|
15
|
+
let sceneEventsInstalled = false;
|
|
16
|
+
let observedAssets = new WeakSet();
|
|
17
|
+
let observedEntities = new WeakSet();
|
|
17
18
|
let workspaceSyncTimer = null;
|
|
19
|
+
let sceneSyncTimer = null;
|
|
20
|
+
let sceneInteractionHoldStartedAt = 0;
|
|
21
|
+
let pointerDownActive = false;
|
|
22
|
+
let workspaceMutationDepth = 0;
|
|
23
|
+
const dirtyAssetIds = new Set();
|
|
24
|
+
const removedAssetIds = new Set();
|
|
25
|
+
const dirtyEntityIds = new Set();
|
|
26
|
+
const removedEntityIds = new Set();
|
|
27
|
+
let sceneSettingsDirty = false;
|
|
28
|
+
let fullSceneDirty = false;
|
|
29
|
+
|
|
30
|
+
const TEXT_ASSET_TYPES = new Set(["script", "text", "json", "html", "css", "shader"]);
|
|
31
|
+
const DATA_JSON_ASSET_TYPES = new Set(["material", "template", "cubemap", "sprite", "animstategraph", "bundle"]);
|
|
32
|
+
const META_SUMMARY_KEYS = ["width", "height", "format", "alpha", "depth", "srgb", "meshes", "vertices", "triangles", "nodes", "materials", "textures", "animations", "duration", "channels", "sampleRate"];
|
|
18
33
|
|
|
19
34
|
function formatLogArg(value) {
|
|
20
35
|
if (typeof value === "string") return value;
|
|
@@ -86,6 +101,15 @@
|
|
|
86
101
|
|
|
87
102
|
patchConsoleCapture();
|
|
88
103
|
|
|
104
|
+
function installPointerTracking() {
|
|
105
|
+
if (typeof window.addEventListener !== "function") return;
|
|
106
|
+
const onDown = () => { pointerDownActive = true; };
|
|
107
|
+
const onUp = () => { pointerDownActive = false; };
|
|
108
|
+
for (const ev of ["pointerdown", "mousedown"]) window.addEventListener(ev, onDown, true);
|
|
109
|
+
for (const ev of ["pointerup", "pointercancel", "mouseup", "blur"]) window.addEventListener(ev, onUp, true);
|
|
110
|
+
}
|
|
111
|
+
installPointerTracking();
|
|
112
|
+
|
|
89
113
|
function safeGetEditorValue(value, path) {
|
|
90
114
|
try {
|
|
91
115
|
return value && typeof value.get === "function" ? value.get(path) : undefined;
|
|
@@ -193,8 +217,11 @@
|
|
|
193
217
|
}
|
|
194
218
|
|
|
195
219
|
function getTargetKind() {
|
|
196
|
-
if (location.hostname === "launch.playcanvas.com") return "launch";
|
|
197
|
-
|
|
220
|
+
if (location.hostname === "launch.playcanvas.com" || location.hostname === "pc-launch.sightp.com") return "launch";
|
|
221
|
+
const selfhost = Boolean(window.config && window.config.selfhost);
|
|
222
|
+
if ((location.hostname === "pc-editor.sightp.com" || selfhost) && /^\/launch\//.test(location.pathname)) return "launch";
|
|
223
|
+
if ((location.hostname === "playcanvas.com" || location.hostname === "pc-editor.sightp.com" || selfhost) &&
|
|
224
|
+
/^\/editor(?:\/|$)/.test(location.pathname)) return "editor";
|
|
198
225
|
return "playcanvas";
|
|
199
226
|
}
|
|
200
227
|
|
|
@@ -395,7 +422,10 @@
|
|
|
395
422
|
const editorMatch = location.href.match(/\/editor\/scene\/([^/?#]+)/);
|
|
396
423
|
if (editorMatch) return editorMatch[1];
|
|
397
424
|
|
|
398
|
-
|
|
425
|
+
const selfhostLaunchMatch = location.pathname.match(/^\/launch\/([^/?#]+)/);
|
|
426
|
+
if (selfhostLaunchMatch) return selfhostLaunchMatch[1];
|
|
427
|
+
|
|
428
|
+
if (location.hostname === "launch.playcanvas.com" || location.hostname === "pc-launch.sightp.com") {
|
|
399
429
|
const launchMatch = location.pathname.match(/^\/([^/?#]+)/);
|
|
400
430
|
if (launchMatch) return launchMatch[1];
|
|
401
431
|
}
|
|
@@ -450,6 +480,9 @@
|
|
|
450
480
|
const ready = kind === "launch" ? launchReady : editorReady;
|
|
451
481
|
return {
|
|
452
482
|
kind,
|
|
483
|
+
platform: config.selfhost ? "selfhost" : "official",
|
|
484
|
+
bridgeProtocolVersion: 1,
|
|
485
|
+
processingProtocolVersion: config.selfhost && kind === "editor" && window.editor?.methods?.has("assets:import:request") ? 1 : null,
|
|
453
486
|
url: location.href,
|
|
454
487
|
title: document.title || "",
|
|
455
488
|
ready,
|
|
@@ -509,17 +542,48 @@
|
|
|
509
542
|
}
|
|
510
543
|
};
|
|
511
544
|
const path = get("path") || [];
|
|
512
|
-
|
|
545
|
+
const type = get("type");
|
|
546
|
+
const result = {
|
|
513
547
|
id: get("id"),
|
|
514
548
|
name: get("name"),
|
|
515
|
-
type
|
|
549
|
+
type,
|
|
516
550
|
source: get("source") || false,
|
|
517
551
|
source_asset_id: get("source_asset_id") || null,
|
|
518
552
|
path,
|
|
519
553
|
folder: Array.isArray(path) && path.length ? path[path.length - 1] : null,
|
|
520
554
|
tags: get("tags") || [],
|
|
555
|
+
preload: get("preload") !== false,
|
|
521
556
|
file: get("file") || null
|
|
522
557
|
};
|
|
558
|
+
if (DATA_JSON_ASSET_TYPES.has(type)) {
|
|
559
|
+
const plain = plainAssetJson(asset);
|
|
560
|
+
const rawData = plain && plain.data !== undefined ? plain.data : get("data");
|
|
561
|
+
result.data = clonePlainJson(rawData) || {};
|
|
562
|
+
}
|
|
563
|
+
const metaSummary = summarizeAssetMeta(get("meta"));
|
|
564
|
+
if (metaSummary) result.meta = metaSummary;
|
|
565
|
+
return result;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
function summarizeAssetMeta(rawMeta) {
|
|
569
|
+
if (!rawMeta || typeof rawMeta !== "object" || Array.isArray(rawMeta)) return null;
|
|
570
|
+
const summary = {};
|
|
571
|
+
for (const key of META_SUMMARY_KEYS) {
|
|
572
|
+
const val = rawMeta[key];
|
|
573
|
+
if (typeof val === "number" || typeof val === "boolean" || typeof val === "string") {
|
|
574
|
+
summary[key] = val;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
return Object.keys(summary).length ? summary : null;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
function clonePlainJson(value) {
|
|
581
|
+
if (value === undefined) return null;
|
|
582
|
+
try {
|
|
583
|
+
return JSON.parse(JSON.stringify(value));
|
|
584
|
+
} catch {
|
|
585
|
+
return null;
|
|
586
|
+
}
|
|
523
587
|
}
|
|
524
588
|
|
|
525
589
|
function splitAssetPath(path) {
|
|
@@ -614,43 +678,334 @@
|
|
|
614
678
|
return { response, filename };
|
|
615
679
|
}
|
|
616
680
|
|
|
617
|
-
function
|
|
681
|
+
function getActiveSceneId() {
|
|
682
|
+
const config = window.config || {};
|
|
683
|
+
const id = (config.scene && config.scene.id ? String(config.scene.id) : "") || getSceneIdFromUrl() || "";
|
|
684
|
+
return id || null;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
function getActiveSceneName() {
|
|
688
|
+
const liveName = window.editor?.api?.globals?.realtime?.scenes?.current?.data?.name;
|
|
689
|
+
if (liveName) return String(liveName);
|
|
690
|
+
const config = window.config || {};
|
|
691
|
+
return (config.scene && config.scene.name ? String(config.scene.name) : "") || null;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
function readEntityPlain(entity) {
|
|
695
|
+
if (!entity) return null;
|
|
696
|
+
if (typeof entity.json === "function") return clonePlainJson(entity.json());
|
|
697
|
+
if (entity.observer && typeof entity.observer.json === "function") return clonePlainJson(entity.observer.json());
|
|
698
|
+
return null;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
function readActiveScene() {
|
|
702
|
+
const globals = window.editor && window.editor.api && window.editor.api.globals;
|
|
703
|
+
const entitiesApi = globals && globals.entities;
|
|
704
|
+
if (!entitiesApi || !entitiesApi.root || typeof entitiesApi.list !== "function") return null;
|
|
705
|
+
const id = getActiveSceneId();
|
|
706
|
+
if (!id) return null;
|
|
707
|
+
const entities = {};
|
|
708
|
+
for (const entity of entitiesApi.list()) {
|
|
709
|
+
const data = readEntityPlain(entity);
|
|
710
|
+
if (data && data.resource_id) {
|
|
711
|
+
entities[String(data.resource_id)] = data;
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
const settingsApi = globals.settings && globals.settings.scene;
|
|
715
|
+
const settings = settingsApi && typeof settingsApi.json === "function"
|
|
716
|
+
? clonePlainJson(settingsApi.json()) || {}
|
|
717
|
+
: {};
|
|
718
|
+
return {
|
|
719
|
+
id,
|
|
720
|
+
name: getActiveSceneName() || "Scene",
|
|
721
|
+
active: true,
|
|
722
|
+
settings,
|
|
723
|
+
entities
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
function listProjectScenesIfSupported() {
|
|
728
|
+
if (!window.editor || typeof window.editor.call !== "function") return Promise.resolve(null);
|
|
729
|
+
if (!window.editor.methods || typeof window.editor.methods.has !== "function" || !window.editor.methods.has("scenes:list")) {
|
|
730
|
+
return Promise.resolve(null);
|
|
731
|
+
}
|
|
732
|
+
return new Promise((resolve) => {
|
|
733
|
+
const timer = setTimeout(() => resolve(null), 5000);
|
|
734
|
+
try {
|
|
735
|
+
window.editor.call(
|
|
736
|
+
"scenes:list",
|
|
737
|
+
(list) => {
|
|
738
|
+
clearTimeout(timer);
|
|
739
|
+
if (!Array.isArray(list)) { resolve(null); return; }
|
|
740
|
+
resolve(list.map((item) => ({
|
|
741
|
+
id: String(item.id),
|
|
742
|
+
uniqueId: item.uniqueId !== undefined ? String(item.uniqueId) : undefined,
|
|
743
|
+
name: String(item.name || "Scene"),
|
|
744
|
+
modified: item.modified ? String(item.modified) : null
|
|
745
|
+
})));
|
|
746
|
+
},
|
|
747
|
+
() => {
|
|
748
|
+
clearTimeout(timer);
|
|
749
|
+
resolve(null);
|
|
750
|
+
}
|
|
751
|
+
);
|
|
752
|
+
} catch {
|
|
753
|
+
clearTimeout(timer);
|
|
754
|
+
resolve(null);
|
|
755
|
+
}
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
async function workspaceSnapshot() {
|
|
618
760
|
if (!window.editor || !window.editor.api || !window.editor.api.globals) {
|
|
619
761
|
throw new Error("Workspace operations require a ready PlayCanvas Editor target.");
|
|
620
762
|
}
|
|
621
|
-
|
|
763
|
+
const result = {
|
|
622
764
|
assets: window.editor.api.globals.assets.list().map(readAsset)
|
|
623
765
|
};
|
|
766
|
+
const activeScene = readActiveScene();
|
|
767
|
+
if (activeScene) result.activeScene = activeScene;
|
|
768
|
+
const scenes = await listProjectScenesIfSupported();
|
|
769
|
+
if (scenes) result.scenes = scenes;
|
|
770
|
+
return result;
|
|
624
771
|
}
|
|
625
772
|
|
|
626
|
-
function
|
|
773
|
+
async function withWorkspaceMutationSuppression(fn) {
|
|
774
|
+
workspaceMutationDepth += 1;
|
|
775
|
+
try {
|
|
776
|
+
return await fn();
|
|
777
|
+
} finally {
|
|
778
|
+
workspaceMutationDepth = Math.max(0, workspaceMutationDepth - 1);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
function emitWorkspaceDelta(delta) {
|
|
783
|
+
window.postMessage({
|
|
784
|
+
channel: CHANNEL,
|
|
785
|
+
side: "main",
|
|
786
|
+
type: "workspace-delta",
|
|
787
|
+
delta
|
|
788
|
+
}, "*");
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
function flushAssetDelta() {
|
|
792
|
+
workspaceSyncTimer = null;
|
|
793
|
+
if (!window.editor?.api?.globals?.assets) return;
|
|
794
|
+
const assetsApi = window.editor.api.globals.assets;
|
|
795
|
+
const upsertedAssets = [];
|
|
796
|
+
for (const id of dirtyAssetIds) {
|
|
797
|
+
if (removedAssetIds.has(id)) continue;
|
|
798
|
+
const asset = assetsApi.get(Number(id));
|
|
799
|
+
if (asset) upsertedAssets.push(readAsset(asset));
|
|
800
|
+
}
|
|
801
|
+
const removed = [...removedAssetIds];
|
|
802
|
+
dirtyAssetIds.clear();
|
|
803
|
+
removedAssetIds.clear();
|
|
804
|
+
if (!upsertedAssets.length && !removed.length) return;
|
|
805
|
+
emitWorkspaceDelta({
|
|
806
|
+
upsertedAssets,
|
|
807
|
+
removedAssetIds: removed
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
function scheduleAssetDelta(assetId, removed = false) {
|
|
812
|
+
if (workspaceMutationDepth > 0) return;
|
|
813
|
+
if (assetId !== undefined && assetId !== null) {
|
|
814
|
+
const id = String(assetId);
|
|
815
|
+
if (removed) {
|
|
816
|
+
dirtyAssetIds.delete(id);
|
|
817
|
+
removedAssetIds.add(id);
|
|
818
|
+
} else {
|
|
819
|
+
removedAssetIds.delete(id);
|
|
820
|
+
dirtyAssetIds.add(id);
|
|
821
|
+
}
|
|
822
|
+
}
|
|
627
823
|
if (workspaceSyncTimer) clearTimeout(workspaceSyncTimer);
|
|
628
|
-
workspaceSyncTimer = setTimeout(
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
824
|
+
workspaceSyncTimer = setTimeout(flushAssetDelta, 300);
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
function flushSceneDelta() {
|
|
828
|
+
sceneSyncTimer = null;
|
|
829
|
+
if (pointerDownActive && Date.now() - sceneInteractionHoldStartedAt < 5000) {
|
|
830
|
+
sceneSyncTimer = setTimeout(flushSceneDelta, 400);
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
sceneInteractionHoldStartedAt = 0;
|
|
834
|
+
const sceneId = getActiveSceneId();
|
|
835
|
+
const globals = window.editor?.api?.globals;
|
|
836
|
+
if (!sceneId || !globals?.entities?.root) {
|
|
837
|
+
dirtyEntityIds.clear();
|
|
838
|
+
removedEntityIds.clear();
|
|
839
|
+
sceneSettingsDirty = false;
|
|
840
|
+
fullSceneDirty = false;
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
if (fullSceneDirty) {
|
|
844
|
+
dirtyEntityIds.clear();
|
|
845
|
+
removedEntityIds.clear();
|
|
846
|
+
sceneSettingsDirty = false;
|
|
847
|
+
fullSceneDirty = false;
|
|
848
|
+
const activeScene = readActiveScene();
|
|
849
|
+
if (activeScene) emitWorkspaceDelta({ activeScene });
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
const upsertedEntities = {};
|
|
853
|
+
for (const id of dirtyEntityIds) {
|
|
854
|
+
if (removedEntityIds.has(id)) continue;
|
|
855
|
+
const entity = globals.entities.get(id);
|
|
856
|
+
const plain = readEntityPlain(entity);
|
|
857
|
+
if (plain && plain.resource_id) upsertedEntities[String(plain.resource_id)] = plain;
|
|
858
|
+
}
|
|
859
|
+
const removed = [...removedEntityIds];
|
|
860
|
+
const includeSettings = sceneSettingsDirty;
|
|
861
|
+
dirtyEntityIds.clear();
|
|
862
|
+
removedEntityIds.clear();
|
|
863
|
+
sceneSettingsDirty = false;
|
|
864
|
+
if (!Object.keys(upsertedEntities).length && !removed.length && !includeSettings) return;
|
|
865
|
+
const sceneDelta = {
|
|
866
|
+
sceneId,
|
|
867
|
+
sceneName: getActiveSceneName() || "Scene",
|
|
868
|
+
upsertedEntities,
|
|
869
|
+
removedEntityIds: removed
|
|
870
|
+
};
|
|
871
|
+
if (includeSettings && globals.settings?.scene?.json) {
|
|
872
|
+
sceneDelta.settings = clonePlainJson(globals.settings.scene.json()) || {};
|
|
873
|
+
}
|
|
874
|
+
emitWorkspaceDelta({ sceneDelta });
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
function scheduleSceneDelta(options = {}) {
|
|
878
|
+
if (workspaceMutationDepth > 0) return;
|
|
879
|
+
if (options.full) fullSceneDirty = true;
|
|
880
|
+
if (options.settings) sceneSettingsDirty = true;
|
|
881
|
+
if (options.entityId) {
|
|
882
|
+
const id = String(options.entityId);
|
|
883
|
+
if (options.removed) {
|
|
884
|
+
dirtyEntityIds.delete(id);
|
|
885
|
+
removedEntityIds.add(id);
|
|
886
|
+
} else {
|
|
887
|
+
removedEntityIds.delete(id);
|
|
888
|
+
dirtyEntityIds.add(id);
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
if (!sceneInteractionHoldStartedAt) sceneInteractionHoldStartedAt = Date.now();
|
|
892
|
+
if (sceneSyncTimer) clearTimeout(sceneSyncTimer);
|
|
893
|
+
sceneSyncTimer = setTimeout(flushSceneDelta, 800);
|
|
632
894
|
}
|
|
633
895
|
|
|
634
896
|
function installWorkspaceEvents() {
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
897
|
+
const globals = window.editor?.api?.globals;
|
|
898
|
+
if (globals?.assets && !workspaceEventsInstalled) {
|
|
899
|
+
const assets = globals.assets;
|
|
900
|
+
workspaceEventsInstalled = true;
|
|
901
|
+
const observeAsset = (asset) => {
|
|
902
|
+
if (!asset || typeof asset.on !== "function" || observedAssets.has(asset)) return;
|
|
903
|
+
observedAssets.add(asset);
|
|
904
|
+
const id = () => safeGetEditorValue(asset, "id");
|
|
905
|
+
const target = asset.observer && typeof asset.observer.on === "function" ? asset.observer : asset;
|
|
906
|
+
try {
|
|
907
|
+
target.on("*:set", (path) => {
|
|
908
|
+
if (!path) return;
|
|
909
|
+
if (path.startsWith("thumbnails") || path === "has_thumbnail" || path === "file.url") return;
|
|
910
|
+
scheduleAssetDelta(id(), false);
|
|
911
|
+
});
|
|
912
|
+
target.on("*:unset", (path) => {
|
|
913
|
+
if (!path || path.startsWith("thumbnails")) return;
|
|
914
|
+
scheduleAssetDelta(id(), false);
|
|
915
|
+
});
|
|
916
|
+
target.on("*:insert", () => scheduleAssetDelta(id(), false));
|
|
917
|
+
target.on("*:remove", () => scheduleAssetDelta(id(), false));
|
|
918
|
+
target.on("*:move", () => scheduleAssetDelta(id(), false));
|
|
919
|
+
} catch {
|
|
920
|
+
for (const event of ["file:hash:set", "file:filename:set", "path:set", "name:set"]) {
|
|
921
|
+
try { asset.on(event, () => scheduleAssetDelta(id(), false)); } catch { /* optional */ }
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
};
|
|
925
|
+
for (const asset of assets.list()) observeAsset(asset);
|
|
926
|
+
try {
|
|
927
|
+
assets.on("add", (asset) => {
|
|
928
|
+
observeAsset(asset);
|
|
929
|
+
scheduleAssetDelta(safeGetEditorValue(asset, "id"), false);
|
|
930
|
+
});
|
|
931
|
+
} catch { /* optional */ }
|
|
932
|
+
try {
|
|
933
|
+
assets.on("remove", (asset) => {
|
|
934
|
+
scheduleAssetDelta(safeGetEditorValue(asset, "id"), true);
|
|
935
|
+
});
|
|
936
|
+
} catch { /* optional */ }
|
|
937
|
+
try {
|
|
938
|
+
assets.on("move", (asset) => {
|
|
939
|
+
scheduleAssetDelta(safeGetEditorValue(asset, "id"), false);
|
|
940
|
+
});
|
|
941
|
+
} catch { /* optional */ }
|
|
942
|
+
try {
|
|
943
|
+
assets.on("load:all", () => {
|
|
944
|
+
for (const asset of assets.list()) observeAsset(asset);
|
|
945
|
+
if (workspaceMutationDepth === 0) {
|
|
946
|
+
void daemonRequest("/workspace/sync", "POST", {}, 60000).catch(() => undefined);
|
|
947
|
+
}
|
|
948
|
+
});
|
|
949
|
+
} catch { /* optional */ }
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
if (globals?.entities && typeof globals.entities.list === "function" && !sceneEventsInstalled) {
|
|
953
|
+
sceneEventsInstalled = true;
|
|
954
|
+
const entities = globals.entities;
|
|
955
|
+
const observeEntity = (entity) => {
|
|
956
|
+
if (!entity || observedEntities.has(entity)) return;
|
|
957
|
+
const target = entity.observer && typeof entity.observer.on === "function"
|
|
958
|
+
? entity.observer
|
|
959
|
+
: typeof entity.on === "function" ? entity : null;
|
|
960
|
+
if (!target) return;
|
|
961
|
+
observedEntities.add(entity);
|
|
962
|
+
const id = () => safeGetEditorValue(entity, "resource_id");
|
|
963
|
+
for (const ev of ["*:set", "*:unset", "*:insert", "*:remove", "*:move"]) {
|
|
964
|
+
try { target.on(ev, () => scheduleSceneDelta({ entityId: id() })); } catch { /* optional */ }
|
|
965
|
+
}
|
|
966
|
+
};
|
|
967
|
+
for (const entity of entities.list()) observeEntity(entity);
|
|
968
|
+
if (typeof entities.on === "function") {
|
|
969
|
+
try {
|
|
970
|
+
entities.on("add", (entity) => {
|
|
971
|
+
observeEntity(entity);
|
|
972
|
+
scheduleSceneDelta({ entityId: safeGetEditorValue(entity, "resource_id") });
|
|
973
|
+
});
|
|
974
|
+
} catch { /* optional */ }
|
|
975
|
+
try {
|
|
976
|
+
entities.on("remove", (entity) => {
|
|
977
|
+
scheduleSceneDelta({ entityId: safeGetEditorValue(entity, "resource_id"), removed: true });
|
|
978
|
+
});
|
|
979
|
+
} catch { /* optional */ }
|
|
980
|
+
}
|
|
981
|
+
const sceneSettings = globals.settings?.scene;
|
|
982
|
+
const settingsTarget = sceneSettings?.observer && typeof sceneSettings.observer.on === "function"
|
|
983
|
+
? sceneSettings.observer
|
|
984
|
+
: typeof sceneSettings?.on === "function" ? sceneSettings : null;
|
|
985
|
+
if (settingsTarget) {
|
|
986
|
+
for (const ev of ["*:set", "*:unset", "*:insert", "*:remove", "*:move"]) {
|
|
987
|
+
try { settingsTarget.on(ev, () => scheduleSceneDelta({ settings: true })); } catch { /* optional */ }
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
if (window.editor && typeof window.editor.on === "function") {
|
|
991
|
+
try {
|
|
992
|
+
window.editor.on("scene:load", () => {
|
|
993
|
+
for (const entity of entities.list()) observeEntity(entity);
|
|
994
|
+
scheduleSceneDelta({ full: true });
|
|
995
|
+
});
|
|
996
|
+
window.editor.on("scene:name", () => {
|
|
997
|
+
scheduleSceneDelta({ settings: true });
|
|
998
|
+
});
|
|
999
|
+
} catch { /* optional */ }
|
|
642
1000
|
}
|
|
643
|
-
};
|
|
644
|
-
for (const asset of assets.list()) observe(asset);
|
|
645
|
-
for (const event of ["add", "remove", "move", "load:all"]) {
|
|
646
|
-
try { assets.on(event, (asset) => { observe(asset); scheduleWorkspaceSync(); }); } catch { /* optional assets event */ }
|
|
647
1001
|
}
|
|
648
1002
|
}
|
|
649
1003
|
|
|
650
1004
|
async function readAssetText(params) {
|
|
651
1005
|
const asset = requireEditorAsset(params.assetId);
|
|
652
|
-
|
|
653
|
-
|
|
1006
|
+
const type = String(asset.get("type") || "");
|
|
1007
|
+
if (!TEXT_ASSET_TYPES.has(type)) {
|
|
1008
|
+
throw new Error("Asset is not a text asset: " + params.assetId);
|
|
654
1009
|
}
|
|
655
1010
|
const { response, filename } = await fetchAssetFile(asset);
|
|
656
1011
|
return {
|
|
@@ -711,7 +1066,7 @@
|
|
|
711
1066
|
};
|
|
712
1067
|
}
|
|
713
1068
|
|
|
714
|
-
async function
|
|
1069
|
+
async function writeAssetFileInner(params) {
|
|
715
1070
|
const asset = requireEditorAsset(params.assetId);
|
|
716
1071
|
if (asset.get("type") === "script") {
|
|
717
1072
|
throw new Error("Use the script text writer for script assets: " + params.assetId);
|
|
@@ -751,7 +1106,11 @@
|
|
|
751
1106
|
};
|
|
752
1107
|
}
|
|
753
1108
|
|
|
754
|
-
|
|
1109
|
+
function writeAssetFile(params) {
|
|
1110
|
+
return withWorkspaceMutationSuppression(() => writeAssetFileInner(params));
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
async function renameAssetInner(params) {
|
|
755
1114
|
const asset = requireEditorAsset(params.assetId);
|
|
756
1115
|
const assets = window.editor.api.globals.assets;
|
|
757
1116
|
const folderResult = await resolveFolder(assets, params);
|
|
@@ -775,15 +1134,31 @@
|
|
|
775
1134
|
return { asset: readAsset(asset), createdFolders: folderResult.created.map(readAsset) };
|
|
776
1135
|
}
|
|
777
1136
|
|
|
778
|
-
|
|
1137
|
+
function renameAsset(params) {
|
|
1138
|
+
return withWorkspaceMutationSuppression(() => renameAssetInner(params));
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
function textMimeForAssetType(type) {
|
|
1142
|
+
if (type === "script") return "text/javascript";
|
|
1143
|
+
if (type === "css") return "text/css";
|
|
1144
|
+
if (type === "html") return "text/html";
|
|
1145
|
+
if (type === "json") return "application/json";
|
|
1146
|
+
return "text/plain";
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
async function writeAssetTextInner(params, requireScript = false) {
|
|
779
1150
|
const asset = requireEditorAsset(params.assetId);
|
|
780
|
-
|
|
1151
|
+
const type = String(asset.get("type") || "");
|
|
1152
|
+
if (requireScript && type !== "script") {
|
|
781
1153
|
throw new Error("Asset is not a script: " + params.assetId);
|
|
782
1154
|
}
|
|
783
|
-
|
|
1155
|
+
if (!TEXT_ASSET_TYPES.has(type)) {
|
|
1156
|
+
throw new Error("Asset is not a text asset: " + params.assetId);
|
|
1157
|
+
}
|
|
1158
|
+
const filename = String(params.filename || asset.get("file.filename") || asset.get("name") || "asset.txt");
|
|
784
1159
|
const form = new FormData();
|
|
785
1160
|
form.append("filename", filename);
|
|
786
|
-
form.append("file", new Blob([String(params.text || "")], { type:
|
|
1161
|
+
form.append("file", new Blob([String(params.text || "")], { type: textMimeForAssetType(type) }), filename);
|
|
787
1162
|
const branchId =
|
|
788
1163
|
(window.config && window.config.self && window.config.self.branch && window.config.self.branch.id) ||
|
|
789
1164
|
(window.config && window.config.branch && window.config.branch.id);
|
|
@@ -801,118 +1176,288 @@
|
|
|
801
1176
|
throw new Error(body.error || "Failed to update script asset.");
|
|
802
1177
|
}
|
|
803
1178
|
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
1179
|
+
let scripts = {};
|
|
1180
|
+
let parsed = false;
|
|
1181
|
+
if (type === "script" && window.editor && typeof window.editor.call === "function") {
|
|
1182
|
+
const parseResult = await new Promise((resolve, reject) => {
|
|
1183
|
+
window.editor.call("scripts:parse", asset.observer, (error, data) => {
|
|
1184
|
+
if (error) reject(new Error(String(error)));
|
|
1185
|
+
else resolve(data || {});
|
|
1186
|
+
});
|
|
808
1187
|
});
|
|
809
|
-
|
|
1188
|
+
scripts = parseResult.scripts || {};
|
|
1189
|
+
parsed = true;
|
|
1190
|
+
}
|
|
810
1191
|
return {
|
|
811
1192
|
assetId: String(asset.get("id")),
|
|
812
1193
|
filename,
|
|
813
|
-
parsed
|
|
814
|
-
scripts
|
|
1194
|
+
parsed,
|
|
1195
|
+
scripts,
|
|
815
1196
|
response: body
|
|
816
1197
|
};
|
|
817
1198
|
}
|
|
818
1199
|
|
|
819
|
-
function
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
1200
|
+
function writeScriptText(params) {
|
|
1201
|
+
return withWorkspaceMutationSuppression(() => writeAssetTextInner(params, true));
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
function writeAssetText(params) {
|
|
1205
|
+
return withWorkspaceMutationSuppression(() => writeAssetTextInner(params, false));
|
|
824
1206
|
}
|
|
825
1207
|
|
|
826
|
-
function
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
1208
|
+
function createDataAsset(params) {
|
|
1209
|
+
return withWorkspaceMutationSuppression(async () => {
|
|
1210
|
+
if (!window.editor || !window.editor.api || !window.editor.api.globals) {
|
|
1211
|
+
throw new Error("Workspace operations require a ready PlayCanvas Editor target.");
|
|
1212
|
+
}
|
|
1213
|
+
const type = String(params.type || "");
|
|
1214
|
+
if (!DATA_JSON_ASSET_TYPES.has(type)) {
|
|
1215
|
+
throw new Error("Unsupported structured JSON asset type: " + type);
|
|
1216
|
+
}
|
|
1217
|
+
const name = String(params.name || "New Asset");
|
|
1218
|
+
const assets = window.editor.api.globals.assets;
|
|
1219
|
+
const folderResult = await resolveFolder(assets, params);
|
|
1220
|
+
const defaultData = window.editor.api.globals.schema?.assets?.getDefaultData?.(type) || {};
|
|
1221
|
+
const incomingData = params.data && typeof params.data === "object" && !Array.isArray(params.data)
|
|
1222
|
+
? params.data
|
|
1223
|
+
: {};
|
|
1224
|
+
const mergedData = { ...defaultData, ...incomingData };
|
|
1225
|
+
const asset = await assets.upload(
|
|
1226
|
+
{
|
|
1227
|
+
name,
|
|
1228
|
+
type,
|
|
1229
|
+
folder: folderResult.folder || undefined,
|
|
1230
|
+
preload: params.preload !== false,
|
|
1231
|
+
tags: Array.isArray(params.tags) ? params.tags : [],
|
|
1232
|
+
data: mergedData
|
|
1233
|
+
},
|
|
1234
|
+
null
|
|
1235
|
+
);
|
|
1236
|
+
if (asset && typeof asset.set === "function") {
|
|
1237
|
+
if (Array.isArray(params.tags)) asset.set("tags", params.tags);
|
|
1238
|
+
if (typeof params.preload === "boolean") asset.set("preload", params.preload);
|
|
1239
|
+
for (const [key, value] of Object.entries(incomingData)) {
|
|
1240
|
+
asset.set("data." + key, value);
|
|
836
1241
|
}
|
|
837
|
-
} else if (child && typeof child === "object") {
|
|
838
|
-
collectAssetReferences(assets, child, ids, depth + 1);
|
|
839
1242
|
}
|
|
840
|
-
|
|
1243
|
+
return {
|
|
1244
|
+
asset: readAsset(asset),
|
|
1245
|
+
createdFolders: folderResult.created.map(readAsset)
|
|
1246
|
+
};
|
|
1247
|
+
});
|
|
841
1248
|
}
|
|
842
1249
|
|
|
843
|
-
function
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
const
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
1250
|
+
function patchAssetData(params) {
|
|
1251
|
+
return withWorkspaceMutationSuppression(async () => {
|
|
1252
|
+
const asset = requireEditorAsset(params.assetId);
|
|
1253
|
+
const type = String(asset.get("type") || "");
|
|
1254
|
+
if (!DATA_JSON_ASSET_TYPES.has(type)) {
|
|
1255
|
+
throw new Error("Asset is not a structured JSON asset: " + params.assetId);
|
|
1256
|
+
}
|
|
1257
|
+
if (typeof params.name === "string" && params.name && params.name !== asset.get("name")) {
|
|
1258
|
+
await new Promise((resolve, reject) => {
|
|
1259
|
+
const error = window.editor.call("assets:rename", asset.observer, params.name, (callbackError) => {
|
|
1260
|
+
if (callbackError) reject(new Error(String(callbackError)));
|
|
1261
|
+
else resolve();
|
|
1262
|
+
});
|
|
1263
|
+
if (error) reject(new Error(String(error)));
|
|
1264
|
+
});
|
|
1265
|
+
}
|
|
1266
|
+
if (Object.prototype.hasOwnProperty.call(params, "folder")) {
|
|
1267
|
+
const assets = window.editor.api.globals.assets;
|
|
1268
|
+
const folderResult = await resolveFolder(assets, params);
|
|
1269
|
+
if (typeof window.editor.call === "function") {
|
|
1270
|
+
const error = window.editor.call(
|
|
1271
|
+
"assets:fs:move",
|
|
1272
|
+
[asset.observer],
|
|
1273
|
+
folderResult.folder ? folderResult.folder.observer : null
|
|
1274
|
+
);
|
|
1275
|
+
if (error) throw new Error(String(error));
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
if (typeof params.preload === "boolean") asset.set("preload", params.preload);
|
|
1279
|
+
if (Array.isArray(params.tags)) asset.set("tags", params.tags);
|
|
1280
|
+
for (const path of Array.isArray(params.unsets) ? params.unsets : []) {
|
|
1281
|
+
if (typeof asset.unset === "function") asset.unset("data." + path);
|
|
1282
|
+
else if (asset.observer && typeof asset.observer.unset === "function") asset.observer.unset("data." + path);
|
|
1283
|
+
}
|
|
1284
|
+
for (const change of Array.isArray(params.sets) ? params.sets : []) {
|
|
1285
|
+
if (change && typeof change.path === "string") {
|
|
1286
|
+
asset.set("data." + change.path, change.value);
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
return { asset: readAsset(asset) };
|
|
1290
|
+
});
|
|
851
1291
|
}
|
|
852
1292
|
|
|
853
|
-
function
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
entity.components.script.scripts &&
|
|
858
|
-
entity.components.script.scripts.sdsTinyRootHandler &&
|
|
859
|
-
entity.components.script.scripts.sdsTinyRootHandler.attributes;
|
|
860
|
-
if (attributes && attributes.type === "testTemplate" && Number.isInteger(attributes.testTemplate)) {
|
|
861
|
-
ids.add(String(attributes.testTemplate));
|
|
1293
|
+
function deleteAssets(params) {
|
|
1294
|
+
return withWorkspaceMutationSuppression(async () => {
|
|
1295
|
+
if (!window.editor || !window.editor.api || !window.editor.api.globals) {
|
|
1296
|
+
throw new Error("Workspace operations require a ready PlayCanvas Editor target.");
|
|
862
1297
|
}
|
|
863
|
-
|
|
864
|
-
|
|
1298
|
+
const assets = window.editor.api.globals.assets;
|
|
1299
|
+
const ids = Array.isArray(params.assetIds) ? params.assetIds : [params.assetId];
|
|
1300
|
+
const found = [];
|
|
1301
|
+
const deletedIds = [];
|
|
1302
|
+
for (const rawId of ids) {
|
|
1303
|
+
if (rawId === undefined || rawId === null) continue;
|
|
1304
|
+
const asset = assets.get(Number(rawId));
|
|
1305
|
+
if (asset) {
|
|
1306
|
+
found.push(asset);
|
|
1307
|
+
deletedIds.push(String(asset.get("id")));
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
if (!found.length) return { deletedIds: [] };
|
|
1311
|
+
if (typeof assets.delete === "function") {
|
|
1312
|
+
await assets.delete(found);
|
|
1313
|
+
} else if (typeof window.editor.call === "function") {
|
|
1314
|
+
window.editor.call("assets:delete", found.map((item) => item.observer || item));
|
|
1315
|
+
} else {
|
|
1316
|
+
throw new Error("Editor assets delete API is unavailable.");
|
|
1317
|
+
}
|
|
1318
|
+
return { deletedIds };
|
|
1319
|
+
});
|
|
865
1320
|
}
|
|
866
1321
|
|
|
867
|
-
function
|
|
1322
|
+
async function readScene(params) {
|
|
868
1323
|
if (!window.editor || !window.editor.api || !window.editor.api.globals) {
|
|
869
|
-
throw new Error("
|
|
870
|
-
}
|
|
871
|
-
const
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
const
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
assets.list().find((asset) =>
|
|
890
|
-
asset.get("type") === "script" &&
|
|
891
|
-
(asset.get("name") === name || asset.get("data.scripts." + name) !== undefined)
|
|
892
|
-
);
|
|
893
|
-
if (!scriptAsset) continue;
|
|
894
|
-
const scriptJson = plainAssetJson(scriptAsset);
|
|
895
|
-
const filename = scriptJson && scriptJson.file && scriptJson.file.filename;
|
|
896
|
-
if (
|
|
897
|
-
name.startsWith("sds") ||
|
|
898
|
-
String(filename || (scriptJson && scriptJson.name) || "").startsWith("sds")
|
|
899
|
-
) continue;
|
|
900
|
-
ids.add(String(scriptAsset.get("id")));
|
|
901
|
-
scripts.push({ name, assetId: String(scriptAsset.get("id")) });
|
|
1324
|
+
throw new Error("Scene operations require a ready PlayCanvas Editor target.");
|
|
1325
|
+
}
|
|
1326
|
+
const requestedId = String(params.sceneId || getActiveSceneId() || "");
|
|
1327
|
+
if (!requestedId) throw new Error("sceneId is required.");
|
|
1328
|
+
const activeId = getActiveSceneId();
|
|
1329
|
+
if (activeId && requestedId === activeId) {
|
|
1330
|
+
const activeScene = readActiveScene();
|
|
1331
|
+
if (activeScene) return activeScene;
|
|
1332
|
+
}
|
|
1333
|
+
if (typeof window.editor.call !== "function") {
|
|
1334
|
+
throw new Error("Editor scenes:get method is unavailable.");
|
|
1335
|
+
}
|
|
1336
|
+
const data = await new Promise((resolve, reject) => {
|
|
1337
|
+
window.editor.call("scenes:get", requestedId, (error, sceneData) => {
|
|
1338
|
+
if (error) reject(new Error(typeof error === "string" ? error : JSON.stringify(error)));
|
|
1339
|
+
else resolve(sceneData);
|
|
1340
|
+
});
|
|
1341
|
+
});
|
|
1342
|
+
if (!data || typeof data !== "object") {
|
|
1343
|
+
throw new Error("Scene not found: " + requestedId);
|
|
902
1344
|
}
|
|
903
|
-
|
|
904
1345
|
return {
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
: null,
|
|
912
|
-
sceneUrl: location.href
|
|
1346
|
+
id: String(data.id || requestedId),
|
|
1347
|
+
name: String(data.name || "Scene"),
|
|
1348
|
+
active: false,
|
|
1349
|
+
modified: data.modified ? String(data.modified) : null,
|
|
1350
|
+
settings: clonePlainJson(data.settings) || {},
|
|
1351
|
+
entities: clonePlainJson(data.entities) || {}
|
|
913
1352
|
};
|
|
914
1353
|
}
|
|
915
1354
|
|
|
1355
|
+
function patchScene(params) {
|
|
1356
|
+
return withWorkspaceMutationSuppression(async () => {
|
|
1357
|
+
const globals = window.editor && window.editor.api && window.editor.api.globals;
|
|
1358
|
+
if (!globals || !globals.entities || !globals.entities.root) {
|
|
1359
|
+
throw new Error("Active scene is not loaded in the PlayCanvas Editor.");
|
|
1360
|
+
}
|
|
1361
|
+
const activeId = getActiveSceneId();
|
|
1362
|
+
if (params.sceneId && activeId && String(params.sceneId) !== activeId) {
|
|
1363
|
+
throw new Error(`Scene ${params.sceneId} is not active in the Editor (active: ${activeId}).`);
|
|
1364
|
+
}
|
|
1365
|
+
if (typeof params.name === "string" && params.name.trim()) {
|
|
1366
|
+
const nextName = params.name.trim();
|
|
1367
|
+
const currentRealtimeScene = globals.realtime && globals.realtime.scenes && globals.realtime.scenes.current;
|
|
1368
|
+
if (currentRealtimeScene && currentRealtimeScene.data && typeof window.editor.call === "function") {
|
|
1369
|
+
const oldName = currentRealtimeScene.data.name || "";
|
|
1370
|
+
if (oldName !== nextName) {
|
|
1371
|
+
window.editor.call("realtime:scene:op", { p: ["name"], od: oldName, oi: nextName });
|
|
1372
|
+
if (typeof window.editor.emit === "function") window.editor.emit("scene:name", nextName);
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
if (window.config && window.config.scene) window.config.scene.name = nextName;
|
|
1376
|
+
}
|
|
1377
|
+
const entities = globals.entities;
|
|
1378
|
+
|
|
1379
|
+
for (const item of Array.isArray(params.creates) ? params.creates : []) {
|
|
1380
|
+
if (!item || !item.entity) continue;
|
|
1381
|
+
const data = { ...item.entity, children: [] };
|
|
1382
|
+
if (typeof data.parent === "string") {
|
|
1383
|
+
const parent = entities.get(data.parent);
|
|
1384
|
+
if (!parent) throw new Error("Parent entity not found: " + data.parent);
|
|
1385
|
+
data.parent = parent;
|
|
1386
|
+
} else if (!data.parent) {
|
|
1387
|
+
data.parent = entities.root;
|
|
1388
|
+
}
|
|
1389
|
+
const options = { history: true, select: false };
|
|
1390
|
+
if (Number.isInteger(item.index) && item.index >= 0) options.index = item.index;
|
|
1391
|
+
const created = entities.create(data, options);
|
|
1392
|
+
if (!created) throw new Error("Failed to create entity " + (data.resource_id || data.name || ""));
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
for (const move of Array.isArray(params.reparents) ? params.reparents : []) {
|
|
1396
|
+
const entity = entities.get(move.id);
|
|
1397
|
+
const parent = entities.get(move.parent);
|
|
1398
|
+
if (!entity || !parent) continue;
|
|
1399
|
+
if (typeof entity.reparent === "function") {
|
|
1400
|
+
entity.reparent(parent, move.index, { history: true, preserveTransform: false });
|
|
1401
|
+
} else if (typeof entities.reparent === "function") {
|
|
1402
|
+
await entities.reparent([{ entity, parent, index: move.index }], {
|
|
1403
|
+
history: true,
|
|
1404
|
+
preserveTransform: false
|
|
1405
|
+
});
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
for (const update of Array.isArray(params.updates) ? params.updates : []) {
|
|
1410
|
+
const entity = entities.get(update.id);
|
|
1411
|
+
if (!entity) continue;
|
|
1412
|
+
for (const comp of Array.isArray(update.removedComponents) ? update.removedComponents : []) {
|
|
1413
|
+
if (entity.get("components." + comp)) entity.removeComponent(comp);
|
|
1414
|
+
}
|
|
1415
|
+
if (update.addedComponents && typeof update.addedComponents === "object") {
|
|
1416
|
+
for (const [comp, compData] of Object.entries(update.addedComponents)) {
|
|
1417
|
+
if (entity.get("components." + comp)) entity.set("components." + comp, compData || {});
|
|
1418
|
+
else entity.addComponent(comp, compData || {});
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
for (const path of Array.isArray(update.unsets) ? update.unsets : []) {
|
|
1422
|
+
if (typeof entity.unset === "function") entity.unset(path);
|
|
1423
|
+
else if (entity.observer && typeof entity.observer.unset === "function") entity.observer.unset(path);
|
|
1424
|
+
}
|
|
1425
|
+
for (const change of Array.isArray(update.sets) ? update.sets : []) {
|
|
1426
|
+
if (change && typeof change.path === "string") entity.set(change.path, change.value);
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1430
|
+
const deleteIds = Array.isArray(params.deletes) ? params.deletes : [];
|
|
1431
|
+
if (deleteIds.length) {
|
|
1432
|
+
const toDelete = deleteIds.map((id) => entities.get(id)).filter(Boolean);
|
|
1433
|
+
if (toDelete.length) {
|
|
1434
|
+
if (typeof entities.delete === "function") await entities.delete(toDelete);
|
|
1435
|
+
else for (const entity of toDelete) entities.remove(entity);
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
|
|
1439
|
+
const sceneSettings = globals.settings && globals.settings.scene;
|
|
1440
|
+
if (sceneSettings) {
|
|
1441
|
+
for (const path of Array.isArray(params.settingsUnsets) ? params.settingsUnsets : []) {
|
|
1442
|
+
if (typeof sceneSettings.unset === "function") sceneSettings.unset(path);
|
|
1443
|
+
else if (sceneSettings.observer && typeof sceneSettings.observer.unset === "function") sceneSettings.observer.unset(path);
|
|
1444
|
+
}
|
|
1445
|
+
for (const change of Array.isArray(params.settingsSets) ? params.settingsSets : []) {
|
|
1446
|
+
if (change && typeof change.path === "string") sceneSettings.set(change.path, change.value);
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
return { scene: readActiveScene() };
|
|
1451
|
+
});
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
function plainAssetJson(asset) {
|
|
1455
|
+
if (!asset) return null;
|
|
1456
|
+
if (typeof asset.json === "function") return asset.json();
|
|
1457
|
+
if (asset.observer && typeof asset.observer.json === "function") return asset.observer.json();
|
|
1458
|
+
return null;
|
|
1459
|
+
}
|
|
1460
|
+
|
|
916
1461
|
function withTimeout(promise, timeoutMs) {
|
|
917
1462
|
return new Promise((resolve, reject) => {
|
|
918
1463
|
const timer = setTimeout(() => reject(new Error(`Timed out after ${timeoutMs}ms.`)), timeoutMs);
|
|
@@ -949,165 +1494,83 @@
|
|
|
949
1494
|
});
|
|
950
1495
|
}
|
|
951
1496
|
|
|
952
|
-
function
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
(Array.isArray(selection.items) && selection.items) ||
|
|
964
|
-
(typeof selection.list === "function" && selection.list()) ||
|
|
965
|
-
(typeof selection.get === "function" && selection.get())
|
|
966
|
-
);
|
|
967
|
-
return Array.isArray(items)
|
|
968
|
-
? { known: true, asset: items[0] || null }
|
|
969
|
-
: { known: false, asset: null };
|
|
1497
|
+
async function disposePlugin(id) {
|
|
1498
|
+
const state = loadedPlugins.get(id);
|
|
1499
|
+
if (!state) return;
|
|
1500
|
+
loadedPlugins.delete(id);
|
|
1501
|
+
for (const dispose of state.disposers.reverse()) {
|
|
1502
|
+
try {
|
|
1503
|
+
await dispose();
|
|
1504
|
+
} catch (error) {
|
|
1505
|
+
console.warn(`[pcbridge] plugin ${id} dispose failed`, error);
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
970
1508
|
}
|
|
971
1509
|
|
|
972
|
-
function
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
1510
|
+
async function loadPlugin(spec) {
|
|
1511
|
+
const state = { code: spec.code, handlers: new Map(), disposers: [] };
|
|
1512
|
+
loadedPlugins.set(spec.id, state);
|
|
1513
|
+
const plugin = {
|
|
1514
|
+
id: spec.id,
|
|
1515
|
+
version: spec.version,
|
|
1516
|
+
kind: getTargetKind(),
|
|
1517
|
+
handle(name, handler) { state.handlers.set(String(name), handler); },
|
|
1518
|
+
onDispose(dispose) { state.disposers.push(dispose); },
|
|
1519
|
+
call(name, params, timeoutMs) {
|
|
1520
|
+
return daemonRequest(
|
|
1521
|
+
`/plugins/${spec.id}/actions/${encodeURIComponent(name)}`,
|
|
1522
|
+
"POST",
|
|
1523
|
+
{ params: params || {} },
|
|
1524
|
+
timeoutMs
|
|
1525
|
+
);
|
|
1526
|
+
}
|
|
1527
|
+
};
|
|
1528
|
+
const run = new window.Function(
|
|
1529
|
+
"plugin",
|
|
1530
|
+
"editor",
|
|
1531
|
+
"pc",
|
|
1532
|
+
"pcui",
|
|
1533
|
+
"runtimeApp",
|
|
1534
|
+
`return (async () => {\n${spec.code}\n})();\n//# sourceURL=pcbridge-plugin/${spec.id}/${plugin.kind}.js`
|
|
1535
|
+
);
|
|
1536
|
+
await withTimeout(run(plugin, window.editor, window.pc, window.pcui, getRuntimeApp()), 10000);
|
|
977
1537
|
}
|
|
978
1538
|
|
|
979
|
-
function
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
1539
|
+
async function applyPluginSync(params) {
|
|
1540
|
+
const wanted = new Map(params.plugins.map((spec) => [spec.id, spec]));
|
|
1541
|
+
for (const [id, state] of [...loadedPlugins]) {
|
|
1542
|
+
const spec = wanted.get(id);
|
|
1543
|
+
if (!spec || spec.code !== state.code) await disposePlugin(id);
|
|
983
1544
|
}
|
|
984
|
-
|
|
985
|
-
|
|
1545
|
+
const plugins = [];
|
|
1546
|
+
for (const spec of wanted.values()) {
|
|
1547
|
+
if (!loadedPlugins.has(spec.id)) {
|
|
1548
|
+
try {
|
|
1549
|
+
await loadPlugin(spec);
|
|
1550
|
+
} catch (error) {
|
|
1551
|
+
await disposePlugin(spec.id);
|
|
1552
|
+
plugins.push({ id: spec.id, ok: false, error: String((error && error.message) || error) });
|
|
1553
|
+
continue;
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
plugins.push({ id: spec.id, ok: true });
|
|
1557
|
+
}
|
|
1558
|
+
return { plugins };
|
|
986
1559
|
}
|
|
987
1560
|
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
row.append(control);
|
|
994
|
-
container.append(row);
|
|
1561
|
+
// A sync must finish before the next starts, or a plugin still activating could outlive its disposal.
|
|
1562
|
+
function syncPlugins(params) {
|
|
1563
|
+
const run = () => applyPluginSync(params);
|
|
1564
|
+
pluginSyncQueue = pluginSyncQueue.then(run, run);
|
|
1565
|
+
return pluginSyncQueue;
|
|
995
1566
|
}
|
|
996
1567
|
|
|
997
|
-
function
|
|
998
|
-
const
|
|
999
|
-
const
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
}
|
|
1004
|
-
const pcui = window.pcui;
|
|
1005
|
-
const layout = window.editor && window.editor.call && window.editor.call("layout.attributes");
|
|
1006
|
-
if (!pcui || !pcui.Container || !pcui.Button || !pcui.Label || !pcui.TextInput || !layout) return;
|
|
1007
|
-
if (builderPanel && selectedTemplateId === id && builderPanelIsAttached(layout)) return;
|
|
1008
|
-
destroyBuilderPanel();
|
|
1009
|
-
|
|
1010
|
-
const projectId = window.config && window.config.project && window.config.project.id
|
|
1011
|
-
? String(window.config.project.id)
|
|
1012
|
-
: "project";
|
|
1013
|
-
const suffixKey = `pcbridge:builder:${id}:suffix`;
|
|
1014
|
-
const prefixKey = `pcbridge:builder:${projectId}:prefix`;
|
|
1015
|
-
const container = new pcui.Container();
|
|
1016
|
-
container.flex = true;
|
|
1017
|
-
container.flexDirection = "column";
|
|
1018
|
-
if (container.class && typeof container.class.add === "function") {
|
|
1019
|
-
container.class.add("pcbridge-tiny-builder");
|
|
1020
|
-
}
|
|
1021
|
-
container.append(new pcui.Label({ text: "pcbridge Tiny Builder" }));
|
|
1022
|
-
const prefixInput = new pcui.TextInput({ value: localStorage.getItem(prefixKey) || "" });
|
|
1023
|
-
const suffixInput = new pcui.TextInput({ value: localStorage.getItem(suffixKey) || "" });
|
|
1024
|
-
if (typeof prefixInput.on === "function") {
|
|
1025
|
-
prefixInput.on("change", () => localStorage.setItem(prefixKey, String(prefixInput.value || "")));
|
|
1026
|
-
suffixInput.on("change", () => localStorage.setItem(suffixKey, String(suffixInput.value || "")));
|
|
1027
|
-
}
|
|
1028
|
-
appendRow(pcui, container, "上传目录", prefixInput);
|
|
1029
|
-
appendRow(pcui, container, "文件后缀", suffixInput);
|
|
1030
|
-
const pathLabel = new pcui.Label({ text: localStorage.getItem(`pcbridge:builder:${id}:url`) || "" });
|
|
1031
|
-
appendRow(pcui, container, "引用路径", pathLabel);
|
|
1032
|
-
const buildButton = new pcui.Button({ text: "构建并上传到 S3" });
|
|
1033
|
-
const copyButton = new pcui.Button({ text: "复制引用路径" });
|
|
1034
|
-
const logLabel = new pcui.Label({ text: "从项目 .env 或工作区 .env 读取 S3 配置" });
|
|
1035
|
-
copyButton.on("click", () => {
|
|
1036
|
-
if (pathLabel.text) void navigator.clipboard.writeText(pathLabel.text);
|
|
1037
|
-
});
|
|
1038
|
-
buildButton.on("click", async () => {
|
|
1039
|
-
buildButton.enabled = false;
|
|
1040
|
-
try {
|
|
1041
|
-
let job = await daemonRequest("/builder/jobs", "POST", {
|
|
1042
|
-
templateAssetId: id,
|
|
1043
|
-
suffix: String(suffixInput.value || ""),
|
|
1044
|
-
prefix: String(prefixInput.value || "")
|
|
1045
|
-
});
|
|
1046
|
-
while (job && job.state !== "completed" && job.state !== "error") {
|
|
1047
|
-
logLabel.text = job.message || job.state;
|
|
1048
|
-
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
1049
|
-
job = await daemonRequest(
|
|
1050
|
-
"/builder/jobs/" + encodeURIComponent(job.id),
|
|
1051
|
-
"GET",
|
|
1052
|
-
{},
|
|
1053
|
-
30000
|
|
1054
|
-
);
|
|
1055
|
-
}
|
|
1056
|
-
if (!job || job.state === "error") {
|
|
1057
|
-
throw new Error((job && job.error) || "Template build failed.");
|
|
1058
|
-
}
|
|
1059
|
-
pathLabel.text = job.publicUrl || "";
|
|
1060
|
-
localStorage.setItem(`pcbridge:builder:${id}:url`, pathLabel.text);
|
|
1061
|
-
logLabel.text = job.message || "上传完成";
|
|
1062
|
-
} catch (error) {
|
|
1063
|
-
logLabel.text = "构建失败: " + String((error && error.message) || error);
|
|
1064
|
-
} finally {
|
|
1065
|
-
buildButton.enabled = true;
|
|
1066
|
-
}
|
|
1067
|
-
});
|
|
1068
|
-
container.append(buildButton);
|
|
1069
|
-
container.append(copyButton);
|
|
1070
|
-
container.append(logLabel);
|
|
1071
|
-
layout.append(container);
|
|
1072
|
-
builderPanel = container;
|
|
1073
|
-
selectedTemplateId = id;
|
|
1074
|
-
selectedTemplateAsset = asset;
|
|
1075
|
-
}
|
|
1076
|
-
|
|
1077
|
-
function scheduleBuilderPanelRefresh(asset) {
|
|
1078
|
-
if (builderRefreshTimer !== null) clearTimeout(builderRefreshTimer);
|
|
1079
|
-
builderRefreshTimer = setTimeout(() => {
|
|
1080
|
-
builderRefreshTimer = null;
|
|
1081
|
-
const selection = selectedAssetStateFromEditor();
|
|
1082
|
-
if (selection.known) showBuilderPanel(selection.asset);
|
|
1083
|
-
else showBuilderPanel(asset || selectedTemplateAsset);
|
|
1084
|
-
}, 150);
|
|
1085
|
-
}
|
|
1086
|
-
|
|
1087
|
-
function installBuilderPanel() {
|
|
1088
|
-
if (!location.pathname.startsWith("/editor") || typeof setInterval !== "function") return;
|
|
1089
|
-
let subscribed = false;
|
|
1090
|
-
setInterval(() => {
|
|
1091
|
-
if (!window.editor || !window.editor.api || !window.editor.api.globals) return;
|
|
1092
|
-
if (!subscribed) {
|
|
1093
|
-
subscribed = true;
|
|
1094
|
-
const refresh = (asset) => scheduleBuilderPanelRefresh(asset);
|
|
1095
|
-
const legacy = window.editor.selection;
|
|
1096
|
-
if (legacy && typeof legacy.on === "function") {
|
|
1097
|
-
legacy.on("add", refresh);
|
|
1098
|
-
legacy.on("remove", () => refresh());
|
|
1099
|
-
}
|
|
1100
|
-
const selection = window.editor.api.globals.selection;
|
|
1101
|
-
if (selection && typeof selection.on === "function") {
|
|
1102
|
-
selection.on("add", refresh);
|
|
1103
|
-
selection.on("remove", () => refresh());
|
|
1104
|
-
selection.on("change", () => refresh());
|
|
1105
|
-
}
|
|
1106
|
-
}
|
|
1107
|
-
const selection = selectedAssetStateFromEditor();
|
|
1108
|
-
if (selection.known) showBuilderPanel(selection.asset);
|
|
1109
|
-
else if (selectedTemplateAsset) showBuilderPanel(selectedTemplateAsset);
|
|
1110
|
-
}, 1000);
|
|
1568
|
+
function callPlugin(method, params) {
|
|
1569
|
+
const [, id, name] = method.match(/^plugin:([^:]+):(.+)$/) || [];
|
|
1570
|
+
const state = loadedPlugins.get(id);
|
|
1571
|
+
const handler = state && state.handlers.get(name);
|
|
1572
|
+
if (!handler) throw new Error(`Plugin page handler not found: ${method}`);
|
|
1573
|
+
return handler(params);
|
|
1111
1574
|
}
|
|
1112
1575
|
|
|
1113
1576
|
async function evalInPage(params, requestId) {
|
|
@@ -1306,7 +1769,7 @@
|
|
|
1306
1769
|
return { affected };
|
|
1307
1770
|
}
|
|
1308
1771
|
|
|
1309
|
-
async function
|
|
1772
|
+
async function uploadAssetInner(params) {
|
|
1310
1773
|
if (!params.base64) throw new Error("base64 file content is required.");
|
|
1311
1774
|
if (!params.name) throw new Error("name is required.");
|
|
1312
1775
|
|
|
@@ -1333,6 +1796,10 @@
|
|
|
1333
1796
|
};
|
|
1334
1797
|
}
|
|
1335
1798
|
|
|
1799
|
+
function uploadAsset(params) {
|
|
1800
|
+
return withWorkspaceMutationSuppression(() => uploadAssetInner(params));
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1336
1803
|
function focusViewport(params) {
|
|
1337
1804
|
const ids = Array.isArray(params.ids) && params.ids.length ? params.ids : [params.id];
|
|
1338
1805
|
const entitiesApi = window.editor.api.globals.entities;
|
|
@@ -1389,6 +1856,28 @@
|
|
|
1389
1856
|
}
|
|
1390
1857
|
|
|
1391
1858
|
async function callMethod(method, params, requestId) {
|
|
1859
|
+
if (method === "bridge:importJob") {
|
|
1860
|
+
if (getTargetKind() !== "editor" || !window.config?.selfhost || !window.editor?.api?.globals) {
|
|
1861
|
+
throw Object.assign(new Error("Selfhost Editor required"), { code: "SELFHOST_EDITOR_REQUIRED" });
|
|
1862
|
+
}
|
|
1863
|
+
if (!window.editor.methods?.has("assets:import:request")) {
|
|
1864
|
+
throw Object.assign(new Error("Refresh the upgraded selfhost Editor before importing"), { code: "PROCESSING_EDITOR_UNSUPPORTED" });
|
|
1865
|
+
}
|
|
1866
|
+
const api = window.editor.api.globals;
|
|
1867
|
+
const context = { projectId: api.projectId, branchId: api.branchId };
|
|
1868
|
+
const action = params.action;
|
|
1869
|
+
let path, verb = "GET", body;
|
|
1870
|
+
if (action === "list") path = `/jobs?projectId=${api.projectId}&branchId=${encodeURIComponent(api.branchId)}`;
|
|
1871
|
+
else if (action === "submit") {
|
|
1872
|
+
if (!/^\d+$/.test(params.assetId || "") || !["model", "texture"].includes(params.kind)) throw new Error("Invalid import submission");
|
|
1873
|
+
path = `/${params.assetId}/${params.kind === "texture" ? "texture" : "reimport"}`;
|
|
1874
|
+
verb = "POST"; body = { ...context, options: params.options || {} };
|
|
1875
|
+
} else if (["status", "cancel", "retry"].includes(action) && /^[a-zA-Z0-9-]{1,100}$/.test(params.jobId || "")) {
|
|
1876
|
+
path = `/jobs/${params.jobId}${action === "status" ? "" : `/${action}`}`;
|
|
1877
|
+
if (action !== "status") { verb = "POST"; body = {}; }
|
|
1878
|
+
} else throw new Error("Invalid import action");
|
|
1879
|
+
return window.editor.call("assets:import:request", path, verb, body);
|
|
1880
|
+
}
|
|
1392
1881
|
if (method === "bridge:ping") return { pong: true, href: location.href };
|
|
1393
1882
|
if (method === "bridge:describeTarget") return describeTarget();
|
|
1394
1883
|
if (method === "bridge:eval") return evalInPage(params || {}, requestId);
|
|
@@ -1401,13 +1890,20 @@
|
|
|
1401
1890
|
installWorkspaceEvents();
|
|
1402
1891
|
return workspaceSnapshot();
|
|
1403
1892
|
}
|
|
1404
|
-
if (method === "bridge:
|
|
1893
|
+
if (method === "bridge:syncPlugins") return syncPlugins(params || {});
|
|
1894
|
+
if (method.startsWith("plugin:")) return callPlugin(method, params || {});
|
|
1405
1895
|
if (method === "bridge:readAssetText") return readAssetText(params || {});
|
|
1406
1896
|
if (method === "bridge:readAssetFile") return readAssetFile(params || {});
|
|
1407
1897
|
if (method === "bridge:readAssetResource") return readAssetResource(params || {});
|
|
1408
1898
|
if (method === "bridge:writeAssetFile") return writeAssetFile(params || {});
|
|
1409
1899
|
if (method === "bridge:renameAsset") return renameAsset(params || {});
|
|
1410
1900
|
if (method === "bridge:writeScriptText") return writeScriptText(params || {});
|
|
1901
|
+
if (method === "bridge:writeAssetText") return writeAssetText(params || {});
|
|
1902
|
+
if (method === "bridge:createDataAsset") return createDataAsset(params || {});
|
|
1903
|
+
if (method === "bridge:patchAssetData") return patchAssetData(params || {});
|
|
1904
|
+
if (method === "bridge:deleteAssets") return deleteAssets(params || {});
|
|
1905
|
+
if (method === "bridge:readScene") return readScene(params || {});
|
|
1906
|
+
if (method === "bridge:patchScene") return patchScene(params || {});
|
|
1411
1907
|
throw new Error(`Unknown bridge method: ${method}`);
|
|
1412
1908
|
}
|
|
1413
1909
|
|
|
@@ -1456,6 +1952,5 @@
|
|
|
1456
1952
|
}
|
|
1457
1953
|
});
|
|
1458
1954
|
|
|
1459
|
-
installBuilderPanel();
|
|
1460
1955
|
window.postMessage({ channel: CHANNEL, side: "main", type: "ready" }, "*");
|
|
1461
1956
|
})();
|