Skip to content

Commit

Permalink
core: Replace num_enum with num_derive
Browse files Browse the repository at this point in the history
  • Loading branch information
relrelb authored and Herschel committed Jun 5, 2021
1 parent f82faf9 commit aee3727
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ ruffle_macros = { path = "macros" }
swf = { path = "../swf" }
bitflags = "1.2.1"
smallvec = "1.6.1"
num_enum = "0.5.1"
num-traits = "0.2"
num-derive = "0.3"
quick-xml = "0.22.0"
downcast-rs = "1.2.0"
url = "2.2.2"
weak-table = "0.3.0"
percent-encoding = "2.1.0"
thiserror = "1.0"
chrono = "0.4"
num-traits = "0.2"
instant = "0.1"
encoding_rs = "0.8.28"
rand = { version = "0.8.3", features = ["std", "small_rng"], default-features = false }
Expand Down
5 changes: 2 additions & 3 deletions core/src/avm1/globals/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::avm1::property_decl::{define_properties_on, Declaration};
use crate::avm1::{Object, ScriptObject, Value};
use crate::events::KeyCode;
use gc_arena::MutationContext;
use std::convert::TryFrom;

const OBJECT_DECLS: &[Declaration] = declare_properties! {
"ALT" => int(18; DONT_ENUM | DONT_DELETE | READ_ONLY);
Expand Down Expand Up @@ -40,7 +39,7 @@ pub fn is_down<'gc>(
if let Some(key) = args
.get(0)
.and_then(|v| v.coerce_to_f64(activation).ok())
.and_then(|k| KeyCode::try_from(k as u8).ok())
.and_then(|k| KeyCode::from_u8(k as u8))
{
Ok(activation.context.ui.is_key_down(key).into())
} else {
Expand All @@ -62,7 +61,7 @@ pub fn get_code<'gc>(
_this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
let code: u8 = activation.context.ui.last_key_code().into();
let code = activation.context.ui.last_key_code().to_u8();
Ok(code.into())
}

Expand Down
13 changes: 8 additions & 5 deletions core/src/avm1/globals/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use crate::avm_warn;
use bitflags::bitflags;
use core::fmt;
use gc_arena::MutationContext;
use num_enum::TryFromPrimitive;
use std::convert::TryFrom;

const OBJECT_DECLS: &[Declaration] = declare_properties! {
"exactSettings" => property(get_exact_settings, set_exact_settings);
Expand Down Expand Up @@ -215,15 +213,20 @@ impl fmt::Display for PlayerType {
}
}

#[derive(Debug, Copy, Clone, TryFromPrimitive)]
#[repr(u8)]
#[derive(Debug, Copy, Clone, FromPrimitive)]
enum SettingsPanel {
Privacy = 0,
LocalStorage = 1,
Microphone = 2,
Camera = 3,
}

impl SettingsPanel {
pub fn from_u8(n: u8) -> Option<Self> {
num_traits::FromPrimitive::from_u8(n)
}
}

bitflags! {
pub struct SystemCapabilities: u32 {
const AV_HARDWARE = 1 << 0;
Expand Down Expand Up @@ -438,7 +441,7 @@ pub fn show_settings<'gc>(
.unwrap_or(&Value::Number(last_panel_pos as f64))
.coerce_to_i32(activation)?;

let panel = SettingsPanel::try_from(panel_pos as u8).unwrap_or(SettingsPanel::Privacy);
let panel = SettingsPanel::from_u8(panel_pos as u8).unwrap_or(SettingsPanel::Privacy);

avm_warn!(
activation,
Expand Down
5 changes: 1 addition & 4 deletions core/src/display_object/avm1_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::types::{Degrees, Percent};
use crate::vminterface::Instantiator;
use gc_arena::{Collect, GcCell, MutationContext};
use std::collections::BTreeMap;
use std::convert::TryFrom;
use std::sync::Arc;
use swf::ButtonActionCondition;

Expand Down Expand Up @@ -55,9 +54,7 @@ impl<'gc> Avm1Button<'gc> {
actions.push(ButtonAction {
action_data: action_data.clone(),
condition: ButtonActionCondition::from_bits_truncate(bit),
key_code: action
.key_code
.and_then(|k| ButtonKeyCode::try_from(k).ok()),
key_code: action.key_code.and_then(ButtonKeyCode::from_u8),
});
}
bit <<= 1;
Expand Down
5 changes: 1 addition & 4 deletions core/src/display_object/avm2_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::tag_utils::{SwfMovie, SwfSlice};
use crate::types::{Degrees, Percent};
use crate::vminterface::Instantiator;
use gc_arena::{Collect, GcCell, MutationContext};
use std::convert::TryFrom;
use std::sync::Arc;
use swf::ButtonActionCondition;

Expand Down Expand Up @@ -77,9 +76,7 @@ impl<'gc> Avm2Button<'gc> {
actions.push(ButtonAction {
action_data: action_data.clone(),
condition: ButtonActionCondition::from_bits_truncate(bit),
key_code: action
.key_code
.and_then(|k| ButtonKeyCode::try_from(k).ok()),
key_code: action.key_code.and_then(ButtonKeyCode::from_u8),
});
}
bit <<= 1;
Expand Down
3 changes: 1 addition & 2 deletions core/src/display_object/movie_clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use gc_arena::{Collect, Gc, GcCell, MutationContext};
use smallvec::SmallVec;
use std::cell::{Ref, RefCell, RefMut};
use std::collections::HashMap;
use std::convert::TryFrom;
use std::sync::Arc;
use swf::extensions::ReadSwfExt;
use swf::{FrameLabelData, Tag};
Expand Down Expand Up @@ -3498,7 +3497,7 @@ impl ClipAction {
ClipEventFlag::KEY_DOWN => ClipEvent::KeyDown,
ClipEventFlag::KEY_PRESS => ClipEvent::KeyPress {
key_code: key_code
.and_then(|k| ButtonKeyCode::try_from(k).ok())
.and_then(ButtonKeyCode::from_u8)
.unwrap_or(ButtonKeyCode::Unknown),
},
ClipEventFlag::LOAD => ClipEvent::Load,
Expand Down
24 changes: 18 additions & 6 deletions core/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use num_enum::{IntoPrimitive, TryFromPrimitive};

#[derive(Debug)]
pub enum PlayerEvent {
KeyDown { key_code: KeyCode },
Expand Down Expand Up @@ -137,8 +135,7 @@ impl ClipEvent {
}

/// Flash virtual keycode.
#[derive(Debug, Copy, Clone, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, FromPrimitive, ToPrimitive)]
pub enum KeyCode {
Unknown = 0,
Backspace = 8,
Expand Down Expand Up @@ -238,13 +235,22 @@ pub enum KeyCode {
Apostrophe = 222,
}

impl KeyCode {
pub fn from_u8(n: u8) -> Option<Self> {
num_traits::FromPrimitive::from_u8(n)
}

pub fn to_u8(&self) -> u8 {
num_traits::ToPrimitive::to_u8(self).unwrap()
}
}

/// Key codes for SWF4 keyPress button handlers. These are annoyingly different than
/// `Key.isDown` key codes.
/// TODO: After 18, these are mostly ASCII... should we just use u8? How are different
/// keyboard layouts/languages handled?
/// SWF19 pp. 198-199
#[derive(Debug, PartialEq, Eq, Copy, Clone, TryFromPrimitive, IntoPrimitive)]
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Copy, Clone, FromPrimitive)]
pub enum ButtonKeyCode {
Unknown = 0,
Left = 1,
Expand Down Expand Up @@ -358,6 +364,12 @@ pub enum ButtonKeyCode {
Tilde = 126,
}

impl ButtonKeyCode {
pub fn from_u8(n: u8) -> Option<Self> {
num_traits::FromPrimitive::from_u8(n)
}
}

pub fn key_code_to_button_key_code(key_code: KeyCode) -> Option<ButtonKeyCode> {
let out = match key_code {
KeyCode::Left => ButtonKeyCode::Left,
Expand Down
3 changes: 3 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ extern crate smallvec;
#[macro_use]
extern crate downcast_rs;

#[macro_use]
extern crate num_derive;

#[macro_use]
mod avm1;
mod avm2;
Expand Down
3 changes: 1 addition & 2 deletions core/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use instant::Instant;
use log::info;
use rand::{rngs::SmallRng, SeedableRng};
use std::collections::{HashMap, VecDeque};
use std::convert::TryFrom;
use std::ops::DerefMut;
use std::sync::{Arc, Mutex, Weak};
use std::time::Duration;
Expand Down Expand Up @@ -826,7 +825,7 @@ impl Player {
if codepoint as u32 >= 32 && codepoint as u32 <= 126 =>
{
Some(ClipEvent::KeyPress {
key_code: ButtonKeyCode::try_from(codepoint as u8).unwrap(),
key_code: ButtonKeyCode::from_u8(codepoint as u8).unwrap(),
})
}

Expand Down

0 comments on commit aee3727

Please sign in to comment.