Skip to content

Commit

Permalink
Fix zip times and use temp files
Browse files Browse the repository at this point in the history
  • Loading branch information
mcbegamerxx954 committed Jul 26, 2024
1 parent 96ff91a commit ff9a203
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 10 deletions.
115 changes: 115 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ clap = { version = "4.5.6", features = ["derive"] }
console = "0.15.8"
materialbin = { version = "0.1.1", git = "https://github.com/mcbegamerxx954/materialbin" }
scroll = "0.12.0"
zip = { version = "2.1.3", default-features = false, features = ["deflate"] }
tempfile = "3.10.1"
zip = { version = "2.1.3", default-features = false, features = ["deflate", "time"] }
zune-inflate = "0.2.54"

33 changes: 24 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
fs::File,
io::{BufReader, Read, Seek, Write},
io::{self, BufReader, Read, Seek, Write},
path::{Path, PathBuf},
};

Expand All @@ -15,6 +15,7 @@ use clap::{
use console::style;
use materialbin::{CompiledMaterialDefinition, MinecraftVersion};
use scroll::Pread;
use tempfile::tempfile;
use zip::{
read::ZipFile,
write::{ExtendedFileOptions, FileOptions},
Expand Down Expand Up @@ -95,9 +96,14 @@ fn main() -> anyhow::Result<()> {
auto_name
}
};
let mut output_file = file_to_shrodinger(&output_filename, opts.yeet)?;
let mut tmp_file = tempfile()?;
let mut output_file = file_to_shrodinger(&mut tmp_file, opts.yeet)?;
println!("Processing input {}", style(opts.file).cyan());
file_update(&mut input_file, &mut output_file, mcversion)?;
if !opts.yeet {
let mut output_file = File::create(output_filename)?;
io::copy(&mut tmp_file, &mut output_file)?;
}
return Ok(());
}
if opts.file.ends_with(".zip") || opts.file.ends_with(".mcpack") {
Expand All @@ -116,22 +122,31 @@ fn main() -> anyhow::Result<()> {
auto_name
}
};
let mut output_file = file_to_shrodinger(&output_filename, opts.yeet)?;
let mut tmp_file = tempfile()?;
let mut output_file = file_to_shrodinger(&mut tmp_file, opts.yeet)?;
println!("Processing input zip {}", style(opts.file).cyan());
zip_update(
&mut input_file,
&mut output_file,
mcversion,
opts.zip_compression,
)?;
tmp_file.rewind()?;
if !opts.yeet {
let mut output_file = File::create(output_filename)?;
io::copy(&mut tmp_file, &mut output_file)?;
}
}
Ok(())
}
fn file_to_shrodinger(file: &Path, dissapear: bool) -> anyhow::Result<ShrodingerOutput> {
fn file_to_shrodinger<'a>(
file: &'a mut File,
dissapear: bool,
) -> anyhow::Result<ShrodingerOutput<'a>> {
if dissapear {
Ok(ShrodingerOutput::Nothing)
} else {
Ok(ShrodingerOutput::File(File::create(file)?))
Ok(ShrodingerOutput::File(file))
}
}
fn update_filename(
Expand Down Expand Up @@ -217,11 +232,11 @@ fn read_material(data: &[u8]) -> anyhow::Result<CompiledMaterialDefinition> {

anyhow::bail!("Material file is invalid");
}
enum ShrodingerOutput {
File(File),
enum ShrodingerOutput<'a> {
File(&'a mut File),
Nothing,
}
impl Write for ShrodingerOutput {
impl<'a> Write for ShrodingerOutput<'a> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
match self {
Self::File(f) => f.write(buf),
Expand All @@ -235,7 +250,7 @@ impl Write for ShrodingerOutput {
}
}
}
impl Seek for ShrodingerOutput {
impl<'a> Seek for ShrodingerOutput<'a> {
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
match self {
Self::File(f) => f.seek(pos),
Expand Down

0 comments on commit ff9a203

Please sign in to comment.