Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions randomizer/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ ootrr-cosmetics = { path = "../cosmetics" }

[dev-dependencies]
ootrr-game = { path = "../game", features = ["fixtures"] }
mimalloc = { version = "0.1", default-features = false }
4 changes: 4 additions & 0 deletions randomizer/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,7 @@ pub mod teleport_sim;
pub mod teleport_start;

pub const TITLE_ID: &str = "0400486024DEC000";

#[cfg(test)]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
1 change: 1 addition & 0 deletions randomizer/cosmetics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ ruzstd = "0.9.1"

[dev-dependencies]
ootrr-game = { path = "../game", features = ["fixtures"] }
mimalloc = { version = "0.1", default-features = false }
4 changes: 4 additions & 0 deletions randomizer/cosmetics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ pub mod cosmetic;
pub mod sfx;
pub mod slink;
pub mod title_logo;

#[cfg(test)]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
14 changes: 9 additions & 5 deletions randomizer/cosmetics/src/sfx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,8 @@ mod tests {
let Some(root) = std::env::var("OOTRR_ROMFS").map(PB::from).ok().filter(|p| p.join("Pack").join("ZsDic.pack.zs").is_file()) else {
return;
};
let mut rf: Romfs = Romfs::open(&root).unwrap();
let mut maps: Vec<BM<String, u32>> = Vec::new();
for seed in ["first", "second"] {
let check = |seed: &str| -> BM<String, u32> {
let mut rf: Romfs = Romfs::open(&root).unwrap();
let out: PB = std::env::temp_dir().join(format!("ootrr_sfx_{}_{seed}", std::process::id()));
let _ = fs::remove_dir_all(&out);
let notes: Vec<String> = apply(&out, &root, Sfx::Grouped, seed).unwrap();
Expand Down Expand Up @@ -615,9 +614,14 @@ mod tests {
assert_eq!(checked.len(), streams.len());
println!("{seed}: {} streamed and {shorts} short bank entries changed", streams.len());
assert!(shorts > 2000, "{shorts}");
maps.push(streams);
let _ = fs::remove_dir_all(&out);
}
streams
};
let check = &check;
let maps: Vec<BM<String, u32>> = std::thread::scope(|sc| {
let hs: Vec<std::thread::ScopedJoinHandle<BM<String, u32>>> = ["first", "second"].into_iter().map(|seed| sc.spawn(move || check(seed))).collect();
hs.into_iter().map(|h| h.join().unwrap()).collect()
});
assert!(maps[0].len() > 1000);
assert_ne!(maps[0], maps[1]);
}
Expand Down
113 changes: 67 additions & 46 deletions randomizer/game/src/byml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,37 +214,32 @@ impl<'a> Rd<'a> {
}
}

struct Wr {
struct Wr<'a> {
out: Vec<u8>,
be: bool,
keys: Vec<String>,
strs: Vec<String>,
keys: Vec<&'a str>,
strs: Vec<&'a str>,
wides: HashMap<(u8, u64), u32>,
seen: HashMap<Vec<u8>, u32>,
ids: Ids,
seen: HashMap<u32, u32>,
}

fn fold(s: &str) -> Vec<(u8, char)> {
s.chars()
.map(|c| {
let cls: u8 = if c.is_alphabetic() {
2
} else if c.is_ascii_digit() {
1
} else {
0
};
(cls, c.to_lowercase().next().unwrap_or(c))
})
.collect()
fn fold(s: &str) -> impl Iterator<Item = (u8, char)> + '_ {
s.chars().map(|c| {
let cls: u8 = if c.is_alphabetic() {
2
} else if c.is_ascii_digit() {
1
} else {
0
};
(cls, c.to_lowercase().next().unwrap_or(c))
})
}

fn order(m: &[(String, Node)]) -> Vec<usize> {
let mut ix: Vec<usize> = (0..m.len()).collect();
ix.sort_by(|a, b| {
fold(&m[*a].0)
.cmp(&fold(&m[*b].0))
.then_with(|| m[*b].0.cmp(&m[*a].0))
});
ix.sort_by(|a, b| fold(&m[*a].0).cmp(fold(&m[*b].0)).then_with(|| m[*b].0.cmp(&m[*a].0)));
ix
}

Expand All @@ -267,7 +262,7 @@ fn gather(n: &Node, out: &mut Vec<(u8, u64)>) {
}
}

fn ident(n: &Node, out: &mut Vec<u8>) {
fn ident(n: &Node, out: &mut Vec<u8>, ids: &mut Ids) {
match n {
Node::Null => out.push(0xFF),
Node::Bool(x) => out.extend_from_slice(&[0xD0, *x as u8]),
Expand Down Expand Up @@ -306,28 +301,46 @@ fn ident(n: &Node, out: &mut Vec<u8>) {
out.extend_from_slice(d);
}
Node::Arr(a) => {
out.push(0xC0);
out.extend_from_slice(&(a.len() as u32).to_le_bytes());
let mut key: Vec<u8> = vec![0xC0];
key.extend_from_slice(&(a.len() as u32).to_le_bytes());
for x in a {
ident(x, out);
ident(x, &mut key, ids);
}
ids.close(n, key, 0xC0, out);
}
Node::Map(m) => {
out.push(0xC1);
out.extend_from_slice(&(m.len() as u32).to_le_bytes());
let mut key: Vec<u8> = vec![0xC1];
key.extend_from_slice(&(m.len() as u32).to_le_bytes());
for e in m {
out.extend_from_slice(&(e.0.len() as u32).to_le_bytes());
out.extend_from_slice(e.0.as_bytes());
ident(&e.1, out);
key.extend_from_slice(&(e.0.len() as u32).to_le_bytes());
key.extend_from_slice(e.0.as_bytes());
ident(&e.1, &mut key, ids);
}
ids.close(n, key, 0xC1, out);
}
}
}

fn collect(n: &Node, keys: &mut BTreeSet<String>, strs: &mut BTreeSet<String>) {
#[derive(Default)]
struct Ids {
of: HashMap<*const Node, u32>,
shapes: HashMap<Vec<u8>, u32>,
}

impl Ids {
fn close(&mut self, n: &Node, key: Vec<u8>, tag: u8, out: &mut Vec<u8>) {
let next: u32 = self.shapes.len() as u32;
let id: u32 = *self.shapes.entry(key).or_insert(next);
self.of.insert(n as *const Node, id);
out.push(tag);
out.extend_from_slice(&id.to_le_bytes());
}
}

fn collect<'a>(n: &'a Node, keys: &mut BTreeSet<&'a str>, strs: &mut BTreeSet<&'a str>) {
match n {
Node::Str(s) => {
strs.insert(s.clone());
strs.insert(s);
}
Node::Arr(a) => {
for x in a {
Expand All @@ -336,15 +349,15 @@ fn collect(n: &Node, keys: &mut BTreeSet<String>, strs: &mut BTreeSet<String>) {
}
Node::Map(m) => {
for (k, v) in m {
keys.insert(k.clone());
keys.insert(k);
collect(v, keys, strs);
}
}
_ => {}
}
}

impl Wr {
impl Wr<'_> {
fn p32(&mut self, v: u32) {
let b: [u8; 4] = if self.be { v.to_be_bytes() } else { v.to_le_bytes() };
self.out.extend_from_slice(&b);
Expand Down Expand Up @@ -384,14 +397,22 @@ impl Wr {
}
}

fn id(&self, n: &Node) -> Res<u32> {
self.ids
.of
.get(&(n as *const Node))
.copied()
.ok_or(Error::Format("byml: container missing from the shape table"))
}

fn wide(&self, t: u8, v: u64) -> Res<u32> {
self.wides
.get(&(t, v))
.copied()
.ok_or(Error::Format("byml: wide value missing from block"))
}

fn table(&mut self, v: &[String]) {
fn table(&mut self, v: &[&str]) {
let at: usize = self.out.len();
self.out.push(0xC2);
self.p24(v.len() as u32);
Expand Down Expand Up @@ -423,7 +444,7 @@ impl Wr {
Node::Str(s) => {
let i: usize = self
.strs
.binary_search(s)
.binary_search(&s.as_str())
.map_err(|_| Error::Format("byml: string missing from table"))?;
Ok((0xA0, i as u32))
}
Expand All @@ -436,8 +457,7 @@ impl Wr {
Ok((0xA1, at))
}
Node::Arr(a) => {
let mut id: Vec<u8> = Vec::new();
ident(n, &mut id);
let id: u32 = self.id(n)?;
if let Some(at) = self.seen.get(&id) {
return Ok((0xC0, *at));
}
Expand Down Expand Up @@ -466,8 +486,7 @@ impl Wr {
if !es.iter().all(|e| names.insert(e.0.as_str())) {
return Err(Error::Format("byml: duplicate map key"));
}
let mut id: Vec<u8> = Vec::new();
ident(n, &mut id);
let id: u32 = self.id(n)?;
if let Some(at) = self.seen.get(&id) {
return Ok((0xC1, *at));
}
Expand All @@ -485,7 +504,7 @@ impl Wr {
let e: &(String, Node) = es[i];
let k: usize = self
.keys
.binary_search(&e.0)
.binary_search(&e.0.as_str())
.map_err(|_| Error::Format("byml: key missing from table"))?;
let (t, v): (u8, u32) = self.emit(&e.1)?;
let p: usize = slots + 8 * i;
Expand Down Expand Up @@ -540,17 +559,19 @@ impl Byml {
}

pub fn write(&self) -> Res<Vec<u8>> {
let mut ks: BTreeSet<String> = BTreeSet::new();
let mut ss: BTreeSet<String> = BTreeSet::new();
let mut ks: BTreeSet<&str> = BTreeSet::new();
let mut ss: BTreeSet<&str> = BTreeSet::new();
collect(&self.root, &mut ks, &mut ss);
let mut w: Wr = Wr {
out: Vec::new(),
be: self.big,
keys: ks.into_iter().collect(),
strs: ss.into_iter().collect(),
wides: HashMap::new(),
ids: Ids::default(),
seen: HashMap::new(),
};
ident(&self.root, &mut Vec::new(), &mut w.ids);
w.out.extend_from_slice(if self.big { b"BY" } else { b"YB" });
let v: [u8; 2] = if self.big { self.version.to_be_bytes() } else { self.version.to_le_bytes() };
w.out.extend_from_slice(&v);
Expand All @@ -561,12 +582,12 @@ impl Byml {
let mut so: u32 = 0;
if !w.keys.is_empty() {
ko = w.out.len() as u32;
let k: Vec<String> = w.keys.clone();
let k: Vec<&str> = w.keys.clone();
w.table(&k);
}
if !w.strs.is_empty() {
so = w.out.len() as u32;
let s: Vec<String> = w.strs.clone();
let s: Vec<&str> = w.strs.clone();
w.table(&s);
}
w.put_wides(&self.root);
Expand Down
Loading