Skip to content

Commit

Permalink
avm2/core: Use EventObject::progress_event when constructing progress…
Browse files Browse the repository at this point in the history
… events
  • Loading branch information
Lord-McSweeney authored and Lord-McSweeney committed Jan 14, 2025
1 parent 7c63d74 commit ec8f226
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 108 deletions.
26 changes: 9 additions & 17 deletions core/src/avm2/globals/flash/media/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
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;
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};

Expand Down Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions core/src/avm2/globals/flash/net/file_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
4 changes: 2 additions & 2 deletions core/src/avm2/object/event_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ impl<'gc> EventObject<'gc> {
pub fn progress_event<S>(
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>
Expand Down
74 changes: 26 additions & 48 deletions core/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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,
);
Expand Down
26 changes: 10 additions & 16 deletions core/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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);
}
Expand Down
42 changes: 17 additions & 25 deletions core/src/socket.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit ec8f226

Please sign in to comment.