sb3-decoder 0.1.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
//! The target module containing the [`Target`] enum.
//!
//! It also contains the [`Sprite`], [`Broadcast`] and [`Stage`] structs.

use std::collections::HashMap;

use indexmap::IndexMap;
use zip::ZipArchive;

use crate::{
    decoder::{RawSprite, RawStage, RawTarget},
    error::DecodeError,
    structs::{decode_scripts, Costume, List, Script, Sound, Variable},
};

/// The [`Target`] enum represents either a sprite or the stage in a Scratch 3.0 project.
#[derive(Debug, Clone)]
pub enum Target {
    /// A sprite target.
    Sprite(Sprite),

    /// The stage target.
    Stage(Stage),
}

impl Target {
    pub fn new(
        raw: RawTarget,
        zip: &mut ZipArchive<std::io::Cursor<Vec<u8>>>,
    ) -> Result<Self, DecodeError> {
        Ok(match raw {
            RawTarget::Sprite(raw_sprite) => Target::Sprite(Sprite::new(raw_sprite, zip)?),
            RawTarget::Stage(raw_stage) => Target::Stage(Stage::new(raw_stage, zip)?),
        })
    }
}

/// The [`Sprite`] struct represents a sprite in a Scratch 3.0 project.
#[derive(Debug, Clone)]
pub struct Sprite {
    /// The name of the sprite.
    pub name: String,

    /// Contains all the variables of the sprite.
    pub variables: HashMap<String, Variable>,

    /// Contains all the lists of the sprite.
    pub lists: HashMap<String, List>,

    /// Contains all the scripts of the sprite.
    pub scripts: Vec<Script>,

    /// The index of the current costume.
    pub current_costume: usize,

    /// Contains all the costumes of the sprite.
    costumes: IndexMap<String, Costume>,

    /// Contains all the sounds of the sprite.
    pub sounds: HashMap<String, Sound>,

    /// The sprite's volume (0.0 = 0%, 1.0 = 100%)
    pub volume: f32,

    /// The layer of the sprite.
    pub layer_order: isize,

    /// If the sprite is visible.
    pub visible: bool,

    /// The position of the sprite.
    pub position: (i32, i32),

    /// The size of the sprite (0.0 = 0%, 1.0 = 100%)
    pub size: f32,

    /// The direction of the sprite in degrees.
    pub direction: i32,

    /// If the sprite is draggable.
    pub draggable: bool,

    /// The rotation style of the sprite.
    pub rotation_style: RotationStyle,
}

impl Sprite {
    /// Returns a new sprite from a [`RawSprite`] and the zip archive.
    pub fn new(
        raw: RawSprite,
        zip: &mut ZipArchive<std::io::Cursor<Vec<u8>>>,
    ) -> Result<Self, DecodeError> {
        let variables = raw
            .variables
            .into_iter()
            .map(|(_, raw_var)| (raw_var.name.clone(), Variable(raw_var.value.into())))
            .collect();
        #[cfg(feature = "costume_png")]
        let costumes = raw
            .costumes
            .into_iter()
            .map(|raw| {
                let mut file = zip.by_name(&raw.md5ext)
                    .map_err(|_| DecodeError::NotFound(raw.md5ext.clone()))?;
                let mut buffer = Vec::new();
                std::io::copy(&mut file, &mut buffer)?;
                match raw.data_format.as_str() {
                    "svg" => {
                        let svg_data = std::str::from_utf8(&buffer).map_err(|_| {
                            DecodeError::InvalidData("SVG data is not valid UTF-8".to_string())
                        })?;
                        let svg_tree =
                            resvg::usvg::Tree::from_str(svg_data, &resvg::usvg::Options::default())
                                .map_err(|e| {
                                    DecodeError::InvalidData(format!("Failed to parse SVG: {}", e))
                                })?;
                        let image = crate::utils::rasterize_svg_to_png(
                            &svg_tree,
                            svg_tree.size().width() as u32,
                            svg_tree.size().height() as u32,
                        )
                        .map_err(|e| {
                            DecodeError::InvalidData(format!("Failed to rasterize SVG: {}", e))
                        })?;
                        Ok((raw.name, Costume(image)))
                    }
                    _ => {
                        let image = image::load_from_memory(&buffer).map_err(|e| {
                            DecodeError::InvalidData(format!("Failed to load image: {}", e))
                        })?;
                        Ok((raw.name, Costume(image)))
                    }
                }
            })
            .collect::<Result<IndexMap<String, Costume>, DecodeError>>()?;
        #[cfg(feature = "costume_svg")]
        let costumes = raw
            .costumes
            .into_iter()
            .map(|raw| {
                let mut file = zip.by_name(&raw.md5ext)
                    .map_err(|_| DecodeError::NotFound(raw.md5ext.clone()))?;
                let mut buffer = Vec::new();
                std::io::copy(&mut file, &mut buffer)?;
                match raw.data_format.as_str() {
                    "svg" => {
                        let svg_data = std::str::from_utf8(&buffer).map_err(|_| {
                            DecodeError::InvalidData("SVG data is not valid UTF-8".to_string())
                        })?;
                        let svg_tree = usvg::Tree::from_str(svg_data, &usvg::Options::default())
                            .map_err(|e| {
                                DecodeError::InvalidData(format!("Failed to parse SVG: {}", e))
                            })?;
                        Ok((raw.name, Costume(svg_tree)))
                    }
                    _ => Err(DecodeError::InvalidData(format!(
                        "Unsupported data format: {}",
                        raw.data_format
                    ))),
                }
            })
            .collect::<Result<IndexMap<String, Costume>, DecodeError>>()?;
        let sounds = raw
            .sounds
            .into_iter()
            .map(|raw| {
                let mut file = zip.by_name(&raw.md5ext)
                    .map_err(|_| DecodeError::NotFound(raw.md5ext.clone()))?;
                let mut buffer = Vec::new();
                std::io::copy(&mut file, &mut buffer)?;
                let wav_reader = hound::WavReader::new(&buffer[..]).map_err(|e| {
                    DecodeError::InvalidData(format!("Failed to read WAV data: {}", e))
                })?;
                let spec = wav_reader.spec();
                let samples: Vec<i16> = wav_reader
                    .into_samples::<i16>()
                    .collect::<Result<_, _>>()
                    .map_err(|e| {
                        DecodeError::InvalidData(format!("Failed to read WAV samples: {}", e))
                    })?;
                Ok((
                    raw.name.clone(),
                    Sound {
                        data: samples,
                        rate: spec.sample_rate,
                    },
                ))
            })
            .collect::<Result<HashMap<String, Sound>, DecodeError>>()?;
        let lists = raw
            .lists
            .into_iter()
            .map(|(_, raw_list)| {
                (
                    raw_list.0.clone(),
                    List(raw_list.1.iter().map(|v| v.clone().into()).collect()),
                )
            })
            .collect();

        Ok(Self {
            name: raw.name,
            variables,
            lists,
            scripts: decode_scripts(&raw.blocks)?,
            current_costume: raw.current_costume,
            costumes,
            sounds,
            volume: raw.volume as f32 / 100.0,
            layer_order: raw.layer_order,
            visible: raw.visible,
            position: (raw.x, raw.y),
            size: raw.size as f32 / 100.0,
            direction: raw.direction,
            draggable: raw.draggable,
            rotation_style: RotationStyle::try_from(raw.rotation_style.as_str())
                .map_err(|_| DecodeError::InvalidData("Invalid rotation style".to_string()))?,
        })
    }

    /// Returns a reference to a costume by its name, if it exists.
    pub fn get_costume(&self, name: &str) -> Option<&Costume> {
        self.costumes.get(name)
    }

    /// Returns a mutable reference to a costume by its name, if it exists.
    pub fn get_costume_mut(&mut self, name: &str) -> Option<&mut Costume> {
        self.costumes.get_mut(name)
    }

    /// Returns all the costume names in the sprite.
    pub fn costume_names(&self) -> Vec<&String> {
        self.costumes.keys().collect()
    }

    /// Returns all the costumes in the sprite.
    pub fn costumes(&self) -> Vec<&Costume> {
        self.costumes.values().collect()
    }
}

/// The [`Stage`] struct represents the stage in a Scratch 3.0 project.
#[derive(Debug, Clone)]
pub struct Stage {
    /// Contains all the global variables in the project.
    pub variables: HashMap<String, Variable>,

    // Contains all the global lists in the project.
    pub lists: HashMap<String, List>,

    /// Contains all the broadcasts in the project.
    pub broadcasts: Vec<Broadcast>,

    /// Contains all the scripts in the stage.
    pub scripts: Vec<Script>,

    /// The index of the current backdrop.
    pub current_backdrop: usize,

    /// Contains all the backdrops in the project.
    backdrops: IndexMap<String, Costume>,

    /// Contains all the sounds of the stage.
    pub sounds: HashMap<String, Sound>,

    /// The stage's volume (0.0 = 0%, 1.0 = 100%)
    pub volume: f32,
}

impl Stage {
    /// Returns a new stage from a [`RawStage`] and the zip archive.
    pub fn new(
        raw: RawStage,
        zip: &mut ZipArchive<std::io::Cursor<Vec<u8>>>,
    ) -> Result<Self, DecodeError> {
        let variables = raw
            .variables
            .into_iter()
            .map(|(_, raw_var)| (raw_var.name.clone(), Variable(raw_var.value.into())))
            .collect();
        #[cfg(feature = "costume_png")]
        let backdrops = raw
            .backdrops
            .into_iter()
            .map(|raw| {
                let mut file = zip.by_name(&raw.md5ext)
                    .map_err(|_| DecodeError::NotFound(raw.md5ext.clone()))?;
                let mut buffer = Vec::new();
                std::io::copy(&mut file, &mut buffer)?;
                match raw.data_format.as_str() {
                    "svg" => {
                        let svg_data = std::str::from_utf8(&buffer).map_err(|_| {
                            DecodeError::InvalidData("SVG data is not valid UTF-8".to_string())
                        })?;
                        let svg_tree =
                            resvg::usvg::Tree::from_str(svg_data, &resvg::usvg::Options::default())
                                .map_err(|e| {
                                    DecodeError::InvalidData(format!("Failed to parse SVG: {}", e))
                                })?;
                        let image = crate::utils::rasterize_svg_to_png(
                            &svg_tree,
                            svg_tree.size().width() as u32,
                            svg_tree.size().height() as u32,
                        )
                        .map_err(|e| {
                            DecodeError::InvalidData(format!("Failed to rasterize SVG: {}", e))
                        })?;
                        Ok((raw.name, Costume(image)))
                    }
                    _ => {
                        let image = image::load_from_memory(&buffer).map_err(|e| {
                            DecodeError::InvalidData(format!("Failed to load image: {}", e))
                        })?;
                        Ok((raw.name, Costume(image)))
                    }
                }
            })
            .collect::<Result<IndexMap<String, Costume>, DecodeError>>()?;
        #[cfg(feature = "costume_svg")]
        let backdrops = raw
            .backdrops
            .into_iter()
            .map(|raw| {
                let mut file = zip.by_name(&raw.md5ext)
                    .map_err(|_| DecodeError::NotFound(raw.md5ext.clone()))?;
                let mut buffer = Vec::new();
                std::io::copy(&mut file, &mut buffer)?;
                match raw.data_format.as_str() {
                    "svg" => {
                        let svg_data = std::str::from_utf8(&buffer).map_err(|_| {
                            DecodeError::InvalidData("SVG data is not valid UTF-8".to_string())
                        })?;
                        let svg_tree = usvg::Tree::from_str(svg_data, &usvg::Options::default())
                            .map_err(|e| {
                                DecodeError::InvalidData(format!("Failed to parse SVG: {}", e))
                            })?;
                        Ok((raw.name, Costume(svg_tree)))
                    }
                    _ => Err(DecodeError::InvalidData(format!(
                        "Unsupported data format: {}",
                        raw.data_format
                    ))),
                }
            })
            .collect::<Result<IndexMap<String, Costume>, DecodeError>>()?;
        let sounds = raw
            .sounds
            .into_iter()
            .map(|raw| {
                let mut file = zip.by_name(&raw.md5ext)
                    .map_err(|_| DecodeError::NotFound(raw.md5ext.clone()))?;
                let mut buffer = Vec::new();
                std::io::copy(&mut file, &mut buffer)?;
                let wav_reader = hound::WavReader::new(&buffer[..]).map_err(|e| {
                    DecodeError::InvalidData(format!("Failed to read WAV data: {}", e))
                })?;
                let spec = wav_reader.spec();
                let samples: Vec<i16> = wav_reader
                    .into_samples::<i16>()
                    .collect::<Result<_, _>>()
                    .map_err(|e| {
                        DecodeError::InvalidData(format!("Failed to read WAV samples: {}", e))
                    })?;
                Ok((
                    raw.name.clone(),
                    Sound {
                        data: samples,
                        rate: spec.sample_rate,
                    },
                ))
            })
            .collect::<Result<HashMap<String, Sound>, DecodeError>>()?;
        let lists = raw
            .lists
            .into_iter()
            .map(|(_, raw_list)| {
                (
                    raw_list.0.clone(),
                    List(raw_list.1.iter().map(|v| v.clone().into()).collect()),
                )
            })
            .collect();

        Ok(Self {
            variables,
            lists,
            broadcasts: raw
                .broadcasts
                .into_iter()
                .map(|(_, msg)| Broadcast(msg))
                .collect(),
            scripts: decode_scripts(&raw.blocks)?,
            current_backdrop: raw.current_backdrop,
            backdrops,
            sounds,
            volume: raw.volume as f32 / 100.0,
        })
    }

    /// Returns a reference to a backdrop by its name, if it exists.
    pub fn get_backdrop(&self, name: &str) -> Option<&Costume> {
        self.backdrops.get(name)
    }

    /// Returns a mutable reference to a backdrop by its name, if it exists.
    pub fn get_backdrop_mut(&mut self, name: &str) -> Option<&mut Costume> {
        self.backdrops.get_mut(name)
    }

    /// Returns all the backdrop names in the stage.
    pub fn backdrop_names(&self) -> Vec<&String> {
        self.backdrops.keys().collect()
    }

    /// Returns all the backdrops in the stage.
    pub fn backdrops(&self) -> Vec<&Costume> {
        self.backdrops.values().collect()
    }
}

/// The [`Broadcast`] struct represents a usable broadcast message in a Scratch 3.0 project.
#[derive(Debug, Clone)]
pub struct Broadcast(pub String);

/// The [`RotationStyle`] enum represents the rotation style of a sprite in a Scratch 3.0 project.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RotationStyle {
    /// The sprite rotates freely.
    AllAround,

    /// The sprite rotates left and right.
    LeftRight,

    /// The sprite does not rotate.
    DontRotate,
}

impl TryFrom<&str> for RotationStyle {
    type Error = &'static str;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "all around" => Ok(RotationStyle::AllAround),
            "left-right" => Ok(RotationStyle::LeftRight),
            "don't rotate" => Ok(RotationStyle::DontRotate),
            _ => Err("Invalid rotation style"),
        }
    }
}