dsh-ccx 0.8.9 → 0.9.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 (49) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +23 -23
  3. package/README.md +151 -143
  4. package/assets/icon.svg +27 -27
  5. package/cordis.patch.yml +16 -16
  6. package/lib/client.js +1481 -2244
  7. package/lib/index.js +716 -449
  8. package/lib/vendor/code-codex/ascii-tiles.js +145 -0
  9. package/lib/vendor/code-codex/aurora-ionosphere.js +7 -6
  10. package/lib/vendor/code-codex/black-hole.js +10 -7
  11. package/lib/vendor/code-codex/blinking-squares.js +6 -13
  12. package/lib/vendor/code-codex/cloud-train.js +9 -8
  13. package/lib/vendor/code-codex/galleries.json +100 -100
  14. package/lib/vendor/code-codex/glow-horizon.js +1 -1
  15. package/lib/vendor/code-codex/heavenly-cloud.js +5 -4
  16. package/lib/vendor/code-codex/milky-way.js +3 -2
  17. package/lib/vendor/code-codex/mountain.js +5 -4
  18. package/lib/vendor/code-codex/particle-image.js +435 -14
  19. package/lib/vendor/code-codex/pixel-sculpt.js +85 -5
  20. package/lib/vendor/code-codex/scene-defaults.json +94 -1
  21. package/lib/vendor/code-codex/scene-schemas.json +1008 -0
  22. package/lib/vendor/code-codex/shallow-ocean-thumbnails.json +12 -0
  23. package/lib/vendor/code-codex/shallow-ocean.js +5094 -0
  24. package/lib/vendor/code-codex/spectral-drape.js +172 -0
  25. package/package.json +70 -59
  26. package/src/client/api.ts +90 -0
  27. package/src/client/background/layer.ts +187 -0
  28. package/src/client/background/pixel-sculpt.ts +167 -0
  29. package/src/client/background/scene-runner.ts +317 -0
  30. package/src/client/background/shallow-ocean.ts +344 -0
  31. package/src/client/background/vendor-loader.ts +297 -0
  32. package/src/client/background/wallpaper.ts +87 -0
  33. package/src/client/index.ts +247 -0
  34. package/src/client/surfaces/corner-patch.ts +100 -0
  35. package/src/client/surfaces/surface-css.ts +155 -0
  36. package/src/client/ui/draft.ts +105 -0
  37. package/src/client/ui/icons.ts +47 -0
  38. package/src/client/ui/panel.ts +1524 -0
  39. package/src/client/ui/shallow-ocean-panel.ts +376 -0
  40. package/src/client/ui/styles.ts +1471 -0
  41. package/src/host/fence.ts +78 -0
  42. package/src/host/index.ts +26 -0
  43. package/src/host/routes.ts +367 -0
  44. package/src/host/storage.ts +109 -0
  45. package/src/host/window-chrome.ts +53 -0
  46. package/src/shared/constants.ts +239 -0
  47. package/src/shared/sanitize.ts +110 -0
  48. package/src/shared/types.ts +145 -0
  49. package/tsconfig.json +16 -0
@@ -0,0 +1,187 @@
1
+ import { SCENE_LAYER_BG } from '../../shared/constants';
2
+ import { clamp, finiteNum } from '../../shared/sanitize';
3
+ import type { AppearanceSettings } from '../../shared/types';
4
+ import { DIM_ID, LAYER_ID } from '../surfaces/surface-css';
5
+
6
+ export interface LayerNodes {
7
+ layer: HTMLDivElement;
8
+ bgWrap: HTMLDivElement;
9
+ bgHost: HTMLDivElement;
10
+ dimEl: HTMLDivElement;
11
+ }
12
+
13
+ const STAGE_RETIRE_DELAY_MS = 4000;
14
+ const HEX6_RE = /^#[\da-fA-F]{6}$/;
15
+
16
+ let nodes: LayerNodes | null = null;
17
+ let currentStage: HTMLDivElement | null = null;
18
+ const retireTimers = new Set<ReturnType<typeof setTimeout>>();
19
+
20
+ export function ensureLayer(): LayerNodes {
21
+ if (nodes && document.body && document.body.contains(nodes.layer)) {
22
+ return nodes;
23
+ }
24
+ const layer = document.createElement('div');
25
+ layer.id = LAYER_ID;
26
+ layer.setAttribute('data-dsh-ccx', 'background');
27
+ layer.style.cssText =
28
+ 'position:fixed;inset:0;z-index:-1;pointer-events:none;overflow:hidden;' +
29
+ 'width:100vw;height:100vh;margin:0;padding:0;contain:strict;background:#000;--ap-layer-bg:#000;';
30
+
31
+ const bgWrap = document.createElement('div');
32
+ bgWrap.style.cssText = 'position:absolute;inset:0;overflow:hidden;';
33
+
34
+ const bgHost = document.createElement('div');
35
+ bgHost.style.cssText = 'position:absolute;inset:0;';
36
+
37
+ const dimEl = document.createElement('div');
38
+ dimEl.id = DIM_ID;
39
+ dimEl.style.cssText =
40
+ 'position:absolute;inset:0;background:transparent;--ap-dim-bg:transparent;';
41
+
42
+ bgWrap.appendChild(bgHost);
43
+ layer.appendChild(bgWrap);
44
+ layer.appendChild(dimEl);
45
+
46
+ const body = document.body || document.documentElement;
47
+ body.prepend(layer);
48
+
49
+ nodes = { layer, bgWrap, bgHost, dimEl };
50
+ return nodes;
51
+ }
52
+
53
+ export function getLayer(): LayerNodes | null {
54
+ return nodes;
55
+ }
56
+
57
+ export function setLayerBackground(color: string): void {
58
+ const { layer } = ensureLayer();
59
+ layer.style.setProperty('--ap-layer-bg', color || '#000000');
60
+ }
61
+
62
+ export function resolveSceneLayerBg(sceneId: string, state: AppearanceSettings | null): string {
63
+ if (
64
+ sceneId === 'particle-image' ||
65
+ sceneId === 'blinking-squares' ||
66
+ sceneId === 'ascii-tiles' ||
67
+ sceneId === 'spectral-drape'
68
+ ) {
69
+ const params = state?.sceneParams?.[sceneId];
70
+ const c = params && typeof params.backgroundColor === 'string' ? params.backgroundColor : null;
71
+ if (c && HEX6_RE.test(c)) return c;
72
+ }
73
+ return SCENE_LAYER_BG[sceneId] || '#000000';
74
+ }
75
+
76
+ export function newStage(): HTMLDivElement {
77
+ const { bgHost } = ensureLayer();
78
+ const stage = document.createElement('div');
79
+ stage.style.cssText = 'position:absolute;inset:0;';
80
+ bgHost.appendChild(stage);
81
+ currentStage = stage;
82
+ return stage;
83
+ }
84
+
85
+ export function activeStage(): HTMLDivElement {
86
+ const { bgHost } = ensureLayer();
87
+ if (!currentStage || !bgHost.contains(currentStage)) {
88
+ return newStage();
89
+ }
90
+ return currentStage;
91
+ }
92
+
93
+ export function getCurrentStage(): HTMLDivElement | null {
94
+ return currentStage;
95
+ }
96
+
97
+ export function releaseMediaInStage(stage: HTMLElement): void {
98
+ try {
99
+ const videos = stage.querySelectorAll('video');
100
+ for (let i = 0; i < videos.length; i++) {
101
+ const v = videos[i];
102
+ try {
103
+ v.pause();
104
+ v.removeAttribute('src');
105
+ v.load();
106
+ } catch {
107
+ // Ignore media cleanup failures during detach
108
+ }
109
+ }
110
+ } catch {
111
+ // Ignore DOM query errors on detached stage
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Retire an old stage without breaking particle-framework startup-hold coordination.
117
+ * Immediately hides it visually to avoid overlapping two scenes, then removes DOM after 4s.
118
+ */
119
+ export function retireStage(stage: HTMLDivElement | null): void {
120
+ if (!stage) return;
121
+ if (stage === currentStage) currentStage = null;
122
+ try {
123
+ stage.style.visibility = 'hidden';
124
+ stage.style.pointerEvents = 'none';
125
+ } catch {
126
+ // Ignore style errors
127
+ }
128
+ releaseMediaInStage(stage);
129
+ const timer = setTimeout(() => {
130
+ retireTimers.delete(timer);
131
+ try {
132
+ stage.parentNode?.removeChild(stage);
133
+ } catch {
134
+ // Already removed
135
+ }
136
+ }, STAGE_RETIRE_DELAY_MS);
137
+ retireTimers.add(timer);
138
+ }
139
+
140
+ export function paintFallback(text: string): void {
141
+ const stage = activeStage();
142
+ releaseMediaInStage(stage);
143
+ stage.textContent = '';
144
+ const tip = document.createElement('div');
145
+ tip.style.cssText =
146
+ 'position:absolute;inset:0;display:flex;align-items:center;justify-content:center;' +
147
+ 'color:#4d5a78;font-size:13px;font-family:system-ui,sans-serif;background:' +
148
+ 'radial-gradient(120% 90% at 50% 110%,#101a33 0%,#07090f 60%);';
149
+ tip.textContent = text || '';
150
+ stage.appendChild(tip);
151
+ }
152
+
153
+ export function applyLayerAdjustments(state: AppearanceSettings): void {
154
+ const { layer, bgWrap, dimEl } = ensureLayer();
155
+ const bgOn = state.enabled && state.mode !== 'off';
156
+ layer.style.display = bgOn ? '' : 'none';
157
+
158
+ const blurPx = clamp(finiteNum(state.blur, 0), 0, 20);
159
+ bgWrap.style.filter = blurPx > 0 ? `blur(${blurPx}px)` : '';
160
+ bgWrap.style.transform =
161
+ blurPx > 0 ? `scale(${(1 + Math.min(0.08, blurPx * 0.006)).toFixed(3)})` : '';
162
+
163
+ const dimAlpha = bgOn ? clamp(finiteNum(state.dim, 0), 0, 0.8) : 0;
164
+ const dimBg = bgOn ? `rgba(3,6,14,${dimAlpha.toFixed(3)})` : 'transparent';
165
+ dimEl.style.background = dimBg;
166
+ dimEl.style.setProperty('--ap-dim-bg', dimBg);
167
+ }
168
+
169
+ export function disposeLayer(): void {
170
+ for (const timer of retireTimers) {
171
+ clearTimeout(timer);
172
+ }
173
+ retireTimers.clear();
174
+ if (currentStage) {
175
+ releaseMediaInStage(currentStage);
176
+ currentStage = null;
177
+ }
178
+ if (nodes) {
179
+ releaseMediaInStage(nodes.layer);
180
+ try {
181
+ nodes.layer.parentNode?.removeChild(nodes.layer);
182
+ } catch {
183
+ // Ignore
184
+ }
185
+ nodes = null;
186
+ }
187
+ }
@@ -0,0 +1,167 @@
1
+ import type { AppearanceSettings, VendorRendererInstance } from '../../shared/types';
2
+ import { ensureGallery, ensureVendorScene } from './vendor-loader';
3
+
4
+ export interface PixelSculptEditor {
5
+ stage: HTMLDivElement;
6
+ canvas: HTMLCanvasElement;
7
+ inst: {
8
+ setActive: (active: boolean, opts?: { paused?: boolean }) => void;
9
+ setSettings: (s: Record<string, unknown>) => void;
10
+ mountControls: (host: HTMLElement, locale?: string) => void;
11
+ reset: () => void;
12
+ replay: () => void;
13
+ } | null;
14
+ ready: Promise<PixelSculptEditor>;
15
+ }
16
+
17
+ let psEd: PixelSculptEditor | null = null;
18
+
19
+ export function isPsActive(state: AppearanceSettings | null): boolean {
20
+ return Boolean(state && state.enabled && state.mode === 'scene' && state.scene === 'pixel-sculpt');
21
+ }
22
+
23
+ export function isPsPaused(state: AppearanceSettings | null): boolean {
24
+ const p = state?.sceneParams?.['pixel-sculpt'];
25
+ return Boolean(p && typeof p === 'object' && p.paused);
26
+ }
27
+
28
+ export function getPsEditor(): PixelSculptEditor | null {
29
+ return psEd;
30
+ }
31
+
32
+ export function ensurePsEditor(
33
+ getState: () => AppearanceSettings | null,
34
+ onStatus: (msg: string, isError?: boolean) => void,
35
+ ): Promise<PixelSculptEditor> {
36
+ if (psEd) return psEd.ready;
37
+
38
+ const stage = document.createElement('div');
39
+ stage.style.cssText = 'position:absolute;inset:0;';
40
+ const canvas = document.createElement('canvas');
41
+ canvas.style.cssText = 'width:100%;height:100%;display:block';
42
+ stage.appendChild(canvas);
43
+
44
+ const holder: PixelSculptEditor = {
45
+ stage,
46
+ canvas,
47
+ inst: null,
48
+ ready: null as unknown as Promise<PixelSculptEditor>,
49
+ };
50
+
51
+ holder.ready = ensureGallery('pixel-sculpt')
52
+ .catch(() => null)
53
+ .then(() => ensureVendorScene('pixel-sculpt'))
54
+ .then((entry) => {
55
+ const seed = entry.prepareDefaults ? entry.prepareDefaults() : null;
56
+ return Promise.resolve(seed)
57
+ .catch(() => null)
58
+ .then(() => {
59
+ const Ctor = entry.exports?.PixelSculptRenderer as
60
+ | (new (
61
+ stage: HTMLElement,
62
+ canvas: HTMLCanvasElement,
63
+ settings: unknown,
64
+ onStatus: (msg: string) => void,
65
+ persist: boolean,
66
+ ) => PixelSculptEditor['inst'])
67
+ | undefined;
68
+ if (typeof Ctor !== 'function') {
69
+ throw new Error('PixelSculptRenderer 不可用');
70
+ }
71
+ holder.inst = new Ctor(
72
+ stage,
73
+ canvas,
74
+ null,
75
+ (msg: string) => {
76
+ if (msg && isPsActive(getState())) {
77
+ onStatus(`Pixel Sculpt:${String(msg).slice(0, 60)}`, true);
78
+ }
79
+ },
80
+ true,
81
+ );
82
+ return holder;
83
+ });
84
+ });
85
+
86
+ holder.ready.catch(() => {
87
+ if (psEd === holder) psEd = null;
88
+ });
89
+
90
+ psEd = holder;
91
+ return holder.ready;
92
+ }
93
+
94
+ export function attachPsScene(
95
+ stage: HTMLDivElement,
96
+ getState: () => AppearanceSettings | null,
97
+ onStatus: (msg: string, isError?: boolean) => void,
98
+ isStillCurrent: () => boolean,
99
+ onAttached: (instance: VendorRendererInstance) => void,
100
+ onError: (err: unknown) => void,
101
+ ): void {
102
+ ensurePsEditor(getState, onStatus)
103
+ .then((ed) => {
104
+ if (!isStillCurrent() || !isPsActive(getState())) return;
105
+ stage.appendChild(ed.stage);
106
+ try {
107
+ ed.inst?.setActive(true, { paused: isPsPaused(getState()) });
108
+ } catch {
109
+ // Ignore vendor lifecycle glitch
110
+ }
111
+ const adapter: VendorRendererInstance = {
112
+ setSettings: (s) => {
113
+ try {
114
+ ed.inst?.setSettings(s || {});
115
+ } catch {
116
+ // Ignore
117
+ }
118
+ },
119
+ reset: () => {
120
+ try {
121
+ ed.inst?.reset();
122
+ } catch {
123
+ // Ignore
124
+ }
125
+ },
126
+ replay: () => {
127
+ try {
128
+ ed.inst?.replay();
129
+ } catch {
130
+ // Ignore
131
+ }
132
+ },
133
+ dispose: () => {
134
+ try {
135
+ ed.stage.parentNode?.removeChild(ed.stage);
136
+ } catch {
137
+ // Ignore
138
+ }
139
+ try {
140
+ ed.inst?.setActive(false, { paused: isPsPaused(getState()) });
141
+ } catch {
142
+ // Ignore
143
+ }
144
+ },
145
+ };
146
+ onAttached(adapter);
147
+ })
148
+ .catch((err) => {
149
+ if (!isStillCurrent()) return;
150
+ onError(err);
151
+ });
152
+ }
153
+
154
+ export function disposePsEditor(): void {
155
+ if (!psEd) return;
156
+ try {
157
+ psEd.inst?.setActive(false, { paused: true });
158
+ } catch {
159
+ // Ignore
160
+ }
161
+ try {
162
+ psEd.stage.parentNode?.removeChild(psEd.stage);
163
+ } catch {
164
+ // Ignore
165
+ }
166
+ psEd = null;
167
+ }
@@ -0,0 +1,317 @@
1
+ import { GALLERY_SCENES, SCENE_IDS, STORE_KEYS } from '../../shared/constants';
2
+ import type { AppearanceSettings, SceneParamMap, VendorRendererInstance } from '../../shared/types';
3
+ import { getCachedSchemas, schemaOf } from '../api';
4
+ import {
5
+ getCurrentStage,
6
+ newStage,
7
+ paintFallback,
8
+ resolveSceneLayerBg,
9
+ retireStage,
10
+ setLayerBackground,
11
+ } from './layer';
12
+ import { attachPsScene, disposePsEditor } from './pixel-sculpt';
13
+ import { disposeShallowOceanController, getShallowOceanController } from './shallow-ocean';
14
+ import { ensureGallery, ensureGallerySelection, ensureVendorScene, registry } from './vendor-loader';
15
+ import { renderWallpaper } from './wallpaper';
16
+
17
+ const GALLERY_OWNED_KEYS = new Set(['selectedImageIds', 'activeImageId']);
18
+ const STORE_MIGRATION_KEY = 'dsh-ap:scene-stores-v2';
19
+
20
+ let bgInstance: VendorRendererInstance | null = null;
21
+ let bgSceneId: string | null = null;
22
+ let renderSeq = 0;
23
+ let lastRenderSig: string | null = null;
24
+ let lastParamsSig: string | null = null;
25
+
26
+ export function getBgInstance(): VendorRendererInstance | null {
27
+ return bgInstance;
28
+ }
29
+
30
+ export function getBgSceneId(): string | null {
31
+ return bgSceneId;
32
+ }
33
+
34
+ export function clearRenderSignature(): void {
35
+ lastRenderSig = null;
36
+ }
37
+
38
+ export function writeSceneSetting(sceneId: string, patch: Record<string, unknown>): void {
39
+ const key = STORE_KEYS[sceneId];
40
+ if (!key) return;
41
+ try {
42
+ let cur: Record<string, unknown> = {};
43
+ try {
44
+ cur = JSON.parse(localStorage.getItem(key) ?? '{}') || {};
45
+ } catch {
46
+ cur = {};
47
+ }
48
+ localStorage.setItem(key, JSON.stringify({ ...cur, ...patch }));
49
+ } catch {
50
+ // Ignore localStorage failures
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Full settings object for a scene store: schema defaults merged with persisted overrides.
56
+ */
57
+ export function buildStoreSettings(sceneId: string, state: AppearanceSettings | null): SceneParamMap {
58
+ const schemas = getCachedSchemas();
59
+ const sch = schemas?.[sceneId] ?? schemaOf(sceneId);
60
+ const base: SceneParamMap = sch?.defaults ? { ...sch.defaults } : {};
61
+ const overrides = state?.sceneParams?.[sceneId];
62
+ if (overrides && typeof overrides === 'object') {
63
+ for (const k of Object.keys(overrides)) {
64
+ base[k] = overrides[k];
65
+ }
66
+ }
67
+ if (GALLERY_SCENES[sceneId]) {
68
+ for (const gk of GALLERY_OWNED_KEYS) {
69
+ if (!(overrides && Object.prototype.hasOwnProperty.call(overrides, gk))) {
70
+ delete base[gk];
71
+ }
72
+ }
73
+ }
74
+ return base;
75
+ }
76
+
77
+ export function pushLiveSettings(inst: VendorRendererInstance | null, full: SceneParamMap): boolean {
78
+ if (!inst) return false;
79
+ const fn = inst.setSettings || inst.updateSettings || inst.setRenderSettings;
80
+ if (typeof fn !== 'function') return false;
81
+ try {
82
+ fn.call(inst, full);
83
+ return true;
84
+ } catch {
85
+ return false;
86
+ }
87
+ }
88
+
89
+ export function replayActiveScene(sceneId: string): void {
90
+ const inst = bgInstance && bgSceneId === sceneId ? bgInstance : null;
91
+ if (!inst) return;
92
+ if (typeof inst.replayOpening === 'function') {
93
+ try {
94
+ inst.replayOpening();
95
+ } catch {
96
+ // Ignore
97
+ }
98
+ return;
99
+ }
100
+ if (typeof inst.replay === 'function') {
101
+ try {
102
+ inst.replay();
103
+ } catch {
104
+ // Ignore
105
+ }
106
+ }
107
+ }
108
+
109
+ export function disposeBgInstance(): void {
110
+ bgSceneId = null;
111
+ if (bgInstance) {
112
+ try {
113
+ bgInstance.dispose?.();
114
+ } catch {
115
+ // Ignore
116
+ }
117
+ bgInstance = null;
118
+ }
119
+ retireStage(getCurrentStage());
120
+ }
121
+
122
+ export function migratePoisonedStores(): void {
123
+ try {
124
+ if (localStorage.getItem(STORE_MIGRATION_KEY)) return;
125
+ for (const id of Object.keys(STORE_KEYS)) {
126
+ if (GALLERY_SCENES[id]) continue;
127
+ try {
128
+ localStorage.removeItem(STORE_KEYS[id]);
129
+ } catch {
130
+ // Ignore
131
+ }
132
+ }
133
+ localStorage.setItem(STORE_MIGRATION_KEY, '1');
134
+ } catch {
135
+ // Ignore
136
+ }
137
+ }
138
+
139
+ function paramsSignature(state: AppearanceSettings | null): string {
140
+ if (!state) return '{}';
141
+ return JSON.stringify(state.sceneParams?.[state.scene] || {});
142
+ }
143
+
144
+ function renderSignature(state: AppearanceSettings | null): string | null {
145
+ if (!(state && state.enabled && state.mode === 'scene')) return null;
146
+ return `${state.mode}:${state.scene}`;
147
+ }
148
+
149
+ export interface RunnerCallbacks {
150
+ getState: () => AppearanceSettings | null;
151
+ onStatus: (msg: string, isError?: boolean) => void;
152
+ }
153
+
154
+ export function syncBackgroundRunner(cb: RunnerCallbacks): void {
155
+ const state = cb.getState();
156
+ if (!state) return;
157
+
158
+ const bgOn = state.enabled && state.mode !== 'off';
159
+ if (bgOn && state.mode === 'scene') {
160
+ const sig = renderSignature(state);
161
+ const pSig = paramsSignature(state);
162
+ if (sig !== lastRenderSig) {
163
+ renderScene(cb);
164
+ } else if (pSig !== lastParamsSig) {
165
+ lastParamsSig = pSig;
166
+ const fullSettings = buildStoreSettings(state.scene, state);
167
+ writeSceneSetting(state.scene, fullSettings);
168
+ if (!(bgInstance && bgSceneId === state.scene && pushLiveSettings(bgInstance, fullSettings))) {
169
+ renderScene(cb);
170
+ }
171
+ }
172
+ return;
173
+ }
174
+
175
+ if (bgOn && state.mode === 'wallpaper') {
176
+ renderSeq++;
177
+ lastRenderSig = null;
178
+ disposeBgInstance();
179
+ renderWallpaper(state);
180
+ return;
181
+ }
182
+
183
+ renderSeq++;
184
+ disposeBgInstance();
185
+ lastRenderSig = null;
186
+ }
187
+
188
+ export function renderScene(cb: RunnerCallbacks): void {
189
+ const seq = ++renderSeq;
190
+ const state = cb.getState();
191
+ const sig = renderSignature(state);
192
+ lastRenderSig = sig;
193
+ lastParamsSig = paramsSignature(state);
194
+
195
+ if (!sig || !state) {
196
+ disposeBgInstance();
197
+ return;
198
+ }
199
+
200
+ const id = SCENE_IDS.includes(state.scene) ? state.scene : 'milky-way';
201
+ setLayerBackground(resolveSceneLayerBg(id, state));
202
+ const initialSettings = buildStoreSettings(id, state);
203
+ writeSceneSetting(id, initialSettings);
204
+
205
+ const prepared = GALLERY_SCENES[id]
206
+ ? ensureGallery(id).then(() => {
207
+ const entry0 = registry()?.get(id);
208
+ if (entry0 && typeof entry0.prepareDefaults === 'function') {
209
+ return entry0.prepareDefaults();
210
+ }
211
+ return null;
212
+ })
213
+ : Promise.resolve(null);
214
+
215
+ const isStillCurrent = () => {
216
+ const cur = cb.getState();
217
+ return seq === renderSeq && Boolean(cur && cur.enabled && cur.mode === 'scene' && cur.scene === id);
218
+ };
219
+
220
+ prepared
221
+ .then(() => ensureGallerySelection(id))
222
+ .then(() => {
223
+ if (id === 'shallow-ocean') {
224
+ if (!isStillCurrent()) return;
225
+ disposeBgInstance();
226
+ const stage = newStage();
227
+ const controller = getShallowOceanController();
228
+ return controller
229
+ .mount(
230
+ stage,
231
+ buildStoreSettings('shallow-ocean', cb.getState()),
232
+ (msg) => {
233
+ if (seq === renderSeq && msg) cb.onStatus(`场景提示:${String(msg).slice(0, 60)}`);
234
+ },
235
+ isStillCurrent,
236
+ )
237
+ .then((inst) => {
238
+ if (!isStillCurrent()) {
239
+ try {
240
+ inst.dispose?.();
241
+ } catch {
242
+ // Ignore
243
+ }
244
+ return;
245
+ }
246
+ bgInstance = inst;
247
+ bgSceneId = id;
248
+ if (inst.ready && typeof inst.ready.then === 'function') {
249
+ inst.ready.catch((err) => {
250
+ if (seq === renderSeq) {
251
+ cb.onStatus(`场景未就绪:${String((err as Error)?.message || err).slice(0, 60)}`, true);
252
+ }
253
+ });
254
+ }
255
+ });
256
+ }
257
+
258
+ return ensureVendorScene(id).then((entry) => {
259
+ if (!isStillCurrent()) return;
260
+ disposeBgInstance();
261
+
262
+ if (id === 'pixel-sculpt') {
263
+ attachPsScene(
264
+ newStage(),
265
+ cb.getState,
266
+ cb.onStatus,
267
+ isStillCurrent,
268
+ (adapter) => {
269
+ bgInstance = adapter;
270
+ bgSceneId = id;
271
+ },
272
+ (err) => {
273
+ paintFallback(`Pixel Sculpt 启动失败:${String((err as Error)?.message || err).slice(0, 50)}`);
274
+ cb.onStatus('Pixel Sculpt 启动失败', true);
275
+ },
276
+ );
277
+ return;
278
+ }
279
+
280
+ const stage = newStage();
281
+ try {
282
+ const inst = entry.startup(stage, (msg?: string) => {
283
+ if (seq === renderSeq && msg) cb.onStatus(`场景提示:${String(msg).slice(0, 60)}`);
284
+ });
285
+ bgInstance = inst;
286
+ bgSceneId = id;
287
+ if (inst.ready && typeof inst.ready.then === 'function') {
288
+ inst.ready.catch((err) => {
289
+ if (seq === renderSeq) {
290
+ cb.onStatus(`场景未就绪:${String((err as Error)?.message || err).slice(0, 60)}`, true);
291
+ }
292
+ });
293
+ }
294
+ } catch (err) {
295
+ bgInstance = null;
296
+ paintFallback(`场景渲染器启动失败:${id}(${String((err as Error)?.message || err).slice(0, 60)})`);
297
+ cb.onStatus(`场景渲染器启动失败:${id}`, true);
298
+ }
299
+ });
300
+ })
301
+ .catch((err) => {
302
+ if (seq !== renderSeq) return;
303
+ if (String((err as Error)?.message || err) === 'stale mount') return;
304
+ disposeBgInstance();
305
+ paintFallback(`场景加载失败:${id}(${String((err as Error)?.message || err).slice(0, 50)})`);
306
+ cb.onStatus(`场景加载失败:${id}`, true);
307
+ });
308
+ }
309
+
310
+ export function disposeAllRunners(): void {
311
+ renderSeq++;
312
+ lastRenderSig = null;
313
+ lastParamsSig = null;
314
+ disposeBgInstance();
315
+ disposePsEditor();
316
+ disposeShallowOceanController();
317
+ }