Skip to main content

read_entities/
read_entities.rs

1use brdb::{Brdb, IntoReader};
2use std::path::PathBuf;
3
4/// Reads a world and prints out all entities with their structs and class names
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    // Get global data to access entity type names and class names
19    let data = db.global_data()?;
20    println!("=== Entity Type Names ===");
21    for ((i, name), class) in data
22        .entity_type_names
23        .iter()
24        .enumerate()
25        .zip(data.entity_data_class_names.iter())
26    {
27        println!("{i}: {name} - {class}");
28    }
29    println!();
30
31    // Read all entity chunks
32    println!("=== Entity Chunks ===");
33    let entity_chunk_indices = db.entity_chunk_index()?;
34    println!("Found {} entity chunks", entity_chunk_indices.len());
35    println!();
36
37    let mut total_entities = 0;
38    for chunk_index in entity_chunk_indices {
39        println!("--- Chunk {} ---", chunk_index);
40        let entities = db.entity_chunk(chunk_index)?;
41
42        for (i, entity) in entities.iter().enumerate() {
43            total_entities += 1;
44            println!("  Entity {i}:");
45            println!("    Asset: {}", entity.asset);
46            println!("    ID: {:?}", entity.id);
47            println!("    Owner Index: {:?}", entity.owner_index);
48            println!("    Location: {:?}", entity.location);
49            println!("    Rotation: {:?}", entity.rotation);
50            println!("    Velocity: {:?}", entity.velocity);
51            println!("    Angular Velocity: {:?}", entity.angular_velocity);
52            println!("    Frozen: {:?}", entity.frozen);
53            println!("    Sleeping: {:?}", entity.sleeping);
54            println!();
55        }
56    }
57
58    println!("=== Summary ===");
59    println!("Total entities: {total_entities}");
60
61    // Also print the entity chunk SoA data
62    println!();
63    println!("=== Entity Chunk SoA Data ===");
64    for chunk_index in db.entity_chunk_index()? {
65        println!("--- Chunk {} (SoA) ---", chunk_index);
66        let (soa, entity_data) = db.entity_chunk_soa(chunk_index)?;
67
68        println!("  Type Counters: {:?}", soa.type_counters);
69        println!("  Number of entities with data: {}", entity_data.len());
70
71        for (i, data) in entity_data.iter().enumerate() {
72            if let Some(struct_data) = data {
73                println!("  Entity {i} struct: {struct_data}");
74            } else {
75                println!("  Entity {i}: None");
76            }
77        }
78        println!();
79    }
80
81    Ok(())
82}