sb3-decoder 0.2.0

A Rust library for decoding Scratch 3.0 project files (.sb3)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
use crate::{macros::string_id, model::sprite::RotationStyle, prelude::*};

/// The representation of a Scratch script with maybe one hat block and many action blocks.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Script {
    pub hat: Option<Hat>,
    pub actions: Vec<Action>,
}

/// The representation of a Scratch hat block.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum Hat {
    // Events
    WhenFlagClicked,
    WhenKeyPressed { key: KeyField },
    WhenThisSpriteClicked,
    WhenStageClicked,
    WhenBackdropSwitchesTo { backdrop: String },

    WhenGreaterThan { loudness_timer: LoudnessTimerField, val: Reporter },

    WhenIReceive { broadcast: code::BroadcastId },

    // Control
    WhenIStartAsAClone,

    // Procedures
    DefineProcedure { prototype: ProcPrototype },
}

/// The representation of a Scratch action block.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum Action {
    // Motion
    MoveSteps { steps: Reporter },
    TurnRight { deg: Reporter },
    TurnLeft { deg: Reporter },

    GoTo { loc: Reporter },
    GoToXY { x: Reporter, y: Reporter },
    GlideSecsTo { secs: Reporter, loc: Reporter },
    GlideSecsToXY { secs: Reporter, x: Reporter, y: Reporter },

    PointInDirection { dir: Reporter },
    PointTowards { loc: Reporter },

    ChangeXBy { inc: Reporter },
    SetXTo { x: Reporter },
    ChangeYBy { inc: Reporter },
    SetYTo { y: Reporter },

    IfOnEdgeBounce,

    SetRotationStyle { style: RotationStyle },

    // Looks
    SayForSeconds { content: Reporter, secs: Reporter },
    Say { content: Reporter },
    ThinkForSeconds { content: Reporter, secs: Reporter },
    Think { content: Reporter },

    SwitchCostumeTo { costume: Reporter },
    NextCostume,
    SwitchBackdropTo { backdrop: Reporter },
    SwitchBackdropToAndWait { backdrop: Reporter },
    NextBackdrop,

    ChangeSizeBy { inc: Reporter },
    SetSizeTo { size: Reporter },

    ChangeGraphicEffectBy { effect: GraphicEffectField, inc: Reporter },
    SetGraphicEffectTo { effect: GraphicEffectField, val: Reporter },
    ClearGraphicEffects,

    Show,
    Hide,

    GoToLayer { front_back: FrontBackField },
    GoLayers { forward_backward: ForwardBackwardField, num: Reporter },

    // Sound
    PlaySoundUntilDone { sound: Reporter },
    PlaySound { sound: Reporter },
    StopAllSounds,

    ChangeSoundEffectBy { effect: SoundEffectField, inc: Reporter },
    SetSoundEffectTo { effect: SoundEffectField, val: Reporter },
    ClearSoundEffects,

    ChangeVolumeBy { inc: Reporter },
    SetVolumeTo { volume: Reporter },

    // Events
    Broadcast { broadcast: Reporter },
    BroadcastAndWait { broadcast: Reporter },

    // Control
    WaitSecs { secs: Reporter },

    Repeat { times: Reporter, body: Vec<Action> },
    Forever { body: Vec<Action> },

    If { cond: Reporter, then: Vec<Action> },
    IfElse { cond: Reporter, then: Vec<Action>, otherwise: Vec<Action> },
    WaitUntil { cond: Reporter },
    RepeatUntil { cond: Reporter, body: Vec<Action> },
    While { cond: Reporter, body: Vec<Action> },

    Stop { stop: StopField },

    CreateCloneOf { menu: Reporter },
    DeleteThisClone,

    // Sensing
    AskAndWait { content: Reporter },

    SetDragMode { draggable: bool },

    ResetTimer,

    // Data
    SetVariableTo { var: code::VarId, val: Reporter },
    ChangeVariableBy { var: code::VarId, inc: Reporter },
    ShowVariable { var: code::VarId },
    HideVariable { var: code::VarId },

    AddToList { list: code::ListId, val: Reporter },

    DeleteOfList { list: code::ListId, index: Reporter },
    DeleteAllOfList { list: code::ListId },
    InsertAtOfList { list: code::ListId, val: Reporter, index: Reporter },
    ReplaceItemOfListWith { list: code::ListId, index: Reporter, val: Reporter },

    ShowList { list: code::ListId },
    HideList { list: code::ListId },

    // Procedures
    CallProcedure { proccode: String, args: Vec<Reporter> },

    // Unknown
    Unknown(UnknownBlock),
}

/// The representation of a Scratch reporter block.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum Reporter {
    // Primitives and Empty
    Number { num: f64 },                        // 4
    PosNumber { num: f64 },                     // 5
    PosInteger { num: u64 },                    // 6
    Integer { num: i64 },                       // 7
    Angle { num: f64 },                         // 8
    Color { r: u8, g: u8, b: u8 },              // 9
    Text { text: String },                      // 10
    Broadcast { broadcast: code::BroadcastId }, // 11
    Variable { var: code::VarId },              // 12
    List { list: code::ListId },                // 13
    Empty,

    // Motion
    LocationMenu { menu: LocationMenu },

    XPosition,
    YPosition,
    Direction,

    // Looks
    CostumeMenu { costume: String },
    BackdropMenu { backdrop: String },

    CurrentCostume { number_name: NumberNameField },
    CurrentBackdrop { number_name: NumberNameField },
    Size,

    // Sound
    SoundMenu { sound: String },

    Volume,

    // Control
    CloneOfMenu { menu: CloneOfMenu },

    // Sensing
    KeyMenu { menu: KeyField },
    TouchingObjectMenu { menu: TouchingObjectMenu },
    DistanceToMenu { menu: DistanceToMenu },
    OfObjectMenu { menu: OfObjectMenu },

    TouchingObject { object: Box<Reporter> },
    TouchingColor { color: Box<Reporter> },
    ColorTouchingColor { a: Box<Reporter>, b: Box<Reporter> },
    DistanceToObject { object: Box<Reporter> },

    Answer,
    KeyPressed { key: Box<Reporter> },
    MouseDown,
    MouseX,
    MouseY,

    Loudness,
    Timer,

    PropertyOfObject { property: PropertyField, object: Box<Reporter> },

    Current { current: CurrentField },
    DaysSince2000,

    Username,

    // Operators
    Add { a: Box<Reporter>, b: Box<Reporter> },
    Sub { a: Box<Reporter>, b: Box<Reporter> },
    Mul { a: Box<Reporter>, b: Box<Reporter> },
    Div { a: Box<Reporter>, b: Box<Reporter> },

    PickRandom { min: Box<Reporter>, max: Box<Reporter> },

    Gt { a: Box<Reporter>, b: Box<Reporter> },
    Lt { a: Box<Reporter>, b: Box<Reporter> },
    Eq { a: Box<Reporter>, b: Box<Reporter> },

    And { a: Box<Reporter>, b: Box<Reporter> },
    Or { a: Box<Reporter>, b: Box<Reporter> },
    Not { x: Box<Reporter> },

    Join { a: Box<Reporter>, b: Box<Reporter> },
    LetterOf { index: Box<Reporter>, text: Box<Reporter> },
    LengthOfText { text: Box<Reporter> },

    TextContains { haystack: Box<Reporter>, needle: Box<Reporter> },

    Mod { a: Box<Reporter>, b: Box<Reporter> },
    Round { x: Box<Reporter> },

    MathFunc { func: MathFuncField, x: Box<Reporter> },

    // Data
    ItemOfList { list: code::ListId, index: Box<Reporter> },
    ItemNumOfItemInList { list: code::ListId, val: Box<Reporter> },
    LengthOfList { list: code::ListId },
    ListContains { haystack: code::ListId, needle: Box<Reporter> },

    // Procedures
    Argument { name: String },

    // Unknown
    Unknown(UnknownBlock),
}

#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ProcPrototype {
    pub proccode: String,
    pub params: Vec<Param>,
    pub warp: bool,
}

#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Param {
    pub id: ArgId,
    pub name: String,
    pub kind: ParamKind,
    pub default: code::Value,
}

string_id! {
    /// The ID of a procedure parameter.
    ArgId
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParamKind {
    StringNumber,
    Boolean,
}

/// A block this crate doesn't understand, kept in a lowered but uninterpreted form. If you think
/// this block should be added, create an issue on the repository. If you don't want to allow
/// unknown blocks, use [`Decoder::allow_unknown_blocks`].
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct UnknownBlock {
    pub opcode: String,
    pub inputs: Vec<(String, UnknownInput)>,
    pub fields: Vec<(String, UnknownField)>,
}

#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum UnknownInput {
    /// A literal or reference, already lowered.
    Reporter(Reporter),
    /// A C-block substack.
    Stack(Vec<Action>),
}

#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct UnknownField {
    pub value: String,
    pub id: Option<String>,
}

/// The location menu used by some motion blocks.
///
/// Specifically:
/// - `go to (menu)`
/// - `glide (secs) secs to (menu)`
/// - `point towards (menu)`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum LocationMenu {
    Random,
    MousePointer,
    Sprite(String),
}

/// The effect field used by some looks blocks.
///
/// Specifically:
/// - `change [graphic effect] effect by (inc)`
/// - `set [graphic effect] effect to (value)`
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GraphicEffectField {
    Color,
    Fisheye,
    Whirl,
    Pixelate,
    Mosaic,
    Brightness,
    Ghost,
}

/// The front-back field used by `go to [front-back] layer`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FrontBackField {
    Front,
    Back,
}

/// The forward-backward field used by `go [forward-backward] (num) layers`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ForwardBackwardField {
    Forward,
    Backward,
}

/// The number-name field used by some looks blocks.
///
/// Specifically:
/// - `(costume [number-name])`
/// - `(backdrop [number-name])`
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NumberNameField {
    Number,
    Name,
}

/// The effect field used by some sound blocks.
///
/// Specifically:
/// - `change [sound effect] effect by (inc)`
/// - `set [sound effect] effect to (value)`
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SoundEffectField {
    Pitch,
    PanLeftRight,
}

/// The key field used by `when [key] key pressed`. Additionally, [`Reporter::KeyMenu`] also uses
/// this enum.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum KeyField {
    Space,
    UpArrow,
    DownArrow,
    LeftArrow,
    RightArrow,
    Any,
    Letter(char),
    Digit(u8),
    Other(String),
}

/// The loudness-timer field used by `when [loudness-timer] > (val)`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum LoudnessTimerField {
    Loudness,
    Timer,
}

/// The stop field used by `stop [stop]`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StopField {
    All,
    ThisScript,
    OtherScriptsInSprite,
}

/// The object menu used by `<touching (menu)?>`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum TouchingObjectMenu {
    MousePointer,
    Edge,
    Sprite(String),
}

/// The object menu used by `(distance to (menu))`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DistanceToMenu {
    MousePointer,
    Sprite(String),
}

/// The object menu used by `([property] of (object))`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum OfObjectMenu {
    Stage,
    Sprite(String),
}

/// The property field used by `([property] of (object))`.
///
/// Not every property is meaningful for every object (for example,
/// `BackdropNumber` only applies to the stage); Scratch itself does not enforce this.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PropertyField {
    // Sprite properties
    XPosition,
    YPosition,
    Direction,
    CostumeNumber,
    CostumeName,
    Size,

    // Stage properties
    BackdropNumber,
    BackdropName,

    // Both
    Volume,

    /// Any other string is a variable name on the target.
    Variable(String),
}

/// The current field used by `(current [current])`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CurrentField {
    Year,
    Month,
    Date,
    DayOfWeek,
    Hour,
    Minute,
    Second,
}

/// The clone of menu used by `create clone of [menu]`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CloneOfMenu {
    Myself,
    Sprite(String),
}

/// The math function field used by `([math-func] of (x))`
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MathFuncField {
    Abs,
    Floor,
    Ceiling,
    Sqrt,
    Sin,
    Cos,
    Tan,
    Asin,
    Acos,
    Atan,
    Ln,
    Log,
    EToPower,
    TenToPower,
}