read_world/
read_world.rs

1use brdb::{BrFsReader, Brdb, IntoReader};
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    // world file from argv
7    let filename = std::env::args()
8        .nth(1)
9        .unwrap_or_else(|| "world.brdb".to_string());
10    let path = PathBuf::from(filename);
11    if !path.exists() {
12        eprintln!("File does not exist: {}", path.display());
13        std::process::exit(1);
14    }
15
16    let db = Brdb::open(path)?.into_reader();
17
18    let data = db.global_data()?;
19    println!("Basic Brick assets: {:?}", data.basic_brick_asset_names);
20    println!("Wire ports: {:?}", data.component_wire_port_names);
21    println!("Component types: {:?}", data.component_type_names);
22    println!("Component structs: {:?}", data.component_data_struct_names);
23    println!("Component schemas: {}", db.components_schema()?);
24
25    let mut grid_ids = vec![1];
26
27    // Iterate all entity chunks to find dynamic brick grids...
28    // This could totally be a helper function
29    for index in db.entity_chunk_index()? {
30        for e in db.entity_chunk(index)? {
31            // Ensure the chunk is a dynamic brick grid
32            if !e.is_brick_grid() {
33                continue;
34            }
35            let Some(id) = e.id else {
36                continue;
37            };
38            grid_ids.push(id);
39        }
40    }
41
42    for gid in grid_ids {
43        println!("Reading grid {gid}");
44        let chunks = db.brick_chunk_index(gid)?;
45        println!("Brick chunks: {chunks:?}");
46        for chunk in chunks {
47            let soa = db.brick_chunk_soa(gid, chunk.index)?;
48            println!("Brick Soa {chunk}: {soa:?}");
49            if chunk.num_components > 0 {
50                let (_soa, components) = db.component_chunk_soa(gid, chunk.index)?;
51                // println!("Components soa: {soa}");
52                for c in components {
53                    println!("Component: {c}");
54                }
55            }
56            if chunk.num_wires > 0 {
57                let soa = db.wire_chunk_soa(gid, chunk.index)?;
58                println!("Wires soa: {soa}");
59            }
60        }
61    }
62
63    println!("Files: {}", db.get_fs()?.render());
64
65    Ok(())
66}