world_move_bricks/
world_move_bricks.rs

1use brdb::{Brdb, IntoReader, Position, UnsavedGrid};
2use std::path::PathBuf;
3
4/// Reads a world and prints out some of its information
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let path = PathBuf::from("world.brdb");
7    let dst = PathBuf::from("world_patched.brdb");
8
9    println!("Warning - This code will break if the brick chunk struct changes!!");
10
11    let db = Brdb::open(path)?.into_reader();
12
13    let data = db.global_data()?;
14    let mut grid = UnsavedGrid::default();
15
16    let mut total_bricks = 0;
17    for chunk in db.brick_chunk_index(1)? {
18        for brick in db
19            .brick_chunk_soa(1, chunk.index)?
20            .iter_bricks(chunk.index, data.clone())
21        {
22            // If we wanted wires/components, we'd need to track the bricks here by their chunk index and brick index
23            total_bricks += 1;
24
25            let mut brick = brick?;
26            brick.position += Position::new(3000, 0, 0);
27            grid.add_brick(data.as_ref(), &brick);
28        }
29
30        if chunk.num_components > 0 {
31            println!("sorry, this example doesn't handle components");
32        }
33        if chunk.num_wires > 0 {
34            println!("sorry, this example doesn't handle wires");
35        }
36    }
37    println!("{total_bricks} bricks");
38
39    let mut pending = db.to_pending()?;
40
41    // Replace the main grid (1) with the grid we created
42    *pending.cd_mut("World/0/Bricks/Grids/1")? = grid.to_pending(
43        data.proc_brick_starting_index(),
44        db.components_schema()?.as_ref(),
45    )?;
46
47    if dst.exists() {
48        std::fs::remove_file(&dst)?;
49    }
50    Brdb::new(&dst)?.write_pending("Move the bricks", pending)?;
51
52    // Verify bricks can be read
53    let db = Brdb::open(dst)?.into_reader();
54    for chunk in db.brick_chunk_index(1)? {
55        let _ = db.brick_chunk_soa(1, chunk.index)?;
56    }
57
58    Ok(())
59}