From 167e13c941c33fe1908062ffb3fa97c3b2b50739 Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Sun, 15 May 2022 21:32:20 -0700 Subject: [PATCH] renaming and extraction display --- src-tauri/src/file_helpers.rs | 25 +++++++++++++++++++ src-tauri/src/main.rs | 3 +++ src-tauri/src/unzip.rs | 4 --- src/ui/components/common/MainProgressBar.tsx | 7 +++++- src/ui/components/menu/Downloads.tsx | 26 ++++++++++++++++---- src/utils/download.ts | 1 + src/utils/zip_utils.ts | 9 ++++++- 7 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 src-tauri/src/file_helpers.rs diff --git a/src-tauri/src/file_helpers.rs b/src-tauri/src/file_helpers.rs new file mode 100644 index 0000000..7e16792 --- /dev/null +++ b/src-tauri/src/file_helpers.rs @@ -0,0 +1,25 @@ +use std::fs; + +#[tauri::command] +pub fn rename(path: String, new_name: String) { + let mut new_path = path.clone(); + + // Check if file/folder to replace exists + if !fs::metadata(&path).is_ok() { + return; + } + + // Check if path uses forward or back slashes + if new_path.contains("\\") { + new_path = path.replace("\\", "/"); + } + + let path_replaced = &path.replace(&new_path.split("/").last().unwrap(), &new_name); + + fs::rename(path, &path_replaced).unwrap(); +} + +#[tauri::command] +pub fn dir_exists(path: String) -> bool { + return fs::metadata(&path).is_ok(); +} \ No newline at end of file diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 634d07a..a8cd0b9 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -6,6 +6,7 @@ windows_subsystem = "windows" use open; use structs::{APIQuery}; +mod file_helpers; mod unzip; mod downloader; mod lang; @@ -21,6 +22,8 @@ fn main() { run_program, run_jar, unzip::unzip, + file_helpers::rename, + file_helpers::dir_exists, open_in_browser, req_get, get_bg_file, diff --git a/src-tauri/src/unzip.rs b/src-tauri/src/unzip.rs index 845d8ef..e76c5b1 100644 --- a/src-tauri/src/unzip.rs +++ b/src-tauri/src/unzip.rs @@ -20,8 +20,6 @@ pub fn unzip(window: tauri::Window, zipfile: String, destpath: String) { thread::spawn(move || { let fullPath = writePath; - println!("Unzipping file! {}", &zipfile); - window.emit("extract_start", &zipfile); match zip_extract::extract(f, &fullPath, true) { @@ -33,8 +31,6 @@ pub fn unzip(window: tauri::Window, zipfile: String, destpath: String) { } }; - println!("Unzipping done!"); - window.emit("extract_end", &zipfile); }); } \ No newline at end of file diff --git a/src/ui/components/common/MainProgressBar.tsx b/src/ui/components/common/MainProgressBar.tsx index 78c7d6d..3a8071f 100644 --- a/src/ui/components/common/MainProgressBar.tsx +++ b/src/ui/components/common/MainProgressBar.tsx @@ -9,6 +9,7 @@ interface IProps { interface IState { average: number, files: number, + extracting: number, total: number, speed: string, } @@ -20,11 +21,12 @@ export default class ProgressBar extends React.Component { constructor(props: IProps) { super(props) - const { average, files, totalSize } = this.props.downloadManager.getTotalAverage() + const { average, files, extracting, totalSize } = this.props.downloadManager.getTotalAverage() this.state = { average, files, + extracting, total: totalSize, speed: '0 B/s' } @@ -37,6 +39,7 @@ export default class ProgressBar extends React.Component { this.setState({ average: prog?.average || 0, files: prog?.files, + extracting: prog?.extracting, total: prog?.totalSize || 0, speed: prog?.speed || '0 B/s', }) @@ -65,6 +68,8 @@ export default class ProgressBar extends React.Component {
Files Downloading: {this.state.files} ({this.state.speed}) +
+ Files Extracting: {this.state.extracting}
) diff --git a/src/ui/components/menu/Downloads.tsx b/src/ui/components/menu/Downloads.tsx index 80e291e..5a0a2f6 100644 --- a/src/ui/components/menu/Downloads.tsx +++ b/src/ui/components/menu/Downloads.tsx @@ -8,6 +8,7 @@ import BigButton from '../common/BigButton' import './Downloads.css' import Divider from './Divider' import { getConfigOption } from '../../../utils/configuration' +import { invoke } from '@tauri-apps/api' const STABLE_DOWNLOAD = 'https://nightly.link/Grasscutters/Grasscutter/workflows/build/stable/Grasscutter.zip' const DEV_DOWNLOAD = 'https://nightly.link/Grasscutters/Grasscutter/workflows/build/development/Grasscutter.zip' @@ -22,6 +23,7 @@ interface IState { grasscutter_downloading: boolean resources_downloading: boolean grasscutter_set: boolean + resources_exist: boolean } export default class Downloads extends React.Component { @@ -31,7 +33,8 @@ export default class Downloads extends React.Component { this.state = { grasscutter_downloading: this.props.downloadManager.downloadingJar(), resources_downloading: this.props.downloadManager.downloadingResources(), - grasscutter_set: false + grasscutter_set: false, + resources_exist: false } this.getGrasscutterFolder = this.getGrasscutterFolder.bind(this) @@ -43,9 +46,16 @@ export default class Downloads extends React.Component { async componentDidMount() { const gc_path = await getConfigOption('grasscutter_path') + const path = gc_path.substring(0, gc_path.lastIndexOf('\\')) + if (gc_path) { + const resources_exist: boolean = await invoke('dir_exists', { + path: path + '\\resources' + }) + this.setState({ - grasscutter_set: gc_path !== '' + grasscutter_set: gc_path !== '', + resources_exist }) } } @@ -83,8 +93,14 @@ export default class Downloads extends React.Component { async downloadResources() { const folder = await this.getGrasscutterFolder() - this.props.downloadManager.addDownload(RESOURCES_DOWNLOAD, folder + '\\resources.zip', () => { - unzip(folder + '\\resources.zip', folder + '\\') + this.props.downloadManager.addDownload(RESOURCES_DOWNLOAD, folder + '\\resources.zip', async () => { + await unzip(folder + '\\resources.zip', folder + '\\', () => { + // Rename folder to resources + invoke('rename', { + path: folder + '\\Resources', + newName: 'resources' + }) + }) }) this.disableButtons() @@ -129,7 +145,7 @@ export default class Downloads extends React.Component {
- +
diff --git a/src/utils/download.ts b/src/utils/download.ts index 09c294b..697ddcc 100644 --- a/src/utils/download.ts +++ b/src/utils/download.ts @@ -144,6 +144,7 @@ export default class DownloadHandler { return { average: (progress / total) * 100 || 0, files: this.downloads.filter(d => d.status === 'downloading').length, + extracting: this.downloads.filter(d => d.status === 'extracting').length, totalSize: total, speed: speedStr } diff --git a/src/utils/zip_utils.ts b/src/utils/zip_utils.ts index b6f1723..1c20785 100644 --- a/src/utils/zip_utils.ts +++ b/src/utils/zip_utils.ts @@ -1,8 +1,15 @@ import { invoke } from '@tauri-apps/api' +import { listen } from '@tauri-apps/api/event' -export function unzip(file: string, dest: string) { +export function unzip(file: string, dest: string, onFinish?: () => void) { invoke('unzip', { zipfile: file, destpath: dest, }) + + listen('extract_end', ({payload}) => { + if (payload === file && onFinish) { + onFinish() + } + }) } \ No newline at end of file