@swf2es/web 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +43 -2
- package/dist/air-files.d.ts +40 -0
- package/dist/air-files.d.ts.map +1 -0
- package/dist/air-files.js +304 -0
- package/dist/air-files.js.map +1 -0
- package/dist/air-items.d.ts +28 -0
- package/dist/air-items.d.ts.map +1 -0
- package/dist/air-items.js +186 -0
- package/dist/air-items.js.map +1 -0
- package/dist/air.d.ts +81 -0
- package/dist/air.d.ts.map +1 -0
- package/dist/air.js +409 -0
- package/dist/air.js.map +1 -0
- package/dist/config.d.ts +123 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +127 -0
- package/dist/config.js.map +1 -0
- package/dist/element.d.ts +80 -0
- package/dist/element.d.ts.map +1 -0
- package/dist/element.js +725 -0
- package/dist/element.js.map +1 -0
- package/dist/external.d.ts +49 -0
- package/dist/external.d.ts.map +1 -0
- package/dist/external.js +268 -0
- package/dist/external.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/layout.d.ts +47 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +75 -0
- package/dist/layout.js.map +1 -0
- package/dist/playback.d.ts +107 -0
- package/dist/playback.d.ts.map +1 -0
- package/dist/playback.js +308 -0
- package/dist/playback.js.map +1 -0
- package/dist/replace.d.ts +22 -0
- package/dist/replace.d.ts.map +1 -0
- package/dist/replace.js +141 -0
- package/dist/replace.js.map +1 -0
- package/dist/stage3d.d.ts +25 -0
- package/dist/stage3d.d.ts.map +1 -0
- package/dist/stage3d.js +90 -0
- package/dist/stage3d.js.map +1 -0
- package/package.json +38 -3
- package/src/air-files.ts +371 -0
- package/src/air-items.ts +217 -0
- package/src/air.ts +497 -0
- package/src/config.ts +244 -0
- package/src/element.ts +902 -0
- package/src/external.ts +338 -0
- package/src/index.ts +33 -0
- package/src/layout.ts +127 -0
- package/src/playback.ts +388 -0
- package/src/replace.ts +166 -0
- package/src/stage3d.ts +112 -0
package/src/playback.ts
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
// A player shown in its element: the renderer and its canvas, sized to
|
|
2
|
+
// the element and the device's pixels, the pointer, the keys and the
|
|
3
|
+
// focus, and the loop that plays and draws, all of which destroy lets go.
|
|
4
|
+
|
|
5
|
+
import { bindActivation, bindKeyboard, type Player, type Scripting } from "@swf2es/player";
|
|
6
|
+
import type { RenderView } from "@swf2es/player/scene";
|
|
7
|
+
import { place, type ScaleMode, viewport } from "./layout.js";
|
|
8
|
+
import type { Stage3DSession } from "./stage3d.js";
|
|
9
|
+
|
|
10
|
+
/** How the element shows its player, read again as the page changes them. */
|
|
11
|
+
export interface Look {
|
|
12
|
+
scale: ScaleMode;
|
|
13
|
+
salign: string | null;
|
|
14
|
+
/** "transparent" leaves the stage's background out, so the page shows through. */
|
|
15
|
+
wmode: string;
|
|
16
|
+
/** The background the page gives, 0xRRGGBB, in place of the SWF's. */
|
|
17
|
+
bgcolor: number | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class Playback {
|
|
21
|
+
private frame = 0;
|
|
22
|
+
private last: number | null = null;
|
|
23
|
+
/**
|
|
24
|
+
* The ms the last call to advance took, by which the player tells frames
|
|
25
|
+
* that overran. Drawing is left out: it costs the same however many frames
|
|
26
|
+
* a call plays, and counted with one it would keep the player at one a call.
|
|
27
|
+
*/
|
|
28
|
+
private busy = 0;
|
|
29
|
+
private drawn = -1;
|
|
30
|
+
/** Something the host changed, a size or a look, which the next frame draws though the player has not. */
|
|
31
|
+
private dirty = true;
|
|
32
|
+
private dpr = 0;
|
|
33
|
+
private readonly resize: ResizeObserver;
|
|
34
|
+
private readonly unbind: (() => void)[] = [];
|
|
35
|
+
private destroyed = false;
|
|
36
|
+
/** The stage's quality it last drew by. */
|
|
37
|
+
private quality = "";
|
|
38
|
+
|
|
39
|
+
private constructor(
|
|
40
|
+
private readonly box: HTMLElement,
|
|
41
|
+
private readonly player: Player,
|
|
42
|
+
private readonly scripting: Scripting | null,
|
|
43
|
+
private readonly view: RenderView,
|
|
44
|
+
private readonly look: () => Look,
|
|
45
|
+
private readonly report: (error: unknown) => void,
|
|
46
|
+
private readonly stage3D: Stage3DSession | null,
|
|
47
|
+
) {
|
|
48
|
+
const canvas = view.canvas;
|
|
49
|
+
canvas.style.position = "absolute";
|
|
50
|
+
canvas.style.display = "block";
|
|
51
|
+
if (stage3D) {
|
|
52
|
+
canvas.style.zIndex = "1";
|
|
53
|
+
}
|
|
54
|
+
box.append(canvas);
|
|
55
|
+
if (scripting) {
|
|
56
|
+
scripting.drawer = view;
|
|
57
|
+
if (stage3D) {
|
|
58
|
+
scripting.onStageLayout = this.stageLayout;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// An AVM1 movie's BitmapData draws display objects by it too.
|
|
63
|
+
if (player.avm1) {
|
|
64
|
+
player.avm1.drawer = view;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
this.unbind.push(this.view.bindPointer(player));
|
|
68
|
+
// The element's keys alone, as a plug-in had them only while focused.
|
|
69
|
+
const host = (box.getRootNode() as ShadowRoot).host ?? box;
|
|
70
|
+
this.unbind.push(bindKeyboard(player, host));
|
|
71
|
+
// Its keys' releases are lost when the window or the element loses focus: the player lets them go.
|
|
72
|
+
this.unbind.push(bindActivation(player, window, host as HTMLElement));
|
|
73
|
+
this.resize = new ResizeObserver(() => this.layout());
|
|
74
|
+
this.resize.observe(box);
|
|
75
|
+
this.layout();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A renderer for `player` in `box` (docs/renderer.md), which takes WebGL 2.
|
|
80
|
+
* The canvas is not multisampled: the stage is drawn into buffers that
|
|
81
|
+
* are, but at low quality, which Flash draws aliased. It keeps alpha where
|
|
82
|
+
* it may ever be left clear, for a transparent wmode or Stage3D's layers
|
|
83
|
+
* below.
|
|
84
|
+
*/
|
|
85
|
+
static async create(
|
|
86
|
+
box: HTMLElement,
|
|
87
|
+
player: Player,
|
|
88
|
+
scripting: Scripting | null,
|
|
89
|
+
look: () => Look,
|
|
90
|
+
report: (error: unknown) => void,
|
|
91
|
+
stage3D: Stage3DSession | null = null,
|
|
92
|
+
): Promise<Playback> {
|
|
93
|
+
// The view makes its own tessellator, and throws where the page cannot run the render module.
|
|
94
|
+
const { RenderView } = await import("@swf2es/player/scene");
|
|
95
|
+
const view = new RenderView({
|
|
96
|
+
width: player.width,
|
|
97
|
+
height: player.height,
|
|
98
|
+
resolution: 1,
|
|
99
|
+
background: player.background,
|
|
100
|
+
transparent: !!stage3D || look().wmode === "transparent",
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
return new Playback(box, player, scripting, view, look, report, stage3D);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The renderer's WebGL context, null once destroyed. */
|
|
107
|
+
get gl(): WebGL2RenderingContext | null {
|
|
108
|
+
if (this.destroyed) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return this.view.gl;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Play and draw on every animation frame, from now. */
|
|
116
|
+
run(): void {
|
|
117
|
+
if (!this.destroyed && !this.frame) {
|
|
118
|
+
this.frame = requestAnimationFrame(this.tick);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Something the host shows differently: draw again on the next frame. */
|
|
123
|
+
changed(): void {
|
|
124
|
+
this.dirty = true;
|
|
125
|
+
this.layout();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** A host layer changed composition without changing stage placement. */
|
|
129
|
+
invalidate(): void {
|
|
130
|
+
this.dirty = true;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private readonly tick = (now: number) => {
|
|
134
|
+
this.frame = requestAnimationFrame(this.tick);
|
|
135
|
+
const dt = this.last === null ? 0 : now - this.last;
|
|
136
|
+
this.last = now;
|
|
137
|
+
// Moved to a display of another density, or zoomed: drawn anew for its pixels.
|
|
138
|
+
if (devicePixelRatio !== this.dpr) {
|
|
139
|
+
this.layout();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const start = performance.now();
|
|
143
|
+
try {
|
|
144
|
+
this.player.advance(dt, this.busy);
|
|
145
|
+
} catch (error) {
|
|
146
|
+
this.report(error);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
this.busy = performance.now() - start;
|
|
150
|
+
|
|
151
|
+
if (this.player.quality !== this.quality) {
|
|
152
|
+
this.requality();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (this.player.changes !== this.drawn || this.dirty) {
|
|
156
|
+
this.draw();
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Drawn by the stage's quality as a script or the page changes it:
|
|
162
|
+
* Flash's low draws edges aliased and smooths no bitmap; medium, high and
|
|
163
|
+
* best anti-alias the edges, as the GPU's multisampling does.
|
|
164
|
+
*/
|
|
165
|
+
private requality(): void {
|
|
166
|
+
this.quality = this.player.quality;
|
|
167
|
+
this.resample();
|
|
168
|
+
this.dirty = true;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* The samples the stage and the filter layers take, by the quality and the
|
|
173
|
+
* canvas's pixels: 4 at medium, 8 at high and 16 at best. Flash covers an
|
|
174
|
+
* edge with 16 at high, but 8 look alike but on the longest, shallowest
|
|
175
|
+
* edges, and cost half the memory; best takes Flash's 16. Every
|
|
176
|
+
* sample costs its pixels' 4 bytes of colour and 4 of depth and stencil, so
|
|
177
|
+
* a large canvas takes fewer: at most what fits STAGE_BYTES, and 4 at least,
|
|
178
|
+
* as before samples could be chosen. Those are a stage pixel's: a canvas
|
|
179
|
+
* finer than the stage, as at a device pixel ratio of 2 or 3, takes fewer
|
|
180
|
+
* for each of its pixels (denseSamples). Only as fine as the screen makes
|
|
181
|
+
* it, though: a stage zoomed into a large window on a screen of ratio 1
|
|
182
|
+
* shows each canvas pixel at its full size, where 4 levels of coverage
|
|
183
|
+
* show as rough edges.
|
|
184
|
+
*/
|
|
185
|
+
private resample(): void {
|
|
186
|
+
const canvas = this.view.canvas;
|
|
187
|
+
const quality = this.player.quality;
|
|
188
|
+
this.view.quality = quality;
|
|
189
|
+
this.view.samples = stageSamples(
|
|
190
|
+
quality,
|
|
191
|
+
canvas.width * canvas.height,
|
|
192
|
+
this.view.canvasResolution,
|
|
193
|
+
devicePixelRatio || 1,
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private draw(): void {
|
|
198
|
+
const look = this.look();
|
|
199
|
+
// A script may set Stage.color, which moves `changes`: read it as it draws.
|
|
200
|
+
const color = look.bgcolor ?? this.player.background;
|
|
201
|
+
const transparent = look.wmode === "transparent";
|
|
202
|
+
const overlay = this.stage3D?.active ?? false;
|
|
203
|
+
// A transparent clear must contain no RGB for the browser's premultiplied
|
|
204
|
+
// compositor; the stage color belongs below the opaque Stage3D viewports.
|
|
205
|
+
const background = overlay ? 0 : color;
|
|
206
|
+
const clear = transparent || overlay;
|
|
207
|
+
// What showAll leaves around the stage is the stage's colour, as in Flash.
|
|
208
|
+
this.box.style.background = transparent ? "" : `#${color.toString(16).padStart(6, "0")}`;
|
|
209
|
+
this.drawn = this.player.changes;
|
|
210
|
+
this.dirty = false;
|
|
211
|
+
this.view.background = background;
|
|
212
|
+
this.view.transparent = clear;
|
|
213
|
+
this.view.render(this.player.stage);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* The SWF set its stage's scaleMode or align, or began: placed again, and
|
|
218
|
+
* the page told by a stagelayout event, once the script that set it
|
|
219
|
+
* returns, so that no listener runs inside it.
|
|
220
|
+
*/
|
|
221
|
+
private readonly stageLayout = () => {
|
|
222
|
+
this.layout();
|
|
223
|
+
const scripting = this.scripting;
|
|
224
|
+
if (!scripting) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
queueMicrotask(() => {
|
|
229
|
+
if (this.destroyed) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const detail = { scaleMode: scripting.stageScaleMode, align: scripting.stageAlign };
|
|
234
|
+
this.box.dispatchEvent(
|
|
235
|
+
new CustomEvent("stagelayout", { bubbles: true, composed: true, detail }),
|
|
236
|
+
);
|
|
237
|
+
});
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
/** The canvas placed in the box by the look, and drawn at the device's pixels. */
|
|
241
|
+
private layout(): void {
|
|
242
|
+
if (this.destroyed) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const look = this.look();
|
|
247
|
+
const scripting = this.stage3D ? this.scripting : null;
|
|
248
|
+
scripting?.setStageDefaults(look.scale, look.salign ?? "");
|
|
249
|
+
const mode = scripting?.stageScaleMode ?? look.scale;
|
|
250
|
+
const align = scripting?.stageAlign ?? look.salign;
|
|
251
|
+
// Live noScale makes the stage the box's size; every other mode keeps the SWF's.
|
|
252
|
+
const noScale = !!scripting && mode === "noScale";
|
|
253
|
+
const stageWidth = noScale ? this.box.clientWidth : this.player.width;
|
|
254
|
+
const stageHeight = noScale ? this.box.clientHeight : this.player.height;
|
|
255
|
+
this.dpr = devicePixelRatio || 1;
|
|
256
|
+
const placed = place(
|
|
257
|
+
this.player.width || 1,
|
|
258
|
+
this.player.height || 1,
|
|
259
|
+
this.box.clientWidth,
|
|
260
|
+
this.box.clientHeight,
|
|
261
|
+
mode,
|
|
262
|
+
align,
|
|
263
|
+
this.dpr,
|
|
264
|
+
!!scripting,
|
|
265
|
+
);
|
|
266
|
+
const view = viewport(
|
|
267
|
+
this.player.width || 1,
|
|
268
|
+
this.player.height || 1,
|
|
269
|
+
this.box.clientWidth,
|
|
270
|
+
this.box.clientHeight,
|
|
271
|
+
mode,
|
|
272
|
+
placed,
|
|
273
|
+
);
|
|
274
|
+
const canvas = this.view.canvas;
|
|
275
|
+
canvas.style.left = `${view.css.x}px`;
|
|
276
|
+
canvas.style.top = `${view.css.y}px`;
|
|
277
|
+
canvas.style.width = `${view.css.width}px`;
|
|
278
|
+
canvas.style.height = `${view.css.height}px`;
|
|
279
|
+
const screenWidth = Math.max(1, view.width);
|
|
280
|
+
const screenHeight = Math.max(1, view.height);
|
|
281
|
+
// Compared in the canvas's pixels, which resize rounds the screen to: a fractional
|
|
282
|
+
// screen would otherwise never compare equal and resize on every layout.
|
|
283
|
+
if (
|
|
284
|
+
view.resolution !== this.view.canvasResolution ||
|
|
285
|
+
canvas.width !== Math.round(screenWidth * view.resolution) ||
|
|
286
|
+
canvas.height !== Math.round(screenHeight * view.resolution)
|
|
287
|
+
) {
|
|
288
|
+
this.view.resize(screenWidth, screenHeight, view.resolution);
|
|
289
|
+
this.resample();
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// AVM1's _xmouse is measured in the screen's pixels, as Flash Player measures it.
|
|
293
|
+
if (this.player.avm1) {
|
|
294
|
+
this.player.avm1.pixelRatio = placed.resolution;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Flash takes the pointer in the margins too, at stage coordinates past its edges.
|
|
298
|
+
if (this.scripting) {
|
|
299
|
+
this.scripting.stageViewportX = view.x;
|
|
300
|
+
this.scripting.stageViewportY = view.y;
|
|
301
|
+
this.scripting.stageViewportWidth = screenWidth;
|
|
302
|
+
this.scripting.stageViewportHeight = screenHeight;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
this.view.setViewport(view.x, view.y, screenWidth, screenHeight);
|
|
306
|
+
this.stage3D?.layout(placed, this.player.width, this.player.height);
|
|
307
|
+
this.dirty = true;
|
|
308
|
+
// Dispatch only after placement is current: a resize listener may set
|
|
309
|
+
// another layout or reconfigure its Stage3D back buffer synchronously.
|
|
310
|
+
scripting?.resizeStage(stageWidth, stageHeight);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** Stop the loop and let go of the renderer, its GL context, the canvas and the input. */
|
|
314
|
+
destroy(): void {
|
|
315
|
+
if (this.destroyed) {
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
this.destroyed = true;
|
|
320
|
+
cancelAnimationFrame(this.frame);
|
|
321
|
+
this.resize.disconnect();
|
|
322
|
+
for (const unbind of this.unbind.splice(0)) {
|
|
323
|
+
unbind();
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (this.scripting?.drawer === this.view) {
|
|
327
|
+
this.scripting.drawer = null;
|
|
328
|
+
}
|
|
329
|
+
if (this.scripting?.onStageLayout === this.stageLayout) {
|
|
330
|
+
this.scripting.onStageLayout = null;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (this.player.avm1?.drawer === this.view) {
|
|
334
|
+
this.player.avm1.drawer = null;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
this.view.destroy();
|
|
338
|
+
this.view.canvas.remove();
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** The most a canvas's multisampled buffers may take, colour and depth-stencil together. */
|
|
343
|
+
const STAGE_BYTES = 256 * 1024 * 1024;
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* The samples a canvas of `pixels` takes at `quality`, at `resolution` of its
|
|
347
|
+
* pixels to a stage pixel on a screen of `ratio` device pixels to a CSS pixel.
|
|
348
|
+
*/
|
|
349
|
+
export function stageSamples(
|
|
350
|
+
quality: string,
|
|
351
|
+
pixels: number,
|
|
352
|
+
resolution: number,
|
|
353
|
+
ratio: number,
|
|
354
|
+
): number {
|
|
355
|
+
const samples = quality === "MEDIUM" ? 4 : quality === "HIGH" ? 8 : 16;
|
|
356
|
+
// Fewer where a stage pixel spans several of the screen's, not where it is only zoomed.
|
|
357
|
+
return denseSamples(Math.min(samples, fittingSamples(pixels)), Math.min(resolution, ratio));
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* The samples a pixel of the canvas takes for `samples` a stage pixel, at
|
|
362
|
+
* `resolution` of the canvas's pixels to a stage pixel, at most the
|
|
363
|
+
* screen's pixels to a CSS pixel: where a stage pixel
|
|
364
|
+
* spans several, as many as cover it with as many samples as at one, a
|
|
365
|
+
* power of two, 4 at least and `samples` at most. At three device pixels a
|
|
366
|
+
* stage pixel, 4 samples each are 36 a stage pixel, more than best's 16 at
|
|
367
|
+
* one; 8 would double the multisampled canvas's memory traffic for edges a
|
|
368
|
+
* third of a stage pixel wide.
|
|
369
|
+
*/
|
|
370
|
+
export function denseSamples(samples: number, resolution: number): number {
|
|
371
|
+
const wanted = samples / Math.max(1, resolution * resolution);
|
|
372
|
+
let dense = 4;
|
|
373
|
+
while (dense < wanted) {
|
|
374
|
+
dense *= 2;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
return Math.min(samples, dense);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** The most samples, a power of two and 4 at least, whose buffers for `pixels` fit STAGE_BYTES. */
|
|
381
|
+
export function fittingSamples(pixels: number): number {
|
|
382
|
+
let samples = 16;
|
|
383
|
+
while (samples > 4 && pixels * samples * 8 > STAGE_BYTES) {
|
|
384
|
+
samples /= 2;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
return samples;
|
|
388
|
+
}
|
package/src/replace.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// A page's Flash tags, <object> and <embed>, swapped for <swf2es-player>
|
|
2
|
+
// with what they told Flash: the movie, its FlashVars, how it is shown
|
|
3
|
+
// and scripted, its size, and the id and name the page's scripts find it by.
|
|
4
|
+
import { defineElement, type Swf2esPlayerElement, TAG } from "./element.js";
|
|
5
|
+
|
|
6
|
+
const FLASH_TYPES = new Set([
|
|
7
|
+
"application/x-shockwave-flash",
|
|
8
|
+
"application/futuresplash",
|
|
9
|
+
"application/vnd.adobe.flash.movie",
|
|
10
|
+
]);
|
|
11
|
+
const FLASH_CLASSID = "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000";
|
|
12
|
+
|
|
13
|
+
/** The parameters the element takes, by their lower-case names in a tag. */
|
|
14
|
+
const CARRIED = [
|
|
15
|
+
"flashvars",
|
|
16
|
+
"wmode",
|
|
17
|
+
"scale",
|
|
18
|
+
"salign",
|
|
19
|
+
"base",
|
|
20
|
+
"allowscriptaccess",
|
|
21
|
+
"allownetworking",
|
|
22
|
+
"quality",
|
|
23
|
+
"bgcolor",
|
|
24
|
+
"menu",
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
/** Attributes the element keeps as they were, for the page's styles and scripts. */
|
|
28
|
+
const KEPT = ["id", "name", "class", "style", "title", "width", "height", "align"];
|
|
29
|
+
|
|
30
|
+
const isSwfUrl = (url: string | null) => /\.swf$/i.test((url ?? "").replace(/[?#].*$/s, ""));
|
|
31
|
+
|
|
32
|
+
/** The <param>s directly in an <object>, by lower-case name. */
|
|
33
|
+
function params(object: Element): Map<string, string> {
|
|
34
|
+
const found = new Map<string, string>();
|
|
35
|
+
for (const child of object.children) {
|
|
36
|
+
const name = child.getAttribute("name")?.toLowerCase();
|
|
37
|
+
if (child.localName === "param" && name && !found.has(name)) {
|
|
38
|
+
found.set(name, child.getAttribute("value") ?? "");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return found;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Whether `element` is a Flash <object> or <embed>: by its type, its ActiveX class, or a .swf for its movie. */
|
|
46
|
+
export function isFlash(element: Element): boolean {
|
|
47
|
+
const type = element.getAttribute("type")?.toLowerCase().trim() ?? "";
|
|
48
|
+
if (element.localName === "embed") {
|
|
49
|
+
return FLASH_TYPES.has(type) || isSwfUrl(element.getAttribute("src"));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (element.localName !== "object") {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const p = params(element);
|
|
57
|
+
return (
|
|
58
|
+
FLASH_TYPES.has(type) ||
|
|
59
|
+
element.getAttribute("classid")?.toLowerCase().trim() === FLASH_CLASSID ||
|
|
60
|
+
isSwfUrl(element.getAttribute("data")) ||
|
|
61
|
+
isSwfUrl(p.get("movie") ?? p.get("src") ?? null)
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* What a Flash tag and the Flash tags nested in it as fallbacks tell, the
|
|
67
|
+
* outer first, as attributes of the element: an IE <object> with <param>s
|
|
68
|
+
* often wraps an <embed> with the same as attributes, or with what the
|
|
69
|
+
* <object> left out.
|
|
70
|
+
*/
|
|
71
|
+
export function flashAttributes(outer: Element): Map<string, string> {
|
|
72
|
+
const attributes = new Map<string, string>();
|
|
73
|
+
const set = (name: string, value: string | null | undefined) => {
|
|
74
|
+
if (value !== null && value !== undefined && !attributes.has(name)) {
|
|
75
|
+
attributes.set(name, value);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const tags = [outer, ...outer.querySelectorAll("object, embed")].filter(isFlash);
|
|
79
|
+
for (const tag of tags) {
|
|
80
|
+
const p = tag.localName === "object" ? params(tag) : new Map<string, string>();
|
|
81
|
+
const at = (name: string) => tag.getAttribute(name);
|
|
82
|
+
set(
|
|
83
|
+
"src",
|
|
84
|
+
tag.localName === "embed" ? at("src") : (p.get("movie") ?? p.get("src") ?? at("data")),
|
|
85
|
+
);
|
|
86
|
+
for (const name of CARRIED) {
|
|
87
|
+
set(name, p.get(name) ?? at(name));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
for (const name of KEPT) {
|
|
91
|
+
set(name, at(name));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return attributes;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Swap each Flash <object> and <embed> under `root` for a <swf2es-player>
|
|
100
|
+
* carrying its parameters, in its place: the outermost of nested ones, its
|
|
101
|
+
* fallbacks going with it. Returns the elements made.
|
|
102
|
+
*/
|
|
103
|
+
export function replaceFlash(root: ParentNode = document): Swf2esPlayerElement[] {
|
|
104
|
+
// Where it cannot be defined, a tag swapped for it would play nothing.
|
|
105
|
+
if (!defineElement()) {
|
|
106
|
+
return [];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const made: Swf2esPlayerElement[] = [];
|
|
110
|
+
const tags = [...root.querySelectorAll("object, embed")];
|
|
111
|
+
if (root instanceof Element && (root.localName === "object" || root.localName === "embed")) {
|
|
112
|
+
tags.unshift(root);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
for (const tag of tags) {
|
|
116
|
+
// A fallback of an outer one goes with it.
|
|
117
|
+
const outer = tag.parentElement?.closest("object, embed");
|
|
118
|
+
if (!isFlash(tag) || (outer && isFlash(outer))) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const attributes = flashAttributes(tag);
|
|
123
|
+
if (!attributes.get("src")) {
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const player = document.createElement(TAG) as Swf2esPlayerElement;
|
|
128
|
+
for (const [name, value] of attributes) {
|
|
129
|
+
player.setAttribute(name, value);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
tag.replaceWith(player);
|
|
133
|
+
made.push(player);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return made;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* replaceFlash now, and again for each Flash tag the page adds under `root`
|
|
141
|
+
* later, until the returned function is called.
|
|
142
|
+
*/
|
|
143
|
+
export function watchFlash(root: ParentNode & Node = document): () => void {
|
|
144
|
+
replaceFlash(root);
|
|
145
|
+
const observer = new MutationObserver((mutations) => {
|
|
146
|
+
for (const mutation of mutations) {
|
|
147
|
+
// A tag added, or one made Flash after it was added, its src or type set by a script.
|
|
148
|
+
const added = mutation.type === "attributes" ? [mutation.target] : mutation.addedNodes;
|
|
149
|
+
for (const node of added) {
|
|
150
|
+
// A <param>'s tag is its <object>.
|
|
151
|
+
const tag =
|
|
152
|
+
node instanceof Element && node.localName === "param" ? node.parentElement : node;
|
|
153
|
+
if (tag instanceof Element && tag.isConnected) {
|
|
154
|
+
replaceFlash(tag);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
observer.observe(root, {
|
|
160
|
+
childList: true,
|
|
161
|
+
subtree: true,
|
|
162
|
+
attributes: true,
|
|
163
|
+
attributeFilter: ["src", "type", "data", "classid", "value"],
|
|
164
|
+
});
|
|
165
|
+
return () => observer.disconnect();
|
|
166
|
+
}
|
package/src/stage3d.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { Scripting } from "@swf2es/player";
|
|
2
|
+
import { installContext3D } from "@swf2es/player/context3d";
|
|
3
|
+
import { type Stage3DSurface, stage3DCanvasHost } from "@swf2es/player-hosts/stage3d";
|
|
4
|
+
import type { Placement } from "./layout.js";
|
|
5
|
+
|
|
6
|
+
/** A fresh, exclusively owned surface for each context requested by a player. */
|
|
7
|
+
export type Stage3DSurfaceFactory = (slot: number) => Stage3DSurface;
|
|
8
|
+
|
|
9
|
+
/** One load's Stage3D layers, below its transparent display-list canvas. */
|
|
10
|
+
export class Stage3DSession {
|
|
11
|
+
private readonly layer = document.createElement("div");
|
|
12
|
+
private readonly slots = new Map<number, { x: number; y: number; visible: boolean }>();
|
|
13
|
+
private readonly host: ReturnType<typeof stage3DCanvasHost>;
|
|
14
|
+
private readonly close: () => void;
|
|
15
|
+
private destroyed = false;
|
|
16
|
+
private x = 0;
|
|
17
|
+
private y = 0;
|
|
18
|
+
private sx = 1;
|
|
19
|
+
private sy = 1;
|
|
20
|
+
private readonly box: HTMLElement;
|
|
21
|
+
private readonly isolation: string;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
box: HTMLElement,
|
|
25
|
+
scripting: Scripting,
|
|
26
|
+
create: Stage3DSurfaceFactory,
|
|
27
|
+
changed: () => void,
|
|
28
|
+
) {
|
|
29
|
+
this.box = box;
|
|
30
|
+
this.isolation = box.style.isolation;
|
|
31
|
+
this.layer.style.cssText = "position:absolute;inset:0;pointer-events:none;z-index:0";
|
|
32
|
+
const host = stage3DCanvasHost(this.layer, create);
|
|
33
|
+
this.host = host;
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
box.append(this.layer);
|
|
37
|
+
// Keep canvases from different overlapping players in separate stacks.
|
|
38
|
+
box.style.isolation = "isolate";
|
|
39
|
+
this.close = installContext3D(scripting, {
|
|
40
|
+
create: (slot, lifecycle) => {
|
|
41
|
+
const context = host.create(slot, lifecycle);
|
|
42
|
+
this.slots.set(slot, { x: 0, y: 0, visible: true });
|
|
43
|
+
changed();
|
|
44
|
+
|
|
45
|
+
return context;
|
|
46
|
+
},
|
|
47
|
+
place: (slot, x, y, visible) => {
|
|
48
|
+
const position = this.slots.get(slot);
|
|
49
|
+
if (position) {
|
|
50
|
+
position.x = x;
|
|
51
|
+
position.y = y;
|
|
52
|
+
position.visible = visible;
|
|
53
|
+
this.place(slot, position);
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
remove: (slot) => {
|
|
57
|
+
host.remove(slot);
|
|
58
|
+
if (this.slots.delete(slot) && !this.destroyed) {
|
|
59
|
+
changed();
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
} catch (error) {
|
|
64
|
+
this.layer.remove();
|
|
65
|
+
box.style.isolation = this.isolation;
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
get active(): boolean {
|
|
71
|
+
return this.slots.size !== 0;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
layout(placed: Placement, width: number, height: number): void {
|
|
75
|
+
this.x = placed.x;
|
|
76
|
+
this.y = placed.y;
|
|
77
|
+
this.sx = placed.width / (width || 1);
|
|
78
|
+
this.sy = placed.height / (height || 1);
|
|
79
|
+
|
|
80
|
+
// AIR transforms viewport positions with the stage, but keeps configured
|
|
81
|
+
// back-buffer sizes in pixels instead of scaling the entire surface.
|
|
82
|
+
for (let slot = 0; slot < 4; slot++) {
|
|
83
|
+
const position = this.slots.get(slot);
|
|
84
|
+
if (position) {
|
|
85
|
+
this.place(slot, position);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private place(slot: number, position: { x: number; y: number; visible: boolean }): void {
|
|
91
|
+
this.host.place(
|
|
92
|
+
slot,
|
|
93
|
+
this.x + position.x * this.sx,
|
|
94
|
+
this.y + position.y * this.sy,
|
|
95
|
+
position.visible,
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
destroy(): void {
|
|
100
|
+
if (this.destroyed) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
this.destroyed = true;
|
|
105
|
+
try {
|
|
106
|
+
this.close();
|
|
107
|
+
} finally {
|
|
108
|
+
this.layer.remove();
|
|
109
|
+
this.box.style.isolation = this.isolation;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|