From 0b1ebfc77b3fc2f4a858ba1fcaa8a9a46ae5c214 Mon Sep 17 00:00:00 2001 From: FlareFlo Date: Mon, 19 Aug 2024 22:03:47 +0200 Subject: [PATCH] Convert to new file struct --- Cargo.lock | 2 +- src/subcommands/unpack_vromf.rs | 35 ++++++++++++-------------------- src/subcommands/vromf_version.rs | 8 ++++---- 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7ccc463..8c87cf6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1036,7 +1036,7 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "wt_blk" version = "0.2.3" -source = "git+https://github.com/Warthunder-Open-Source-Foundation/wt_blk?branch=master#f0342e7b6d08aca6ddfa232240676afd56815337" +source = "git+https://github.com/Warthunder-Open-Source-Foundation/wt_blk?branch=master#6ce604df5cb2c795bf288f614946e2f4ae4d8dc6" dependencies = [ "cfg-if", "color-eyre", diff --git a/src/subcommands/unpack_vromf.rs b/src/subcommands/unpack_vromf.rs index 0cc943a..4075e1a 100644 --- a/src/subcommands/unpack_vromf.rs +++ b/src/subcommands/unpack_vromf.rs @@ -14,19 +14,18 @@ use std::{ use clap::{parser::ValueSource, ArgMatches}; use color_eyre::{ - eyre::{Context, ContextCompat, Result}, + eyre::{ContextCompat, Result}, Help, }; use tracing::info; use wt_blk::{ blk::util::maybe_blk, - vromf::{BlkOutputFormat, VromfUnpacker}, + vromf::{BlkOutputFormat, VromfUnpacker, File as BlkFile}, }; use zip::{write::SimpleFileOptions, CompressionMethod}; use crate::{ arced, - context, error::CliError, image_conversion::{Converter, ImageConverter}, util::CrlfWriter, @@ -118,13 +117,8 @@ pub fn unpack_vromf(args: &ArgMatches) -> Result<()> { thread::Builder::new().name(file.file_name().to_string_lossy().to_string()); threads.push(Box::new(thread_builder.spawn(move || { - let read = fs::read(file.path()).with_context(context!(format!( - "Failed to read vromf {:?}", - file.path() - )))?; parse_and_write_one_vromf( - file.path(), - read, + BlkFile::new(file.path())?, output_folder, mode, crlf, @@ -164,10 +158,8 @@ pub fn unpack_vromf(args: &ArgMatches) -> Result<()> { .ok_or(CliError::InvalidPath)? .to_owned(), }; - let read = fs::read(&parsed_input_dir)?; parse_and_write_one_vromf( - parsed_input_dir, - read, + BlkFile::new(parsed_input_dir)?, output_folder, mode, crlf, @@ -184,8 +176,7 @@ pub fn unpack_vromf(args: &ArgMatches) -> Result<()> { } fn parse_and_write_one_vromf( - file_path: PathBuf, - read: Vec, + file: BlkFile, output_dir: PathBuf, format: Option, crlf: bool, @@ -197,9 +188,9 @@ fn parse_and_write_one_vromf( ffmpeg: Arc, check_integrity: bool, ) -> Result<()> { - let parser = VromfUnpacker::from_file((file_path.clone(), read), check_integrity)?; + let parser = VromfUnpacker::from_file(&file, check_integrity)?; - let mut vromf_name = PathBuf::from(file_path.file_name().ok_or(CliError::InvalidPath)?); + let mut vromf_name = file.path().to_path_buf(); let mut old_extension = vromf_name .extension() .ok_or(CliError::InvalidPath)? @@ -207,15 +198,15 @@ fn parse_and_write_one_vromf( old_extension.push("_u"); vromf_name.set_extension(old_extension); - let writer = |file: &mut (PathBuf, Vec)| { + let writer = |file: &mut BlkFile| { { // The version file in some vromfs is prefixed with /, which is incorrect as this causes // all relative paths to resolve to / - if file.0.starts_with("/") { - file.0 = file.0.strip_prefix("/")?.to_path_buf(); + if file.path().starts_with("/") { + *file.path_mut() = file.path().strip_prefix("/")?.to_path_buf(); } - let rel_file_path = vromf_name.clone().join(&file.0); + let rel_file_path = vromf_name.clone().join(file.path()); let mut joined_final_path = output_dir.join(&rel_file_path); let is_blk = maybe_blk(file); @@ -229,11 +220,11 @@ fn parse_and_write_one_vromf( fs::create_dir_all(joined_final_path.parent().ok_or(CliError::InvalidPath)?)?; if avif2png { - if file.0.extension() == Some(&OsStr::new("avif")) { + if file.path().extension() == Some(&OsStr::new("avif")) { // Convert image joined_final_path.set_extension("png"); ffmpeg.convert_and_write( - take(&mut file.1), + take(&mut file.buf_mut()), joined_final_path .to_str() .context("Final path is not a valid str")?, diff --git a/src/subcommands/vromf_version.rs b/src/subcommands/vromf_version.rs index fad075d..3da280a 100644 --- a/src/subcommands/vromf_version.rs +++ b/src/subcommands/vromf_version.rs @@ -1,8 +1,8 @@ -use std::{fs, iter::once, path::PathBuf, str::FromStr}; +use std::{iter::once, path::PathBuf, str::FromStr}; use clap::ArgMatches; use serde_json::{json, Map, Value}; -use wt_blk::vromf::VromfUnpacker; +use wt_blk::vromf::{File, VromfUnpacker}; use crate::error::CliError; @@ -14,7 +14,7 @@ pub fn vromf_version(args: &ArgMatches) -> color_eyre::Result<()> { let versions: Vec<_> = if parsed_input_dir.is_file() { let unpacker = VromfUnpacker::from_file( - (parsed_input_dir.clone(), fs::read(&parsed_input_dir)?), + &File::new(parsed_input_dir.clone()).unwrap(), true, )?; vec![( @@ -30,7 +30,7 @@ pub fn vromf_version(args: &ArgMatches) -> color_eyre::Result<()> { let mut versions = vec![]; for file in dir { let p = file?.path(); - let unpacker = VromfUnpacker::from_file((p.clone(), fs::read(&p)?), true)?; + let unpacker = VromfUnpacker::from_file(&File::new(p.clone())?, true)?; versions.push(( p.file_name().unwrap().to_string_lossy().to_string(), unpacker.latest_version()?,