2019-10-08 18:35:23 +00:00
|
|
|
use crate::avm1::object::{Attribute::*, Object};
|
2019-10-27 18:58:30 +00:00
|
|
|
use crate::avm1::{Avm1, UpdateContext, Value};
|
2019-10-26 22:04:52 +00:00
|
|
|
use crate::display_object::{DisplayNode, MovieClip};
|
2019-10-10 00:37:24 +00:00
|
|
|
use enumset::EnumSet;
|
2019-10-10 03:15:48 +00:00
|
|
|
use gc_arena::{GcCell, MutationContext};
|
2019-08-30 23:25:04 +00:00
|
|
|
|
|
|
|
macro_rules! with_movie_clip {
|
|
|
|
( $gc_context: ident, $object:ident, $($name:expr => $fn:expr),* ) => {{
|
|
|
|
$(
|
2019-09-27 13:46:53 +00:00
|
|
|
$object.force_set_function(
|
2019-08-30 23:25:04 +00:00
|
|
|
$name,
|
2019-09-03 00:44:24 +00:00
|
|
|
|_avm, _context, this, args| -> Value<'gc> {
|
2019-08-30 23:25:04 +00:00
|
|
|
if let Some(display_object) = this.read().display_node() {
|
|
|
|
if let Some(movie_clip) = display_object.read().as_movie_clip() {
|
|
|
|
return $fn(movie_clip, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Value::Undefined
|
|
|
|
},
|
|
|
|
$gc_context,
|
2019-10-08 18:35:23 +00:00
|
|
|
DontDelete | ReadOnly | DontEnum,
|
2019-08-30 23:25:04 +00:00
|
|
|
);
|
|
|
|
)*
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! with_movie_clip_mut {
|
|
|
|
( $gc_context: ident, $object:ident, $($name:expr => $fn:expr),* ) => {{
|
|
|
|
$(
|
2019-09-27 13:46:53 +00:00
|
|
|
$object.force_set_function(
|
2019-08-30 23:25:04 +00:00
|
|
|
$name,
|
2019-10-27 18:58:30 +00:00
|
|
|
|_avm, context: &mut UpdateContext<'_, 'gc, '_>, this, args| -> Value<'gc> {
|
2019-08-30 23:25:04 +00:00
|
|
|
if let Some(display_object) = this.read().display_node() {
|
2019-09-02 17:28:38 +00:00
|
|
|
if let Some(movie_clip) = display_object.write(context.gc_context).as_movie_clip_mut() {
|
2019-10-27 18:58:30 +00:00
|
|
|
return $fn(movie_clip, context, display_object, args);
|
2019-08-30 23:25:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Value::Undefined
|
2019-10-18 04:10:13 +00:00
|
|
|
} as crate::avm1::function::NativeFunction<'gc>,
|
2019-08-30 23:25:04 +00:00
|
|
|
$gc_context,
|
2019-10-08 18:35:23 +00:00
|
|
|
DontDelete | ReadOnly | DontEnum,
|
2019-08-30 23:25:04 +00:00
|
|
|
);
|
|
|
|
)*
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2019-10-10 03:15:48 +00:00
|
|
|
pub fn overwrite_root<'gc>(
|
|
|
|
_avm: &mut Avm1<'gc>,
|
2019-10-27 18:58:30 +00:00
|
|
|
ac: &mut UpdateContext<'_, 'gc, '_>,
|
2019-10-10 03:15:48 +00:00
|
|
|
this: GcCell<'gc, Object<'gc>>,
|
|
|
|
args: &[Value<'gc>],
|
|
|
|
) -> Value<'gc> {
|
|
|
|
let new_val = args
|
|
|
|
.get(0)
|
|
|
|
.map(|v| v.to_owned())
|
|
|
|
.unwrap_or(Value::Undefined);
|
|
|
|
this.write(ac.gc_context)
|
|
|
|
.force_set("_root", new_val, EnumSet::new());
|
|
|
|
|
|
|
|
Value::Undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn overwrite_global<'gc>(
|
|
|
|
_avm: &mut Avm1<'gc>,
|
2019-10-27 18:58:30 +00:00
|
|
|
ac: &mut UpdateContext<'_, 'gc, '_>,
|
2019-10-10 03:15:48 +00:00
|
|
|
this: GcCell<'gc, Object<'gc>>,
|
|
|
|
args: &[Value<'gc>],
|
|
|
|
) -> Value<'gc> {
|
|
|
|
let new_val = args
|
|
|
|
.get(0)
|
|
|
|
.map(|v| v.to_owned())
|
|
|
|
.unwrap_or(Value::Undefined);
|
|
|
|
this.write(ac.gc_context)
|
|
|
|
.force_set("_global", new_val, EnumSet::new());
|
|
|
|
|
|
|
|
Value::Undefined
|
|
|
|
}
|
|
|
|
|
2019-08-30 23:25:04 +00:00
|
|
|
pub fn create_movie_object<'gc>(gc_context: MutationContext<'gc, '_>) -> Object<'gc> {
|
2019-08-31 15:54:15 +00:00
|
|
|
let mut object = Object::object(gc_context);
|
2019-08-30 23:25:04 +00:00
|
|
|
|
|
|
|
with_movie_clip_mut!(
|
|
|
|
gc_context,
|
|
|
|
object,
|
2019-10-27 18:58:30 +00:00
|
|
|
"nextFrame" => |movie_clip: &mut MovieClip<'gc>, context: &mut UpdateContext<'_, 'gc, '_>, _cell: DisplayNode<'gc>, _args| {
|
|
|
|
movie_clip.next_frame(context);
|
2019-08-30 23:25:04 +00:00
|
|
|
Value::Undefined
|
|
|
|
},
|
2019-10-27 18:58:30 +00:00
|
|
|
"prevFrame" => |movie_clip: &mut MovieClip<'gc>, context: &mut UpdateContext<'_, 'gc, '_>, _cell: DisplayNode<'gc>, _args| {
|
|
|
|
movie_clip.prev_frame(context);
|
2019-08-30 23:25:04 +00:00
|
|
|
Value::Undefined
|
|
|
|
},
|
2019-10-27 18:58:30 +00:00
|
|
|
"play" => |movie_clip: &mut MovieClip<'gc>, _context: &mut UpdateContext<'_, 'gc, '_>, _cell: DisplayNode<'gc>, _args| {
|
2019-08-30 23:25:04 +00:00
|
|
|
movie_clip.play();
|
|
|
|
Value::Undefined
|
|
|
|
},
|
2019-10-27 18:58:30 +00:00
|
|
|
"stop" => |movie_clip: &mut MovieClip<'gc>, _context: &mut UpdateContext<'_, 'gc, '_>, _cell: DisplayNode<'gc>, _args| {
|
2019-08-30 23:25:04 +00:00
|
|
|
movie_clip.stop();
|
|
|
|
Value::Undefined
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
with_movie_clip!(
|
|
|
|
gc_context,
|
|
|
|
object,
|
2019-10-18 04:10:13 +00:00
|
|
|
"getBytesLoaded" => |_movie_clip: &MovieClip<'gc>, _args| {
|
2019-08-30 23:25:04 +00:00
|
|
|
// TODO find a correct value
|
2019-10-07 07:33:30 +00:00
|
|
|
Value::Number(1.0)
|
2019-08-30 23:25:04 +00:00
|
|
|
},
|
2019-10-18 04:10:13 +00:00
|
|
|
"getBytesTotal" => |_movie_clip: &MovieClip<'gc>, _args| {
|
2019-08-30 23:25:04 +00:00
|
|
|
// TODO find a correct value
|
2019-10-07 07:33:30 +00:00
|
|
|
Value::Number(1.0)
|
2019-08-30 23:25:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-10-10 00:37:24 +00:00
|
|
|
object.force_set_virtual(
|
|
|
|
"_global",
|
|
|
|
|avm, context, _this, _args| avm.global_object(context),
|
2019-10-10 03:15:48 +00:00
|
|
|
Some(overwrite_global),
|
2019-10-10 00:37:24 +00:00
|
|
|
EnumSet::new(),
|
|
|
|
);
|
|
|
|
|
|
|
|
object.force_set_virtual(
|
|
|
|
"_root",
|
|
|
|
|avm, context, _this, _args| avm.root_object(context),
|
2019-10-10 03:15:48 +00:00
|
|
|
Some(overwrite_root),
|
2019-10-10 00:37:24 +00:00
|
|
|
EnumSet::new(),
|
|
|
|
);
|
|
|
|
|
|
|
|
object.force_set_virtual(
|
|
|
|
"_parent",
|
|
|
|
|_avm, _context, this, _args| {
|
|
|
|
this.read()
|
|
|
|
.display_node()
|
|
|
|
.and_then(|mc| mc.read().parent())
|
|
|
|
.and_then(|dn| dn.read().object().as_object().ok())
|
|
|
|
.map(|o| Value::Object(o.to_owned()))
|
|
|
|
.unwrap_or(Value::Undefined)
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
EnumSet::new(),
|
|
|
|
);
|
|
|
|
|
2019-08-30 23:25:04 +00:00
|
|
|
object
|
|
|
|
}
|