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: Remove incorrect properties from display object prototypes (fix #768) #6921

Merged
merged 6 commits into from
May 10, 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 @@ -1788,7 +1788,7 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {
let target_clip = self.resolve_target_display_object(start_clip, target, true)?;

if let Some(target_clip) = target_clip {
crate::avm1::globals::display_object::remove_display_object(target_clip, self);
crate::avm1::globals::remove_display_object(target_clip, self);
} else {
avm_warn!(self, "RemoveSprite: Source is not a display object");
}
Expand Down
47 changes: 46 additions & 1 deletion core/src/avm1/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::avm1::function::{Executable, FunctionObject};
use crate::avm1::property::Attribute;
use crate::avm1::property_decl::{define_properties_on, Declaration};
use crate::avm1::{Object, ScriptObject, TObject, Value};
use crate::display_object::{DisplayObject, Lists, TDisplayObject, TDisplayObjectContainer};
use crate::string::{AvmString, WStr, WString};
use gc_arena::Collect;
use gc_arena::MutationContext;
Expand All @@ -26,7 +27,6 @@ pub(crate) mod context_menu_item;
pub mod convolution_filter;
mod date;
pub mod displacement_map_filter;
pub(crate) mod display_object;
pub mod drop_shadow_filter;
pub(crate) mod error;
mod external_interface;
Expand Down Expand Up @@ -1245,6 +1245,51 @@ pub fn create_globals<'gc>(
)
}

/// Depths used/returned by ActionScript are offset by this amount from depths used inside the SWF/by the VM.
/// The depth of objects placed on the timeline in the Flash IDE start from 0 in the SWF,
/// but are negative when queried from MovieClip.getDepth().
/// Add this to convert from AS -> SWF depth.
const AVM_DEPTH_BIAS: i32 = 16384;

/// The maximum depth that the AVM will allow you to swap or attach clips to.
/// What is the derivation of this number...?
const AVM_MAX_DEPTH: i32 = 2_130_706_428;

/// The maximum depth that the AVM will allow you to remove clips from.
/// What is the derivation of this number...?
const AVM_MAX_REMOVE_DEPTH: i32 = 2_130_706_416;

fn get_depth<'gc>(
activation: &mut Activation<'_, 'gc, '_>,
this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(display_object) = this.as_display_object() {
if activation.swf_version() >= 6 {
let depth = display_object.depth().wrapping_sub(AVM_DEPTH_BIAS);
return Ok(depth.into());
}
}
Ok(Value::Undefined)
}

pub fn remove_display_object<'gc>(
this: DisplayObject<'gc>,
activation: &mut Activation<'_, 'gc, '_>,
) {
let depth = this.depth().wrapping_sub(0);
// Can only remove positive depths (when offset by the AVM depth bias).
// Generally this prevents you from removing non-dynamically created clips,
// although you can get around it with swapDepths.
// TODO: Figure out the derivation of this range.
if depth >= AVM_DEPTH_BIAS && depth < AVM_MAX_REMOVE_DEPTH && !this.removed() {
// Need a parent to remove from.
if let Some(mut parent) = this.avm1_parent().and_then(|o| o.as_movie_clip()) {
parent.remove_child(&mut activation.context, this, Lists::all());
}
}
}

#[cfg(test)]
#[allow(clippy::unreadable_literal)]
mod tests {
Expand Down
7 changes: 2 additions & 5 deletions core/src/avm1/globals/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

use crate::avm1::activation::Activation;
use crate::avm1::error::Error;
use crate::avm1::globals::display_object;
use crate::avm1::property_decl::{define_properties_on, Declaration};
use crate::avm1::{Object, ScriptObject, TObject, Value};
use crate::avm1::{globals, Object, ScriptObject, TObject, Value};
use crate::display_object::{Avm1Button, TDisplayObject};
use gc_arena::MutationContext;

Expand Down Expand Up @@ -37,6 +36,7 @@ macro_rules! button_setter {

const PROTO_DECLS: &[Declaration] = declare_properties! {
"enabled" => property(button_getter!(enabled), button_setter!(set_enabled));
"getDepth" => method(globals::get_depth; DONT_ENUM | DONT_DELETE | READ_ONLY; version(6));
"useHandCursor" => property(button_getter!(use_hand_cursor), button_setter!(set_use_hand_cursor));
};

Expand All @@ -46,10 +46,7 @@ pub fn create_proto<'gc>(
fn_proto: Object<'gc>,
) -> Object<'gc> {
let object = ScriptObject::object(gc_context, Some(proto));

display_object::define_display_object_proto(gc_context, object, fn_proto);
define_properties_on(PROTO_DECLS, gc_context, object, fn_proto);

object.into()
}

Expand Down
170 changes: 0 additions & 170 deletions core/src/avm1/globals/display_object.rs

This file was deleted.

6 changes: 3 additions & 3 deletions core/src/avm1/globals/movie_clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use crate::avm1::activation::Activation;
use crate::avm1::error::Error;
use crate::avm1::globals::display_object::{self, AVM_DEPTH_BIAS, AVM_MAX_DEPTH};
use crate::avm1::globals::matrix::gradient_object_to_matrix;
use crate::avm1::globals::{self, AVM_DEPTH_BIAS, AVM_MAX_DEPTH};
use crate::avm1::property_decl::{define_properties_on, Declaration};
use crate::avm1::{self, Object, ScriptObject, TObject, Value};
use crate::avm_error;
Expand Down Expand Up @@ -70,6 +70,7 @@ const PROTO_DECLS: &[Declaration] = declare_properties! {
"getBounds" => method(mc_method!(get_bounds); DONT_ENUM | DONT_DELETE);
"getBytesLoaded" => method(mc_method!(get_bytes_loaded); DONT_ENUM | DONT_DELETE);
"getBytesTotal" => method(mc_method!(get_bytes_total); DONT_ENUM | DONT_DELETE);
"getDepth" => method(globals::get_depth; DONT_ENUM | DONT_DELETE | READ_ONLY; version(6));
"getInstanceAtDepth" => method(mc_method!(get_instance_at_depth); DONT_ENUM | DONT_DELETE; version(7));
"getNextHighestDepth" => method(mc_method!(get_next_highest_depth); DONT_ENUM | DONT_DELETE; version(7));
"getRect" => method(mc_method!(get_rect); DONT_ENUM | DONT_DELETE; version(8));
Expand Down Expand Up @@ -167,7 +168,6 @@ pub fn create_proto<'gc>(
fn_proto: Object<'gc>,
) -> Object<'gc> {
let object = ScriptObject::object(gc_context, Some(proto));
display_object::define_display_object_proto(gc_context, object, fn_proto);
define_properties_on(PROTO_DECLS, gc_context, object, fn_proto);
object.into()
}
Expand Down Expand Up @@ -1042,7 +1042,7 @@ fn remove_movie_clip<'gc>(
// `removeMovieClip` can remove all types of display object,
// e.g. `MovieClip.prototype.removeMovieClip.apply(textField);`
if let Some(this) = this.as_display_object() {
crate::avm1::globals::display_object::remove_display_object(this, activation);
crate::avm1::globals::remove_display_object(this, activation);
}

Ok(Value::Undefined)
Expand Down
7 changes: 3 additions & 4 deletions core/src/avm1/globals/text_field.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::avm1::activation::Activation;
use crate::avm1::error::Error;
use crate::avm1::globals::display_object;
use crate::avm1::object::text_format_object::TextFormatObject;
use crate::avm1::property_decl::{define_properties_on, Declaration};
use crate::avm1::{Object, ScriptObject, TObject, Value};
use crate::avm1::{globals, Object, ScriptObject, TObject, Value};
use crate::avm_error;
use crate::display_object::{AutoSizeMode, EditText, TDisplayObject, TextSelection};
use crate::font::round_down_to_pixel;
Expand Down Expand Up @@ -66,6 +65,7 @@ const PROTO_DECLS: &[Declaration] = declare_properties! {
"borderColor" => property(tf_getter!(border_color), tf_setter!(set_border_color));
"bottomScroll" => property(tf_getter!(bottom_scroll));
"embedFonts" => property(tf_getter!(embed_fonts), tf_setter!(set_embed_fonts));
"getDepth" => method(globals::get_depth; DONT_ENUM | DONT_DELETE | READ_ONLY; version(6));
"hscroll" => property(tf_getter!(hscroll), tf_setter!(set_hscroll));
"html" => property(tf_getter!(html), tf_setter!(set_html));
"htmlText" => property(tf_getter!(html_text), tf_setter!(set_html_text));
Expand Down Expand Up @@ -100,7 +100,6 @@ pub fn create_proto<'gc>(
fn_proto: Object<'gc>,
) -> Object<'gc> {
let object = ScriptObject::object(gc_context, Some(proto));
display_object::define_display_object_proto(gc_context, object, fn_proto);
define_properties_on(PROTO_DECLS, gc_context, object, fn_proto);
object.into()
}
Expand Down Expand Up @@ -260,7 +259,7 @@ pub fn remove_text_field<'gc>(
activation: &mut Activation<'_, 'gc, '_>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
display_object::remove_display_object(text_field.into(), activation);
globals::remove_display_object(text_field.into(), activation);
Ok(Value::Undefined)
}

Expand Down
6 changes: 1 addition & 5 deletions core/src/avm1/globals/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use crate::avm1::activation::Activation;
use crate::avm1::error::Error;
use crate::avm1::globals::display_object;
use crate::avm1::object::Object;
use crate::avm1::value::Value;
use crate::avm1::ScriptObject;
Expand All @@ -20,11 +19,8 @@ pub fn constructor<'gc>(
pub fn create_proto<'gc>(
gc_context: MutationContext<'gc, '_>,
proto: Object<'gc>,
fn_proto: Object<'gc>,
_fn_proto: Object<'gc>,
) -> Object<'gc> {
let object = ScriptObject::object(gc_context, Some(proto));

display_object::define_display_object_proto(gc_context, object, fn_proto);

object.into()
}
Loading