1pub use crate::action::*;
2use crate::error::InvalidActionError;
3use crate::CatchTarget;
4use crate::{action, FunctionFlags, Parameter};
5#[cfg(feature = "serde")]
6use serde::{Deserialize, Serialize};
7use static_assertions::const_assert;
8use std::convert::TryFrom;
9
10#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
11#[cfg_attr(
12  feature = "serde",
13  derive(Serialize, Deserialize),
14  serde(tag = "action", rename_all = "PascalCase")
15)]
16pub enum Action {
17  Add,
18  Add2,
19  And,
20  AsciiToChar,
21  BitAnd,
22  BitOr,
23  BitLShift,
24  BitRShift,
25  BitURShift,
26  BitXor,
27  Call,
28  CallFunction,
29  CallMethod,
30  CharToAscii,
31  CastOp,
32  CloneSprite,
33  ConstantPool(action::ConstantPool),
34  Decrement,
35  DefineFunction(Box<DefineFunction>),
36  DefineFunction2(Box<DefineFunction2>),
37  DefineLocal,
38  DefineLocal2,
39  Delete,
40  Delete2,
41  Divide,
42  EndDrag,
43  Enumerate,
44  Enumerate2,
45  Equals,
46  Equals2,
47  Extends,
48  FsCommand2,
49  GetMember,
50  GetProperty,
51  GetTime,
52  GetUrl(Box<action::GetUrl>),
53  GetUrl2(action::GetUrl2),
54  GetVariable,
55  GotoFrame(action::GotoFrame),
56  GotoFrame2(action::GotoFrame2),
57  GotoLabel(action::GoToLabel),
58  Greater,
59  ImplementsOp,
60  Increment,
61  InitArray,
62  InitObject,
63  InstanceOf,
64  Less,
65  Less2,
66  MbAsciiToChar,
67  MbCharToAscii,
68  MbStringExtract,
69  MbStringLength,
70  Modulo,
71  Multiply,
72  NewMethod,
73  NewObject,
74  NextFrame,
75  Not,
76  Or,
77  Play,
78  Pop,
79  PrevFrame,
80  Push(action::Push),
81  PushDuplicate,
82  RandomNumber,
83  Raw(Box<action::Raw>),
84  RemoveSprite,
85  SetMember,
86  SetProperty,
87  SetTarget(action::SetTarget),
88  SetTarget2,
89  SetVariable,
90  StackSwap,
91  StartDrag,
92  Stop,
93  StopSounds,
94  StoreRegister(action::StoreRegister),
95  StrictEquals,
96  StrictMode(action::StrictMode),
97  StringAdd,
98  StringEquals,
99  StringExtract,
100  StringGreater,
101  StringLength,
102  StringLess,
103  Subtract,
104  TargetPath,
105  ToInteger,
106  ToNumber,
107  ToString,
108  ToggleQuality,
109  Trace,
110  TypeOf,
111
112  End,
115  Jump(Jump),
116  If(If),
117  Throw,
118  Return,
119  Try(Box<Try>),
120  WaitForFrame(WaitForFrame),
121  WaitForFrame2(WaitForFrame2),
122  With(With),
123  Error(Error),
124}
125
126const_assert!(std::mem::size_of::<Action>() <= 32);
127
128#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
129#[cfg_attr(
130  feature = "serde",
131  derive(Serialize, Deserialize),
132  serde(tag = "action", rename_all = "PascalCase")
133)]
134pub enum FromCfgActionError {
135  DefineFunction(Box<crate::cfg::DefineFunction>),
136  DefineFunction2(Box<crate::cfg::DefineFunction2>),
137}
138
139impl TryFrom<crate::cfg::Action> for Action {
140  type Error = FromCfgActionError;
141
142  fn try_from(value: crate::cfg::Action) -> Result<Self, FromCfgActionError> {
143    match value {
144      crate::cfg::Action::Add => Ok(Self::Add),
145      crate::cfg::Action::Add2 => Ok(Self::Add2),
146      crate::cfg::Action::And => Ok(Self::And),
147      crate::cfg::Action::AsciiToChar => Ok(Self::AsciiToChar),
148      crate::cfg::Action::BitAnd => Ok(Self::BitAnd),
149      crate::cfg::Action::BitOr => Ok(Self::BitOr),
150      crate::cfg::Action::BitLShift => Ok(Self::BitLShift),
151      crate::cfg::Action::BitRShift => Ok(Self::BitRShift),
152      crate::cfg::Action::BitURShift => Ok(Self::BitURShift),
153      crate::cfg::Action::BitXor => Ok(Self::BitXor),
154      crate::cfg::Action::Call => Ok(Self::Call),
155      crate::cfg::Action::CallFunction => Ok(Self::CallFunction),
156      crate::cfg::Action::CallMethod => Ok(Self::CallMethod),
157      crate::cfg::Action::CharToAscii => Ok(Self::CharToAscii),
158      crate::cfg::Action::CastOp => Ok(Self::CastOp),
159      crate::cfg::Action::CloneSprite => Ok(Self::CloneSprite),
160      crate::cfg::Action::ConstantPool(action) => Ok(Self::ConstantPool(action)),
161      crate::cfg::Action::Decrement => Ok(Self::Decrement),
162      crate::cfg::Action::DefineFunction(action) => Err(FromCfgActionError::DefineFunction(action)),
163      crate::cfg::Action::DefineFunction2(action) => Err(FromCfgActionError::DefineFunction2(action)),
164      crate::cfg::Action::DefineLocal => Ok(Self::DefineLocal),
165      crate::cfg::Action::DefineLocal2 => Ok(Self::DefineLocal2),
166      crate::cfg::Action::Delete => Ok(Self::Delete),
167      crate::cfg::Action::Delete2 => Ok(Self::Delete2),
168      crate::cfg::Action::Divide => Ok(Self::Divide),
169      crate::cfg::Action::EndDrag => Ok(Self::EndDrag),
170      crate::cfg::Action::Enumerate => Ok(Self::Enumerate),
171      crate::cfg::Action::Enumerate2 => Ok(Self::Enumerate2),
172      crate::cfg::Action::Equals => Ok(Self::Equals),
173      crate::cfg::Action::Equals2 => Ok(Self::Equals2),
174      crate::cfg::Action::Extends => Ok(Self::Extends),
175      crate::cfg::Action::FsCommand2 => Ok(Self::FsCommand2),
176      crate::cfg::Action::GetMember => Ok(Self::GetMember),
177      crate::cfg::Action::GetProperty => Ok(Self::GetProperty),
178      crate::cfg::Action::GetTime => Ok(Self::GetTime),
179      crate::cfg::Action::GetUrl(action) => Ok(Self::GetUrl(action)),
180      crate::cfg::Action::GetUrl2(action) => Ok(Self::GetUrl2(action)),
181      crate::cfg::Action::GetVariable => Ok(Self::GetVariable),
182      crate::cfg::Action::GotoFrame(action) => Ok(Self::GotoFrame(action)),
183      crate::cfg::Action::GotoFrame2(action) => Ok(Self::GotoFrame2(action)),
184      crate::cfg::Action::GotoLabel(action) => Ok(Self::GotoLabel(action)),
185      crate::cfg::Action::Greater => Ok(Self::Greater),
186      crate::cfg::Action::ImplementsOp => Ok(Self::ImplementsOp),
187      crate::cfg::Action::Increment => Ok(Self::Increment),
188      crate::cfg::Action::InitArray => Ok(Self::InitArray),
189      crate::cfg::Action::InitObject => Ok(Self::InitObject),
190      crate::cfg::Action::InstanceOf => Ok(Self::InstanceOf),
191      crate::cfg::Action::Less => Ok(Self::Less),
192      crate::cfg::Action::Less2 => Ok(Self::Less2),
193      crate::cfg::Action::MbAsciiToChar => Ok(Self::MbAsciiToChar),
194      crate::cfg::Action::MbCharToAscii => Ok(Self::MbCharToAscii),
195      crate::cfg::Action::MbStringExtract => Ok(Self::MbStringExtract),
196      crate::cfg::Action::MbStringLength => Ok(Self::MbStringLength),
197      crate::cfg::Action::Modulo => Ok(Self::Modulo),
198      crate::cfg::Action::Multiply => Ok(Self::Multiply),
199      crate::cfg::Action::NewMethod => Ok(Self::NewMethod),
200      crate::cfg::Action::NewObject => Ok(Self::NewObject),
201      crate::cfg::Action::NextFrame => Ok(Self::NextFrame),
202      crate::cfg::Action::Not => Ok(Self::Not),
203      crate::cfg::Action::Or => Ok(Self::Or),
204      crate::cfg::Action::Play => Ok(Self::Play),
205      crate::cfg::Action::Pop => Ok(Self::Pop),
206      crate::cfg::Action::PrevFrame => Ok(Self::PrevFrame),
207      crate::cfg::Action::Push(action) => Ok(Self::Push(action)),
208      crate::cfg::Action::PushDuplicate => Ok(Self::PushDuplicate),
209      crate::cfg::Action::RandomNumber => Ok(Self::RandomNumber),
210      crate::cfg::Action::Raw(action) => Ok(Self::Raw(action)),
211      crate::cfg::Action::RemoveSprite => Ok(Self::RemoveSprite),
212      crate::cfg::Action::SetMember => Ok(Self::SetMember),
213      crate::cfg::Action::SetProperty => Ok(Self::SetProperty),
214      crate::cfg::Action::SetTarget(action) => Ok(Self::SetTarget(action)),
215      crate::cfg::Action::SetTarget2 => Ok(Self::SetTarget2),
216      crate::cfg::Action::SetVariable => Ok(Self::SetVariable),
217      crate::cfg::Action::StackSwap => Ok(Self::StackSwap),
218      crate::cfg::Action::StartDrag => Ok(Self::StartDrag),
219      crate::cfg::Action::Stop => Ok(Self::Stop),
220      crate::cfg::Action::StopSounds => Ok(Self::StopSounds),
221      crate::cfg::Action::StoreRegister(action) => Ok(Self::StoreRegister(action)),
222      crate::cfg::Action::StrictEquals => Ok(Self::StrictEquals),
223      crate::cfg::Action::StrictMode(action) => Ok(Self::StrictMode(action)),
224      crate::cfg::Action::StringAdd => Ok(Self::StringAdd),
225      crate::cfg::Action::StringEquals => Ok(Self::StringEquals),
226      crate::cfg::Action::StringExtract => Ok(Self::StringExtract),
227      crate::cfg::Action::StringGreater => Ok(Self::StringGreater),
228      crate::cfg::Action::StringLength => Ok(Self::StringLength),
229      crate::cfg::Action::StringLess => Ok(Self::StringLess),
230      crate::cfg::Action::Subtract => Ok(Self::Subtract),
231      crate::cfg::Action::TargetPath => Ok(Self::TargetPath),
232      crate::cfg::Action::ToInteger => Ok(Self::ToInteger),
233      crate::cfg::Action::ToNumber => Ok(Self::ToNumber),
234      crate::cfg::Action::ToString => Ok(Self::ToString),
235      crate::cfg::Action::ToggleQuality => Ok(Self::ToggleQuality),
236      crate::cfg::Action::Trace => Ok(Self::Trace),
237      crate::cfg::Action::TypeOf => Ok(Self::TypeOf),
238    }
239  }
240}
241
242#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
243#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
244pub struct DefineFunction {
245  pub name: String,
247  pub parameters: Vec<String>,
248  pub body_size: u16,
249}
250
251#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
252#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
253pub struct DefineFunction2 {
254  pub name: String,
256  pub register_count: u8,
257  #[cfg_attr(feature = "serde", serde(flatten))]
258  pub flags: FunctionFlags,
259  pub parameters: Vec<Parameter>,
260  pub body_size: u16,
261}
262
263#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
264#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
265pub struct Error {
266  #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
267  pub error: Option<InvalidActionError>,
268}
269
270#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
271#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
272pub struct If {
273  pub offset: i16,
274}
275
276#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
277#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
278pub struct Jump {
279  pub offset: i16,
280}
281
282#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
283#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
284pub struct CatchBlock {
285  pub target: CatchTarget,
286  pub size: u16,
287}
288
289#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
290#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
291pub struct Try {
292  pub r#try: u16,
293  #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
294  pub catch: Option<CatchBlock>,
295  #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
296  pub finally: Option<u16>,
297}
298
299#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
300#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
301pub struct WaitForFrame {
302  pub frame: u16,
303  pub skip: u8,
304}
305
306#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
307#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
308pub struct WaitForFrame2 {
309  pub skip: u8,
310}
311
312#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
313#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
314pub struct With {
315  pub size: u16,
316}