Skip to content

Commit

Permalink
avm1: Implement SharedObject.getSize()
Browse files Browse the repository at this point in the history
  • Loading branch information
n0samu authored and Dinnerbone committed Jul 16, 2023
1 parent e1819f7 commit fea0df1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 18 deletions.
51 changes: 33 additions & 18 deletions core/src/avm1/globals/shared_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,18 @@ fn deserialize_lso<'gc>(
Ok(obj.into())
}

fn new_lso<'gc>(activation: &mut Activation<'_, 'gc>, name: &str, data: Object<'gc>) -> Lso {
let mut w = Amf0Writer::default();
recursive_serialize(activation, data, &mut w);
w.commit_lso(
&name
.split('/')
.last()
.map(|e| e.to_string())
.unwrap_or_else(|| "<unknown>".to_string()),
)
}

pub fn get_local<'gc>(
activation: &mut Activation<'_, 'gc>,
_this: Object<'gc>,
Expand Down Expand Up @@ -510,32 +522,35 @@ pub fn flush<'gc>(
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
let data = this.get("data", activation)?.coerce_to_object(activation);

let this_obj = this.as_shared_object().unwrap();
let name = this_obj.get_name();

let mut w = Amf0Writer::default();
recursive_serialize(activation, data, &mut w);
let mut lso = w.commit_lso(
&name
.split('/')
.last()
.map(|e| e.to_string())
.unwrap_or_else(|| "<unknown>".to_string()),
);

let bytes = flash_lso::write::write_to_bytes(&mut lso).unwrap_or_default();

Ok(activation.context.storage.put(&name, &bytes).into())
let mut lso = new_lso(activation, &name, data);
flash_lso::write::write_to_bytes(&mut lso).unwrap_or_default();
// Flash does not write empty LSOs to disk
if lso.body.is_empty() {
Ok(true.into())
} else {
let bytes = flash_lso::write::write_to_bytes(&mut lso).unwrap_or_default();
Ok(activation.context.storage.put(&name, &bytes).into())
}
}

pub fn get_size<'gc>(
activation: &mut Activation<'_, 'gc>,
_this: Object<'gc>,
this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
avm1_stub!(activation, "SharedObject", "getSize");
Ok(Value::Undefined)
let data = this.get("data", activation)?.coerce_to_object(activation);
let this_obj = this.as_shared_object().unwrap();
let name = this_obj.get_name();
let mut lso = new_lso(activation, &name, data);
// Flash returns 0 for empty LSOs, but the actual number of bytes (including the header) otherwise
if lso.body.is_empty() {
Ok(0.into())
} else {
let bytes = flash_lso::write::write_to_bytes(&mut lso).unwrap_or_default();
Ok(bytes.len().into())
}
}

pub fn send<'gc>(
Expand Down
3 changes: 3 additions & 0 deletions tests/tests/swfs/avm1/shared_object/output1.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
No data found. Initializing...
size: 0
size: 279
size: 279
1 change: 1 addition & 0 deletions tests/tests/swfs/avm1/shared_object/output2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ o.b: b
delete
false
saved: true
size: 279
6 changes: 6 additions & 0 deletions tests/tests/swfs/avm1/shared_object/test.as
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class test {

if(obj.data.saved === undefined) {
trace("No data found. Initializing...");
trace("size: " + obj.getSize());
obj.data.saved = true;
obj.data.num = 10;
obj.data.str = "hello";
Expand All @@ -23,7 +24,10 @@ class test {
obj.data.testxml = new XML("<test>Test</test>");

obj.data.o = {a: "a", b: "b"};

trace("size: " + obj.getSize());
obj.flush();
trace("size: " + obj.getSize());
} else {
trace("saved: " + obj.data.saved);
trace("num: " + obj.data.num);
Expand All @@ -44,9 +48,11 @@ class test {

trace("o.a: " + obj.data.o.a);
trace("o.b: " + obj.data.o.b);

trace("delete");
trace(delete obj.data);
trace("saved: " + obj.data.saved);
trace("size: " + obj.getSize());
}
}
}
Binary file modified tests/tests/swfs/avm1/shared_object/test.fla
Binary file not shown.
Binary file modified tests/tests/swfs/avm1/shared_object/test.swf
Binary file not shown.

0 comments on commit fea0df1

Please sign in to comment.