//! Shuffles montyformat files into one, game by game, picking each next game //! from a file chosen in proportion to the bytes it has left. //! //! interleave ... use std::{ fs::{self, File}, io::{BufReader, BufWriter, Write}, }; use montyformat::{FastDeserialise, MontyFormat}; fn main() -> std::io::Result<()> { let mut args = std::env::args().skip(1); let output = args.next().expect("usage: interleave ..."); let mut inputs: Vec = Vec::new(); for arg in args { let path = std::path::Path::new(&arg); if path.is_dir() { let mut files: Vec = fs::read_dir(path)? .filter_map(|e| e.ok().map(|e| e.path())) .filter(|p| p.is_file() && p.extension().and_then(|e| e.to_str()) == Some("binpack")) .map(|p| p.to_string_lossy().into_owned()) .collect(); files.sort(); inputs.extend(files); } else { inputs.push(arg); } } assert!(!inputs.is_empty(), "no input files"); assert!(!inputs.contains(&output), "the output must not be one of the inputs"); println!("Writing to {output:#?}"); println!("Reading from:\n{inputs:#?}"); let mut streams = Vec::new(); let mut total = 0; let target = File::create(&output)?; let mut writer = BufWriter::new(target); for path in &inputs { let file = File::open(path)?; let count = file.metadata()?.len(); if count > 0 { streams.push((count, BufReader::new(file))); total += count; } } let mut remaining = total; let mut rng = RandU64::default(); const INTERVAL: u64 = 1024 * 1024 * 256; let mut prev = remaining / INTERVAL; let mut buffer = Vec::new(); while remaining > 0 { let mut spot = rng.rand() % remaining; let mut idx = 0; while streams[idx].0 < spot { spot -= streams[idx].0; idx += 1; } let (count, reader) = &mut streams[idx]; MontyFormat::deserialise_fast_into_buffer(reader, &mut buffer)?; writer.write_all(&buffer)?; let size = buffer.len() as u64; remaining -= size; *count -= size; if *count == 0 { streams.swap_remove(idx); } if remaining / INTERVAL < prev { prev = remaining / INTERVAL; let written = total - remaining; print!("Written {written}/{total} Bytes ({:.2}%)\r", written as f64 / total as f64 * 100.0); let _ = std::io::stdout().flush(); } } Ok(()) } struct RandU64(u64); impl Default for RandU64 { fn default() -> Self { Self( (std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).expect("valid").as_nanos() & 0xFFFF_FFFF_FFFF_FFFF) as u64, ) } } impl RandU64 { fn rand(&mut self) -> u64 { self.0 ^= self.0 << 13; self.0 ^= self.0 >> 7; self.0 ^= self.0 << 17; self.0 } }