From f818cf5376e0cbe1eee24bd4fd679a5d7f75333a Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Mon, 29 Apr 2024 19:09:58 -0400 Subject: [PATCH] Add a simple extractor example --- examples/extractor.rs | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 examples/extractor.rs diff --git a/examples/extractor.rs b/examples/extractor.rs new file mode 100644 index 0000000..766e4e2 --- /dev/null +++ b/examples/extractor.rs @@ -0,0 +1,48 @@ +// SPDX-FileCopyrightText: 2024 Joshua Goins +// SPDX-License-Identifier: GPL-3.0-or-later + +use std::env; +use std::fs::File; +use std::io::Write; +use physis::common::Platform; +use physis::gamedata::GameData; + +/// A simple program that allows a user to extract raw files from the game +fn main() { + let args: Vec = env::args().collect(); + if args.len() != 4 { + println!("Usage: extractor [game dir] [filepath_to_extract] [destination]"); + return; + } + + /// Collect our arguments + let game_dir = &args[1]; + let file_path = &args[2]; + let destination_path = &args[3]; + + /// Create a GameData struct, this manages the repositories. It allows us to easily extract files. + let Some(mut game_data) = GameData::from_existing(Platform::Win32, game_dir) else { + println!("Invalid game directory ({})!", game_dir); + return; + }; + + /// Extract said file: + let Some(game_file) = game_data.extract(file_path) else { + println!("File {} not found!", file_path); + return; + }; + + /// Create the file to write into. + let Ok(mut file) = File::create(destination_path) else { + println!("Failed to open file {} for writing.", destination_path); + return; + }; + + /// Since GameData::extract returns a byte buffer, it's trivial to write that to a file on disk. + if !file.write_all(&game_file).is_ok() { + println!("Failed to write to file {}.", destination_path); + return; + }; + + println!("Successfully extracted {} to {}!", file_path, destination_path); +} \ No newline at end of file