2019-08-24 23:56:46 +00:00
|
|
|
import load_ruffle from "./load-ruffle";
|
2019-08-22 01:02:43 +00:00
|
|
|
import ruffle_shadow_template from "./shadow-template";
|
2019-10-20 17:04:22 +00:00
|
|
|
import { lookup_element } from "./register-element";
|
2019-08-22 01:02:43 +00:00
|
|
|
|
2019-08-26 00:11:10 +00:00
|
|
|
export let FLASH_MIMETYPE = "application/x-shockwave-flash";
|
|
|
|
export let FUTURESPLASH_MIMETYPE = "application/futuresplash";
|
|
|
|
export let FLASH_ACTIVEX_CLASSID = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000";
|
|
|
|
|
|
|
|
export class RufflePlayer extends HTMLElement {
|
2019-08-22 01:02:43 +00:00
|
|
|
constructor(...args) {
|
|
|
|
let self = super(...args);
|
|
|
|
|
|
|
|
self.shadow = self.attachShadow({mode: 'closed'});
|
|
|
|
self.shadow.appendChild(ruffle_shadow_template.content.cloneNode(true));
|
|
|
|
|
2019-08-25 19:46:38 +00:00
|
|
|
self.dynamic_styles = self.shadow.getElementById("dynamic_styles");
|
2019-08-22 01:02:43 +00:00
|
|
|
self.canvas = self.shadow.getElementById("player");
|
2019-08-24 23:56:46 +00:00
|
|
|
self.instance = null;
|
|
|
|
|
|
|
|
self.Ruffle = load_ruffle();
|
2019-08-22 01:02:43 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2019-08-25 19:46:38 +00:00
|
|
|
connectedCallback() {
|
|
|
|
this.update_styles();
|
|
|
|
}
|
|
|
|
|
|
|
|
attributeChangedCallback(name, oldValue, newValue) {
|
|
|
|
if (name === "width" || name === "height") {
|
|
|
|
this.update_styles();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 04:27:59 +00:00
|
|
|
disconnectedCallback() {
|
|
|
|
if (this.instance) {
|
|
|
|
this.instance.destroy();
|
|
|
|
this.instance = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 19:46:38 +00:00
|
|
|
update_styles() {
|
|
|
|
for (var i = 0; i < this.dynamic_styles.sheet.rules.length; i += 1) {
|
|
|
|
this.dynamic_styles.sheet.deleteRule(i);
|
|
|
|
}
|
|
|
|
|
2019-08-28 23:19:05 +00:00
|
|
|
if (this.attributes.width && !isNaN(parseInt(this.attributes.width.value))) {
|
2019-08-25 19:46:38 +00:00
|
|
|
this.dynamic_styles.sheet.insertRule(":host { width: " + this.attributes.width.value + "px; }");
|
|
|
|
}
|
|
|
|
|
2019-08-28 23:19:05 +00:00
|
|
|
if (this.attributes.height && !isNaN(parseInt(this.attributes.height.value))) {
|
2019-08-25 19:46:38 +00:00
|
|
|
this.dynamic_styles.sheet.insertRule(":host { height: " + this.attributes.height.value + "px; }");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 04:27:59 +00:00
|
|
|
/**
|
|
|
|
* Determine if this element is the fallback content of another Ruffle
|
|
|
|
* player.
|
|
|
|
*
|
|
|
|
* This heurustic assumes Ruffle objects will never use their fallback
|
|
|
|
* content. If this changes, then this code also needs to change.
|
|
|
|
*/
|
|
|
|
is_unused_fallback_object() {
|
|
|
|
let parent = this.parentNode;
|
2019-10-20 17:04:22 +00:00
|
|
|
let element = lookup_element("ruffle-object");
|
2019-08-26 04:27:59 +00:00
|
|
|
|
2019-10-20 17:04:22 +00:00
|
|
|
if (element !== null) {
|
|
|
|
do {
|
|
|
|
if (parent.nodeName === element.name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent = parent.parentNode;
|
|
|
|
} while (parent != document);
|
|
|
|
}
|
2019-08-26 04:27:59 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-24 23:56:46 +00:00
|
|
|
async stream_swf_url(url) {
|
2019-08-22 01:02:43 +00:00
|
|
|
//TODO: Actually stream files...
|
2019-08-24 23:56:46 +00:00
|
|
|
try {
|
2019-08-26 04:27:59 +00:00
|
|
|
if (this.isConnected && !this.is_unused_fallback_object()) {
|
|
|
|
let abs_url = new URL(url, window.location.href).toString();
|
|
|
|
console.log("Loading SWF file " + url);
|
|
|
|
|
|
|
|
let response = await fetch(abs_url);
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
let data = await response.arrayBuffer();
|
|
|
|
await this.play_swf_data(data);
|
|
|
|
console.log("Playing " + url);
|
|
|
|
} else {
|
|
|
|
console.error("SWF load failed: " + response.status + " " + response.statusText + " for " + url);
|
|
|
|
}
|
2019-08-22 01:02:43 +00:00
|
|
|
} else {
|
2019-08-26 04:27:59 +00:00
|
|
|
console.warn("Ignoring attempt to play a disconnected or suspended Ruffle element");
|
2019-08-22 01:02:43 +00:00
|
|
|
}
|
2019-08-24 23:56:46 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error("Serious error occured loading SWF file: " + err);
|
|
|
|
throw err;
|
|
|
|
}
|
2019-08-22 01:02:43 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 23:56:46 +00:00
|
|
|
async play_swf_data(data) {
|
2019-08-26 04:27:59 +00:00
|
|
|
if (this.isConnected && !this.is_unused_fallback_object()) {
|
|
|
|
console.log("Got SWF data");
|
2019-08-24 23:56:46 +00:00
|
|
|
|
2019-08-26 04:27:59 +00:00
|
|
|
if (this.instance) {
|
|
|
|
this.instance.destroy();
|
|
|
|
this.instance = null;
|
|
|
|
}
|
2019-08-22 01:02:43 +00:00
|
|
|
|
2019-08-26 04:27:59 +00:00
|
|
|
let Ruffle = await this.Ruffle.catch(function (e) {
|
|
|
|
console.error("Serious error loading Ruffle: " + e);
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.instance = Ruffle.new(this.canvas, new Uint8Array(data));
|
|
|
|
} else {
|
|
|
|
console.warn("Ignoring attempt to play a disconnected or suspended Ruffle element");
|
|
|
|
}
|
2019-08-22 01:02:43 +00:00
|
|
|
}
|
|
|
|
}
|