From 6805b5c142ec452ee217b20bff6e8c6e1b8f3abf Mon Sep 17 00:00:00 2001 From: spuds <71292624+bananaturtlesandwich@users.noreply.github.com> Date: Wed, 20 Sep 2023 20:21:15 +0100 Subject: [PATCH] break get_oodle into separate package --- Cargo.toml | 1 + get_oodle/Cargo.toml | 16 +++++++++++++++ get_oodle/src/lib.rs | 35 +++++++++++++++++++++++++++++++++ repak_cli/Cargo.toml | 8 ++------ repak_cli/src/main.rs | 45 ++----------------------------------------- 5 files changed, 56 insertions(+), 49 deletions(-) create mode 100644 get_oodle/Cargo.toml create mode 100644 get_oodle/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 41d7c06..43472ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ aes = "0.8.2" base64 = "0.21.0" strum = { version = "0.24", features = ["derive"] } sha1 = "0.10" +hex = "0.4" # generated by 'cargo dist init' [profile.dist] diff --git a/get_oodle/Cargo.toml b/get_oodle/Cargo.toml new file mode 100644 index 0000000..7b03cee --- /dev/null +++ b/get_oodle/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "get_oodle" +repository.workspace = true +authors.workspace = true +license.workspace = true +version.workspace = true +edition.workspace = true + +[dependencies] +repak = { path = "../repak" } +sha1 = { workspace = true } +ureq = "2.6" +once_cell = "1.17" +hex-literal = "0.4" +libloading = "0.7" +hex = { workspace = true } \ No newline at end of file diff --git a/get_oodle/src/lib.rs b/get_oodle/src/lib.rs new file mode 100644 index 0000000..d4afc3f --- /dev/null +++ b/get_oodle/src/lib.rs @@ -0,0 +1,35 @@ +pub fn decompress() -> repak::Decompress { + *unsafe { OODLE.as_ref().unwrap().get(b"OodleLZ_Decompress") }.unwrap() +} + +use once_cell::sync::Lazy; +static OODLE: Lazy> = + Lazy::new(|| get_oodle().map_err(|e| e.to_string())); +static OODLE_HASH: [u8; 20] = hex_literal::hex!("4bcc73614cb8fd2b0bce8d0f91ee5f3202d9d624"); + +fn get_oodle() -> Result { + use sha1::{Digest, Sha1}; + + let oodle = std::env::current_exe()?.with_file_name("oo2core_9_win64.dll"); + if !oodle.exists() { + let mut data = vec![]; + ureq::get("https://cdn.discordapp.com/attachments/817251677086285848/992648087371792404/oo2core_9_win64.dll") + .call().map_err(|e| repak::Error::Other(e.to_string()))? + .into_reader().read_to_end(&mut data)?; + + std::fs::write(&oodle, data)?; + } + + let mut hasher = Sha1::new(); + hasher.update(std::fs::read(&oodle)?); + let hash = hasher.finalize(); + (hash[..] == OODLE_HASH).then_some(()).ok_or_else(|| { + repak::Error::Other(format!( + "oodle hash mismatch expected: {} got: {} ", + hex::encode(OODLE_HASH), + hex::encode(hash) + )) + })?; + + unsafe { libloading::Library::new(oodle) }.map_err(|_| repak::Error::OodleFailed) +} diff --git a/repak_cli/Cargo.toml b/repak_cli/Cargo.toml index b8c9f71..f3ab94a 100644 --- a/repak_cli/Cargo.toml +++ b/repak_cli/Cargo.toml @@ -12,20 +12,16 @@ path = "src/main.rs" [target.'cfg(windows)'.dependencies] repak = { path = "../repak", features = ["oodle"] } -sha1 = { workspace = true } -ureq = "2.6" -once_cell = "1.17" -hex-literal = "0.4" -libloading = "0.7" [target.'cfg(not(windows))'.dependencies] repak = { path = "../repak" } [dependencies] +get_oodle = { path = "../get_oodle" } aes = { workspace = true } base64 = { workspace = true } clap = { version = "4.1.4", features = ["derive"] } -hex = "0.4.3" +hex = { workspace = true } indicatif = { version = "0.17.3", features = ["rayon"] } path-clean = "0.1.0" path-slash = "0.2.1" diff --git a/repak_cli/src/main.rs b/repak_cli/src/main.rs index e7204e1..6d876dc 100644 --- a/repak_cli/src/main.rs +++ b/repak_cli/src/main.rs @@ -284,7 +284,7 @@ fn unpack(aes_key: Option, action: ActionUnpack) -> Result<(), repa for input in &action.input { let mut builder = repak::PakBuilder::new(); #[cfg(windows)] - builder.oodle(decompress); + builder.oodle(get_oodle::decompress); if let Some(aes_key) = aes_key.clone() { builder.key(aes_key) } @@ -457,7 +457,7 @@ fn get(aes_key: Option, args: ActionGet) -> Result<(), repak::Error let mut reader = BufReader::new(File::open(&args.input)?); let mut builder = repak::PakBuilder::new(); #[cfg(windows)] - builder.oodle(decompress); + builder.oodle(get_oodle::decompress); if let Some(aes_key) = aes_key { builder.key(aes_key) } @@ -477,44 +477,3 @@ fn get(aes_key: Option, args: ActionGet) -> Result<(), repak::Error std::io::stdout().write_all(&pak.get(&file.to_slash_lossy(), &mut reader)?)?; Ok(()) } - -#[cfg(windows)] -fn decompress() -> repak::Decompress { - *unsafe { OODLE.as_ref().unwrap().get(b"OodleLZ_Decompress") }.unwrap() -} - -#[cfg(windows)] -use once_cell::sync::Lazy; -#[cfg(windows)] -static OODLE: Lazy> = - Lazy::new(|| get_oodle().map_err(|e| e.to_string())); -#[cfg(windows)] -static OODLE_HASH: [u8; 20] = hex_literal::hex!("4bcc73614cb8fd2b0bce8d0f91ee5f3202d9d624"); - -#[cfg(windows)] -fn get_oodle() -> Result { - use sha1::{Digest, Sha1}; - - let oodle = std::env::current_exe()?.with_file_name("oo2core_9_win64.dll"); - if !oodle.exists() { - let mut data = vec![]; - ureq::get("https://cdn.discordapp.com/attachments/817251677086285848/992648087371792404/oo2core_9_win64.dll") - .call().map_err(|e| repak::Error::Other(e.to_string()))? - .into_reader().read_to_end(&mut data)?; - - std::fs::write(&oodle, data)?; - } - - let mut hasher = Sha1::new(); - hasher.update(std::fs::read(&oodle)?); - let hash = hasher.finalize(); - (hash[..] == OODLE_HASH).then_some(()).ok_or_else(|| { - repak::Error::Other(format!( - "oodle hash mismatch expected: {} got: {} ", - hex::encode(OODLE_HASH), - hex::encode(hash) - )) - })?; - - unsafe { libloading::Library::new(oodle) }.map_err(|_| repak::Error::OodleFailed) -}