web: Construct SwfMovie with spoofed URL when loaded from data

This commit is contained in:
nosamu 2023-01-22 03:48:14 -06:00 committed by Nathan Adams
parent c6eb25194e
commit cfa3363416
4 changed files with 28 additions and 4 deletions

View File

@ -316,4 +316,11 @@ export interface DataLoadOptions extends BaseLoadOptions {
* The data to load a movie from. * The data to load a movie from.
*/ */
data: Iterable<number>; data: Iterable<number>;
/**
* The filename of the SWF movie to provide to ActionScript.
*
* @default "movie.swf"
*/
swfFileName?: string;
} }

View File

@ -640,7 +640,8 @@ export class RufflePlayer extends HTMLElement {
console.log("Loading SWF data"); console.log("Loading SWF data");
this.instance!.load_data( this.instance!.load_data(
new Uint8Array(options.data), new Uint8Array(options.data),
sanitizeParameters(options.parameters) sanitizeParameters(options.parameters),
options.swfFileName || "movie.swf"
); );
} }
} catch (err) { } catch (err) {

View File

@ -144,7 +144,7 @@ async function loadFile(file) {
} }
hideSample(); hideSample();
const data = await new Response(file).arrayBuffer(); const data = await new Response(file).arrayBuffer();
load({ data, ...defaultConfig }); load({ data: data, swfFileName: file.name, ...defaultConfig });
} }
function loadSample() { function loadSample() {

View File

@ -29,6 +29,7 @@ use std::{cell::RefCell, error::Error, num::NonZeroI32};
use tracing_subscriber::layer::{Layered, SubscriberExt}; use tracing_subscriber::layer::{Layered, SubscriberExt};
use tracing_subscriber::registry::Registry; use tracing_subscriber::registry::Registry;
use tracing_wasm::{WASMLayer, WASMLayerConfigBuilder}; use tracing_wasm::{WASMLayer, WASMLayerConfigBuilder};
use url::Url;
use wasm_bindgen::{prelude::*, JsCast, JsValue}; use wasm_bindgen::{prelude::*, JsCast, JsValue};
use web_sys::{ use web_sys::{
AddEventListenerOptions, Element, Event, EventTarget, HtmlCanvasElement, HtmlElement, AddEventListenerOptions, Element, Event, EventTarget, HtmlCanvasElement, HtmlElement,
@ -240,8 +241,23 @@ impl Ruffle {
/// Play an arbitrary movie on this instance. /// Play an arbitrary movie on this instance.
/// ///
/// This method should only be called once per player. /// This method should only be called once per player.
pub fn load_data(&mut self, swf_data: Uint8Array, parameters: JsValue) -> Result<(), JsValue> { pub fn load_data(
let mut movie = SwfMovie::from_data(&swf_data.to_vec(), None, None) &mut self,
swf_data: Uint8Array,
parameters: JsValue,
swf_name: String,
) -> Result<(), JsValue> {
let window = web_sys::window().ok_or("Expected window")?;
let mut url = Url::from_str(&window.location().href()?)
.map_err(|e| format!("Error creating url: {e}"))?;
url.set_query(None);
url.set_fragment(None);
if let Ok(mut segments) = url.path_segments_mut() {
segments.pop();
segments.push(&swf_name);
}
let mut movie = SwfMovie::from_data(&swf_data.to_vec(), Some(url.to_string()), None)
.map_err(|e| format!("Error loading movie: {e}"))?; .map_err(|e| format!("Error loading movie: {e}"))?;
movie.append_parameters(parse_movie_parameters(&parameters)); movie.append_parameters(parse_movie_parameters(&parameters));