core: Replace num_enum with num_derive

This commit is contained in:
relrelb 2021-06-05 15:41:05 +03:00 committed by Mike Welsh
parent f82faf9603
commit aee37276ec
10 changed files with 38 additions and 29 deletions

2
Cargo.lock generated
View File

@ -3094,8 +3094,8 @@ dependencies = [
"log", "log",
"minimp3", "minimp3",
"nellymoser-rs", "nellymoser-rs",
"num-derive",
"num-traits", "num-traits",
"num_enum 0.5.1",
"percent-encoding", "percent-encoding",
"png", "png",
"pretty_assertions 0.7.2", "pretty_assertions 0.7.2",

View File

@ -21,7 +21,8 @@ ruffle_macros = { path = "macros" }
swf = { path = "../swf" } swf = { path = "../swf" }
bitflags = "1.2.1" bitflags = "1.2.1"
smallvec = "1.6.1" smallvec = "1.6.1"
num_enum = "0.5.1" num-traits = "0.2"
num-derive = "0.3"
quick-xml = "0.22.0" quick-xml = "0.22.0"
downcast-rs = "1.2.0" downcast-rs = "1.2.0"
url = "2.2.2" url = "2.2.2"
@ -29,7 +30,6 @@ weak-table = "0.3.0"
percent-encoding = "2.1.0" percent-encoding = "2.1.0"
thiserror = "1.0" thiserror = "1.0"
chrono = "0.4" chrono = "0.4"
num-traits = "0.2"
instant = "0.1" instant = "0.1"
encoding_rs = "0.8.28" encoding_rs = "0.8.28"
rand = { version = "0.8.3", features = ["std", "small_rng"], default-features = false } rand = { version = "0.8.3", features = ["std", "small_rng"], default-features = false }

View File

@ -5,7 +5,6 @@ use crate::avm1::property_decl::{define_properties_on, Declaration};
use crate::avm1::{Object, ScriptObject, Value}; use crate::avm1::{Object, ScriptObject, Value};
use crate::events::KeyCode; use crate::events::KeyCode;
use gc_arena::MutationContext; use gc_arena::MutationContext;
use std::convert::TryFrom;
const OBJECT_DECLS: &[Declaration] = declare_properties! { const OBJECT_DECLS: &[Declaration] = declare_properties! {
"ALT" => int(18; DONT_ENUM | DONT_DELETE | READ_ONLY); "ALT" => int(18; DONT_ENUM | DONT_DELETE | READ_ONLY);
@ -40,7 +39,7 @@ pub fn is_down<'gc>(
if let Some(key) = args if let Some(key) = args
.get(0) .get(0)
.and_then(|v| v.coerce_to_f64(activation).ok()) .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()) Ok(activation.context.ui.is_key_down(key).into())
} else { } else {
@ -62,7 +61,7 @@ pub fn get_code<'gc>(
_this: Object<'gc>, _this: Object<'gc>,
_args: &[Value<'gc>], _args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'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()) Ok(code.into())
} }

View File

@ -8,8 +8,6 @@ use crate::avm_warn;
use bitflags::bitflags; use bitflags::bitflags;
use core::fmt; use core::fmt;
use gc_arena::MutationContext; use gc_arena::MutationContext;
use num_enum::TryFromPrimitive;
use std::convert::TryFrom;
const OBJECT_DECLS: &[Declaration] = declare_properties! { const OBJECT_DECLS: &[Declaration] = declare_properties! {
"exactSettings" => property(get_exact_settings, set_exact_settings); "exactSettings" => property(get_exact_settings, set_exact_settings);
@ -215,8 +213,7 @@ impl fmt::Display for PlayerType {
} }
} }
#[derive(Debug, Copy, Clone, TryFromPrimitive)] #[derive(Debug, Copy, Clone, FromPrimitive)]
#[repr(u8)]
enum SettingsPanel { enum SettingsPanel {
Privacy = 0, Privacy = 0,
LocalStorage = 1, LocalStorage = 1,
@ -224,6 +221,12 @@ enum SettingsPanel {
Camera = 3, Camera = 3,
} }
impl SettingsPanel {
pub fn from_u8(n: u8) -> Option<Self> {
num_traits::FromPrimitive::from_u8(n)
}
}
bitflags! { bitflags! {
pub struct SystemCapabilities: u32 { pub struct SystemCapabilities: u32 {
const AV_HARDWARE = 1 << 0; const AV_HARDWARE = 1 << 0;
@ -438,7 +441,7 @@ pub fn show_settings<'gc>(
.unwrap_or(&Value::Number(last_panel_pos as f64)) .unwrap_or(&Value::Number(last_panel_pos as f64))
.coerce_to_i32(activation)?; .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!( avm_warn!(
activation, activation,

View File

@ -12,7 +12,6 @@ use crate::types::{Degrees, Percent};
use crate::vminterface::Instantiator; use crate::vminterface::Instantiator;
use gc_arena::{Collect, GcCell, MutationContext}; use gc_arena::{Collect, GcCell, MutationContext};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::convert::TryFrom;
use std::sync::Arc; use std::sync::Arc;
use swf::ButtonActionCondition; use swf::ButtonActionCondition;
@ -55,9 +54,7 @@ impl<'gc> Avm1Button<'gc> {
actions.push(ButtonAction { actions.push(ButtonAction {
action_data: action_data.clone(), action_data: action_data.clone(),
condition: ButtonActionCondition::from_bits_truncate(bit), condition: ButtonActionCondition::from_bits_truncate(bit),
key_code: action key_code: action.key_code.and_then(ButtonKeyCode::from_u8),
.key_code
.and_then(|k| ButtonKeyCode::try_from(k).ok()),
}); });
} }
bit <<= 1; bit <<= 1;

View File

@ -13,7 +13,6 @@ use crate::tag_utils::{SwfMovie, SwfSlice};
use crate::types::{Degrees, Percent}; use crate::types::{Degrees, Percent};
use crate::vminterface::Instantiator; use crate::vminterface::Instantiator;
use gc_arena::{Collect, GcCell, MutationContext}; use gc_arena::{Collect, GcCell, MutationContext};
use std::convert::TryFrom;
use std::sync::Arc; use std::sync::Arc;
use swf::ButtonActionCondition; use swf::ButtonActionCondition;
@ -77,9 +76,7 @@ impl<'gc> Avm2Button<'gc> {
actions.push(ButtonAction { actions.push(ButtonAction {
action_data: action_data.clone(), action_data: action_data.clone(),
condition: ButtonActionCondition::from_bits_truncate(bit), condition: ButtonActionCondition::from_bits_truncate(bit),
key_code: action key_code: action.key_code.and_then(ButtonKeyCode::from_u8),
.key_code
.and_then(|k| ButtonKeyCode::try_from(k).ok()),
}); });
} }
bit <<= 1; bit <<= 1;

View File

@ -33,7 +33,6 @@ use gc_arena::{Collect, Gc, GcCell, MutationContext};
use smallvec::SmallVec; use smallvec::SmallVec;
use std::cell::{Ref, RefCell, RefMut}; use std::cell::{Ref, RefCell, RefMut};
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::TryFrom;
use std::sync::Arc; use std::sync::Arc;
use swf::extensions::ReadSwfExt; use swf::extensions::ReadSwfExt;
use swf::{FrameLabelData, Tag}; use swf::{FrameLabelData, Tag};
@ -3498,7 +3497,7 @@ impl ClipAction {
ClipEventFlag::KEY_DOWN => ClipEvent::KeyDown, ClipEventFlag::KEY_DOWN => ClipEvent::KeyDown,
ClipEventFlag::KEY_PRESS => ClipEvent::KeyPress { ClipEventFlag::KEY_PRESS => ClipEvent::KeyPress {
key_code: key_code key_code: key_code
.and_then(|k| ButtonKeyCode::try_from(k).ok()) .and_then(ButtonKeyCode::from_u8)
.unwrap_or(ButtonKeyCode::Unknown), .unwrap_or(ButtonKeyCode::Unknown),
}, },
ClipEventFlag::LOAD => ClipEvent::Load, ClipEventFlag::LOAD => ClipEvent::Load,

View File

@ -1,5 +1,3 @@
use num_enum::{IntoPrimitive, TryFromPrimitive};
#[derive(Debug)] #[derive(Debug)]
pub enum PlayerEvent { pub enum PlayerEvent {
KeyDown { key_code: KeyCode }, KeyDown { key_code: KeyCode },
@ -137,8 +135,7 @@ impl ClipEvent {
} }
/// Flash virtual keycode. /// Flash virtual keycode.
#[derive(Debug, Copy, Clone, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)] #[derive(Debug, Copy, Clone, PartialEq, Eq, FromPrimitive, ToPrimitive)]
#[repr(u8)]
pub enum KeyCode { pub enum KeyCode {
Unknown = 0, Unknown = 0,
Backspace = 8, Backspace = 8,
@ -238,13 +235,22 @@ pub enum KeyCode {
Apostrophe = 222, 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 codes for SWF4 keyPress button handlers. These are annoyingly different than
/// `Key.isDown` key codes. /// `Key.isDown` key codes.
/// TODO: After 18, these are mostly ASCII... should we just use u8? How are different /// TODO: After 18, these are mostly ASCII... should we just use u8? How are different
/// keyboard layouts/languages handled? /// keyboard layouts/languages handled?
/// SWF19 pp. 198-199 /// SWF19 pp. 198-199
#[derive(Debug, PartialEq, Eq, Copy, Clone, TryFromPrimitive, IntoPrimitive)] #[derive(Debug, PartialEq, Eq, Copy, Clone, FromPrimitive)]
#[repr(u8)]
pub enum ButtonKeyCode { pub enum ButtonKeyCode {
Unknown = 0, Unknown = 0,
Left = 1, Left = 1,
@ -358,6 +364,12 @@ pub enum ButtonKeyCode {
Tilde = 126, 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> { pub fn key_code_to_button_key_code(key_code: KeyCode) -> Option<ButtonKeyCode> {
let out = match key_code { let out = match key_code {
KeyCode::Left => ButtonKeyCode::Left, KeyCode::Left => ButtonKeyCode::Left,

View File

@ -18,6 +18,9 @@ extern crate smallvec;
#[macro_use] #[macro_use]
extern crate downcast_rs; extern crate downcast_rs;
#[macro_use]
extern crate num_derive;
#[macro_use] #[macro_use]
mod avm1; mod avm1;
mod avm2; mod avm2;

View File

@ -34,7 +34,6 @@ use instant::Instant;
use log::info; use log::info;
use rand::{rngs::SmallRng, SeedableRng}; use rand::{rngs::SmallRng, SeedableRng};
use std::collections::{HashMap, VecDeque}; use std::collections::{HashMap, VecDeque};
use std::convert::TryFrom;
use std::ops::DerefMut; use std::ops::DerefMut;
use std::sync::{Arc, Mutex, Weak}; use std::sync::{Arc, Mutex, Weak};
use std::time::Duration; use std::time::Duration;
@ -826,7 +825,7 @@ impl Player {
if codepoint as u32 >= 32 && codepoint as u32 <= 126 => if codepoint as u32 >= 32 && codepoint as u32 <= 126 =>
{ {
Some(ClipEvent::KeyPress { Some(ClipEvent::KeyPress {
key_code: ButtonKeyCode::try_from(codepoint as u8).unwrap(), key_code: ButtonKeyCode::from_u8(codepoint as u8).unwrap(),
}) })
} }