renaming and extraction display

This commit is contained in:
SpikeHD
2022-05-15 21:32:20 -07:00
parent f8af7caaff
commit 167e13c941
7 changed files with 64 additions and 11 deletions

View File

@@ -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();
}

View File

@@ -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,

View File

@@ -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);
});
}

View File

@@ -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<IProps, IState> {
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<IProps, IState> {
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<IProps, IState> {
<div className="MainProgressText">
Files Downloading: {this.state.files} ({this.state.speed})
<br />
Files Extracting: {this.state.extracting}
</div>
</div>
)

View File

@@ -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<IProps, IState> {
@@ -31,7 +33,8 @@ export default class Downloads extends React.Component<IProps, IState> {
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<IProps, IState> {
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<IProps, IState> {
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<IProps, IState> {
<Tr text="downloads.resources" />
</div>
<div className='DownloadValue'>
<BigButton disabled={this.state.resources_downloading || !this.state.grasscutter_set} onClick={this.downloadResources} id="resourcesBtn" >
<BigButton disabled={this.state.resources_downloading || !this.state.grasscutter_set || this.state.resources_exist} onClick={this.downloadResources} id="resourcesBtn" >
<Tr text="components.download" />
</BigButton>
</div>

View File

@@ -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
}

View File

@@ -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()
}
})
}