@swf2es/web 0.1.27 → 0.1.29

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/src/element.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  // <swf2es-player>: a SWF on a page as an <embed> showed one, with the
2
2
  // embedding's attributes, a load() and destroy() of its own, and the
3
3
  // SWF's ExternalInterface callbacks as methods on the element.
4
+ import type { Codegen } from "@swf2es/codegen";
4
5
  import { definesInput, definesSound, isAs3, readActions, readSwf, tags } from "@swf2es/format";
5
6
  import {
6
7
  type AirApplication,
@@ -8,6 +9,8 @@ import {
8
9
  type ExternalInterfaceHost,
9
10
  type FileSystemHost,
10
11
  type FullScreenHost,
12
+ type MenuItem,
13
+ nativeMemory,
11
14
  Player,
12
15
  Scripting,
13
16
  type WorkerHost,
@@ -16,7 +19,15 @@ import type { WebWorkerHost } from "@swf2es/player-hosts/workers";
16
19
  import { AIR_DIRECTORIES, AirApp, airAppForSwf, elementWindow, namesAir } from "./air.js";
17
20
  import { type AirFiles, airFiles } from "./air-files.js";
18
21
  import { type AirItems, airItems } from "./air-items.js";
19
- import { cssLength, parseColor, parseFlashVars, quality, scriptAccess } from "./attributes.js";
22
+ import {
23
+ cssLength,
24
+ parseColor,
25
+ parseFlashVars,
26
+ quality,
27
+ relativeBase,
28
+ relativeUrls,
29
+ scriptAccess,
30
+ } from "./attributes.js";
20
31
  import {
21
32
  captureHost,
22
33
  codegenForPlayer,
@@ -28,7 +39,18 @@ import {
28
39
  import { externalInterfaceHost, type PageBridge, type PageValue } from "./external.js";
29
40
  import { fetchFor, fetchMovie } from "./fetching.js";
30
41
  import { scaleMode } from "./layout.js";
31
- import { type Look, Playback } from "./playback.js";
42
+ import {
43
+ isMeterCorner,
44
+ METER_STYLE,
45
+ type MeterCorner,
46
+ nextCorner,
47
+ PerformanceMeter,
48
+ storeCorner,
49
+ storedCorner,
50
+ storedMeter,
51
+ storeMeter,
52
+ } from "./meter.js";
53
+ import { type FrameListener, type Look, Playback } from "./playback.js";
32
54
  import type { Stage3DSession } from "./stage3d.js";
33
55
  import { UncaughtReports } from "./uncaught.js";
34
56
  import { UNMUTE_STYLE, UnmutePrompt } from "./unmute.js";
@@ -71,6 +93,8 @@ const idOf = (e: HTMLElement): string => String(dom?.id?.call(e) ?? "");
71
93
 
72
94
  /** What one load holds, let go of all at once when the next load or destroy comes. */
73
95
  export interface Session {
96
+ /** The compiler its code is compiled by, an AS3 SWF's or an AVM1 SWF's; null for a SWF without code. */
97
+ codegen: Codegen | null;
74
98
  scripting: Scripting | null;
75
99
  player: Player | null;
76
100
  playback: Playback | null;
@@ -150,9 +174,20 @@ export class Swf2esPlayerElement extends ElementBase {
150
174
  "quality",
151
175
  "menu",
152
176
  "unmuteprompt",
177
+ "performancemeter",
153
178
  "allowcapture",
154
179
  ];
155
180
 
181
+ // For the tests alone, under a key no page types: whether anything hears the loop's frames,
182
+ // the page's frame listener or the meter, which the loop then measures for.
183
+ static {
184
+ Object.defineProperty(Swf2esPlayerElement.prototype, Symbol.for("swf2es.frameHook"), {
185
+ get(this: Swf2esPlayerElement): boolean {
186
+ return !!this.#session?.playback?.onFrame;
187
+ },
188
+ });
189
+ }
190
+
156
191
  #session: Session | null = null;
157
192
  #readiness = deferred();
158
193
  /** The source the element last loaded, to tell a new src from the one playing. */
@@ -163,6 +198,18 @@ export class Swf2esPlayerElement extends ElementBase {
163
198
  #stageSize: [number, number] | null = null;
164
199
  /** The button over the player while its sound is held, made the first time it is. */
165
200
  #prompt: UnmutePrompt | null = null;
201
+ #frameListener: FrameListener | null = null;
202
+ /** The performance meter, made the first time it shows. */
203
+ #meter: PerformanceMeter | null = null;
204
+ #meterShown = false;
205
+ /** Whether the meter shows by the viewer's choice, the menu's or the one kept, not a script's. */
206
+ #meterChosen = false;
207
+ /** The element's corner the meter sits in. */
208
+ #meterCorner: MeterCorner = "top-left";
209
+ /** Whether a script put the meter in its corner, which the viewer's kept corner then leaves. */
210
+ #cornerSet = false;
211
+ /** The page's frame listener and the meter's together, made once both listen. */
212
+ #bothListeners: FrameListener | null = null;
166
213
 
167
214
  constructor() {
168
215
  super();
@@ -172,7 +219,8 @@ export class Swf2esPlayerElement extends ElementBase {
172
219
  style.textContent =
173
220
  ":host { display: inline-block; position: relative; overflow: hidden; outline: none }" +
174
221
  " .box { position: absolute; inset: 0; overflow: hidden }" +
175
- UNMUTE_STYLE;
222
+ UNMUTE_STYLE +
223
+ METER_STYLE;
176
224
  this.#sizing = document.createElement("style");
177
225
  this.#box = document.createElement("div");
178
226
  this.#box.className = "box";
@@ -211,6 +259,216 @@ export class Swf2esPlayerElement extends ElementBase {
211
259
  return this.#session?.playback?.gl ?? null;
212
260
  }
213
261
 
262
+ /**
263
+ * Told after each animation frame what the player did in it: the SWF's
264
+ * frames played, its frame rate, and the ms its frames and the drawing
265
+ * took (FrameStats), for a page's performance meter. The object is the
266
+ * same each time, overwritten by the next frame's: copy what is kept.
267
+ * It holds for every SWF the element plays until set to null, as it
268
+ * starts; unset, the player measures nothing. There is one: setting it
269
+ * replaces the one before.
270
+ */
271
+ get frameListener(): FrameListener | null {
272
+ return this.#frameListener;
273
+ }
274
+
275
+ set frameListener(listener: FrameListener | null) {
276
+ this.#frameListener = typeof listener === "function" ? listener : null;
277
+ this.#listen();
278
+ }
279
+
280
+ /**
281
+ * Whether the performance meter shows over the player: fps against the
282
+ * SWF's rate, the player's ms in a frame, draw calls, the JS heap and the
283
+ * WebAssembly memories. Off by default. The context menu's "Show
284
+ * performance" turns it on and keeps the viewer's choice, for every
285
+ * element of the origin, in localStorage; setting this shows or hides it
286
+ * for the page alone, keeping nothing. The element fires
287
+ * `performancemeterchange` as it shows or hides.
288
+ */
289
+ get performanceMeter(): boolean {
290
+ return this.#meterShown;
291
+ }
292
+
293
+ set performanceMeter(on: boolean) {
294
+ this.#meterChosen = false;
295
+ this.#showMeter(Boolean(on));
296
+ }
297
+
298
+ /**
299
+ * The corner of the player element the meter sits in, which in a
300
+ * letterboxed or noScale player may be in the bars beside the stage:
301
+ * "top-left", the default, "top-right", "bottom-left" or "bottom-right";
302
+ * another value is ignored.
303
+ * The context menu's "Move performance meter" moves it to the next and
304
+ * keeps the viewer's choice; setting this moves it for the page alone,
305
+ * keeping nothing, and fires `performancemeterchange`.
306
+ */
307
+ get performanceMeterCorner(): MeterCorner {
308
+ return this.#meterCorner;
309
+ }
310
+
311
+ set performanceMeterCorner(corner: MeterCorner) {
312
+ if (isMeterCorner(corner)) {
313
+ this.#cornerSet = true;
314
+ this.#placeMeter(corner);
315
+ }
316
+ }
317
+
318
+ /** The meter moved to `corner`, the page told where `tell` and it moved. */
319
+ #placeMeter(corner: MeterCorner, tell = true): void {
320
+ if (corner === this.#meterCorner) {
321
+ return;
322
+ }
323
+
324
+ this.#meterCorner = corner;
325
+ this.#meter?.place(corner);
326
+ if (tell) {
327
+ this.#changedMeter();
328
+ }
329
+ }
330
+
331
+ #changedMeter(): void {
332
+ dispatch(
333
+ this,
334
+ new CustomEvent("performancemeterchange", {
335
+ detail: { shown: this.#meterShown, corner: this.#meterCorner },
336
+ }),
337
+ );
338
+ }
339
+
340
+ /**
341
+ * The viewer's kept choice applied where the menu offers the meter, as a
342
+ * SWF starts and as the offer changes: read then, not as the element is
343
+ * connected, so that a page's configure() after its markup holds. Not
344
+ * offered, a meter the viewer's choice showed goes; a script's stays.
345
+ */
346
+ #applyChoice(): void {
347
+ const shown = this.#meterShown;
348
+ const corner = this.#meterCorner;
349
+ if (!this.#cornerSet) {
350
+ this.#placeMeter(this.#meterOffered() ? storedCorner() : "top-left", false);
351
+ }
352
+
353
+ if (this.#meterOffered()) {
354
+ if (!this.#meterShown && storedMeter()) {
355
+ this.#meterChosen = true;
356
+ this.#showMeter(true, false);
357
+ }
358
+ } else if (this.#meterChosen && this.#meterShown) {
359
+ this.#meterChosen = false;
360
+ this.#showMeter(false, false);
361
+ }
362
+
363
+ // Told once, and only of what shows differently: not of a hidden meter's corner.
364
+ if (this.#meterShown !== shown || (this.#meterShown && this.#meterCorner !== corner)) {
365
+ this.#changedMeter();
366
+ }
367
+ }
368
+
369
+ /** The meter shown or hidden, the page told where `tell`. */
370
+ #showMeter(on: boolean, tell = true): void {
371
+ const parent = this.#box.parentNode;
372
+ if (on === this.#meterShown || !parent) {
373
+ return;
374
+ }
375
+
376
+ this.#meterShown = on;
377
+ if (on) {
378
+ this.#meter ??= new PerformanceMeter(parent, () => this.wasmMemory);
379
+ this.#meter.place(this.#meterCorner);
380
+ const playback = this.#session?.playback;
381
+ this.#meter.attach(playback?.gl ?? null, !!playback);
382
+ }
383
+
384
+ this.#meter?.show(on);
385
+ this.#listen();
386
+ if (tell) {
387
+ this.#changedMeter();
388
+ }
389
+ }
390
+
391
+ /**
392
+ * The context menu's items for the meter, where it offers them: one to
393
+ * show or hide it and, while it shows, one to move it to the next corner.
394
+ * Flash's menus had no submenus, nor has this one. Each is the viewer's own
395
+ * choice, kept for the next time.
396
+ */
397
+ #meterMenu(): MenuItem[] {
398
+ if (!this.#meterOffered()) {
399
+ return [];
400
+ }
401
+
402
+ const items: MenuItem[] = [
403
+ {
404
+ caption: this.#meterShown ? "Hide performance" : "Show performance",
405
+ enabled: true,
406
+ select: () => {
407
+ const on = !this.#meterShown;
408
+ storeMeter(on);
409
+ this.#meterChosen = on;
410
+ this.#showMeter(on);
411
+ },
412
+ },
413
+ ];
414
+ if (this.#meterShown) {
415
+ items.push({
416
+ caption: "Move performance meter",
417
+ enabled: true,
418
+ select: () => {
419
+ const corner = nextCorner(this.#meterCorner);
420
+ storeCorner(corner);
421
+ this.#cornerSet = false;
422
+ this.#placeMeter(corner);
423
+ },
424
+ });
425
+ }
426
+
427
+ return items;
428
+ }
429
+
430
+ /** Whether the context menu offers the meter, by the element's attribute or the page's configuration. */
431
+ #meterOffered(): boolean {
432
+ const own = attribute(this, "performancemeter")?.trim().toLowerCase();
433
+ return own === "true" || (own !== "false" && configuration().performanceMeter !== false);
434
+ }
435
+
436
+ /** The playback told of each frame by whichever listen, the page's listener and the meter's. */
437
+ #listen(): void {
438
+ const playback = this.#session?.playback;
439
+ if (playback) {
440
+ playback.onFrame = this.#frameHook();
441
+ }
442
+ }
443
+
444
+ #frameHook(): FrameListener | null {
445
+ const meter = this.#meterShown ? this.#meter : null;
446
+ if (!meter) {
447
+ return this.#frameListener;
448
+ }
449
+
450
+ if (!this.#frameListener) {
451
+ return meter.heard;
452
+ }
453
+
454
+ this.#bothListeners ??= (stats) => {
455
+ this.#meter?.heard(stats);
456
+ this.#frameListener?.(stats);
457
+ };
458
+ return this.#bothListeners;
459
+ }
460
+
461
+ /**
462
+ * The bytes of the WebAssembly memories behind the player: its compiler's
463
+ * (`codegen`, 0 for a SWF without code or before a load), and the native
464
+ * modules' (`native`: bitmap kernels, tessellators, decoders, zlib), which
465
+ * every player on the page shares, its Flash Workers' left out, as their
466
+ * own threads hold them. A memory grows and never shrinks.
467
+ */
468
+ get wasmMemory(): { codegen: number; native: number } {
469
+ return { codegen: this.#session?.codegen?.memoryBytes() ?? 0, native: nativeMemory() };
470
+ }
471
+
214
472
  get src(): string {
215
473
  return attribute(this, "src") ?? "";
216
474
  }
@@ -274,6 +532,13 @@ export class Swf2esPlayerElement extends ElementBase {
274
532
  return;
275
533
  }
276
534
 
535
+ // Taken from the viewer, the meter they turned on goes too, their choice kept for elsewhere;
536
+ // given back, it shows again.
537
+ if (name === "performancemeter") {
538
+ this.#applyChoice();
539
+ return;
540
+ }
541
+
277
542
  this.#session?.playback?.changed();
278
543
  }
279
544
 
@@ -311,6 +576,7 @@ export class Swf2esPlayerElement extends ElementBase {
311
576
 
312
577
  const ready = this.#readiness;
313
578
  const session: Session = {
579
+ codegen: null,
314
580
  scripting: null,
315
581
  player: null,
316
582
  playback: null,
@@ -368,6 +634,8 @@ export class Swf2esPlayerElement extends ElementBase {
368
634
  const base = attribute(this, "base");
369
635
  let bytes: Uint8Array;
370
636
  let url: string;
637
+ // From the URL it came from, where it came from one.
638
+ let fetched: string | null = null;
371
639
  if (session.air) {
372
640
  // The content SWF, by the app: URL adl names it with.
373
641
  const content = session.air.app.descriptor.content;
@@ -377,10 +645,11 @@ export class Swf2esPlayerElement extends ElementBase {
377
645
  throw new Error("swf2es: nothing to play");
378
646
  } else if (typeof source === "string" || source instanceof URL) {
379
647
  ({ bytes, url } = await fetchMovie(new URL(source, pageUrl).href, session.abort.signal));
648
+ fetched = url;
380
649
  } else {
381
- // A copy: the page may reuse its buffer.
650
+ // A copy: the page may reuse its buffer; its URL is `base`'s, else the page's.
382
651
  bytes = new Uint8Array(source instanceof Uint8Array ? source : new Uint8Array(source));
383
- url = base ? new URL(base, pageUrl).href : pageUrl;
652
+ url = relativeBase(base, null, pageUrl, "movie") ?? pageUrl;
384
653
  }
385
654
 
386
655
  if (!current()) {
@@ -396,6 +665,16 @@ export class Swf2esPlayerElement extends ElementBase {
396
665
  url = `app:/${encodeURIComponent(app.descriptor.content)}`;
397
666
  }
398
667
 
668
+ // Where every SWF's relative URLs resolve: an AIR application's each its own.
669
+ const baseUrl = session.air
670
+ ? null
671
+ : relativeBase(
672
+ base,
673
+ fetched,
674
+ pageUrl,
675
+ relativeUrls(attribute(this, "relativeurls"), configuration().relativeUrls),
676
+ );
677
+
399
678
  // What the application wrote and kept when it last played, before it plays.
400
679
  if (session.air) {
401
680
  await Promise.all([session.air.files.ready, session.air.items.ready]);
@@ -436,7 +715,8 @@ export class Swf2esPlayerElement extends ElementBase {
436
715
  }
437
716
 
438
717
  session.workers = host;
439
- session.scripting = this.#scripting(session, codegen, url, pageUrl, host);
718
+ session.codegen = codegen;
719
+ session.scripting = this.#scripting(session, codegen, url, baseUrl, pageUrl, host);
440
720
  if (stage3D && stage3DModule) {
441
721
  session.stage3D = new stage3DModule.Stage3DSession(
442
722
  this.#box,
@@ -463,16 +743,18 @@ export class Swf2esPlayerElement extends ElementBase {
463
743
  return;
464
744
  }
465
745
 
746
+ session.codegen = codegen;
747
+
466
748
  // Its URL keys its SharedObjects, as a page's AS3 SWFs' are. Its
467
749
  // fscommand, getURL and ExternalInterface reach the page as an AS3
468
750
  // SWF's do: "quit" goes to the page, as a plug-in's did, and stops
469
751
  // nothing.
470
752
  const { internal, offline, allows, objectID } = this.#access(pageUrl);
471
- const base = attribute(this, "base");
472
753
  avm1 = new Avm1(codegen, {
473
754
  url,
474
755
  parameters: parseFlashVars(attribute(this, "flashvars")),
475
- base: base ? new URL(base, pageUrl).href : undefined,
756
+ // Without a base of the page's, its own URL.
757
+ base: baseUrl ?? undefined,
476
758
  // XML, LoadVars and XMLSocket reach the network as an AS3 SWF's loads and sockets do.
477
759
  fetch: fetchFor(session, offline),
478
760
  socket: offline ? undefined : socketHost(),
@@ -513,6 +795,7 @@ export class Swf2esPlayerElement extends ElementBase {
513
795
  session.player.quality = quality(attribute(this, "quality"));
514
796
  session.player.showDefaultContextMenu = attribute(this, "menu")?.toLowerCase() !== "false";
515
797
  session.player.build = configuration().build ?? null;
798
+ session.player.hostMenu = () => this.#meterMenu();
516
799
  const playback = await Playback.create(
517
800
  this.#box,
518
801
  session.player,
@@ -527,6 +810,12 @@ export class Swf2esPlayerElement extends ElementBase {
527
810
  }
528
811
 
529
812
  session.playback = playback;
813
+ playback.onFrame = this.#frameHook();
814
+ if (this.#meterShown) {
815
+ this.#meter?.attach(playback.gl, true);
816
+ }
817
+
818
+ this.#applyChoice();
530
819
  // The workers started ahead, loaded before a script may wait for one;
531
820
  // a script that never says it loaded does not keep the SWF from playing.
532
821
  if (session.workers) {
@@ -608,14 +897,14 @@ export class Swf2esPlayerElement extends ElementBase {
608
897
  session: Session,
609
898
  codegen: Awaited<ReturnType<typeof codegenForPlayer>>,
610
899
  url: string,
900
+ base: string | null,
611
901
  pageUrl: string,
612
902
  workerHost: WorkerHost | null,
613
903
  ): Scripting {
614
904
  const { internal, offline, allows, objectID } = this.#access(pageUrl);
615
- const base = attribute(this, "base");
616
905
  const scripting = new Scripting(codegen, {
617
906
  url,
618
- base: base ? new URL(base, pageUrl).href : undefined,
907
+ base: base ?? undefined,
619
908
  parameters: parseFlashVars(attribute(this, "flashvars")),
620
909
  fetch: fetchFor(session, offline),
621
910
  loadPolicyFile: configuration().loadPolicyFile,
@@ -940,6 +1229,10 @@ export class Swf2esPlayerElement extends ElementBase {
940
1229
 
941
1230
  session.callbacks.clear();
942
1231
  session.abort.abort();
1232
+ if (session.playback) {
1233
+ this.#meter?.attach(null, false);
1234
+ }
1235
+
943
1236
  session.playback?.destroy();
944
1237
  session.stage3D?.destroy();
945
1238
  session.player?.destroy();
package/src/index.ts CHANGED
@@ -10,7 +10,7 @@ import { defineElement } from "./element.js";
10
10
  // SWFs out for.
11
11
  export { filtersGreyOutFiles } from "@swf2es/player";
12
12
  export { AirApp, type AirAppFile, openAirApp } from "./air.js";
13
- export { parseColor, parseFlashVars, scriptAccess } from "./attributes.js";
13
+ export { parseColor, parseFlashVars, type RelativeUrls, scriptAccess } from "./attributes.js";
14
14
  export {
15
15
  type Configuration,
16
16
  configuration,
@@ -21,6 +21,8 @@ export {
21
21
  export { defineElement, Swf2esPlayerElement, type SwfSource, TAG } from "./element.js";
22
22
  export { ExternalInterfaceError, type PageValue } from "./external.js";
23
23
  export { type Placement, place, type ScaleMode, scaleMode } from "./layout.js";
24
+ export type { MeterCorner } from "./meter.js";
25
+ export type { FrameListener, FrameStats } from "./playback.js";
24
26
  export { flashAttributes, isFlash, replaceFlash, watchFlash } from "./replace.js";
25
27
  export type { Stage3DSurfaceFactory, Stage3DSurfaceLoader } from "./stage3d.js";
26
28