mirror of
https://github.com/NinjaCheetah/rustii.git
synced 2026-03-03 11:25:29 -05:00
Improved error handling for fakesign CLI command
This commit is contained in:
@@ -60,7 +60,7 @@ fn main() -> Result<()> {
|
||||
}
|
||||
},
|
||||
Some(Commands::Fakesign { input, output }) => {
|
||||
fakesign::fakesign(input, output)
|
||||
fakesign::fakesign(input, output)?
|
||||
},
|
||||
Some(Commands::Info { input }) => {
|
||||
info::info(input)
|
||||
|
||||
@@ -5,13 +5,14 @@
|
||||
|
||||
use std::{str, fs};
|
||||
use std::path::{Path, PathBuf};
|
||||
use anyhow::{bail, Context, Result};
|
||||
use rustii::{title, title::tmd, title::ticket};
|
||||
use crate::filetypes::{WiiFileType, identify_file_type};
|
||||
|
||||
pub fn fakesign(input: &str, output: &Option<String>) {
|
||||
pub fn fakesign(input: &str, output: &Option<String>) -> Result<()> {
|
||||
let in_path = Path::new(input);
|
||||
if !in_path.exists() {
|
||||
panic!("Error: Input file does not exist.");
|
||||
bail!("Input file \"{}\" does not exist.", in_path.display());
|
||||
}
|
||||
match identify_file_type(input) {
|
||||
Some(WiiFileType::Wad) => {
|
||||
@@ -21,10 +22,11 @@ pub fn fakesign(input: &str, output: &Option<String>) {
|
||||
PathBuf::from(input)
|
||||
};
|
||||
// Load WAD into a Title instance, then fakesign it.
|
||||
let mut title = title::Title::from_bytes(fs::read(in_path).unwrap().as_slice()).expect("could not read WAD file");
|
||||
title.fakesign().expect("could not fakesign WAD");
|
||||
let mut title = title::Title::from_bytes(fs::read(in_path).with_context(|| "Could not open WAD file for reading.")?.as_slice())
|
||||
.with_context(|| "The provided WAD file could not be parsed, and is likely invalid.")?;
|
||||
title.fakesign().with_context(|| "An unknown error occurred while fakesigning the provided WAD.")?;
|
||||
// Write output file.
|
||||
fs::write(out_path, title.to_wad().unwrap().to_bytes().expect("could not create output WAD")).expect("could not write output WAD file");
|
||||
fs::write(out_path, title.to_wad()?.to_bytes()?).with_context(|| "Could not open output file for writing.")?;
|
||||
println!("WAD fakesigned!");
|
||||
},
|
||||
Some(WiiFileType::Tmd) => {
|
||||
@@ -34,10 +36,11 @@ pub fn fakesign(input: &str, output: &Option<String>) {
|
||||
PathBuf::from(input)
|
||||
};
|
||||
// Load TMD into a TMD instance, then fakesign it.
|
||||
let mut tmd = tmd::TMD::from_bytes(fs::read(in_path).unwrap().as_slice()).expect("could not read TMD file");
|
||||
tmd.fakesign().expect("could not fakesign TMD");
|
||||
let mut tmd = tmd::TMD::from_bytes(fs::read(in_path).with_context(|| "Could not open TMD file for reading.")?.as_slice())
|
||||
.with_context(|| "The provided TMD file could not be parsed, and is likely invalid.")?;
|
||||
tmd.fakesign().with_context(|| "An unknown error occurred while fakesigning the provided TMD.")?;
|
||||
// Write output file.
|
||||
fs::write(out_path, tmd.to_bytes().expect("could not create output TMD")).expect("could not write output TMD file");
|
||||
fs::write(out_path, tmd.to_bytes()?).with_context(|| "Could not open output file for writing.")?;
|
||||
println!("TMD fakesigned!");
|
||||
},
|
||||
Some(WiiFileType::Ticket) => {
|
||||
@@ -47,14 +50,16 @@ pub fn fakesign(input: &str, output: &Option<String>) {
|
||||
PathBuf::from(input)
|
||||
};
|
||||
// Load Ticket into a Ticket instance, then fakesign it.
|
||||
let mut ticket = ticket::Ticket::from_bytes(fs::read(in_path).unwrap().as_slice()).expect("could not read Ticket file");
|
||||
ticket.fakesign().expect("could not fakesign Ticket");
|
||||
let mut ticket = ticket::Ticket::from_bytes(fs::read(in_path).with_context(|| "Could not open Ticket file for reading.")?.as_slice())
|
||||
.with_context(|| "The provided Ticket file could not be parsed, and is likely invalid.")?;
|
||||
ticket.fakesign().with_context(|| "An unknown error occurred while fakesigning the provided Ticket.")?;
|
||||
// Write output file.
|
||||
fs::write(out_path, ticket.to_bytes().expect("could not create output Ticket")).expect("could not write output Ticket file");
|
||||
fs::write(out_path, ticket.to_bytes()?).with_context(|| "Could not open output file for writing.")?;
|
||||
println!("Ticket fakesigned!");
|
||||
},
|
||||
None => {
|
||||
panic!("Error: You can only fakesign TMDs, Tickets, and WADs!");
|
||||
bail!("You can only fakesign TMDs, Tickets, and WADs!");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ pub fn convert_wad(input: &str, target: &ConvertTargets, output: &Option<String>
|
||||
Target::Vwii => PathBuf::from(format!("{}_vWii", in_path.file_stem().unwrap().to_str().unwrap())).with_extension("wad"),
|
||||
}
|
||||
};
|
||||
let mut title = title::Title::from_bytes(fs::read(in_path)?.as_slice()).with_context(|| "The provided WAD file could not be loaded, and is likely invalid.")?;
|
||||
let mut title = title::Title::from_bytes(fs::read(in_path)?.as_slice()).with_context(|| "The provided WAD file could not be parsed, and is likely invalid.")?;
|
||||
// Bail if the WAD is already using the selected encryption.
|
||||
if matches!(target, Target::Dev) && title.ticket.is_dev() {
|
||||
bail!("This is already a development WAD!");
|
||||
|
||||
Reference in New Issue
Block a user