Skip to content

Commit 9440d3f

Browse files
committed
switch to tracing for logging
1 parent 6844067 commit 9440d3f

10 files changed

+53
-31
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ binrw = "0.13.3"
2020
rand = "0.8.5"
2121
roxmltree = "0.20.0"
2222
xml-rs = "0.8.20"
23+
tracing = "0.1.40"
24+
tracing-subscriber = { version = "0.3.18", features = ["time"] }
25+
time = { version = "0.3.36", features = ["local-offset"] }
2326

2427
[profile.release]
2528
strip = true

src/command/pack.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::ffi::OsStr;
22
use std::path::PathBuf;
33

44
use anyhow::Context;
5+
use tracing::{info, warn};
56
use walkdir::WalkDir;
67

78
use crate::command::AssetMetadata;
@@ -63,7 +64,7 @@ fn find_input(input: &Option<PathBuf>) -> anyhow::Result<PathBuf> {
6364
}
6465

6566
fn pack_dat(art_key: &String, input: &PathBuf, output: &PathBuf) -> anyhow::Result<()> {
66-
println!("Packing assets...");
67+
info!("Packing assets...");
6768
let mut assets: Vec<AssetMetadata> = Vec::new();
6869
let mut asset_bytes: Vec<u8> = Vec::new();
6970
let mut count = 0;
@@ -94,7 +95,7 @@ fn pack_dat(art_key: &String, input: &PathBuf, output: &PathBuf) -> anyhow::Resu
9495
let mut out = Vec::new();
9596
let header_len = header.len() as i32;
9697
if header_len > i16::MAX as i32 {
97-
println!("!!! Header length {header_len} exceeds 2^15-1. This assets file will only work with a modded game !!!");
98+
warn!("!!! Header length {} exceeds {}. This assets file will only work with a modded game !!!", header_len, i16::MAX);
9899
let len_one = (header_len & 0xFFFF) as u16;
99100
// set sign bit to 1 as a marker for the modded readInt16 to read 4 bytes instead of 2
100101
let len_two = ((header_len >> 16) as u16) | 0x8000;
@@ -106,14 +107,14 @@ fn pack_dat(art_key: &String, input: &PathBuf, output: &PathBuf) -> anyhow::Resu
106107
out.append(&mut header);
107108
out.append(&mut asset_bytes);
108109

109-
println!("Encrypting assets...");
110+
info!("Encrypting assets...");
110111
let key = art_key.clone();
111112
let enc_key = crypto::to_key_array(key.as_str());
112113
let enc_key = enc_key.as_slice();
113114
crypto::encrypt(enc_key, out.as_mut_slice());
114115

115116
std::fs::write(output, out)?;
116-
println!("Packed {} assets", count);
117+
info!("Packed {} assets", count);
117118

118119
Ok(())
119120
}

src/command/patch/assets_patcher.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use anyhow::Context;
77
use binrw::__private::write_zeroes;
88
use binrw::BinWrite;
99
use binrw::io::BufReader;
10+
use tracing::info;
1011
use walkdir::WalkDir;
1112

1213
use crate::command::pack;
@@ -29,7 +30,7 @@ pub fn patch_assets(
2930
game_dir: &PathBuf,
3031
repack_info: RepackInfo,
3132
) -> anyhow::Result<PathBuf> { // patched assets directory
32-
println!("Patching assets..");
33+
info!("Patching assets..");
3334
let patched_assets = temp_dir.join("patched");
3435
let unpacked = temp_dir.join("unpacked");
3536
std::fs::create_dir_all(&patched_assets)
@@ -67,10 +68,10 @@ pub fn patch_assets(
6768
// copy over the patch file if it's a png, csv or txt file
6869
// TODO: csv and txt patching
6970
if ext == OsStr::new("png") || ext == OsStr::new("csv") || ext == OsStr::new("txt") {
70-
println!("Copying patch file for: {}", rel_path.display());
71+
info!("Copying patch file for: {}", rel_path.display());
7172
copy_file(&patch_file.as_path(), rel_path, &patched_assets)?;
7273
} else if ext == OsStr::new("xml") || ext == OsStr::new("fnt") {
73-
println!("Patching xml file: {}", rel_path.display());
74+
info!("Patching xml file: {}", rel_path.display());
7475
patch_xml(&file.path(), &patch_file, rel_path, &patched_assets)?;
7576
} else {
7677
anyhow::bail!("Unsupported file type: {}", patch_file.display());
@@ -102,7 +103,7 @@ pub fn patch_assets(
102103

103104
// copy over the file if it doesn't exist already
104105
if !target.exists() {
105-
println!("Adding new file: {}", rel_path.display());
106+
info!("Adding new file: {}", rel_path.display());
106107
copy_file(&file.path(), rel_path, &patched_assets)?;
107108
}
108109
}
@@ -225,6 +226,6 @@ fn pack_to_assets(temp_dir: &PathBuf, game_dir: &PathBuf, repack: RepackInfo) ->
225226
}
226227
}
227228

228-
println!("Packed {} objects", new_assets.content.objects.len());
229+
info!("Packed {} objects", new_assets.content.objects.len());
229230
Ok(())
230231
}

src/command/patch/audio_patcher.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::path::PathBuf;
88
use anyhow::Context;
99
use binrw::io::BufReader;
1010
use serde::{Deserialize, Serialize};
11-
11+
use tracing::info;
1212
use crate::command::unpack::RepackInfo;
1313
use crate::unity::audio::{AudioClip, AudioCompressionFormat, StreamedResource};
1414
use crate::unity::util::{AlignedString, U8Bool};
@@ -55,7 +55,7 @@ pub fn patch_audio(audio_patches_path: &PathBuf, game_dir: &PathBuf, repack_info
5555
.context("Failed to get parent directory of audio patches file")?;
5656

5757
let mut offset = 0u64;
58-
println!("Patching {} audio clips...", audio_patches.len());
58+
info!("Patching {} audio clips...", audio_patches.len());
5959
for patch in &audio_patches {
6060
if patch.patched_path.extension() != Some(OsStr::new("fsb")) {
6161
anyhow::bail!("Only FSB files are supported for audio patches.");

src/command/patch/locale_patcher.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ use std::path::PathBuf;
44

55
use anyhow::Context;
66
use binrw::io::BufReader;
7+
use tracing::info;
78
use zip::{CompressionMethod, ZipArchive};
89
use zip::write::{ExtendedFileOptions, FileOptions};
910

1011
pub fn patch_locale(patched: &PathBuf, game_dir: &PathBuf) -> anyhow::Result<()> {
1112
let patched = patched.join("assets");
12-
println!("Patching en.zip...");
13+
info!("Patching en.zip...");
1314

1415
let input = BufReader::new(File::open(game_dir.join("StreamingAssets/loc/en.zip-bak"))
1516
.context("Failed to open en.zip-bak")?
@@ -43,7 +44,7 @@ pub fn patch_locale(patched: &PathBuf, game_dir: &PathBuf) -> anyhow::Result<()>
4344

4445
writer.finish().context("Failed to finish writing")?;
4546

46-
println!("Patched en.zip locale with {} entries", zip.len());
47+
info!("Patched en.zip locale with {} entries", zip.len());
4748

4849
Ok(())
4950
}

src/command/patch/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::path::PathBuf;
44

55
use anyhow::Context;
66
use rand::random;
7+
use tracing::info;
78
use unpack::unpack_assets;
89

910
use crate::{I18nCompatMode, Args};
@@ -18,7 +19,7 @@ mod locale_patcher;
1819
pub mod audio_patcher;
1920

2021
pub fn patch(args: &Args, patch: &PathBuf, locale_mode: &I18nCompatMode) -> anyhow::Result<()> {
21-
println!("Patching assets with {:?} with locale mode {:?}", patch, locale_mode);
22+
info!("Patching assets with {:?} with locale mode {:?}", patch, locale_mode);
2223

2324
if !patch.is_dir() {
2425
anyhow::bail!("Patch directory {:?} does not exist", patch);
@@ -43,7 +44,7 @@ pub fn patch(args: &Args, patch: &PathBuf, locale_mode: &I18nCompatMode) -> anyh
4344
patch_locale(&patched_dir, &game_files.game_dir)?;
4445
}
4546

46-
println!("Cleaning up...");
47+
info!("Cleaning up...");
4748
fs::remove_dir_all(&temp_dir).context("Failed to remove temp directory")?;
4849

4950
Ok(())

src/command/revert.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::path::PathBuf;
2+
use tracing::info;
23
use crate::command::DATA_FOLDER_NAME;
34

45
pub fn revert(game_dir: &PathBuf) -> anyhow::Result<()> {
@@ -20,8 +21,8 @@ pub fn revert(game_dir: &PathBuf) -> anyhow::Result<()> {
2021
let locale = game_dir.join("StreamingAssets/loc/en.zip");
2122
let locale_bak = game_dir.join("StreamingAssets/loc/en.zip-bak");
2223
copy_backup(&locale, &locale_bak)?;
23-
24-
println!("Reverted game files to vanilla state");
24+
25+
info!("Reverted game files to vanilla state");
2526

2627
Ok(())
2728
}

src/command/unpack.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::path::{Path, PathBuf};
77
use anyhow::Context;
88
use binrw::BinRead;
99
use binrw::io::BufReader;
10-
10+
use tracing::{info, warn};
1111
use crate::{crypto, Args, unity};
1212
use crate::command::{ArtHeader, DATA_FOLDER_NAME};
1313
use crate::unity::AssetsFile;
@@ -63,7 +63,7 @@ fn find_input(args: &Args, input: &Option<PathBuf>) -> anyhow::Result<PathBuf> {
6363
pub fn unpack_dat(args: &Args, input: &PathBuf, output: &PathBuf) -> anyhow::Result<()> {
6464
let mut data = std::fs::read(input)
6565
.context("Failed to read input file")?;
66-
println!("Unpacking assets from: {}", input.display());
66+
info!("Unpacking assets from: {}", input.display());
6767

6868
// key can be unwrapped safely here
6969
let key = args.art_key.clone().unwrap();
@@ -93,7 +93,7 @@ pub fn unpack_dat(args: &Args, input: &PathBuf, output: &PathBuf) -> anyhow::Res
9393
if let Some(parent) = path.parent() {
9494
std::fs::create_dir_all(parent)?;
9595
if !parent.canonicalize()?.starts_with(&abs_output) {
96-
eprintln!("Skipping asset: {} (Tried escaping output directory)", asset.name);
96+
warn!("Skipping asset: {} (Tried escaping output directory)", asset.name);
9797
continue;
9898
}
9999
}
@@ -102,7 +102,7 @@ pub fn unpack_dat(args: &Args, input: &PathBuf, output: &PathBuf) -> anyhow::Res
102102
.context(format!("Failed to write asset {} to file", asset.name))?;
103103
}
104104

105-
println!("Unpacked {} assets", assets.len());
105+
info!("Unpacked {} assets", assets.len());
106106

107107
Ok(())
108108
}
@@ -136,7 +136,7 @@ pub fn unpack_assets(args: &Args, input_path: &PathBuf, output: &PathBuf, proces
136136

137137
if name == "Art.dat" {
138138
let temp = PathBuf::from("./temp-art.dat");
139-
println!("Found Art.dat in unity assets. Temporarily saving to: {}", temp.display());
139+
info!("Found Art.dat in unity assets. Temporarily saving to: {}", temp.display());
140140

141141
let temp_writer = File::create(&temp)
142142
.context("Failed to create temporary file")?;
@@ -167,9 +167,9 @@ pub fn unpack_assets(args: &Args, input_path: &PathBuf, output: &PathBuf, proces
167167

168168
if let Some(art_file) = art_file {
169169
unpack_dat(args, &art_file, output)?;
170-
println!("Removing temporary file: {}", art_file.display());
170+
info!("Removing temporary file: {}", art_file.display());
171171
if let Err(e) = std::fs::remove_file(art_file) {
172-
eprintln!("Failed to remove temporary file: {}", e);
172+
warn!("Failed to remove temporary file: {}", e);
173173
}
174174
// Any unwraps here are safe because None values would've resulted in earlier bail
175175
Ok(RepackInfo {

src/crypto.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fs::File;
22
use std::io::{Read, Seek};
33
use std::slice;
4-
4+
use tracing::info;
55
use crate::command::DATA_FOLDER_NAME;
66
use crate::Args;
77

@@ -94,7 +94,7 @@ pub fn extract_key(args: &Args) -> anyhow::Result<String> {
9494
let mut key = [0; 16];
9595
file.read_exact(&mut key)?;
9696
let key = String::from_utf8(key.to_vec())?;
97-
println!("Extracted Art.dat decryption key from global metadata");
97+
info!("Extracted Art.dat decryption key from global metadata");
9898

9999
Ok(key)
100100
}

src/main.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use std::path::PathBuf;
22

33
use clap::Parser;
44
use clap_derive::{Parser, Subcommand, ValueEnum};
5-
5+
use tracing::{error, info};
6+
use tracing_subscriber::fmt::time::OffsetTime;
67
use crate::command::{pack, patch, revert, unpack};
78

89
mod crypto;
@@ -81,12 +82,25 @@ enum I18nCompatMode {
8182

8283

8384
fn main() {
85+
tracing_subscriber::fmt()
86+
.compact()
87+
.with_level(true)
88+
.with_target(false)
89+
.with_thread_ids(false)
90+
.with_thread_names(false)
91+
.with_file(false)
92+
.with_line_number(false)
93+
.with_timer(OffsetTime::new(
94+
time::UtcOffset::current_local_offset().unwrap_or_else(|_| time::UtcOffset::UTC),
95+
time::format_description::parse("[hour]:[minute]:[second]").unwrap(),
96+
))
97+
.init();
8498
let mut args = Args::parse();
85-
println!("papers-tools v{} by {}", env!("CARGO_PKG_VERSION"), env!("CARGO_PKG_AUTHORS"));
99+
info!("papers-tools v{} by {}", env!("CARGO_PKG_VERSION"), env!("CARGO_PKG_AUTHORS"));
86100
if args.art_key.is_none() && args.command.needs_key() {
87101
let res = crypto::extract_key(&args);
88102
if let Err(err) = res {
89-
eprintln!("Failed to extract key: {}", err);
103+
error!("Failed to extract key: {}", err);
90104
return;
91105
}
92106
args.art_key = Some(res.unwrap());
@@ -109,7 +123,7 @@ fn main() {
109123
};
110124

111125
if let Err(err) = res {
112-
eprintln!("An error occurred while running the command:");
113-
eprintln!("{err}");
126+
error!("An error occurred while running the command:");
127+
error!("{err}");
114128
}
115129
}

0 commit comments

Comments
 (0)