From aa54db3ae7e8aefacb0ebe3ad1fa3747bde99bf3 Mon Sep 17 00:00:00 2001 From: developomp Date: Fri, 21 Jan 2022 21:17:09 +0900 Subject: [PATCH] install logic improvement - added more install steps event - implemented more install procedures --- src-tauri/src/commands/install.rs | 77 +++++++++++++++++++++++-------- src-tauri/src/util.rs | 4 +- src/pages/Install/index.svelte | 21 ++++++--- 3 files changed, 74 insertions(+), 28 deletions(-) diff --git a/src-tauri/src/commands/install.rs b/src-tauri/src/commands/install.rs index 1d13419..7e03911 100644 --- a/src-tauri/src/commands/install.rs +++ b/src-tauri/src/commands/install.rs @@ -10,7 +10,11 @@ use std::env; #[derive(Clone)] enum InstallSteps { DownloadBepInEx, + InstallBepInEx, + UnixLaunchOption, + LaunchGame, DownloadWbmZip, + InstallWbm, Done, } @@ -59,13 +63,15 @@ pub async fn install(window: Window, game_path: String) -> i64 { // install_bepinex(&window, game_path).await; - download_wbm_zip(&window, game_path).await; + unix_launch_option_setup(&window, game_path).await; + launch_game_once(&window).await; + install_wbm_mod(&window, game_path).await; // // // - util::emit(&window, constants::EVENT_INSTALL, InstallSteps::Done as i64); + emit(&window, InstallSteps::Done); println!("Install complete!"); return InstallResult::NoErr as i64; @@ -102,6 +108,7 @@ async fn install_bepinex(window: &Window, game_path: &str) { println!("Unzipping BepInEx.zip"); match util::unzip(bepinex_path.as_str(), &game_path) { Ok(()) => { + emit(&window, InstallSteps::InstallBepInEx); println!("Successfully unzipped BepInEx.zip to {}", game_path); } @@ -119,14 +126,21 @@ async fn install_bepinex(window: &Window, game_path: &str) { // done - util::emit( - &window, - constants::EVENT_INSTALL, - InstallSteps::DownloadBepInEx as i64, - ); + emit(&window, InstallSteps::DownloadBepInEx); } -async fn download_wbm_zip(window: &Window, game_path: &str) { +async fn unix_launch_option_setup(window: &Window, game_path: &str) { + println!("{}", game_path); + + emit(&window, InstallSteps::UnixLaunchOption); +} + +async fn launch_game_once(window: &Window) { + println!("Launch Game once"); + emit(&window, InstallSteps::LaunchGame); +} + +async fn install_wbm_mod(window: &Window, game_path: &str) { // todo: get url of latest zip let latest_release = &json::parse(util::get_wbm_release_data().await.as_str()).unwrap()[0] @@ -136,8 +150,37 @@ async fn download_wbm_zip(window: &Window, game_path: &str) { util::download_zip_to_cache_dir(latest_release.as_str().unwrap(), "WBM.zip").await; match download_zip_result { - Ok(path) => { - println!("downloaded WBM.zip: {}", path) + Ok(zip_path) => { + let wbm_path = std::path::Path::new(game_path).join("BepInEx/plugins/WBM"); + + println!("Removing existing files"); + match std::fs::remove_dir_all(wbm_path.clone()) { + Ok(_) => {} + Err(_) => { + panic!("Failed to remove existing WBM mod files"); + } + }; + + println!("Creating WBM directory"); + match std::fs::create_dir_all(wbm_path.clone()) { + Ok(_) => {} + Err(_) => { + panic!("Failed to create WBM mod directory"); + } + } + + // unzip file + // todo: handle unwrap error + match util::unzip(zip_path.as_str(), wbm_path.to_str().unwrap()) { + Ok(()) => { + emit(&window, InstallSteps::InstallWbm); + } + + Err(err) => { + // todo: handle error + panic!("Failed to unzip WBM.zip:{:#?}", err); + } + }; } Err(_) => { @@ -146,15 +189,11 @@ async fn download_wbm_zip(window: &Window, game_path: &str) { } } - // unzip file - - println!("{}", game_path); - // done - util::emit( - &window, - constants::EVENT_INSTALL, - InstallSteps::DownloadWbmZip as i64, - ); + emit(&window, InstallSteps::DownloadWbmZip); +} + +fn emit(window: &Window, payload: InstallSteps) { + util::emit(&window, constants::EVENT_INSTALL, payload as i64); } diff --git a/src-tauri/src/util.rs b/src-tauri/src/util.rs index 51d29a8..e218c83 100644 --- a/src-tauri/src/util.rs +++ b/src-tauri/src/util.rs @@ -1,8 +1,6 @@ use tauri::api::path::{cache_dir, home_dir}; use tauri::Window; -use serde::Serialize; - use futures_util::StreamExt; use std::cmp::min; use std::fs; @@ -121,7 +119,7 @@ pub async fn download_zip_to_cache_dir(url: &str, file_name: &str) -> Result(window: &Window, event: &str, payload: S) { +pub fn emit(window: &Window, event: &str, payload: S) { window.emit(event, payload).unwrap(); } diff --git a/src/pages/Install/index.svelte b/src/pages/Install/index.svelte index c7ea4fc..9ad31b1 100644 --- a/src/pages/Install/index.svelte +++ b/src/pages/Install/index.svelte @@ -9,9 +9,13 @@ import { COMMANDS, EVENTS } from "../../constants" - enum Steps { + enum InstallSteps { DownloadBepInEx, + InstallBepInEx, + UnixLaunchOption, + LaunchGame, DownloadWbmZip, + InstallWbm, Done, } @@ -21,31 +25,36 @@ } // - // + // variables // let isInstallButtonClicked = false let wasDefaultGamePathFound = true let spinCog = false + let stepsStatus = { DownloadBepInEx: false, DownloadWbmZip: false, Done: false, } - listen(EVENTS.INSTALL, (event) => { + // + // functions + // + + listen(EVENTS.INSTALL, (event) => { switch (event.payload) { - case Steps.DownloadBepInEx: { + case InstallSteps.DownloadBepInEx: { stepsStatus.DownloadBepInEx = true break } - case Steps.DownloadWbmZip: { + case InstallSteps.DownloadWbmZip: { stepsStatus.DownloadWbmZip = true break } - case Steps.Done: { + case InstallSteps.Done: { spinCog = false stepsStatus.Done = true break