Skip to main content

Crate scratch_test_model

Crate scratch_test_model 

Source
Expand description

§bast3st-scratch

Repository Project page 100% Rust Reference on GitHub Pages brainmade.org

§scratch-test-model

Model to parse the *.sb3 file format of the Scratch block-oriented programming language

(I want to note that I was unable to find the above link to the format specification when I was developing this so I reverse-engineered the format from example files. Luckily, it looks like I correctly understood the meaning of the components. This is the second version of the parser that supports all standard blocks and some extensions even blocks that aren’t supported by the interpreter.)

The program components extracted from a *.sb3 file are represented by the ProjectDoc type which offers different methods for parsing a file or a sequence of bytes representing the file’s content. The simplest is to first parse the document as json as this value will be borrowed internally.

let json = json_from_sb3_file("/path/to/file.sb3").unwrap();
let doc = ProjectDoc::from_json(&json);
println!("{doc:#?}");

§Steps

§JSON extraction

A Scratch file is a ZIP file with images, sounds, … and a project.json that contains all information about the program structure.

If the used decompression algorithm is unable to read the file or if you want to provide the project.json file directly you can use ProjectDoc::from_json which can be called with an already parsed JSON document.

(See ProjectDoc for details.)

(The next sections will explain how all program blocks are extracted from an entered JSON document – regardless how it was obtained in this section.)

§Targets

Each document contains some meta information and – most importantly – a targets field containing a list of “targets”. A target is a sprite or the background and contains different data. (The sprites have local variables and the background’s variables and lists are seen as global.)

The relevant information stored in the modeled target:

  • TargetVariables: all variables of the target (local for sprites and global for the stage)
  • TargetLists: all lists of the target (local for sprites and global for the stage)
  • TargetBlocks: collection of all statement, expression, comparison, event, … blocks of the program
  • TargetProcedures: generated from the blocks. (The information about a procedure is split across multiple blocks in a weird format. This combines this data per procedure)

(See Target for details.)

Re-exports§

pub use scopes::ProjectDoc;

Modules§

attrs
blocks
error
scopes

Functions§

json_from_sb3_file
json_from_sb3_stream

Type Aliases§

Id