avm2: The stage's loader info object allows access to `url`, `bytes`, and `parameters`.

This commit is contained in:
David Wendt 2022-01-15 18:59:48 -05:00 committed by Mike Welsh
parent b181debff6
commit f041cced72
1 changed files with 72 additions and 61 deletions

View File

@ -8,7 +8,7 @@ use crate::avm2::names::{Namespace, QName};
use crate::avm2::object::{loaderinfo_allocator, DomainObject, LoaderStream, Object, TObject};
use crate::avm2::value::Value;
use crate::avm2::{AvmString, Error};
use crate::display_object::TDisplayObject;
use crate::display_object::{TDisplayObject, TDisplayObjectContainer};
use gc_arena::{GcCell, MutationContext};
use swf::{write_swf, Compression};
@ -242,17 +242,21 @@ pub fn url<'gc>(
) -> Result<Value<'gc>, Error> {
if let Some(this) = this {
if let Some(loader_stream) = this.as_loader_stream() {
match &*loader_stream {
LoaderStream::Stage => {
return Err("Error: The stage's loader info does not have a URL".into())
}
LoaderStream::Swf(root, _) => {
let root = match &*loader_stream {
LoaderStream::Stage => activation
.context
.stage
.child_by_index(0)
.and_then(|c| c.movie()),
LoaderStream::Swf(root, _) => Some(root.clone()),
};
if let Some(root) = root {
let url = root.url().unwrap_or("");
return Ok(AvmString::new_utf8(activation.context.gc_context, url).into());
}
}
}
}
Ok(Value::Undefined)
}
@ -287,11 +291,16 @@ pub fn bytes<'gc>(
) -> Result<Value<'gc>, Error> {
if let Some(this) = this {
if let Some(loader_stream) = this.as_loader_stream() {
match &*loader_stream {
LoaderStream::Stage => {
return Err("Error: The stage's loader info does not have a bytestream".into())
}
LoaderStream::Swf(root, _) => {
let root = match &*loader_stream {
LoaderStream::Stage => activation
.context
.stage
.child_by_index(0)
.and_then(|c| c.movie()),
LoaderStream::Swf(root, _) => Some(root.clone()),
};
if let Some(root) = root {
let ba_class = activation.context.avm2.classes().bytearray;
let ba = ba_class.construct(activation, &[])?;
@ -316,8 +325,7 @@ pub fn bytes<'gc>(
// ourselves), so we need to overwrite it ourselves.
ba_write.set_position(4);
ba_write.set_endian(Endian::Little);
ba_write
.write_unsigned_int((root.data().len() + correct_header_length) as u32)?;
ba_write.write_unsigned_int((root.data().len() + correct_header_length) as u32)?;
// Finally, reset the array to the correct state.
ba_write.set_position(0);
@ -327,7 +335,6 @@ pub fn bytes<'gc>(
}
}
}
}
Ok(Value::Undefined)
}
@ -365,11 +372,16 @@ pub fn parameters<'gc>(
) -> Result<Value<'gc>, Error> {
if let Some(this) = this {
if let Some(loader_stream) = this.as_loader_stream() {
match &*loader_stream {
LoaderStream::Stage => {
return Err("Error: The stage's loader info does not have parameters".into())
}
LoaderStream::Swf(root, _) => {
let root = match &*loader_stream {
LoaderStream::Stage => activation
.context
.stage
.child_by_index(0)
.and_then(|c| c.movie()),
LoaderStream::Swf(root, _) => Some(root.clone()),
};
if let Some(root) = root {
let mut params_obj = activation
.avm2()
.classes()
@ -391,7 +403,6 @@ pub fn parameters<'gc>(
}
}
}
}
Ok(Value::Undefined)
}