From bbdf0955dbc6fd43ada81e4ef9d5a031a4ea4ab1 Mon Sep 17 00:00:00 2001 From: Maarten van Heusden <50550545+mmvanheusden@users.noreply.github.com> Date: Sun, 17 Nov 2024 20:59:48 +0100 Subject: [PATCH] fix: use temp dir --- package.json | 3 +- pnpm-lock.yaml | 10 ++++++ src-tauri/Cargo.lock | 26 ++++----------- src-tauri/Cargo.toml | 5 ++- src-tauri/capabilities/default.json | 7 ++-- src-tauri/src/depotdownloader.rs | 16 ++++----- src-tauri/src/main.rs | 52 ++++++++++++++++++----------- src-tauri/src/terminal.rs | 29 ++++++++-------- src-tauri/tauri.conf.json | 2 +- src/index.html | 12 +++++++ src/ts/main.ts | 16 ++++++--- 11 files changed, 105 insertions(+), 73 deletions(-) diff --git a/package.json b/package.json index 4d314ce3..d87d7fbc 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vectum", "private": true, - "version": "3.0.0", + "version": "3.0.1", "type": "module", "license": "GPL-3.0-only", "scripts": { @@ -13,6 +13,7 @@ "dependencies": { "@tauri-apps/api": "2.1.1", "@tauri-apps/plugin-dialog": "2.0.1", + "@tauri-apps/plugin-fs": "~2", "@tauri-apps/plugin-shell": "2.0.1", "jquery": "^3.7.1" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 817499c7..f10e9feb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: '@tauri-apps/plugin-dialog': specifier: 2.0.1 version: 2.0.1 + '@tauri-apps/plugin-fs': + specifier: ~2 + version: 2.0.2 '@tauri-apps/plugin-shell': specifier: 2.0.1 version: 2.0.1 @@ -413,6 +416,9 @@ packages: '@tauri-apps/plugin-dialog@2.0.1': resolution: {integrity: sha512-fnUrNr6EfvTqdls/ufusU7h6UbNFzLKvHk/zTuOiBq01R3dTODqwctZlzakdbfSp/7pNwTKvgKTAgl/NAP/Z0Q==} + '@tauri-apps/plugin-fs@2.0.2': + resolution: {integrity: sha512-4YZaX2j7ta81M5/DL8aN10kTnpUkEpkPo1FTYPT8Dd0ImHe3azM8i8MrtjrDGoyBYLPO3zFv7df/mSCYF8oA0Q==} + '@tauri-apps/plugin-shell@2.0.1': resolution: {integrity: sha512-akU1b77sw3qHiynrK0s930y8zKmcdrSD60htjH+mFZqv5WaakZA/XxHR3/sF1nNv9Mgmt/Shls37HwnOr00aSw==} @@ -1154,6 +1160,10 @@ snapshots: dependencies: '@tauri-apps/api': 2.1.1 + '@tauri-apps/plugin-fs@2.0.2': + dependencies: + '@tauri-apps/api': 2.1.1 + '@tauri-apps/plugin-shell@2.0.1': dependencies: '@tauri-apps/api': 2.1.1 diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 08675368..fe0fa17e 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -75,9 +75,9 @@ checksum = "4c95c10ba0b00a02636238b814946408b1322d5ac4760326e6fb8ec956d85775" [[package]] name = "arbitrary" -version = "1.3.2" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d5a26814d8dcb93b0e5a0ff3c6d80a8843bafb21b39e8e18a6f05471870e110" +checksum = "dde20b3d026af13f561bdd0f15edf01fc734f0dafcedbaf42bba506a9517f223" dependencies = [ "derive_arbitrary", ] @@ -836,9 +836,9 @@ dependencies = [ [[package]] name = "derive_arbitrary" -version = "1.3.2" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67e77553c4162a157adbf834ebae5b415acbecbeafc7a74b0e886657506a7611" +checksum = "30542c1ad912e0e3d22a1935c290e12e8a29d704a420177a31faad4a601a0800" dependencies = [ "proc-macro2", "quote", @@ -3752,19 +3752,6 @@ dependencies = [ "digest", ] -[[package]] -name = "sha256" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18278f6a914fa3070aa316493f7d2ddfb9ac86ebc06fa3b83bffda487e9065b0" -dependencies = [ - "async-trait", - "bytes", - "hex", - "sha2", - "tokio", -] - [[package]] name = "shared_child" version = "1.0.1" @@ -4798,18 +4785,17 @@ checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" [[package]] name = "vectum" -version = "3.0.0" +version = "3.0.1" dependencies = [ - "async-process", "derive-getters", "fix-path-env", "reqwest", "serde", "serde_json", - "sha256", "tauri", "tauri-build", "tauri-plugin-dialog", + "tauri-plugin-fs", "tauri-plugin-shell", "zip", ] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 2a8fd53f..ab29cca7 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "vectum" -version = "3.0.0" +version = "3.0.1" description = "Download older versions of Steam games with DepotDownloader" authors = ["mmvanheusden"] edition = "2021" @@ -19,10 +19,9 @@ tauri-plugin-dialog = "2.0.3" serde = { version = "1", features = ["derive"] } serde_json = "1" derive-getters = "0.5.0" -sha256 = "1.5.0" reqwest = { version = "0.12.9",features = ["blocking"] } zip = "2.2.0" -async-process = "2.3.0" +tauri-plugin-fs = "2" [profile.dev] incremental = true diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json index c4a07459..433cf8bc 100644 --- a/src-tauri/capabilities/default.json +++ b/src-tauri/capabilities/default.json @@ -2,12 +2,15 @@ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "default", "description": "Capability for the main window", - "windows": ["main"], + "windows": [ + "main" + ], "permissions": [ "core:default", "shell:allow-open", "dialog:default", "shell:allow-execute", - "shell:allow-spawn" + "shell:allow-spawn", + "fs:default" ] } \ No newline at end of file diff --git a/src-tauri/src/depotdownloader.rs b/src-tauri/src/depotdownloader.rs index d6b14b58..bba7037f 100644 --- a/src-tauri/src/depotdownloader.rs +++ b/src-tauri/src/depotdownloader.rs @@ -5,15 +5,10 @@ use std::{io::Write, path::Path}; use crate::get_os; use reqwest; -use sha256; pub static DEPOTDOWNLOADER_VERSION: &str = "2.7.4"; -pub fn calc_checksum(path: &Path) -> io::Result { - let bytes = fs::read(path)?; - let hash = sha256::digest(&bytes); - Ok(hash) -} + /** See: [`test_get_depotdownloader_url()`] @@ -35,6 +30,12 @@ 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() { + fs::create_dir_all(p)?; + } + } let mut file = File::create(filename)?; let response = reqwest::get(url) @@ -57,7 +58,7 @@ pub fn unzip(zip_file: &Path) -> io::Result<()> { let outpath = match file.enclosed_name() { Some(path) => path, - None => continue + None => continue, }; println!("Extracted {} from archive.", outpath.display()); @@ -70,7 +71,6 @@ pub fn unzip(zip_file: &Path) -> io::Result<()> { let mut outfile = File::create(&outpath)?; io::copy(&mut file, &mut outfile)?; - // Copy over permissions from enclosed file to extracted file on UNIX systems. #[cfg(unix)] { diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 50b5f4cf..d219ffb9 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -9,8 +9,12 @@ use std::time::Duration; use crate::depotdownloader::{get_depotdownloader_url, DEPOTDOWNLOADER_VERSION}; use crate::terminal::Terminal; -use tauri_plugin_shell::{ShellExt}; -use tauri::{AppHandle, Emitter}; +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; @@ -35,12 +39,15 @@ async fn preload_vectum(app: AppHandle) { Terminal::pretty_name(&TERMINAL.get().unwrap()[0]), ) .unwrap(); + + // set working directory + std::env::set_current_dir(app.path().app_cache_dir().unwrap()).unwrap(); } #[tauri::command] async fn start_download(steam_download: steam::SteamDownload, app: AppHandle) { let default_terminal = TERMINAL.get().unwrap(); - let working_dir = env::current_dir().unwrap(); + let working_dir = app.path().app_cache_dir().unwrap(); let shell = app.shell(); @@ -55,26 +62,30 @@ 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)); - println!("\t- Working directory: {}", working_dir.display()); + 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!("----------------------------------------------------------\n"); - terminal_to_use.create_command(&steam_download, shell).spawn().ok(); + terminal_to_use.create_command(&steam_download, shell, working_dir).spawn().ok(); } /// Downloads the DepotDownloader zip file from the internet based on the OS. #[tauri::command] -async fn download_depotdownloader() { +async fn download_depotdownloader(app: AppHandle) { 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::new(&zip_filename); + let depotdownloader_zip = Path::join(app.path().app_cache_dir().unwrap().as_path(), Path::new(&zip_filename)); - println!("Downloading DepotDownloader for {} to {}/{}", env::consts::OS, env::current_dir().unwrap().display(), depotdownloader_zip.display()); + println!("Downloading DepotDownloader for {} to {}", env::consts::OS, depotdownloader_zip.display()); - match depotdownloader::download_file(url.as_str(), depotdownloader_zip).await { + 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."); @@ -83,13 +94,13 @@ async fn download_depotdownloader() { println!("Failed to download DepotDownloader: {}", e); return; - }, + } _ => {} } println!("Succesfully downloaded DepotDownloader from {}", url); - depotdownloader::unzip(depotdownloader_zip).unwrap(); + depotdownloader::unzip(depotdownloader_zip.as_path()).unwrap(); println!("Succesfully extracted DepotDownloader zip."); } @@ -126,19 +137,20 @@ fn main() { // macOS: change dir to documents because upon opening, our current dir by default is "/". if get_os() == "macos" { let _ = fix_path_env::fix(); // todo: does this actually do something useful - let documents_dir = format!( - "{}/Documents/SteamDepotDownloaderGUI", - std::env::var_os("HOME").unwrap().to_str().unwrap() - ); - let documents_dir = Path::new(&documents_dir); - // println!("{}", documents_dir.display()); + // let documents_dir = format!( + // "{}/Documents/SteamDepotDownloaderGUI", + // std::env::var_os("HOME").unwrap().to_str().unwrap() + // ); + // let documents_dir = Path::new(&documents_dir); + // // println!("{}", documents_dir.display()); - std::fs::create_dir_all(documents_dir).unwrap(); - env::set_current_dir(documents_dir).unwrap(); + // std::fs::create_dir_all(documents_dir).unwrap(); + // env::set_current_dir(documents_dir).unwrap(); } println!(); tauri::Builder::default() + .plugin(tauri_plugin_fs::init()) .plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_shell::init()) .invoke_handler(tauri::generate_handler![ diff --git a/src-tauri/src/terminal.rs b/src-tauri/src/terminal.rs index 4e9e340c..436bdf4a 100644 --- a/src-tauri/src/terminal.rs +++ b/src-tauri/src/terminal.rs @@ -1,6 +1,7 @@ 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; @@ -125,7 +126,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) -> Command { + pub fn create_command(&self, steam_download: &SteamDownload, shell: &Shell, working_dir: PathBuf) -> Command { let command = create_depotdownloader_command(steam_download); match self { @@ -141,72 +142,72 @@ impl Terminal { shell.command("gnome-terminal") .args(&["--", "/usr/bin/env", "sh", "-c"]) .args(command) - .current_dir(env::current_dir().unwrap()) + .current_dir(working_dir.as_path()) } Terminal::GNOMEConsole => { shell.command("kgx") .args(&["-e", "/usr/bin/env", "sh", "-c"]) .args(command) - .current_dir(env::current_dir().unwrap()) + .current_dir(working_dir.as_path()) } Terminal::Konsole => { shell.command("konsole") .args(&["-e", "/usr/bin/env", "sh", "-c"]) .args(command) - .current_dir(env::current_dir().unwrap()) + .current_dir(working_dir.as_path()) } Terminal::Xfce4Terminal => { shell.command("xfce4-terminal") .args(&["-x", "/usr/bin/env", "sh", "-c"]) .args(command) - .current_dir(env::current_dir().unwrap()) + .current_dir(working_dir.as_path()) } Terminal::Terminator => { shell.command("terminator") .args(&["-T", "Downloading depot...", "-e"]) .args(command) - .current_dir(env::current_dir().unwrap()) + .current_dir(working_dir.as_path()) } Terminal::XTerm => { shell.command("xterm") .args(&["-hold", "-T", "Downloading depot...", "-e", "/usr/bin/env", "sh", "-c"]) .args(command) - .current_dir(env::current_dir().unwrap()) + .current_dir(working_dir.as_path()) } Terminal::Kitty => { shell.command("kitty") .args(&["/usr/bin/env", "sh", "-c"]) .args(command) - .current_dir(env::current_dir().unwrap()) + .current_dir(working_dir.as_path()) } Terminal::LXTerminal => { shell.command("lxterminal") .args(&["-e", "/usr/bin/env", "sh", "-c"]) .args(command) - .current_dir(env::current_dir().unwrap()) + .current_dir(working_dir.as_path()) } Terminal::Tilix => { shell.command("tilix") .args(&["-e", "/usr/bin/env", "sh", "-c"]) .args(command) - .current_dir(env::current_dir().unwrap()) + .current_dir(working_dir.as_path()) } Terminal::DeepinTerminal => { shell.command("deepin-terminal") .args(&["-e", "/usr/bin/env", "sh", "-c"]) .args(command) - .current_dir(env::current_dir().unwrap()) + .current_dir(working_dir.as_path()) } Terminal::Alacritty => { shell.command("alacritty") .args(&["-e", "/usr/bin/env", "sh", "-c"]) .args(command) - .current_dir(env::current_dir().unwrap()) + .current_dir(working_dir.as_path()) } Terminal::Terminal => { // Create a bash script and run that. Not very secure but it makes this easier. - let download_script = format!("#!/bin/bash\ncd {}\n{}",env::current_dir().unwrap().display(), command[0]); + let download_script = format!("#!/bin/bash\ncd {}\n{}",working_dir.as_path().display(), command[0]); // println!("{}", download_script); fs::write("./script.sh", download_script).unwrap(); @@ -220,7 +221,7 @@ impl Terminal { shell.command("/usr/bin/open") .args(&["-a", "Terminal", "./script.sh"]) - .current_dir(env::current_dir().unwrap()) + .current_dir(working_dir.as_path()) } } diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 6a8e2f1e..0d547ef0 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,6 +1,6 @@ { "productName": "SteamDepotDownloaderGUI", - "version": "3.0.0", + "version": "3.0.1", "identifier": "net.oopium.depotdownloader", "build": { "beforeDevCommand": "pnpm dev", diff --git a/src/index.html b/src/index.html index c33b9787..645a0122 100644 --- a/src/index.html +++ b/src/index.html @@ -174,6 +174,18 @@ + + +