1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//! Implements the functionality to extract assets bundled by [RenPy](https://renpy.org/) archives.
//!
//! **Credits**: This tool has been ported to Rust from the [rpatool](https://github.com/Shizmob/rpatool) repo, originally written in Python.
//!
//! **Disclaimer**: Use this library only archives on which the authors allow modification or extraction. The unauthorized use is highly
//! discouraged since this poses most likely a license violation.

pub mod rpa;

use std::error::Error;
use std::path::PathBuf;

use log::debug;
use structopt::StructOpt;

// re-exports for public API
pub use rpa::RenpyArchive;
pub use rpa::RpaEntry;

/// Represents the command line arguments passed to the library
#[derive(Debug, Clone, StructOpt)]
#[structopt(
    name = "unrpa_rs",
    about = "Extracts assets from a RenPy archive (RPA) file.",
    author
)]
pub struct Cli {
    /// The path to the archive file to read from
    #[structopt(name = "INPUT", parse(from_os_str))]
    input: PathBuf,
    /// Increase verbosity level (-v, -vv, -vvv, etc.)
    #[structopt(short, long, parse(from_occurrences))]
    verbose: usize,
}

// `Box<dyn Error>` is a trait object, i.e. a type that implements the `Error` trait
/// Calls the respective functionality of the library from the configuration of command line arguments
pub fn run(cli: Cli) -> Result<(), Box<dyn Error>> {
    debug!("Working on archive '{}'", cli.input.display());

    let mut rpa = RenpyArchive::from_file(cli.input.as_path())?;
    print!("Extracting all files from '{}'. ", rpa.path().display());

    // getting all files from the requested archive and writing them sequentially to disk
    for file in rpa.list_indices().iter() {
        let file_buf = rpa.read_file_from_archive(file)?;
        rpa.write_file(file, &file_buf)?;
    }
    println!("Done. TBW: {} MB. Estimated overall write speed: {:.2} MB/s.", rpa.total_bytes_written() as f32 / 1e6, rpa.estimate_overall_write_speed());

    Ok(())
}

impl Cli {
    pub fn verbose(&self) -> usize {
        self.verbose
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use hex_literal::hex;
    use sha2::{Digest, Sha256};
    use std::path::Path;

    const TESTFILE_ONE: &str = "samples/scripts.rpa";

    #[test]
    fn list_indices_name() {
        assert_eq!(
            vec![
                "scripts/alt_cam.rpyc",
                "scripts/ep2.rpyc",
                "scripts/ep3.rpyc",
                "scripts/ep4.rpyc",
                "scripts/ep5.rpyc",
                "scripts/ep6.rpyc",
                "scripts/extras.rpyc",
                "scripts/gui.rpyc",
                "scripts/options.rpyc",
                "scripts/replay.rpyc",
                "scripts/screens.rpyc",
                "scripts/script.rpyc"
            ],
            RenpyArchive::from_file(Path::new(TESTFILE_ONE))
                .unwrap()
                .list_indices()
        );
    }

    #[test]
    fn check_files_integrity() {
        // test case for reading every file and then asserting each with a hash
        let mut rpa = RenpyArchive::from_file(Path::new(TESTFILE_ONE)).unwrap();
        assert_eq!(
            Sha256::digest(&rpa.read_file_from_archive("scripts/alt_cam.rpyc").unwrap())[..],
            hex!("c51c21824877dfee2615d21329e6380d5b5bfd5b65903bab0a6e5f9cd1461462")[..]
        );
        assert_eq!(
            Sha256::digest(&rpa.read_file_from_archive("scripts/ep2.rpyc").unwrap())[..],
            hex!("298db84f88fa6c3d6c73e41368f0f49bee3fa403d810ae271650aa46dd39f6ad")[..]
        );
        assert_eq!(
            Sha256::digest(&rpa.read_file_from_archive("scripts/ep3.rpyc").unwrap())[..],
            hex!("032fe63e950fea63fd6c6b74322be36a66ad861ff4d9768a9d347ae652bb5929")[..]
        );
        assert_eq!(
            Sha256::digest(&rpa.read_file_from_archive("scripts/ep4.rpyc").unwrap())[..],
            hex!("0dac26a4f77157a64a84de923ab91a03dda733ed12117159edcb2468a39f7c13")[..]
        );
        assert_eq!(
            Sha256::digest(&rpa.read_file_from_archive("scripts/ep5.rpyc").unwrap())[..],
            hex!("c829d78336f453351fdb49ba7632538d9e6bd386958ec303a6e0a8e4aed8dabd")[..]
        );
        assert_eq!(
            Sha256::digest(&rpa.read_file_from_archive("scripts/ep6.rpyc").unwrap())[..],
            hex!("ca10971571f11672d466f5c83a7d59ae990cb245f71563e18768ceb8255df532")[..]
        );
        assert_eq!(
            Sha256::digest(&rpa.read_file_from_archive("scripts/extras.rpyc").unwrap())[..],
            hex!("827df70b686eaf27ce8234ea94282a0b34cdba8d58752d6788fed3bd7cbabfa3")[..]
        );
        assert_eq!(
            Sha256::digest(&rpa.read_file_from_archive("scripts/gui.rpyc").unwrap())[..],
            hex!("c24b749402b0cba79f392f3a8b4818e1847d24a98c24ecef81a79b79806d5003")[..]
        );
        assert_eq!(
            Sha256::digest(&rpa.read_file_from_archive("scripts/options.rpyc").unwrap())[..],
            hex!("b056fc1a41555b000f47a361ea95685f618214d4114d46724b2b6dbd3f1ba81b")[..]
        );
        assert_eq!(
            Sha256::digest(&rpa.read_file_from_archive("scripts/replay.rpyc").unwrap())[..],
            hex!("31a2fc6dfa48799532c1ec79867018999383f615761c862743123d796f265270")[..]
        );
        assert_eq!(
            Sha256::digest(&rpa.read_file_from_archive("scripts/screens.rpyc").unwrap())[..],
            hex!("586e756878cc7b230c47e4eccb72a91f85270601becc1070d1c4196292952e53")[..]
        );
        assert_eq!(
            Sha256::digest(&rpa.read_file_from_archive("scripts/script.rpyc").unwrap())[..],
            hex!("b907bafef8746edc9ec5a57c8a617e0d4b27133b3dbe06269a1c7fb960c1d2f1")[..]
        );
    }

    #[test]
    fn list_indices_metadata() {
        let rpa = RenpyArchive::from_file(Path::new(TESTFILE_ONE)).unwrap();
        let coll = rpa.indices_map();

        // check if returned `BTreeMap` contains every key with the respective offset, len, and prefix

        assert_eq!(
            coll.get("scripts/alt_cam.rpyc").unwrap()[0],
            RpaEntry::with_prefix(51, 76668, "")
        );
        assert_eq!(
            coll.get("scripts/replay.rpyc").unwrap()[0],
            RpaEntry::with_prefix(2519600, 38696, "")
        );
        assert_eq!(
            coll.get("scripts/screens.rpyc").unwrap()[0],
            RpaEntry::with_prefix(2558313, 102182, "")
        );
        assert_eq!(
            coll.get("scripts/gui.rpyc").unwrap()[0],
            RpaEntry::with_prefix(2492912, 21564, "")
        );
        assert_eq!(
            coll.get("scripts/ep5.rpyc").unwrap()[0],
            RpaEntry::with_prefix(1441933, 472960, "")
        );
        assert_eq!(
            coll.get("scripts/ep4.rpyc").unwrap()[0],
            RpaEntry::with_prefix(970319, 471597, "")
        );
        assert_eq!(
            coll.get("scripts/script.rpyc").unwrap()[0],
            RpaEntry::with_prefix(2660512, 467467, "")
        );
        assert_eq!(
            coll.get("scripts/ep2.rpyc").unwrap()[0],
            RpaEntry::with_prefix(76736, 407166, "")
        );
        assert_eq!(
            coll.get("scripts/ep6.rpyc").unwrap()[0],
            RpaEntry::with_prefix(1914910, 562190, "")
        );
        assert_eq!(
            coll.get("scripts/extras.rpyc").unwrap()[0],
            RpaEntry::with_prefix(2477117, 15778, "")
        );
        assert_eq!(
            coll.get("scripts/ep3.rpyc").unwrap()[0],
            RpaEntry::with_prefix(483919, 486383, "")
        );
        assert_eq!(
            coll.get("scripts/options.rpyc").unwrap()[0],
            RpaEntry::with_prefix(2514493, 5090, "")
        );
    }
}