From fea0df192f2c31a0c8cd4a2bc500da416abdb2cd Mon Sep 17 00:00:00 2001 From: nosamu <71368227+n0samu@users.noreply.github.com> Date: Sat, 15 Jul 2023 08:23:19 -0500 Subject: [PATCH] avm1: Implement SharedObject.getSize() --- core/src/avm1/globals/shared_object.rs | 51 +++++++++++------- .../tests/swfs/avm1/shared_object/output1.txt | 3 ++ .../tests/swfs/avm1/shared_object/output2.txt | 1 + tests/tests/swfs/avm1/shared_object/test.as | 6 +++ tests/tests/swfs/avm1/shared_object/test.fla | Bin 7066 -> 6273 bytes tests/tests/swfs/avm1/shared_object/test.swf | Bin 687 -> 796 bytes 6 files changed, 43 insertions(+), 18 deletions(-) diff --git a/core/src/avm1/globals/shared_object.rs b/core/src/avm1/globals/shared_object.rs index 68644f640f6e..39b9add3bfc1 100644 --- a/core/src/avm1/globals/shared_object.rs +++ b/core/src/avm1/globals/shared_object.rs @@ -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(|| "".to_string()), + ) +} + pub fn get_local<'gc>( activation: &mut Activation<'_, 'gc>, _this: Object<'gc>, @@ -510,32 +522,35 @@ pub fn flush<'gc>( _args: &[Value<'gc>], ) -> Result, 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(|| "".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, 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>( diff --git a/tests/tests/swfs/avm1/shared_object/output1.txt b/tests/tests/swfs/avm1/shared_object/output1.txt index ab9fb62ebce0..dfa081271949 100644 --- a/tests/tests/swfs/avm1/shared_object/output1.txt +++ b/tests/tests/swfs/avm1/shared_object/output1.txt @@ -1 +1,4 @@ No data found. Initializing... +size: 0 +size: 279 +size: 279 diff --git a/tests/tests/swfs/avm1/shared_object/output2.txt b/tests/tests/swfs/avm1/shared_object/output2.txt index a8adfad23253..c47bdbb2a402 100644 --- a/tests/tests/swfs/avm1/shared_object/output2.txt +++ b/tests/tests/swfs/avm1/shared_object/output2.txt @@ -17,3 +17,4 @@ o.b: b delete false saved: true +size: 279 diff --git a/tests/tests/swfs/avm1/shared_object/test.as b/tests/tests/swfs/avm1/shared_object/test.as index 365ade9cf62c..3bd6377fa4d3 100644 --- a/tests/tests/swfs/avm1/shared_object/test.as +++ b/tests/tests/swfs/avm1/shared_object/test.as @@ -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"; @@ -23,7 +24,10 @@ class test { obj.data.testxml = new XML("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); @@ -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()); } } } diff --git a/tests/tests/swfs/avm1/shared_object/test.fla b/tests/tests/swfs/avm1/shared_object/test.fla index 9b0e18844625c2ee714b6c90bc05b32bd9845cd5..a8a991902aa9972c091bc13c4dfb7f912eb7c949 100644 GIT binary patch delta 2001 zcmV;?2QK)UH-RybihmhAtetcN003?b000jF002Z!O+;^Fb!}yCbS`*pY^_&YkJ>mC zejjQ6gPVQXK9pR@wM|B%u&78gEn2E=S5+UJ!~yR*c4RvR`s-^afw=^xP|Cc7bNSBk zcfR8=@#MqZgk^P!s05pmQ1N;raAK^atX`Yudc7X3hXcl!rhj8uw)y$)c9E@63149< zB%~QK+N^yB6$6;F6b&*~nwx^?ywEhjlH+{L2;+(I$Jo+;%2wkXiJjH5rc+B<3l2;4 zHFq_GtfaCLCN@x7A!GFx;~f^C{|$3S%HL( zganKiz5yx%Wq-kVnHZli&DmNQ;9HB;W-s73l!J5&9>R2?z;*@}NNY@T;D)M^68^{d zz7^eW<9uJ2DN<)@V)V%u70fVQepWHzBg?<7MNn9c2wu{+jOU1V&eliKMlLc$(JWUy z-~wx8r#Z;>LnT#ZF;|bbj8%zIKq64jH-}3kjyb2A?|)I&eBZ~wS~AVVUlgVU<%uDA z{lNSV3dL+$$-V35`U>bzEVgCJh}eB^jWe2&I!9o|QIQyLYbhBWDB&0YOH|G>&7K-P zVKASSCCYI#{;roMWRe(O;n{_U>{pilcxkE!QWqrpU>Q)DI zIPJ5NUe>LaJDpD5>3)}X)5Kzd2+I0HJXoaJ%VRA(Zl;kGP&-$xhKPS)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3WgV5w^mo;g~e;r~b}?9p0z3+-YykE)D6!I7617L{Z;vSc z1w=>se?j}#`MD~B{{}G==-7`xDP<~93C4R$nFvgR@wvNVLlXZ(N?!WU4&b{He=_*5 Z$X!hsikE|$3KT=}9?)aLZ0MiH{{pqJBjf-8 diff --git a/tests/tests/swfs/avm1/shared_object/test.swf b/tests/tests/swfs/avm1/shared_object/test.swf index 00b900f46e92d0c0851eb6ffe796c5f2e8fd4bbb..d59b9fec96dc22432f42aaf00a09902a33781975 100644 GIT binary patch literal 796 zcmV+%1LOQdS5pYH1^@tfoOM)NZxTTiKD$77dZQPs)Y=N(psjWbS}$p8FDbDxsn9?l zOd3N57+`hTZP=x@eeqGBd?Yc(#Q5O<@fYElnPs~Wx+G`MeBU{9zBzN2bBL4xrhWrV zD3F~2eD>9Dx4RRHquZH+5$HWGb6i;LAkd_S1%()NM>(qqb|*R~d<9CSBU<@H>&($b zN;kSa!kKSz^%a!rrd_6{-0ZYvyS96A%H)pgP-xd`COhU_u|Zp`nlFE36&LEvJ+v#B zR;4Zlhn}%2gcriI<$f=-2DEmtxLi`A)XoUw#$F{m=jVKNPCGstqok8<8LVT(K4T>RF$ zaN+&CL%?EChML)S8d5llLNRDj4q73l_ts5j)!hazrQ@Lu>f}FLN1m?bR64bSA!+Ex zuL;hG;EZHXQv7tO550mXYZ>3yuTJMfE<5LE!!^GqySCg1uUw^KBV@OajV9|4<{Xc- zbt-HbZ54Zqt=lUWnaicVF=^d_jtT@P46Txzo&BXkBpiT#M&ulR#Uaag!Vp%7n!|+} zlZ(*z2|i}wRUFU?{0()+frLp!Q!$5(h&{9dPxj(Tjzgoljsl??ul7&jz*rKeo{al* zkC79FgQTAm#+*n`0IoG4liC;t=7S+oGIh0%0-4shdX0Z8WM(%XR4|=GrG7awJ4kAv zg*kC!kiar?OSR$u*?7P_u}j0gDjMaUpOCSlRaAsmOr{(U9qxsETg_;$qAF{P3HYgdmhC#D<3bP~ne)|5oH);I1(nk^4R}=^+n% zB|J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