world_replace_owner/
world_replace_owner.rs

1use brdb::{
2    Brdb, Brz, Guid, IntoReader, OwnerTableSoA, pending::BrPendingFs, schemas::OWNER_TABLE_SOA,
3};
4use std::path::PathBuf;
5
6/// Opens a world and replaces its owners with PUBLIC
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let src = PathBuf::from("world.brdb");
9    let dst = PathBuf::from("world_patched.brz");
10
11    assert!(src.exists());
12
13    let db = Brdb::open(src)?.into_reader();
14
15    let owners = db.owners_soa()?;
16
17    // Parse the owners from BrdbValues
18    let mut new_soa = OwnerTableSoA::try_from(&owners.to_value())?;
19
20    // Modify the owner ids
21    new_soa
22        .display_names
23        .iter_mut()
24        .for_each(|id| *id = "PUBLIC".to_owned());
25    new_soa
26        .user_names
27        .iter_mut()
28        .for_each(|id| *id = "PUBLIC".to_owned());
29    new_soa
30        .user_ids
31        .iter_mut()
32        .for_each(|id| *id = Guid::default());
33
34    // convert the owners struct of arrays into bytes using the owners schema
35    let content = db.owners_schema()?.write_brdb(OWNER_TABLE_SOA, &new_soa)?;
36
37    let patch = BrPendingFs::Root(vec![(
38        "World".to_owned(),
39        BrPendingFs::Folder(Some(vec![(
40            "0".to_string(),
41            BrPendingFs::Folder(Some(vec![(
42                "Owners.mps".to_string(),
43                BrPendingFs::File(Some(content)),
44            )])),
45        )])),
46    )]);
47
48    // use .to_pending_patch() if you want to update the same world
49    let pending = db.to_pending()?.with_patch(patch)?;
50    if dst.exists() {
51        std::fs::remove_file(&dst)?;
52    }
53    Brz::write_pending(&dst, pending)?;
54
55    println!("{}", Brz::open(&dst)?.into_reader().owners_soa()?);
56
57    Ok(())
58}