Skip to content

Commit

Permalink
Convert to new file struct
Browse files Browse the repository at this point in the history
  • Loading branch information
FlareFlo committed Aug 19, 2024
1 parent 94ec3d3 commit 0b1ebfc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

35 changes: 13 additions & 22 deletions src/subcommands/unpack_vromf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -184,8 +176,7 @@ pub fn unpack_vromf(args: &ArgMatches) -> Result<()> {
}

fn parse_and_write_one_vromf(
file_path: PathBuf,
read: Vec<u8>,
file: BlkFile,
output_dir: PathBuf,
format: Option<BlkOutputFormat>,
crlf: bool,
Expand All @@ -197,25 +188,25 @@ fn parse_and_write_one_vromf(
ffmpeg: Arc<ImageConverter>,
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)?
.to_os_string();
old_extension.push("_u");
vromf_name.set_extension(old_extension);

let writer = |file: &mut (PathBuf, Vec<u8>)| {
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);
Expand All @@ -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")?,
Expand Down
8 changes: 4 additions & 4 deletions src/subcommands/vromf_version.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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![(
Expand All @@ -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()?,
Expand Down

0 comments on commit 0b1ebfc

Please sign in to comment.