Skip to content

Commit

Permalink
fix: resources missing
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Jan 31, 2024
1 parent 4a05f77 commit 52e0760
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
4 changes: 2 additions & 2 deletions backend/tauri/src/core/win_uwp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use runas::Command as RunasCommand;
use std::process::Command as StdCommand;

pub async fn invoke_uwptools() -> Result<()> {
let binary_path = dirs::service_path()?;
let tool_path = binary_path.with_file_name("enableLoopback.exe");
let resource_dir = dirs::app_resources_dir()?;
let tool_path = resource_dir.join("enableLoopback.exe");

if !tool_path.exists() {
bail!("enableLoopback exe not found");
Expand Down
61 changes: 61 additions & 0 deletions backend/tauri/src/utils/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,64 @@ pub fn init_resources() -> Result<()> {

Ok(())
}

/// initialize service resources
/// after tauri setup
#[cfg(target_os = "windows")]
pub fn init_service() -> Result<()> {
let service_dir = dirs::service_dir()?;
let res_dir = dirs::app_resources_dir()?;

if !service_dir.exists() {
let _ = fs::create_dir_all(&service_dir);
}
if !res_dir.exists() {
let _ = fs::create_dir_all(&res_dir);
}

let file_list = [
"clash-verge-service.exe",
"install-service.exe",
"uninstall-service.exe",
];

// copy the resource file
// if the source file is newer than the destination file, copy it over
for file in file_list.iter() {
let src_path = res_dir.join(file);
let dest_path = service_dir.join(file);

let handle_copy = || {
match fs::copy(&src_path, &dest_path) {
Ok(_) => log::debug!(target: "app", "resources copied '{file}'"),
Err(err) => {
log::error!(target: "app", "failed to copy resources '{file}', {err}")
}
};
};

if src_path.exists() && !dest_path.exists() {
handle_copy();
continue;
}

let src_modified = fs::metadata(&src_path).and_then(|m| m.modified());
let dest_modified = fs::metadata(&dest_path).and_then(|m| m.modified());

match (src_modified, dest_modified) {
(Ok(src_modified), Ok(dest_modified)) => {
if src_modified > dest_modified {
handle_copy();
} else {
log::debug!(target: "app", "skipping resource copy '{file}'");
}
}
_ => {
log::debug!(target: "app", "failed to get modified '{file}'");
handle_copy();
}
};
}

Ok(())
}
2 changes: 2 additions & 0 deletions backend/tauri/src/utils/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub fn resolve_setup(app: &mut App) {
handle::Handle::global().init(app.app_handle());

log_err!(init::init_resources());
#[cfg(target_os = "windows")]
log_err!(init::init_service());

// 处理随机端口
let enable_random_port = Config::verge().latest().enable_random_port.unwrap_or(false);
Expand Down

0 comments on commit 52e0760

Please sign in to comment.