From 225e7545e61b4df2c7404114ebba64246017b86e Mon Sep 17 00:00:00 2001 From: GeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com> Date: Mon, 15 May 2023 21:50:40 +0200 Subject: feat: Button to install NorthstarLauncher from git main branch (#314) * feat: WIP code to install launcher from git main Works but needs some more cleanup still * fix: Formatting * fix: Address clippy errors * refactor: Move `install_git_main` to own source file * fix: Remove unnecessary `pub` * docs: Update comments * refactor: Move API URL to constants --- src-tauri/src/constants.rs | 4 ++ src-tauri/src/development/mod.rs | 84 +++++++++++++++++++++++++++++++++++ src-tauri/src/github/mod.rs | 4 +- src-tauri/src/github/pull_requests.rs | 2 +- src-tauri/src/main.rs | 3 ++ src-vue/src/views/DeveloperView.vue | 21 +++++++++ 6 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 src-tauri/src/development/mod.rs diff --git a/src-tauri/src/constants.rs b/src-tauri/src/constants.rs index 43df0eeb..4005a139 100644 --- a/src-tauri/src/constants.rs +++ b/src-tauri/src/constants.rs @@ -51,3 +51,7 @@ pub const FLIGHTCORE_REPO_NAME: &str = "R2NorthstarTools/FlightCore"; // Northstar release repo name and org name on GitHub pub const NORTHSTAR_RELEASE_REPO_NAME: &str = "R2Northstar/Northstar"; + +// URL to launcher commits API URL +pub const NS_LAUNCHER_COMMITS_API_URL: &str = + "https://api.github.com/repos/R2Northstar/NorthstarLauncher/commits"; diff --git a/src-tauri/src/development/mod.rs b/src-tauri/src/development/mod.rs new file mode 100644 index 00000000..be02966d --- /dev/null +++ b/src-tauri/src/development/mod.rs @@ -0,0 +1,84 @@ +use crate::constants::NS_LAUNCHER_COMMITS_API_URL; +use crate::github::{ + pull_requests::{check_github_api, download_zip_into_memory, get_launcher_download_link}, + CommitInfo, +}; + +#[tauri::command] +pub async fn install_git_main(game_install_path: &str) -> Result { + // Get list of commits + let commits: Vec = serde_json::from_value( + check_github_api(NS_LAUNCHER_COMMITS_API_URL) + .await + .expect("Failed request"), + ) + .unwrap(); + + // Get latest commit... + let latest_commit_sha = commits[0].sha.clone(); + // ...and according artifact download URL + let download_url = get_launcher_download_link(latest_commit_sha.clone()).await?; + + let archive = match download_zip_into_memory(download_url).await { + Ok(archive) => archive, + Err(err) => return Err(err.to_string()), + }; + + let extract_directory = format!( + "{}/___flightcore-temp-download-dir/launcher-pr-{}", + game_install_path, latest_commit_sha + ); + match std::fs::create_dir_all(extract_directory.clone()) { + Ok(_) => (), + Err(err) => { + return Err(format!( + "Failed creating temporary download directory: {}", + err + )) + } + }; + + let target_dir = std::path::PathBuf::from(extract_directory.clone()); // Doesn't need to exist + match zip_extract::extract(std::io::Cursor::new(archive), &target_dir, true) { + Ok(()) => (), + Err(err) => { + return Err(format!("Failed unzip: {}", err)); + } + }; + + // Copy only necessary files from temp dir + // Copy: + // - NorthstarLauncher.exe + // - Northstar.dll + let files_to_copy = vec!["NorthstarLauncher.exe", "Northstar.dll"]; + for file_name in files_to_copy { + let source_file_path = format!("{}/{}", extract_directory, file_name); + let destination_file_path = format!("{}/{}", game_install_path, file_name); + match std::fs::copy(source_file_path, destination_file_path) { + Ok(_result) => (), + Err(err) => { + return Err(format!( + "Failed to copy necessary file {} from temp dir: {}", + file_name, err + )) + } + }; + } + + // delete extract directory + match std::fs::remove_dir_all(&extract_directory) { + Ok(()) => (), + Err(err) => { + return Err(format!( + "Failed to delete temporary download directory: {}", + err + )) + } + } + + log::info!( + "All done with installing launcher from {}", + latest_commit_sha + ); + Ok(latest_commit_sha) +} diff --git a/src-tauri/src/github/mod.rs b/src-tauri/src/github/mod.rs index a4e390d3..0eab305b 100644 --- a/src-tauri/src/github/mod.rs +++ b/src-tauri/src/github/mod.rs @@ -31,8 +31,8 @@ pub struct TagWrapper { } #[derive(Debug, Deserialize)] -struct CommitInfo { - sha: String, +pub struct CommitInfo { + pub sha: String, commit: Commit, author: Option, } diff --git a/src-tauri/src/github/pull_requests.rs b/src-tauri/src/github/pull_requests.rs index e600e2e4..b30fd272 100644 --- a/src-tauri/src/github/pull_requests.rs +++ b/src-tauri/src/github/pull_requests.rs @@ -127,7 +127,7 @@ pub async fn check_github_api(url: &str) -> Result Result, anyhow::Error> { +pub async fn download_zip_into_memory(download_url: String) -> Result, anyhow::Error> { let client = reqwest::Client::builder() .user_agent(APP_USER_AGENT) .build()?; diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 87f70ae2..9a93d636 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -16,6 +16,8 @@ use winapi::um::winuser::{MessageBoxW, MB_ICONERROR, MB_OK, MB_USERICON}; use crate::constants::REFRESH_DELAY; +mod development; + mod github; use github::release_notes::check_is_flightcore_outdated; @@ -159,6 +161,7 @@ fn main() { github::pull_requests::apply_mods_pr, github::pull_requests::get_launcher_download_link, close_application, + development::install_git_main, get_available_northstar_versions, ]) .run(tauri::generate_context!()) diff --git a/src-vue/src/views/DeveloperView.vue b/src-vue/src/views/DeveloperView.vue index 59c7927f..aa682095 100644 --- a/src-vue/src/views/DeveloperView.vue +++ b/src-vue/src/views/DeveloperView.vue @@ -31,6 +31,10 @@ Launch Northstar via Steam + + Install launcher from main branch + +

@@ -243,6 +247,23 @@ export default defineComponent({ showErrorNotification(error); }); }, + async installLauncherGitMain() { + + const notification = showNotification(`Installing git main`, 'Please wait', 'info', 0); + + await invoke("install_git_main", { gameInstallPath: this.$store.state.game_path }) + .then((message) => { + this.release_notes_text = message; + showNotification("Done", `Installed launcher build from ${message}`); + }) + .catch((error) => { + showErrorNotification(error); + }) + .finally(() => { + // Clear old notification + notification.close(); + }); + }, async getAvailableNorthstarVersions() { await invoke("get_available_northstar_versions") .then((message) => { -- cgit v1.2.3