Skip to content

Commit

Permalink
swf: Use bitflags for FileAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Herschel committed May 22, 2021
1 parent aacdcc4 commit 8960414
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 41 deletions.
2 changes: 1 addition & 1 deletion core/src/display_object/movie_clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl<'gc> MovieClip<'gc> {
let tag_callback = |reader: &mut SwfStream<'_>, tag_code, tag_len| match tag_code {
TagCode::FileAttributes => {
let attributes = reader.read_file_attributes()?;
let avm_type = if attributes.is_action_script_3 {
let avm_type = if attributes.contains(swf::FileAttributes::IS_ACTION_SCRIPT_3) {
log::warn!("This SWF contains ActionScript 3 which is not yet supported by Ruffle. The movie may not work as intended.");
AvmType::Avm2
} else {
Expand Down
6 changes: 5 additions & 1 deletion core/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,11 @@ impl<'gc> Library<'gc> {
if TagCode::from_u16(tag_code) == Some(TagCode::FileAttributes) =>
{
match reader.read_file_attributes() {
Ok(attributes) if attributes.is_action_script_3 => AvmType::Avm2,
Ok(attributes)
if attributes.contains(swf::FileAttributes::IS_ACTION_SCRIPT_3) =>
{
AvmType::Avm2
}
Ok(_) => AvmType::Avm1,
Err(e) => {
log::error!("Got {} when reading FileAttributes", e);
Expand Down
8 changes: 1 addition & 7 deletions swf/src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1848,13 +1848,7 @@ impl<'a> Reader<'a> {

pub fn read_file_attributes(&mut self) -> Result<FileAttributes> {
let flags = self.read_u32()?;
Ok(FileAttributes {
use_direct_blit: (flags & 0b01000000) != 0,
use_gpu: (flags & 0b00100000) != 0,
has_metadata: (flags & 0b00010000) != 0,
is_action_script_3: (flags & 0b00001000) != 0,
use_network_sandbox: (flags & 0b00000001) != 0,
})
Ok(FileAttributes::from_bits_truncate(flags as u8))
}

pub fn read_export_assets(&mut self) -> Result<ExportAssets<'a>> {
Expand Down
8 changes: 1 addition & 7 deletions swf/src/test_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1974,13 +1974,7 @@ pub fn tag_tests() -> Vec<TagTestData> {
),
(
8,
Tag::FileAttributes(FileAttributes {
use_direct_blit: false,
use_gpu: true,
has_metadata: false,
is_action_script_3: true,
use_network_sandbox: false,
}),
Tag::FileAttributes(FileAttributes::USE_GPU | FileAttributes::IS_ACTION_SCRIPT_3),
vec![0b01_000100, 0b00010001, 0b00101000, 0, 0, 0],
),
(
Expand Down
30 changes: 23 additions & 7 deletions swf/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,29 @@ pub enum Language {
TraditionalChinese,
}

#[derive(Debug, PartialEq)]
pub struct FileAttributes {
pub use_direct_blit: bool,
pub use_gpu: bool,
pub has_metadata: bool,
pub is_action_script_3: bool,
pub use_network_sandbox: bool,
bitflags! {
/// Flags that define various characteristic of an SWF file.
///
/// [SWF19 pp.57-58 ClipEvent](https://www.adobe.com/content/dam/acom/en/devnet/pdf/swf-file-format-spec.pdf#page=47)
pub struct FileAttributes: u8 {
/// Whether this SWF requests hardware acceleration to blit to the screen.
const USE_DIRECT_BLIT = 1 << 6;

/// Whether this SWF requests hardware acceleration for compositing.
const USE_GPU = 1 << 5;

/// Whether this SWF contains XMP metadata in a Metadata tag.
const HAS_METADATA = 1 << 4;

/// Whether this SWF uses ActionScript 3 (AVM2).
const IS_ACTION_SCRIPT_3 = 1 << 3;

/// Whether this SWF should be placed in the network sandbox when run locally.
///
/// SWFs in the network sandbox can only access network resources, not local resources.
/// SWFs in the local sandbox can only access local resources, not network resources.
const USE_NETWORK_SANDBOX = 1 << 0;
}
}

#[derive(Debug, PartialEq)]
Expand Down
20 changes: 2 additions & 18 deletions swf/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,25 +979,9 @@ impl<W: Write> Writer<W> {
self.output.write_all(&frame.data)?;
}

Tag::FileAttributes(ref attributes) => {
Tag::FileAttributes(attributes) => {
self.write_tag_header(TagCode::FileAttributes, 4)?;
let mut flags = 0u32;
if attributes.use_direct_blit {
flags |= 0b01000000;
}
if attributes.use_gpu {
flags |= 0b00100000;
}
if attributes.has_metadata {
flags |= 0b00010000;
}
if attributes.is_action_script_3 {
flags |= 0b00001000;
}
if attributes.use_network_sandbox {
flags |= 0b00000001;
}
self.write_u32(flags)?;
self.write_u32(attributes.bits() as u32)?;
}

Tag::FrameLabel(FrameLabel { label, is_anchor }) => {
Expand Down

0 comments on commit 8960414

Please sign in to comment.