Skip to content

Commit 754a73e

Browse files
committed
Add dump-to-inp example
1 parent 9e3fffa commit 754a73e

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

inox2d/examples/dump-to-inp.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::fs::File;
2+
use std::path::PathBuf;
3+
4+
use clap::Parser;
5+
use inox2d::formats::inp::dump_to_inp;
6+
7+
#[derive(Parser, Debug)]
8+
#[command(author, version, about, long_about = None)]
9+
struct Cli {
10+
#[arg(help = "The dump directory.")]
11+
dump_dir: PathBuf,
12+
13+
#[arg(short, long, help = "Output file (.inp or .inx)")]
14+
output: PathBuf,
15+
}
16+
17+
fn main() {
18+
let cli = Cli::parse();
19+
20+
let mut output = File::create(&cli.output).unwrap();
21+
dump_to_inp(&cli.dump_dir, &mut output).unwrap();
22+
}

inox2d/src/formats/inp.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ pub fn parse_inp<R: Read>(mut data: R) -> Result<Model, ParseInpError> {
107107
vendors,
108108
})
109109
}
110+
110111
/// Parse `.inp` and `.inx` files.
111112
pub fn dump_inp<R: Read>(mut data: R, directory: &Path) -> Result<(), ParseInpError> {
112113
// check magic bytes
@@ -186,3 +187,59 @@ pub fn dump_inp<R: Read>(mut data: R, directory: &Path) -> Result<(), ParseInpEr
186187

187188
Ok(())
188189
}
190+
191+
pub fn dump_to_inp<W: Write>(directory: &Path, w: &mut W) -> io::Result<()> {
192+
let mut payload_file = File::open(directory.join("payload.json"))?;
193+
194+
w.write_all(MAGIC)?;
195+
w.write_all(&(payload_file.metadata()?.len() as u32).to_be_bytes())?;
196+
io::copy(&mut payload_file, w)?;
197+
198+
let mut texture_files = Vec::new();
199+
for tex_file in fs::read_dir(directory.join("textures"))? {
200+
let tex_file = tex_file?;
201+
let path = tex_file.path();
202+
let Some(ext) = path.extension() else {
203+
eprintln!("File {:?} has no extension, ignoring", tex_file.file_name());
204+
continue;
205+
};
206+
207+
let Some(ext) = ext.to_str() else {
208+
eprintln!("File {:?} has unrecognized extension, ignoring", tex_file.file_name());
209+
continue;
210+
};
211+
212+
let tex_encoding: u8 = match ext {
213+
"png" => 0,
214+
"tga" => 1,
215+
"bc7" => 2,
216+
ext => {
217+
eprintln!(
218+
"File {:?} has unsupported extension {:?}, ignoring",
219+
tex_file.file_name(),
220+
ext
221+
);
222+
continue;
223+
}
224+
};
225+
226+
texture_files.push((tex_encoding, path));
227+
}
228+
229+
w.write_all(TEX_SECT)?;
230+
w.write_all(&(texture_files.len() as u32).to_be_bytes())?;
231+
for (tex_encoding, tex_path) in texture_files {
232+
let mut tex_file = File::open(tex_path)?;
233+
234+
let file_len = tex_file.metadata()?.len() as u32;
235+
println!("t {} | {:?} {} B", tex_encoding, file_len.to_be_bytes(), file_len);
236+
237+
w.write_all(&file_len.to_be_bytes())?;
238+
w.write_all(&[tex_encoding])?;
239+
240+
io::copy(&mut tex_file, w)?;
241+
}
242+
243+
w.flush().unwrap();
244+
Ok(())
245+
}

0 commit comments

Comments
 (0)