From e8583d3a05b8cf88a066dba28799ae37dad10ce4 Mon Sep 17 00:00:00 2001 From: Samuel Walker Date: Sun, 14 Jul 2024 08:44:16 -0600 Subject: [PATCH] Finished Admin UI --- FCLauncher/src-tauri/Cargo.toml | 3 +- FCLauncher/src-tauri/src/admin.rs | 57 +++++++++++++++++++++++++++- FCLauncher/src-tauri/src/sftp.rs | 2 +- FCLauncher/src-tauri/tauri.conf.json | 5 ++- FCLauncher/src/Admin.html | 1 - FCLauncher/src/admin.js | 7 ---- FCLauncher/src/main.js | 3 +- 7 files changed, 65 insertions(+), 13 deletions(-) diff --git a/FCLauncher/src-tauri/Cargo.toml b/FCLauncher/src-tauri/Cargo.toml index ddc5524..a35076f 100644 --- a/FCLauncher/src-tauri/Cargo.toml +++ b/FCLauncher/src-tauri/Cargo.toml @@ -11,7 +11,7 @@ edition = "2021" tauri-build = { version = "1", features = [] } [dependencies] -tauri = { version = "1", features = [ "dialog-open", "updater", "dialog-ask", "shell-open"] } +tauri = { version = "1", features = [ "process-all", "dialog-open", "updater", "dialog-ask", "shell-open"] } serde = { version = "1", features = ["derive"] } serde_json = "1" suppaftp = { version = "6.0.1", features = ["native-tls"] } @@ -26,6 +26,7 @@ reqwest = { version = "0.12.5", features = ["stream"] } futures-util = "0.3.30" ssh2 = "0.9.4" chrono = "0.4.38" +zip = "2.1.3" [features] # This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!! diff --git a/FCLauncher/src-tauri/src/admin.rs b/FCLauncher/src-tauri/src/admin.rs index 1bc87ea..79c2e11 100644 --- a/FCLauncher/src-tauri/src/admin.rs +++ b/FCLauncher/src-tauri/src/admin.rs @@ -1,7 +1,13 @@ use std::io::Cursor; +use std::io::Read; +use std::fs::File; +use std::io::Seek; +use std::io::Write; +use std::path::Path; use std::path::PathBuf; use crate::modpack::get_modpacks; +use crate::modpack::get_versions; use crate::modpack::VersionEntry; use crate::sftp; use crate::modpack; @@ -9,6 +15,11 @@ use crate::ModpackEntry; use ssh2::Sftp; use ssh2::Session; use chrono; +use zip::unstable::write::FileOptionsExt; +use zip::write::FileOptions; +use zip::write::SimpleFileOptions; +use zip::ZipArchive; +use zip::ZipWriter; //static USERNAME: parking_lot::Mutex = parking_lot::const_mutex(String::new()); //static PASSWORD: parking_lot::Mutex = parking_lot::const_mutex(String::new()); @@ -125,6 +136,50 @@ pub async fn remove_pack(id: String){ } #[tauri::command] -pub async fn update_pack(id: String, path: String, version: String){ +pub async fn update_pack(window: tauri::Window, id: String, path: String, version: String) -> Result<(), String>{ println!("Update modpack {}, to version {}, from file {}", id, version, path); + let file = File::open(Path::new(path.as_str())).or(Err(format!("Unable to open file")))?; + let mut archive = ZipArchive::new(file).or(Err(format!("File not a zip archive!")))?; + let mut buf = Cursor::new(vec![]); + let mut out_archive = ZipWriter::new(&mut buf); + + for i in 0..archive.len() { + let mut file = archive.by_index(i).or(Err(format!("error reading archive")))?; + if(file.name() == "overrides/version.txt"){ + continue; + } + out_archive.start_file(file.name(), SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated)); + std::io::copy(&mut file, &mut out_archive); + } + out_archive.start_file("overrides/version.txt", SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated)); + out_archive.write_all(version.as_bytes()); + out_archive.finish(); + buf.rewind().unwrap(); + let timestamp = format!("{:?}", chrono::offset::Utc::now()); + let path = format!("Versions/{}-{}.mrpack", id, timestamp); + { + let ref mut session = *SESSION.lock().await; + if let Some(session) = session{ + let size = buf.clone().bytes().count(); + let upload_path = format!("/ftp/{}/{}", id, path.clone()); + println!("Uploading to {}", upload_path); + sftp::uplaod(Some(window), session.clone(), PathBuf::from(upload_path), &mut buf, path.clone(), size).await; + } + } + + let mut versions = get_versions(id.clone()).await?; + versions.push(VersionEntry{Version: version, Date: timestamp.clone(), File: path}); + update_versions(id.clone(), versions).await; + let mut modpacks = get_modpacks().await; + let mut index = 0; + for mut pack in modpacks.as_slice(){ + if pack.id == id { + modpacks[index].last_updated = timestamp; + break; + } + index += 1; + } + update_modpacks(modpacks).await; + + Ok(()) } \ No newline at end of file diff --git a/FCLauncher/src-tauri/src/sftp.rs b/FCLauncher/src-tauri/src/sftp.rs index 5eeb951..70759fd 100644 --- a/FCLauncher/src-tauri/src/sftp.rs +++ b/FCLauncher/src-tauri/src/sftp.rs @@ -26,7 +26,7 @@ pub async fn uplaod(window: Option, sess: Session, path: PathBuf, let mut file = sftp.create(path.as_path()).or(Err("Unable to open file"))?; let mut uploaded = 0; while true { - let mut buf = vec![0u8; 1024]; + let mut buf = vec![0u8; 1024*32]; let res = reader.read(&mut buf).unwrap(); if res <= 0 { if let Some(window) = window.clone() { diff --git a/FCLauncher/src-tauri/tauri.conf.json b/FCLauncher/src-tauri/tauri.conf.json index de22266..8f53905 100644 --- a/FCLauncher/src-tauri/tauri.conf.json +++ b/FCLauncher/src-tauri/tauri.conf.json @@ -6,7 +6,7 @@ }, "package": { "productName": "FCLauncher", - "version": "1.0.3" + "version": "1.0.4" }, "tauri": { "updater": { @@ -27,6 +27,9 @@ "all": false, "ask": true, "open": true + }, + "process": { + "all": true } }, "windows": [ diff --git a/FCLauncher/src/Admin.html b/FCLauncher/src/Admin.html index 9bb0ca9..17ac03e 100644 --- a/FCLauncher/src/Admin.html +++ b/FCLauncher/src/Admin.html @@ -22,7 +22,6 @@
-
diff --git a/FCLauncher/src/admin.js b/FCLauncher/src/admin.js index 3ab5b55..e471a6d 100644 --- a/FCLauncher/src/admin.js +++ b/FCLauncher/src/admin.js @@ -13,18 +13,11 @@ const download_progress = listen("download_progress", (progress) => { //console.log("Downloaded "+progress.payload.downloaded/(1024*1024) +"MB / " + progress.payload.total/(1024*1024) + "MB"); let downProgress = (progress.payload.downloaded/(1024*1024)).toFixed(2); let downTotal = (progress.payload.total/(1024*1024)).toFixed(2); - document.getElementById("download_name").textContent = "Downloading "+progress.payload.download_name; - document.getElementById("download_progress").textContent = downProgress + "MB / "+downTotal+"MB"; - document.getElementById("launchGame").disabled = true; downBar.style.width = `${(progress.payload.downloaded / progress.payload.total) * 100}%`; document.querySelector(".progress").style.visibility = "visible"; }); const download_finished = listen("download_finished", (event) => { - document.getElementById("download_name").textContent = ""; - document.getElementById("download_progress").textContent = ""; - document.getElementById("launchGame").disabled = false; - document.getElementById("launchGame").textContent ="Launch Minecraft"; downBar.style.width = 0; document.querySelector(".progress").style.visibility = "hidden"; }); diff --git a/FCLauncher/src/main.js b/FCLauncher/src/main.js index c8ecca9..fdf6b24 100644 --- a/FCLauncher/src/main.js +++ b/FCLauncher/src/main.js @@ -1,6 +1,7 @@ const { invoke } = window.__TAURI__.tauri; const { listen } = window.__TAURI__.event; const { ask } = window.__TAURI__.dialog; +const { exit } = window.__TAURI__.process; const downBar = document.querySelector(".progressFinished"); //import { listen } from '@tauri-apps/api'; @@ -85,7 +86,7 @@ function gameLaunch() { document.getElementById("launchGame").disabled = true; document.getElementById("launchGame").textContent ="Launching..."; //TODO Launch Game - invoke("launch_modpack", { id: selectedId}).then(window.close); + invoke("launch_modpack", { id: selectedId}).then(() => { exit(1); }); }