diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 5de7b7a4..9fa73045 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -2207,7 +2207,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4" dependencies = [ "cfg-if", - "windows-targets 0.48.5", + "windows-targets 0.52.6", ] [[package]] @@ -3626,9 +3626,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.133" +version = "1.0.134" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" +checksum = "d00f4175c42ee48b15416f6193a959ba3a0d67fc699a0db9ad12df9f83991c7d" dependencies = [ "itoa 1.0.11", "memchr", @@ -4797,7 +4797,6 @@ dependencies = [ "tauri", "tauri-build", "tauri-plugin-dialog", - "tauri-plugin-fs", "tauri-plugin-shell", "zip", ] @@ -5741,9 +5740,9 @@ dependencies = [ [[package]] name = "zip" -version = "2.2.1" +version = "2.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99d52293fc86ea7cf13971b3bb81eb21683636e7ae24c729cdaf1b7c4157a352" +checksum = "ae9c1ea7b3a5e1f4b922ff856a129881167511563dc219869afe3787fc0c1a45" dependencies = [ "aes", "arbitrary", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 5720fd8c..1a1da701 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -16,12 +16,11 @@ fix-path-env = { git = "https://github.com/tauri-apps/fix-path-env-rs" } tauri = { version = "2.1.1", features = [] } tauri-plugin-shell = "2.2.0" tauri-plugin-dialog = "2.2.0" -serde = { version = "1", features = ["derive"] } -serde_json = "1" +serde = { version = "1.0.216", features = ["derive"] } +serde_json = "1.0.134" derive-getters = "0.5.0" reqwest = { version = "0.12.9",features = ["blocking"] } -zip = "2.2.1" -tauri-plugin-fs = "2" +zip = "2.2.2" diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json index 433cf8bc..2337b0a4 100644 --- a/src-tauri/capabilities/default.json +++ b/src-tauri/capabilities/default.json @@ -10,7 +10,6 @@ "shell:allow-open", "dialog:default", "shell:allow-execute", - "shell:allow-spawn", - "fs:default" + "shell:allow-spawn" ] } \ No newline at end of file diff --git a/src-tauri/src/depotdownloader.rs b/src-tauri/src/depotdownloader.rs index bba7037f..a895dfba 100644 --- a/src-tauri/src/depotdownloader.rs +++ b/src-tauri/src/depotdownloader.rs @@ -1,15 +1,14 @@ +use crate::get_os; +use reqwest; use std::fs::File; use std::io::ErrorKind::AlreadyExists; +use std::path::PathBuf; use std::{fs, io}; use std::{io::Write, path::Path}; -use crate::get_os; -use reqwest; - pub static DEPOTDOWNLOADER_VERSION: &str = "2.7.4"; - /** See: [`test_get_depotdownloader_url()`] */ @@ -30,6 +29,7 @@ pub async fn download_file(url: &str, filename: &Path) -> io::Result<()> { println!("DEBUG: Not downloading. File already exists."); return Err(io::Error::from(AlreadyExists)); } + // Create any missing directories. if let Some(p) = filename.parent() { if !p.exists() { @@ -49,15 +49,14 @@ pub async fn download_file(url: &str, filename: &Path) -> io::Result<()> { } /// Unzips DepotDownloader zips -pub fn unzip(zip_file: &Path) -> io::Result<()> { +pub fn unzip(zip_file: &Path, working_dir: &PathBuf) -> io::Result<()> { let file = File::open(zip_file)?; let mut archive = zip::ZipArchive::new(file)?; for i in 0..archive.len() { let mut file = archive.by_index(i)?; - let outpath = match file.enclosed_name() { - Some(path) => path, + Some(path) => working_dir.join(path), None => continue, }; @@ -81,9 +80,9 @@ pub fn unzip(zip_file: &Path) -> io::Result<()> { fs::set_permissions(&outpath, fs::Permissions::from_mode(mode))?; } - // Set DepotDownloader executable. - if outpath.display().to_string() == "DepotDownloader" { - fs::set_permissions(&outpath, fs::Permissions::from_mode(0o755))?; // WTF is an octal? + // Set executable permission. + if outpath.file_name().unwrap() == "DepotDownloader" { + fs::set_permissions(&outpath, fs::Permissions::from_mode(0o755))?; } } } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index d219ffb9..e1b29ca4 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,56 +1,48 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -use std::env; -use std::io; -use std::path::Path; -use std::sync::OnceLock; -use std::time::Duration; - -use crate::depotdownloader::{get_depotdownloader_url, DEPOTDOWNLOADER_VERSION}; -use crate::terminal::Terminal; -use tauri::path::BaseDirectory; -use tauri::App; -use tauri::{AppHandle, Emitter, Manager}; -use tauri_plugin_fs::FsExt; -use tauri_plugin_fs::FilePath; -use tauri_plugin_shell::ShellExt; - mod depotdownloader; mod steam; mod terminal; +use crate::depotdownloader::{get_depotdownloader_url, DEPOTDOWNLOADER_VERSION}; +use crate::terminal::Terminal; +use std::env; +use std::io::ErrorKind::AlreadyExists; +use std::path::{Path, PathBuf}; +use std::sync::OnceLock; +use std::time::Duration; +use tauri::{AppHandle, Emitter, Manager}; +use tauri_plugin_shell::ShellExt; + + /// The first terminal found. Used as default terminal. static TERMINAL: OnceLock> = OnceLock::new(); // We create this variable now, and quickly populate it in preload_vectum(). we then later access the data in start_download() +static WORKING_DIR: OnceLock = OnceLock::new(); /// This function is called every time the app is reloaded/started. It quickly populates the [`TERMINAL`] variable with a working terminal. #[tauri::command] async fn preload_vectum(app: AppHandle) { // Only fill these variables once. if TERMINAL.get().is_none() { - TERMINAL - .set(terminal::get_installed_terminals(true, app.shell()).await) - .expect("Failed to set available terminals") + TERMINAL.set(terminal::get_installed_terminals(true, app.shell()).await).expect("Failed to set available terminals") + } + + if WORKING_DIR.get().is_none() { + WORKING_DIR.set(Path::join(&app.path().local_data_dir().unwrap(), "SteamDepotDownloaderGUI")).expect("Failed to configure working directory") } // Send the default terminal name to the frontend. app.emit( "default-terminal", Terminal::pretty_name(&TERMINAL.get().unwrap()[0]), - ) - .unwrap(); - - // set working directory - std::env::set_current_dir(app.path().app_cache_dir().unwrap()).unwrap(); + ).unwrap(); } #[tauri::command] async fn start_download(steam_download: steam::SteamDownload, app: AppHandle) { let default_terminal = TERMINAL.get().unwrap(); - let working_dir = app.path().app_cache_dir().unwrap(); let shell = app.shell(); - - let terminal_to_use = if steam_download.options().terminal().is_none() { default_terminal.first().unwrap() } else { &Terminal::from_index(&steam_download.options().terminal().unwrap()).unwrap() }; println!("\n-------------------------DEBUG INFO------------------------"); @@ -62,45 +54,36 @@ async fn start_download(steam_download: steam::SteamDownload, app: AppHandle) { println!("\t- Manifest ID: {}", steam_download.manifest_id()); println!("\t- Output Path: {}", steam_download.output_path()); println!("\t- Default terminal: {}", Terminal::pretty_name(&default_terminal[0])); - println!("\t- Terminal command: {:?}", terminal_to_use.create_command(&steam_download, shell, working_dir.clone())); - println!("\t- Working directory: {}", working_dir.clone() .display()); - // println!("\t- Working directory2: {}", std::env::current_exe().unwrap().display()); - // println!("\t- Working directory2: {}", app.path().app_cache_dir().unwrap().display()); - - + println!("\t- Working directory: {}", &WORKING_DIR.get().unwrap().display()); + println!("\t- Terminal command: \n\t {:?}", terminal_to_use.create_command(&steam_download, shell, &WORKING_DIR.get().unwrap())); println!("----------------------------------------------------------\n"); - terminal_to_use.create_command(&steam_download, shell, working_dir).spawn().ok(); + terminal_to_use.create_command(&steam_download, shell, &WORKING_DIR.get().unwrap()).spawn().ok(); } /// Downloads the DepotDownloader zip file from the internet based on the OS. #[tauri::command] -async fn download_depotdownloader(app: AppHandle) { +async fn download_depotdownloader() { let url = get_depotdownloader_url(); // Where we store the DepotDownloader zip. let zip_filename = format!("DepotDownloader-v{}-{}.zip", DEPOTDOWNLOADER_VERSION, env::consts::OS); - let depotdownloader_zip = Path::join(app.path().app_cache_dir().unwrap().as_path(), Path::new(&zip_filename)); + let depotdownloader_zip = Path::join(&WORKING_DIR.get().unwrap(), Path::new(&zip_filename)); - println!("Downloading DepotDownloader for {} to {}", env::consts::OS, depotdownloader_zip.display()); - - match depotdownloader::download_file(url.as_str(), depotdownloader_zip.as_path()).await { - Err(e) => { - if e.kind() == io::ErrorKind::AlreadyExists { - println!("DepotDownloader already exists. Skipping download."); - return; - } + if let Err(e) = depotdownloader::download_file(url.as_str(), depotdownloader_zip.as_path()).await { + if e.kind() == AlreadyExists { + println!("DepotDownloader already exists. Skipping download."); + } else { println!("Failed to download DepotDownloader: {}", e); - return; } - _ => {} + return; + } else { + println!("Downloaded DepotDownloader for {} to {}", env::consts::OS, depotdownloader_zip.display()); } - println!("Succesfully downloaded DepotDownloader from {}", url); - - depotdownloader::unzip(depotdownloader_zip.as_path()).unwrap(); + depotdownloader::unzip(depotdownloader_zip.as_path(), &WORKING_DIR.get().unwrap()).unwrap(); println!("Succesfully extracted DepotDownloader zip."); } @@ -149,17 +132,11 @@ fn main() { } println!(); - tauri::Builder::default() - .plugin(tauri_plugin_fs::init()) - .plugin(tauri_plugin_dialog::init()) - .plugin(tauri_plugin_shell::init()) - .invoke_handler(tauri::generate_handler![ + tauri::Builder::default().plugin(tauri_plugin_dialog::init()).plugin(tauri_plugin_shell::init()).invoke_handler(tauri::generate_handler![ start_download, download_depotdownloader, internet_connection, preload_vectum, get_all_terminals - ]) - .run(tauri::generate_context!()) - .expect("error while running tauri application"); -} + ]).run(tauri::generate_context!()).expect("error while running tauri application"); +} \ No newline at end of file diff --git a/src-tauri/src/steam.rs b/src-tauri/src/steam.rs index 2db8c5b8..a43d40b2 100644 --- a/src-tauri/src/steam.rs +++ b/src-tauri/src/steam.rs @@ -1,6 +1,6 @@ -use std::path::PathBuf; use derive_getters::Getters; use serde::Deserialize; +use std::path::PathBuf; /// Represents the data required to download a Steam depot. diff --git a/src-tauri/src/terminal.rs b/src-tauri/src/terminal.rs index 436bdf4a..54bd2b19 100644 --- a/src-tauri/src/terminal.rs +++ b/src-tauri/src/terminal.rs @@ -1,15 +1,14 @@ -use serde::Serialize; -use tauri_plugin_shell::process::Command; -use std::{env, fs}; -use std::path::{Path, PathBuf}; -use tauri::Wry; -use tauri_plugin_shell::Shell; use crate::get_os; use crate::steam::SteamDownload; +use std::fs; +use std::path::PathBuf; +use tauri::Wry; +use tauri_plugin_shell::process::Command; +use tauri_plugin_shell::Shell; /// Represents a terminal that can be used to run commands. /// **Should be in sync with the terminal dropdown in the frontend.** -#[derive(Debug, Serialize, PartialEq)] +#[derive(Debug, PartialEq)] pub enum Terminal { GNOMETerminal, Alacritty, @@ -126,7 +125,7 @@ impl Terminal { | Terminal (macOS) | We create a bash script and run that using `open`. | */ - pub fn create_command(&self, steam_download: &SteamDownload, shell: &Shell, working_dir: PathBuf) -> Command { + pub fn create_command(&self, steam_download: &SteamDownload, shell: &Shell, working_dir: &PathBuf) -> Command { let command = create_depotdownloader_command(steam_download); match self { @@ -214,6 +213,7 @@ impl Terminal { #[cfg(unix)] { + //todo: test if still working use std::os::unix::fs::PermissionsExt; fs::set_permissions("./script.sh", fs::Permissions::from_mode(0o755)).unwrap(); // Won't run without executable permission