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 open;
use structs::{APIQuery}; use structs::{APIQuery};
mod file_helpers;
mod unzip; mod unzip;
mod downloader; mod downloader;
mod lang; mod lang;
@@ -21,6 +22,8 @@ fn main() {
run_program, run_program,
run_jar, run_jar,
unzip::unzip, unzip::unzip,
file_helpers::rename,
file_helpers::dir_exists,
open_in_browser, open_in_browser,
req_get, req_get,
get_bg_file, get_bg_file,

View File

@@ -20,8 +20,6 @@ pub fn unzip(window: tauri::Window, zipfile: String, destpath: String) {
thread::spawn(move || { thread::spawn(move || {
let fullPath = writePath; let fullPath = writePath;
println!("Unzipping file! {}", &zipfile);
window.emit("extract_start", &zipfile); window.emit("extract_start", &zipfile);
match zip_extract::extract(f, &fullPath, true) { 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); window.emit("extract_end", &zipfile);
}); });
} }

View File

@@ -9,6 +9,7 @@ interface IProps {
interface IState { interface IState {
average: number, average: number,
files: number, files: number,
extracting: number,
total: number, total: number,
speed: string, speed: string,
} }
@@ -20,11 +21,12 @@ export default class ProgressBar extends React.Component<IProps, IState> {
constructor(props: IProps) { constructor(props: IProps) {
super(props) super(props)
const { average, files, totalSize } = this.props.downloadManager.getTotalAverage() const { average, files, extracting, totalSize } = this.props.downloadManager.getTotalAverage()
this.state = { this.state = {
average, average,
files, files,
extracting,
total: totalSize, total: totalSize,
speed: '0 B/s' speed: '0 B/s'
} }
@@ -37,6 +39,7 @@ export default class ProgressBar extends React.Component<IProps, IState> {
this.setState({ this.setState({
average: prog?.average || 0, average: prog?.average || 0,
files: prog?.files, files: prog?.files,
extracting: prog?.extracting,
total: prog?.totalSize || 0, total: prog?.totalSize || 0,
speed: prog?.speed || '0 B/s', speed: prog?.speed || '0 B/s',
}) })
@@ -65,6 +68,8 @@ export default class ProgressBar extends React.Component<IProps, IState> {
<div className="MainProgressText"> <div className="MainProgressText">
Files Downloading: {this.state.files} ({this.state.speed}) Files Downloading: {this.state.files} ({this.state.speed})
<br />
Files Extracting: {this.state.extracting}
</div> </div>
</div> </div>
) )

View File

@@ -8,6 +8,7 @@ import BigButton from '../common/BigButton'
import './Downloads.css' import './Downloads.css'
import Divider from './Divider' import Divider from './Divider'
import { getConfigOption } from '../../../utils/configuration' 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 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' const DEV_DOWNLOAD = 'https://nightly.link/Grasscutters/Grasscutter/workflows/build/development/Grasscutter.zip'
@@ -22,6 +23,7 @@ interface IState {
grasscutter_downloading: boolean grasscutter_downloading: boolean
resources_downloading: boolean resources_downloading: boolean
grasscutter_set: boolean grasscutter_set: boolean
resources_exist: boolean
} }
export default class Downloads extends React.Component<IProps, IState> { export default class Downloads extends React.Component<IProps, IState> {
@@ -31,7 +33,8 @@ export default class Downloads extends React.Component<IProps, IState> {
this.state = { this.state = {
grasscutter_downloading: this.props.downloadManager.downloadingJar(), grasscutter_downloading: this.props.downloadManager.downloadingJar(),
resources_downloading: this.props.downloadManager.downloadingResources(), resources_downloading: this.props.downloadManager.downloadingResources(),
grasscutter_set: false grasscutter_set: false,
resources_exist: false
} }
this.getGrasscutterFolder = this.getGrasscutterFolder.bind(this) this.getGrasscutterFolder = this.getGrasscutterFolder.bind(this)
@@ -43,9 +46,16 @@ export default class Downloads extends React.Component<IProps, IState> {
async componentDidMount() { async componentDidMount() {
const gc_path = await getConfigOption('grasscutter_path') const gc_path = await getConfigOption('grasscutter_path')
const path = gc_path.substring(0, gc_path.lastIndexOf('\\'))
if (gc_path) { if (gc_path) {
const resources_exist: boolean = await invoke('dir_exists', {
path: path + '\\resources'
})
this.setState({ 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() { async downloadResources() {
const folder = await this.getGrasscutterFolder() const folder = await this.getGrasscutterFolder()
this.props.downloadManager.addDownload(RESOURCES_DOWNLOAD, folder + '\\resources.zip', () => { this.props.downloadManager.addDownload(RESOURCES_DOWNLOAD, folder + '\\resources.zip', async () => {
unzip(folder + '\\resources.zip', folder + '\\') await unzip(folder + '\\resources.zip', folder + '\\', () => {
// Rename folder to resources
invoke('rename', {
path: folder + '\\Resources',
newName: 'resources'
})
})
}) })
this.disableButtons() this.disableButtons()
@@ -129,7 +145,7 @@ export default class Downloads extends React.Component<IProps, IState> {
<Tr text="downloads.resources" /> <Tr text="downloads.resources" />
</div> </div>
<div className='DownloadValue'> <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" /> <Tr text="components.download" />
</BigButton> </BigButton>
</div> </div>

View File

@@ -144,6 +144,7 @@ export default class DownloadHandler {
return { return {
average: (progress / total) * 100 || 0, average: (progress / total) * 100 || 0,
files: this.downloads.filter(d => d.status === 'downloading').length, files: this.downloads.filter(d => d.status === 'downloading').length,
extracting: this.downloads.filter(d => d.status === 'extracting').length,
totalSize: total, totalSize: total,
speed: speedStr speed: speedStr
} }

View File

@@ -1,8 +1,15 @@
import { invoke } from '@tauri-apps/api' 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', { invoke('unzip', {
zipfile: file, zipfile: file,
destpath: dest, destpath: dest,
}) })
listen('extract_end', ({payload}) => {
if (payload === file && onFinish) {
onFinish()
}
})
} }