|
| 1 | +[![brs on crates.io][cratesio-image]][cratesio] |
| 2 | +[![brs on docs.rs][docsrs-image]][docsrs] |
| 3 | + |
| 4 | +[cratesio-image]: https://img.shields.io/crates/v/brs.svg |
| 5 | +[cratesio]: https://crates.io/crates/brs |
| 6 | +[docsrs-image]: https://docs.rs/brs/badge.svg |
| 7 | +[docsrs]: https://docs.rs/brs |
| 8 | + |
| 9 | +Interfaces for reading and writing Brickadia save files. |
| 10 | + |
| 11 | +Aims to be able to read all previous versions just like the game, |
| 12 | +but only write the newest version of the format. |
| 13 | + |
| 14 | +# Usage |
| 15 | + |
| 16 | +## Reading |
| 17 | + |
| 18 | +First, create a reader from any |
| 19 | +[`Read`](https://doc.rust-lang.org/std/io/trait.Read.html) |
| 20 | +source, such as a file or buffer. |
| 21 | + |
| 22 | +```rust |
| 23 | +let reader = brs::Reader::new(File::open("village.brs")?)?; |
| 24 | +``` |
| 25 | + |
| 26 | +Brickadia save files have information split into sections ordered |
| 27 | +such that one can extract simple information |
| 28 | +without needing to parse the entire file. |
| 29 | + |
| 30 | +This library surfaces this by strictly enforcing the way that data is read |
| 31 | +and made available at the type level; you can't go wrong. |
| 32 | + |
| 33 | +To continue, reading the first header gets you basic information. |
| 34 | +For details on what is available, see |
| 35 | +[`HasHeader1`](https://docs.rs/brs/*/brs/read/trait.HasHeader1.html). |
| 36 | + |
| 37 | +```rust |
| 38 | +use brs::HasHeader1; |
| 39 | +let reader = reader.read_header1(); |
| 40 | +println!("Brick count: {}", reader.brick_count()); |
| 41 | +println!("Map: {}", reader.map()); |
| 42 | +``` |
| 43 | + |
| 44 | +The next header contains data less likely to be relevant for simpler |
| 45 | +introspection, but rather things such as tables for loading bricks. |
| 46 | +See [`HasHeader2`](https://docs.rs/brs/*/brs/read/trait.HasHeader2.html). |
| 47 | + |
| 48 | +```rust |
| 49 | +use brs::HasHeader2; |
| 50 | +let reader = reader.read_header2(); |
| 51 | +println!("Mods: {:?}", reader.mods()); |
| 52 | +println!("Color count: {}", reader.colors().len()); |
| 53 | +// Properties from header 1 are still available: |
| 54 | +println!("Description: {}", reader.description(); |
| 55 | +``` |
| 56 | + |
| 57 | +After both headers have been read, you may now iterate over the bricks. |
| 58 | +See [`Brick`](https://docs.rs/brs/*/brs/struct.Brick.html). |
| 59 | + |
| 60 | +```rust |
| 61 | +for brick in reader.iter_bricks()? { |
| 62 | + let brick = brick?; |
| 63 | + println!("{:?}", brick); |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +You may retain access to the header information while getting the iterator: |
| 68 | + |
| 69 | +```rust |
| 70 | +let (rdr, bricks) = reader.iter_bricks_and_reader()?; |
| 71 | +``` |
| 72 | + |
| 73 | +## Writing |
| 74 | + |
| 75 | +Writing save files isn't as fancy, for now you simply just put all the data |
| 76 | +in the [`WriteData`](https://docs.rs/brs/*/brs/struct.WriteData.html) struct and pass it to |
| 77 | +[`write_save`](https://docs.rs/brs/*/brs/fn.write_save.html) along with a |
| 78 | +[`Write`](https://doc.rust-lang.org/std/io/trait.Write.html) destination. |
| 79 | + |
| 80 | +```rust |
| 81 | +let data = brs::WriteData { |
| 82 | + map: String::from("Plate"), |
| 83 | + description: String::from("A quaint park full of ducks and turkeys."), |
| 84 | + // ... |
| 85 | +}; |
| 86 | +brs::write_save(&mut File::create("park.brs")?, &data)?; |
| 87 | +``` |
0 commit comments