Skip to content

Commit ea2e317

Browse files
committed
Added U8 archive packing command to rustii CLI
1 parent 52e1179 commit ea2e317

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ target/
2323
*.cert
2424
*.footer
2525
*.app
26+
*.arc

src/archive/u8.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ impl U8Archive {
250250
})
251251
}
252252

253+
pub fn from_tree(node_tree: &Rc<RefCell<U8Directory>>) -> Result<Self, U8Error> {
254+
Ok(U8Archive {
255+
node_tree: node_tree.clone(),
256+
})
257+
}
258+
253259
fn pack_dir_recursive(file_names: &mut Vec<String>, file_data: &mut Vec<Vec<u8>>, u8_nodes: &mut Vec<U8Node>, current_node: &Rc<RefCell<U8Directory>>) {
254260
// For files, read their data into the file data list, add their name into the file name
255261
// list, then calculate the offset for their file name and create a new U8Node() for them.

src/bin/rustii/archive/u8.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::path::{Path, PathBuf};
99
use std::rc::Rc;
1010
use anyhow::{bail, Context, Result};
1111
use clap::Subcommand;
12+
use glob::glob;
1213
use rustii::archive::u8;
1314

1415
#[derive(Subcommand)]
@@ -30,8 +31,41 @@ pub enum Commands {
3031
}
3132
}
3233

33-
pub fn pack_u8_archive(_input: &str, _output: &str) -> Result<()> {
34-
todo!();
34+
fn pack_dir_recursive(dir: &Rc<RefCell<u8::U8Directory>>, in_path: PathBuf) -> Result<()> {
35+
let mut files = Vec::new();
36+
let mut dirs = Vec::new();
37+
for entry in glob(&format!("{}/*", in_path.display()))?.flatten() {
38+
match fs::metadata(&entry) {
39+
Ok(meta) if meta.is_file() => files.push(entry),
40+
Ok(meta) if meta.is_dir() => dirs.push(entry),
41+
_ => {} // Anything that isn't a normal file/directory just gets ignored.
42+
}
43+
}
44+
for file in files {
45+
let node = u8::U8File::new(file.file_name().unwrap().to_str().unwrap().to_owned(), fs::read(file)?);
46+
u8::U8Directory::add_file(dir, node);
47+
}
48+
for child_dir in dirs {
49+
let node = u8::U8Directory::new(child_dir.file_name().unwrap().to_str().unwrap().to_owned());
50+
u8::U8Directory::add_dir(dir, node);
51+
let dir = u8::U8Directory::get_child_dir(dir, child_dir.file_name().unwrap().to_str().unwrap()).unwrap();
52+
pack_dir_recursive(&dir, child_dir)?;
53+
}
54+
Ok(())
55+
}
56+
57+
pub fn pack_u8_archive(input: &str, output: &str) -> Result<()> {
58+
let in_path = Path::new(input);
59+
if !in_path.exists() {
60+
bail!("Source directory \"{}\" could not be found.", in_path.display());
61+
}
62+
let out_path = PathBuf::from(output);
63+
let node_tree = u8::U8Directory::new(String::new());
64+
pack_dir_recursive(&node_tree, in_path.to_path_buf()).with_context(|| "A U8 archive could not be packed.")?;
65+
let u8_archive = u8::U8Archive::from_tree(&node_tree).with_context(|| "An unknown error occurred while creating a U8 archive from the data.")?;
66+
fs::write(&out_path, &u8_archive.to_bytes()?).with_context(|| format!("Could not open output file \"{}\" for writing.", out_path.display()))?;
67+
println!("Successfully packed directory \"{}\" into U8 archive \"{}\"!", in_path.display(), out_path.display());
68+
Ok(())
3569
}
3670

3771
fn unpack_dir_recursive(dir: &Rc<RefCell<u8::U8Directory>>, out_path: PathBuf) -> Result<()> {
@@ -51,7 +85,7 @@ fn unpack_dir_recursive(dir: &Rc<RefCell<u8::U8Directory>>, out_path: PathBuf) -
5185
pub fn unpack_u8_archive(input: &str, output: &str) -> Result<()> {
5286
let in_path = Path::new(input);
5387
if !in_path.exists() {
54-
bail!("Source U8 archive \"{}\" could not be found.", input);
88+
bail!("Source U8 archive \"{}\" could not be found.", in_path.display());
5589
}
5690
let out_path = PathBuf::from(output);
5791
if out_path.exists() {

0 commit comments

Comments
 (0)