From d31ddba08af8e483f5d4ee27d7e84c6b88b4d0d8 Mon Sep 17 00:00:00 2001 From: Tim-Paik Date: Sun, 1 May 2022 16:41:32 +0800 Subject: [PATCH] Ready to integrate rcedit on windows --- Cargo.lock | 25 ++++++++++++++++++++++ neutauri_bundler/Cargo.toml | 3 +++ neutauri_bundler/src/bundle.rs | 38 ++++++++++++++++++++++++++-------- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 117a511..a40c57c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1176,6 +1176,7 @@ dependencies = [ "gumdrop", "image", "new_mime_guess", + "rcedit", "serde", "toml", "wry", @@ -1518,6 +1519,24 @@ dependencies = [ "num_cpus", ] +[[package]] +name = "rcedit" +version = "0.1.0" +source = "git+https://github.com/Tim-Paik/rcedit-rs.git#8d323d1015ad6ff2c111d902e9d60b75668fa781" +dependencies = [ + "rcedit-sys", + "thiserror", + "widestring", +] + +[[package]] +name = "rcedit-sys" +version = "0.1.0" +source = "git+https://github.com/Tim-Paik/rcedit-rs.git#8d323d1015ad6ff2c111d902e9d60b75668fa781" +dependencies = [ + "cc", +] + [[package]] name = "redox_syscall" version = "0.2.10" @@ -2093,6 +2112,12 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8b77fdfd5a253be4ab714e4ffa3c49caf146b4de743e97510c0656cf90f1e8e" +[[package]] +name = "widestring" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17882f045410753661207383517a6f62ec3dbeb6a4ed2acce01f0728238d1983" + [[package]] name = "winapi" version = "0.3.9" diff --git a/neutauri_bundler/Cargo.toml b/neutauri_bundler/Cargo.toml index 86828ec..d6739f9 100644 --- a/neutauri_bundler/Cargo.toml +++ b/neutauri_bundler/Cargo.toml @@ -12,3 +12,6 @@ new_mime_guess = "4.0" serde = {version = "1.0", features = ["derive"]} toml = "0.5" wry = {version = "0.15", features = ["devtools"]} + +[target.'cfg(windows)'.dependencies] +rcedit = {git = "https://github.com/Tim-Paik/rcedit-rs.git"} \ No newline at end of file diff --git a/neutauri_bundler/src/bundle.rs b/neutauri_bundler/src/bundle.rs index e34348b..ce86808 100644 --- a/neutauri_bundler/src/bundle.rs +++ b/neutauri_bundler/src/bundle.rs @@ -1,10 +1,9 @@ use crate::data; -use std::{fs, io::Write}; - -#[cfg(windows)] -const RUNTIME_DATA: &[u8] = include_bytes!("../../target/release/neutauri_runtime.exe"); -#[cfg(not(windows))] -const RUNTIME_DATA: &[u8] = include_bytes!("../../target/release/neutauri_runtime"); +use std::{ + env, fs, + hash::{Hash, Hasher}, + io::{self, Write}, +}; fn options() -> fs::OpenOptions { #[cfg(not(windows))] @@ -18,10 +17,31 @@ fn options() -> fs::OpenOptions { options } -pub fn bundle(config_path: String) -> std::io::Result<()> { +#[cfg(not(windows))] +fn get_runtime_data() -> io::Result> { + Ok(include_bytes!("../../target/release/neutauri_runtime").to_vec()) +} + +#[cfg(windows)] +fn get_runtime_data() -> io::Result> { + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + hasher.write(b"neutauri_runtime"); + std::time::SystemTime::now().hash(&mut hasher); + println!("Hash is {:x}!", hasher.finish()); + let temp_path = env::temp_dir().join(format!("{:x}.exe", hasher.finish())); + fs::write( + &temp_path, + include_bytes!("../../target/release/neutauri_runtime.exe"), + )?; + // let mut updater = rcedit::ResourceUpdater::new(); + // updater.load(&temp_path).unwrap(); // TODO: handle error + fs::read(&temp_path) +} + +pub fn bundle(config_path: String) -> io::Result<()> { let config_path = std::path::Path::new(&config_path).canonicalize()?; let config: data::Config = toml::from_str(fs::read_to_string(&config_path)?.as_str()) - .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?; + .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; let source = match config_path.parent() { Some(path) => path.join(&config.source).canonicalize()?, None => config.source.canonicalize()?, @@ -42,7 +62,7 @@ pub fn bundle(config_path: String) -> std::io::Result<()> { } let data = data::Data::build_from_dir(source, config.window_attr()?, config.webview_attr()?)?; let mut f = options().open(&target)?; - f.write_all(RUNTIME_DATA)?; + f.write_all(&get_runtime_data()?)?; f.write_all(&data)?; f.sync_all()?; f.flush()?;