From ec8f2262ab9cb1ff79b2e078d5738ccd681a51b6 Mon Sep 17 00:00:00 2001 From: Lord-McSweeney Date: Mon, 30 Dec 2024 23:39:10 -0800 Subject: [PATCH] avm2/core: Use EventObject::progress_event when constructing progress events --- core/src/avm2/globals/flash/media/sound.rs | 26 +++---- .../avm2/globals/flash/net/file_reference.rs | 2 + core/src/avm2/object/event_object.rs | 4 +- core/src/loader.rs | 74 +++++++------------ core/src/player.rs | 26 +++---- core/src/socket.rs | 42 +++++------ 6 files changed, 66 insertions(+), 108 deletions(-) diff --git a/core/src/avm2/globals/flash/media/sound.rs b/core/src/avm2/globals/flash/media/sound.rs index c372de5a2227..e334ae071d15 100644 --- a/core/src/avm2/globals/flash/media/sound.rs +++ b/core/src/avm2/globals/flash/media/sound.rs @@ -2,7 +2,7 @@ use crate::avm2::activation::Activation; use crate::avm2::globals::slots::flash_net_url_request as url_request_slots; -use crate::avm2::object::{QueuedPlay, SoundChannelObject, TObject}; +use crate::avm2::object::{EventObject, QueuedPlay, SoundChannelObject, TObject}; use crate::avm2::parameters::ParametersExt; use crate::avm2::value::Value; use crate::avm2::Avm2; @@ -10,7 +10,6 @@ use crate::avm2::Error; use crate::backend::navigator::Request; use crate::character::Character; use crate::display_object::SoundTransform; -use crate::string::AvmString; use crate::{avm2_stub_getter, avm2_stub_method}; use swf::{SoundEvent, SoundInfo}; @@ -284,21 +283,14 @@ pub fn load_compressed_data_from_byte_array<'gc>( Error::RustError(format!("Failed to register sound from bytearray: {e:?}").into()) })?; - let progress_evt = activation - .avm2() - .classes() - .progressevent - .construct( - activation, - &[ - "progress".into(), - false.into(), - false.into(), - bytes.len().into(), - bytes.len().into(), - ], - ) - .map_err(|e| Error::AvmError(AvmString::new_utf8(activation.gc(), e.to_string()).into()))?; + let progress_evt = EventObject::progress_event( + activation, + "progress", + bytes.len(), + bytes.len(), + false, + false, + ); Avm2::dispatch_event(activation.context, progress_evt, this); diff --git a/core/src/avm2/globals/flash/net/file_reference.rs b/core/src/avm2/globals/flash/net/file_reference.rs index 49ff0c3c3bba..51e00597492d 100644 --- a/core/src/avm2/globals/flash/net/file_reference.rs +++ b/core/src/avm2/globals/flash/net/file_reference.rs @@ -222,6 +222,8 @@ pub fn load<'gc>( FileReference::FileDialogResult(ref dialog_result) => dialog_result.size().unwrap_or(0), }; + let size = size as usize; + let open_evt = EventObject::bare_default_event(activation.context, "open"); Avm2::dispatch_event(activation.context, open_evt, this.into()); diff --git a/core/src/avm2/object/event_object.rs b/core/src/avm2/object/event_object.rs index be53af909fd3..efedad7ec360 100644 --- a/core/src/avm2/object/event_object.rs +++ b/core/src/avm2/object/event_object.rs @@ -279,8 +279,8 @@ impl<'gc> EventObject<'gc> { pub fn progress_event( activation: &mut Activation<'_, 'gc>, event_type: S, - bytes_loaded: u64, - bytes_total: u64, + bytes_loaded: usize, + bytes_total: usize, bubbles: bool, cancelable: bool, ) -> Value<'gc> diff --git a/core/src/loader.rs b/core/src/loader.rs index 43dd92bd010d..1080e291c5b9 100644 --- a/core/src/loader.rs +++ b/core/src/loader.rs @@ -1623,21 +1623,14 @@ impl<'gc> Loader<'gc> { // FIXME - we should fire "progress" events as we receive data, not // just at the end - let progress_evt = activation - .avm2() - .classes() - .progressevent - .construct( - &mut activation, - &[ - "progress".into(), - false.into(), - false.into(), - total_len.into(), - total_len.into(), - ], - ) - .unwrap(); + let progress_evt = Avm2EventObject::progress_event( + &mut activation, + "progress", + total_len, + total_len, + false, + false, + ); Avm2::dispatch_event(activation.context, progress_evt, target); @@ -1843,21 +1836,14 @@ impl<'gc> Loader<'gc> { // FIXME - As in load_url_loader, we should fire "progress" events as we receive data, // not just at the end - let progress_evt = activation - .avm2() - .classes() - .progressevent - .construct( - &mut activation, - &[ - "progress".into(), - false.into(), - false.into(), - total_len.into(), - total_len.into(), - ], - ) - .unwrap(); + let progress_evt = Avm2EventObject::progress_event( + &mut activation, + "progress", + total_len, + total_len, + false, + false, + ); Avm2::dispatch_event(activation.context, progress_evt, sound_object); @@ -2388,21 +2374,14 @@ impl<'gc> Loader<'gc> { MovieLoaderVMData::Avm2 { loader_info, .. } => { let mut activation = Avm2Activation::from_nothing(uc); - let progress_evt = activation - .avm2() - .classes() - .progressevent - .construct( - &mut activation, - &[ - "progress".into(), - false.into(), - false.into(), - cur_len.into(), - total_len.into(), - ], - ) - .unwrap(); + let progress_evt = Avm2EventObject::progress_event( + &mut activation, + "progress", + cur_len, + total_len, + false, + false, + ); Avm2::dispatch_event(uc, progress_evt, loader_info.into()); } @@ -2934,12 +2913,11 @@ impl<'gc> Loader<'gc> { target_object.into(), ); - let size = data.len() as u64; let progress_evt = Avm2EventObject::progress_event( &mut activation, "progress", - size, - size, + data.len(), + data.len(), false, false, ); diff --git a/core/src/player.rs b/core/src/player.rs index 58451fc343c1..e507d9b184e5 100644 --- a/core/src/player.rs +++ b/core/src/player.rs @@ -5,7 +5,8 @@ use crate::avm1::SystemProperties; use crate::avm1::VariableDumper; use crate::avm1::{Activation, ActivationIdentifier}; use crate::avm1::{TObject, Value}; -use crate::avm2::{Activation as Avm2Activation, Avm2, CallStack, Object as Avm2Object}; +use crate::avm2::object::{EventObject as Avm2EventObject, Object as Avm2Object}; +use crate::avm2::{Activation as Avm2Activation, Avm2, CallStack}; use crate::backend::ui::FontDefinition; use crate::backend::{ audio::{AudioBackend, AudioManager}, @@ -1881,21 +1882,14 @@ impl Player { if let Some(loader_info) = root.loader_info().filter(|_| !was_root_movie_loaded) { let mut activation = Avm2Activation::from_nothing(context); - let progress_evt = activation - .avm2() - .classes() - .progressevent - .construct( - &mut activation, - &[ - "progress".into(), - false.into(), - false.into(), - root.compressed_loaded_bytes().into(), - root.compressed_total_bytes().into(), - ], - ) - .unwrap(); + let progress_evt = Avm2EventObject::progress_event( + &mut activation, + "progress", + root.compressed_loaded_bytes() as usize, + root.compressed_total_bytes() as usize, + false, + false, + ); Avm2::dispatch_event(context, progress_evt, loader_info); } diff --git a/core/src/socket.rs b/core/src/socket.rs index 083d5ab105a2..7489c02eb030 100644 --- a/core/src/socket.rs +++ b/core/src/socket.rs @@ -1,13 +1,13 @@ -use crate::{ - avm1::{ - globals::xml_socket::XmlSocket, Activation as Avm1Activation, ActivationIdentifier, - ExecutionReason, Object as Avm1Object, TObject as Avm1TObject, - }, - avm2::{object::SocketObject, Activation as Avm2Activation, Avm2, EventObject}, - backend::navigator::NavigatorBackend, - context::UpdateContext, - string::AvmString, +use crate::avm1::{ + globals::xml_socket::XmlSocket, Activation as Avm1Activation, ActivationIdentifier, + ExecutionReason, Object as Avm1Object, TObject as Avm1TObject, }; +use crate::avm2::object::{EventObject, SocketObject}; +use crate::avm2::{Activation as Avm2Activation, Avm2}; +use crate::backend::navigator::NavigatorBackend; +use crate::context::UpdateContext; +use crate::string::AvmString; + use async_channel::{unbounded, Receiver, Sender as AsyncSender, Sender}; use gc_arena::Collect; use slotmap::{new_key_type, SlotMap}; @@ -306,22 +306,14 @@ impl<'gc> Sockets<'gc> { let bytes_loaded = data.len(); target.read_buffer().extend(data); - let progress_evt = activation - .avm2() - .classes() - .progressevent - .construct( - &mut activation, - &[ - "socketData".into(), - false.into(), - false.into(), - bytes_loaded.into(), - //NOTE: bytesTotal is not used by socketData event. - 0.into(), - ], - ) - .expect("ProgressEvent should be constructed"); + let progress_evt = EventObject::progress_event( + &mut activation, + "socketData", + bytes_loaded, + 0, // NOTE: bytesTotal is not used by socketData event. + false, + false, + ); Avm2::dispatch_event(activation.context, progress_evt, target.into()); }