WIP: zip stuff

This commit is contained in:
SpikeHD
2022-05-15 16:50:12 -07:00
parent 11ad0e4453
commit 75954e944b
7 changed files with 206 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ import React from 'react'
import Menu from './Menu'
import Tr from '../../../utils/language'
import DownloadHandler from '../../../utils/download'
import { unzip } from '../../../utils/zip_utils'
import BigButton from '../common/BigButton'
import './Downloads.css'
@@ -20,6 +21,7 @@ interface IProps {
interface IState {
grasscutter_downloading: boolean
resources_downloading: boolean
grasscutter_set: boolean
}
export default class Downloads extends React.Component<IProps, IState> {
@@ -28,7 +30,8 @@ export default class Downloads extends React.Component<IProps, IState> {
this.state = {
grasscutter_downloading: this.props.downloadManager.downloadingJar(),
resources_downloading: this.props.downloadManager.downloadingResources()
resources_downloading: this.props.downloadManager.downloadingResources(),
grasscutter_set: false
}
this.getGrasscutterFolder = this.getGrasscutterFolder.bind(this)
@@ -38,6 +41,15 @@ export default class Downloads extends React.Component<IProps, IState> {
this.disableButtons = this.disableButtons.bind(this)
}
async componentDidMount() {
const gc_path = await getConfigOption('grasscutter_path')
if (gc_path) {
this.setState({
grasscutter_set: gc_path !== ''
})
}
}
async getGrasscutterFolder() {
const path = await getConfigOption('grasscutter_path')
let folderPath
@@ -67,7 +79,9 @@ 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')
this.props.downloadManager.addDownload(RESOURCES_DOWNLOAD, folder + '\\resources.zip', () => {
unzip(folder + '\\resources.zip', 'Grasscutter_Resources/Resources/', folder + '/resources')
})
this.disableButtons()
}
@@ -111,7 +125,7 @@ export default class Downloads extends React.Component<IProps, IState> {
<Tr text="downloads.resources" />
</div>
<div className='DownloadValue'>
<BigButton disabled={this.state.resources_downloading} onClick={this.downloadResources} id="resourcesBtn" >
<BigButton disabled={this.state.resources_downloading || !this.state.grasscutter_set} onClick={this.downloadResources} id="resourcesBtn" >
<Tr text="components.download" />
</BigButton>
</div>

View File

@@ -1,4 +1,3 @@
import { invoke } from '@tauri-apps/api/tauri'
import { listen } from '@tauri-apps/api/event'
import { byteToString } from './string'
@@ -12,6 +11,7 @@ export default class DownloadHandler {
startTime: number,
error?: string,
speed?: string,
onFinish?: () => void,
}[]
// Pass tauri invoke function
@@ -44,6 +44,12 @@ export default class DownloadHandler {
// set status to finished
const index = this.downloads.findIndex(download => download.path === filename)
this.downloads[index].status = 'finished'
// Call onFinish callback
if (this.downloads[index]?.onFinish) {
// @ts-expect-error onFinish is checked for existence before being called
this.downloads[index]?.onFinish()
}
})
listen('download_error', (...payload) => {
@@ -74,7 +80,7 @@ export default class DownloadHandler {
return this.downloads.some(d => d.path.includes('resources'))
}
addDownload(url: string, path: string) {
addDownload(url: string, path: string, onFinish?: () => void) {
// Begin download from rust backend, don't add if the download addition fails
invoke('download_file', { url, path })
const obj = {
@@ -83,6 +89,7 @@ export default class DownloadHandler {
total: 0,
status: 'downloading',
startTime: Date.now(),
onFinish,
}
this.downloads.push(obj)

9
src/utils/zip_utils.ts Normal file
View File

@@ -0,0 +1,9 @@
import { invoke } from '@tauri-apps/api'
export function unzip(file: string, zippath: string, dest: string) {
invoke('unzip', {
zipfile: file,
zippath: zippath,
destpath: dest,
})
}