Skip to content

Commit

Permalink
Add revert command
Browse files Browse the repository at this point in the history
ByteZ1337 committed Apr 23, 2024
1 parent 0fe2a6c commit b83cef4
Showing 5 changed files with 63 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/command/mod.rs
Original file line number Diff line number Diff line change
@@ -10,4 +10,5 @@ pub type ArtHeader = Vec<AssetMetadata>;

pub mod pack;
pub mod unpack;
pub mod patch;
pub mod patch;
pub mod revert;
2 changes: 1 addition & 1 deletion src/command/patch/mod.rs
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ pub fn patch(args: &NewArgs, patch: &PathBuf, locale_mode: &I18nCompatMode) -> a
anyhow::bail!("Patch directory {:?} does not exist", patch);
}

let game_files = prepare_game_files(&args.game)?;
let game_files = prepare_game_files(&args.game_dir)?;

let temp_dir = create_temp_dir();
println!("Using temp directory: {}", temp_dir.display());
38 changes: 38 additions & 0 deletions src/command/revert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::path::PathBuf;

pub fn revert(game_dir: &PathBuf) -> anyhow::Result<()> {
// if game_dir is not already PapersPlease_Data, append it
let game_dir = if game_dir.ends_with("PapersPlease_Data") {
game_dir.clone()
} else {
game_dir.join("PapersPlease_Data")
};

if !game_dir.is_dir() {
anyhow::bail!("Game directory {:?} does not exist", game_dir);
}

let assets = game_dir.join("sharedassets0.assets");
let assets_bak = game_dir.join("sharedassets0.assets-bak");
copy_backup(&assets, &assets_bak)?;

let resources = game_dir.join("sharedassets0.resource");
let resources_bak = game_dir.join("sharedassets0.resource-bak");
copy_backup(&resources, &resources_bak)?;

let locale = game_dir.join("StreamingAssets/loc/en.zip");
let locale_bak = game_dir.join("StreamingAssets/loc/en.zip-bak");
copy_backup(&locale, &locale_bak)?;

println!("Reverted game files in: {:?} to vanilla state", game_dir);

Ok(())
}

fn copy_backup(file: &PathBuf, backup: &PathBuf) -> anyhow::Result<()> {
if !backup.exists() {
anyhow::bail!("Couldn't find {} in game directory {:?}. You'll to verify game integrity in steam to revert changes.", backup.display(), backup.parent());
}
std::fs::copy(backup, file)?;
Ok(())
}
2 changes: 1 addition & 1 deletion src/crypto.rs
Original file line number Diff line number Diff line change
@@ -71,7 +71,7 @@ fn as_u32_slice_mut(x: &mut [u8]) -> &mut [u32] {
}

pub fn extract_key(args: &NewArgs) -> anyhow::Result<String> {
let game_dir = Path::new(&args.game);
let game_dir = Path::new(&args.game_dir);
if !game_dir.exists() || !game_dir.is_dir() {
anyhow::bail!("Game directory not found: {}", game_dir.display());
}
26 changes: 21 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use std::path::PathBuf;
use clap::Parser;
use clap_derive::{Parser, Subcommand, ValueEnum};

use crate::command::{pack, patch, unpack};
use crate::command::{pack, patch, revert, unpack};

mod crypto;
mod io_ext;
@@ -38,7 +38,7 @@ struct NewArgs {

/// Path to the Papers, Please game directory
#[arg(short, long)]
game: PathBuf,
game_dir: PathBuf,

/// Optional encryption key to use for Art.dat. If none is provided it will be extracted from the global-metadata.dat file.
#[arg(short, long)]
@@ -50,7 +50,7 @@ struct NewArgs {
enum Command {
/// Pack assets into an Art.dat (For asset bundles, use the patch command)
Pack {
/// Input file. If none is provided, the tool will check for a "assets" and "out" directory in the current working directory.
/// Input file. If none is provided, the tool will check for an "assets" and "out" directory in the current working directory.
#[arg(short, long)]
input: Option<PathBuf>,

@@ -60,7 +60,7 @@ enum Command {
},
/// Unpack assets from an Art.dat or unity asset bundle.
Unpack {
/// Input file. Can either be a Art.dat file or a unity asset bundle. Make sure to either use the .dat or .assets extension.
/// Input file. Can either be an Art.dat file or a unity asset bundle. Make sure to either use the .dat or .assets extension.
#[arg(short, long)]
input: PathBuf,

@@ -78,6 +78,19 @@ enum Command {
#[arg(long, default_value = "none")]
i18n: I18nCompatMode,
},
/// Reverts the game files to their original state.
Revert,
}

impl Command {

fn needs_key(&self) -> bool {
match self {
Command::Revert => false,
_ => true,
}
}

}

#[derive(Debug, Clone, ValueEnum)]
@@ -92,7 +105,7 @@ enum I18nCompatMode {
fn main() {
let mut args = NewArgs::parse();
println!("papers-tools v{} by {}", env!("CARGO_PKG_VERSION"), env!("CARGO_PKG_AUTHORS"));
if args.art_key.is_none() {
if args.art_key.is_none() && args.command.needs_key() {
let res = crypto::extract_key(&args);
if let Err(err) = res {
eprintln!("Failed to extract key: {}", err);
@@ -112,6 +125,9 @@ fn main() {
Command::Patch { patch, i18n } => {
patch::patch(&args, patch, i18n)
}
Command::Revert => {
revert::revert(&args.game_dir)
}
};

if let Err(err) = res {

0 comments on commit b83cef4

Please sign in to comment.