Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avm1: Correct MovieClipLoader methods #6004

Merged
merged 5 commits into from
Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/avm1/activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2810,7 +2810,7 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {
let level: DisplayObject<'_> =
MovieClip::new(self.base_clip().movie().unwrap(), self.context.gc_context).into();

level.set_depth(self.context.gc_context, level_id as i32);
level.set_depth(self.context.gc_context, level_id);
level.set_default_root_name(&mut self.context);
self.context
.stage
Expand Down
165 changes: 98 additions & 67 deletions core/src/avm1/globals/movie_clip_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::avm1::property::Attribute;
use crate::avm1::property_decl::{define_properties_on, Declaration};
use crate::avm1::{ArrayObject, Object, Value};
use crate::backend::navigator::RequestOptions;
use crate::display_object::{DisplayObject, TDisplayObject};
use crate::display_object::{TDisplayObject, TDisplayObjectContainer};
use gc_arena::MutationContext;

const PROTO_DECLS: &[Declaration] = declare_properties! {
Expand Down Expand Up @@ -37,96 +37,127 @@ pub fn constructor<'gc>(
Ok(this.into())
}

pub fn load_clip<'gc>(
fn load_clip<'gc>(
activation: &mut Activation<'_, 'gc, '_>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
let url_val = args.get(0).cloned().unwrap_or(Value::Undefined);
let url = url_val.coerce_to_string(activation)?;
let target = args.get(1).cloned().unwrap_or(Value::Undefined);

if let Value::Object(target) = target {
if let Some(mc) = target
.as_display_object()
.and_then(|dobj| dobj.as_movie_clip())
{
let fetch = activation
.context
.navigator
.fetch(&url.to_utf8_lossy(), RequestOptions::get());
let process = activation.context.load_manager.load_movie_into_clip(
activation.context.player.clone().unwrap(),
DisplayObject::MovieClip(mc),
fetch,
url.to_string(),
None,
Some(this),
);

activation.context.navigator.spawn_future(process);
if let [url, target, ..] = args {
if let Value::String(url) = url {
let target = match target {
Value::String(_) => {
let start_clip = activation.target_clip_or_root()?;
activation.resolve_target_display_object(start_clip, *target, true)?
}
Value::Number(level_id) => {
// Levels are rounded down.
// TODO: What happens with negative levels?
Some(activation.resolve_level(*level_id as i32))
}
Value::Object(object) => object.as_display_object(),
_ => None,
};
Comment on lines +47 to +59
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe extract this logic into a fn since it's used by several of the methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, they have subtle differences:

  • load_clip uses resolve_level.
  • unload_clip and get_progress use child_by_depth
  • get_progress returns undefined for non-DisplayObject objects.

It might be possible, but I doubt how better it will be.

if let Some(target) = target {
let fetch = activation
.context
.navigator
.fetch(&url.to_utf8_lossy(), RequestOptions::get());
let process = activation.context.load_manager.load_movie_into_clip(
activation.context.player.clone().unwrap(),
target,
fetch,
url.to_string(),
None,
Some(this),
);
activation.context.navigator.spawn_future(process);

return Ok(true.into());
}
}

Ok(true.into())
} else {
Ok(false.into())
return Ok(false.into());
}

Ok(Value::Undefined)
}

pub fn unload_clip<'gc>(
fn unload_clip<'gc>(
activation: &mut Activation<'_, 'gc, '_>,
_this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
let target = args.get(0).cloned().unwrap_or(Value::Undefined);

if let Value::Object(target) = target {
if let Some(mut mc) = target
.as_display_object()
.and_then(|dobj| dobj.as_movie_clip())
{
mc.unload(&mut activation.context);
mc.replace_with_movie(activation.context.gc_context, None);

if let [target, ..] = args {
let target = match target {
Value::String(_) => {
let start_clip = activation.target_clip_or_root()?;
activation.resolve_target_display_object(start_clip, *target, true)?
}
Value::Number(level_id) => {
// Levels are rounded down.
// TODO: What happens with negative levels?
activation.context.stage.child_by_depth(*level_id as i32)
}
Value::Object(object) => object.as_display_object(),
_ => None,
};
if let Some(target) = target {
target.unload(&mut activation.context);
if let Some(mut mc) = target.as_movie_clip() {
mc.replace_with_movie(activation.context.gc_context, None);
}
return Ok(true.into());
}

return Ok(false.into());
}

Ok(false.into())
Ok(Value::Undefined)
}

pub fn get_progress<'gc>(
fn get_progress<'gc>(
activation: &mut Activation<'_, 'gc, '_>,
_this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
let target = args.get(0).cloned().unwrap_or(Value::Undefined);

if let Value::Object(target) = target {
if let Some(mc) = target
.as_display_object()
.and_then(|dobj| dobj.as_movie_clip())
{
let ret_obj = ScriptObject::object(activation.context.gc_context, None);
ret_obj.define_value(
activation.context.gc_context,
"bytesLoaded",
mc.movie()
.map(|mv| (mv.uncompressed_len()).into())
.unwrap_or(Value::Undefined),
Attribute::empty(),
);
ret_obj.define_value(
activation.context.gc_context,
"bytesTotal",
mc.movie()
.map(|mv| (mv.uncompressed_len()).into())
.unwrap_or(Value::Undefined),
Attribute::empty(),
);

return Ok(ret_obj.into());
if let [target, ..] = args {
let target = match target {
Value::String(_) => {
let start_clip = activation.target_clip_or_root()?;
activation.resolve_target_display_object(start_clip, *target, true)?
}
Value::Number(level_id) => {
// Levels are rounded down.
// TODO: What happens with negative levels?
activation.context.stage.child_by_depth(*level_id as i32)
}
Value::Object(object) => {
if let Some(object) = object.as_display_object() {
Some(object)
} else {
return Ok(Value::Undefined);
}
}
_ => return Ok(Value::Undefined),
};
let result = ScriptObject::bare_object(activation.context.gc_context);
if let Some(target) = target {
if let Some(movie) = target.movie() {
result.define_value(
activation.context.gc_context,
"bytesLoaded",
movie.compressed_len().into(),
Attribute::empty(),
);
result.define_value(
activation.context.gc_context,
"bytesTotal",
movie.compressed_len().into(),
Attribute::empty(),
);
}
}
return Ok(result.into());
}

Ok(Value::Undefined)
Expand Down
45 changes: 21 additions & 24 deletions core/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,10 @@ impl<'gc> Loader<'gc> {

replacing_root_movie = DisplayObject::ptr_eq(clip, uc.stage.root_clip());

clip.as_movie_clip().unwrap().unload(uc);

clip.as_movie_clip()
.unwrap()
.replace_with_movie(uc.gc_context, None);
if let Some(mut mc) = clip.as_movie_clip() {
mc.unload(uc);
mc.replace_with_movie(uc.gc_context, None);
}

if let Some(broadcaster) = broadcaster {
Avm1::run_stack_frame_for_method(
Expand Down Expand Up @@ -451,25 +450,23 @@ impl<'gc> Loader<'gc> {
);
}

let mut mc = clip
.as_movie_clip()
.expect("Attempted to load movie into not movie clip");

mc.replace_with_movie(uc.gc_context, Some(movie.clone()));
mc.post_instantiation(uc, clip, None, Instantiator::Movie, false);

let mut morph_shapes = fnv::FnvHashMap::default();
mc.preload(uc, &mut morph_shapes);

// Finalize morph shapes.
for (id, static_data) in morph_shapes {
let morph_shape = MorphShape::new(uc.gc_context, static_data);
uc.library
.library_for_movie_mut(movie.clone())
.register_character(
id,
crate::character::Character::MorphShape(morph_shape),
);
if let Some(mut mc) = clip.as_movie_clip() {
mc.replace_with_movie(uc.gc_context, Some(movie.clone()));
mc.post_instantiation(uc, clip, None, Instantiator::Movie, false);

let mut morph_shapes = fnv::FnvHashMap::default();
mc.preload(uc, &mut morph_shapes);

// Finalize morph shapes.
for (id, static_data) in morph_shapes {
let morph_shape = MorphShape::new(uc.gc_context, static_data);
uc.library
.library_for_movie_mut(movie.clone())
.register_character(
id,
crate::character::Character::MorphShape(morph_shape),
);
}
}

if let Some(broadcaster) = broadcaster {
Expand Down
32 changes: 30 additions & 2 deletions tests/tests/swfs/avm1/mcl_getprogress/output.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
Child movie loaded!
68
68
// loader.getProgress()
undefined

// loader.getProgress(obj)
undefined

// loader.getProgress("clip")
{ bytesLoaded: 68, bytesTotal: 68 }

// loader.getProgress("doesNotExist")
{ bytesLoaded: undefined, bytesTotal: undefined }

// loader.getProgress(0)
{ bytesLoaded: 502, bytesTotal: 502 }

// loader.getProgress(0.9)
{ bytesLoaded: 502, bytesTotal: 502 }

// loader.getProgress(1)
{ bytesLoaded: undefined, bytesTotal: undefined }

// loader.getProgress(clip)
{ bytesLoaded: 68, bytesTotal: 68 }

// loader.getProgress({})
undefined

// loader.getProgress(null)
undefined

Binary file modified tests/tests/swfs/avm1/mcl_getprogress/test.fla
Binary file not shown.
Binary file modified tests/tests/swfs/avm1/mcl_getprogress/test.swf
Binary file not shown.
Loading