mirror of
https://github.com/Grasscutters/Cultivation.git
synced 2025-12-14 16:14:48 +01:00
mod enabling and disabling
This commit is contained in:
@@ -36,6 +36,11 @@ const headers = [
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mods currently install into folder labelled with their GB ID
|
||||||
|
*
|
||||||
|
* @TODO Categorizaiton/sorting (by likes, views, etc)
|
||||||
|
*/
|
||||||
export class Mods extends React.Component<IProps, IState> {
|
export class Mods extends React.Component<IProps, IState> {
|
||||||
constructor(props: IProps) {
|
constructor(props: IProps) {
|
||||||
super(props)
|
super(props)
|
||||||
@@ -49,10 +54,6 @@ export class Mods extends React.Component<IProps, IState> {
|
|||||||
this.addDownload = this.addDownload.bind(this)
|
this.addDownload = this.addDownload.bind(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
async componentDidMount() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
async addDownload(mod: ModData) {
|
async addDownload(mod: ModData) {
|
||||||
const modFolder = await getModsFolder()
|
const modFolder = await getModsFolder()
|
||||||
const modPath = `${modFolder}${mod.id}.zip`
|
const modPath = `${modFolder}${mod.id}.zip`
|
||||||
@@ -101,7 +102,7 @@ export class Mods extends React.Component<IProps, IState> {
|
|||||||
</TopBar>
|
</TopBar>
|
||||||
|
|
||||||
<div className="TopDownloads">
|
<div className="TopDownloads">
|
||||||
<ProgressBar downloadManager={this.props.downloadHandler} />
|
<ProgressBar downloadManager={this.props.downloadHandler} withStats={false} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<ModHeader onChange={this.setCategory} headers={headers} defaultHeader={'ripe'} />
|
<ModHeader onChange={this.setCategory} headers={headers} defaultHeader={'ripe'} />
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ export default class Checkbox extends React.Component<IProps, IState> {
|
|||||||
|
|
||||||
handleChange = () => {
|
handleChange = () => {
|
||||||
this.setState({ checked: !this.state.checked })
|
this.setState({ checked: !this.state.checked })
|
||||||
console.log(this.props.onChange)
|
|
||||||
this.props.onChange()
|
this.props.onChange()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ export default class ProgressBar extends React.Component<IProps, IState> {
|
|||||||
></div>
|
></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{this.props.withStats && (
|
{(this.props.withStats === undefined || this.props.withStats) && (
|
||||||
<div className="MainProgressText">
|
<div className="MainProgressText">
|
||||||
<Tr text="main.files_downloading" /> {this.state.files} ({this.state.speed})
|
<Tr text="main.files_downloading" /> {this.state.files} ({this.state.speed})
|
||||||
<br />
|
<br />
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import Download from '../../../resources/icons/download.svg'
|
|||||||
import Folder from '../../../resources/icons/folder.svg'
|
import Folder from '../../../resources/icons/folder.svg'
|
||||||
import { shell } from '@tauri-apps/api'
|
import { shell } from '@tauri-apps/api'
|
||||||
import Checkbox from '../common/Checkbox'
|
import Checkbox from '../common/Checkbox'
|
||||||
|
import { disableMod, enableMod, modIsEnabled } from '../../../utils/mods'
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
mod: ModData | PartialModData
|
mod: ModData | PartialModData
|
||||||
@@ -33,15 +34,51 @@ export class ModTile extends React.Component<IProps, IState> {
|
|||||||
this.toggleMod = this.toggleMod.bind(this)
|
this.toggleMod = this.toggleMod.bind(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getModFolderName() {
|
||||||
|
if (!('id' in this.props.mod)) {
|
||||||
|
return this.props.mod.name.includes('DISABLED_') ? this.props.mod.name.split('DISABLED_')[1] : this.props.mod.name
|
||||||
|
}
|
||||||
|
|
||||||
|
return String(this.props.mod.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
async componentDidMount() {
|
||||||
|
if (!('id' in this.props.mod)) {
|
||||||
|
// Partial mod
|
||||||
|
this.setState({
|
||||||
|
modEnabled: await modIsEnabled(this.props.mod.name),
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(this.props.mod.name + ' ' + this.state.modEnabled)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(this.props.mod.name + ' ' + this.state.modEnabled)
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
modEnabled: await modIsEnabled(String(this.props.mod.id)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
async openInExplorer() {
|
async openInExplorer() {
|
||||||
if (this.props.path) shell.open(this.props.path)
|
if (this.props.path) shell.open(this.props.path)
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleMod() {
|
toggleMod() {
|
||||||
console.log('Mod toggled')
|
this.setState(
|
||||||
this.setState({
|
{
|
||||||
modEnabled: !this.state.modEnabled,
|
modEnabled: !this.state.modEnabled,
|
||||||
})
|
},
|
||||||
|
() => {
|
||||||
|
if (this.state.modEnabled) {
|
||||||
|
enableMod(String(this.getModFolderName()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
disableMod(String(this.getModFolderName()))
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
@@ -60,7 +97,7 @@ export class ModTile extends React.Component<IProps, IState> {
|
|||||||
},
|
},
|
||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
<span className="ModName">{mod.name}</span>
|
<span className="ModName">{mod.name.includes('DISABLED_') ? mod.name.split('DISABLED_')[1] : mod.name}</span>
|
||||||
<span className="ModAuthor">{mod.submitter.name}</span>
|
<span className="ModAuthor">{mod.submitter.name}</span>
|
||||||
<div className="ModImage">
|
<div className="ModImage">
|
||||||
{this.state.hover &&
|
{this.state.hover &&
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { invoke } from '@tauri-apps/api'
|
||||||
import { getConfigOption } from './configuration'
|
import { getConfigOption } from './configuration'
|
||||||
|
|
||||||
export async function getModsFolder() {
|
export async function getModsFolder() {
|
||||||
@@ -11,3 +12,60 @@ export async function getModsFolder() {
|
|||||||
|
|
||||||
return pathArr.join('/') + '/Mods/'
|
return pathArr.join('/') + '/Mods/'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function disableMod(modId: string) {
|
||||||
|
const path = (await getModsFolder()) + modId
|
||||||
|
const pathExists = await invoke('dir_exists', {
|
||||||
|
path,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!pathExists) return console.log("Path doesn't exist")
|
||||||
|
|
||||||
|
const modName = path.replace(/\\/g, '/').split('/').pop()
|
||||||
|
|
||||||
|
await invoke('rename', {
|
||||||
|
path,
|
||||||
|
newName: `DISABLED_${modName}`,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function enableMod(modId: string) {
|
||||||
|
const path = (await getModsFolder()) + `DISABLED_${modId}`
|
||||||
|
const modName = path.replace(/\\/g, '/').split('/').pop()
|
||||||
|
const pathExists = await invoke('dir_exists', {
|
||||||
|
path,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!pathExists) return console.log("Path doesn't exist")
|
||||||
|
|
||||||
|
if (!modName?.includes('DISABLED_')) return
|
||||||
|
|
||||||
|
const newName = modName.replace('DISABLED_', '')
|
||||||
|
|
||||||
|
await invoke('rename', {
|
||||||
|
path,
|
||||||
|
newName,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getModFolderName(modId: string) {
|
||||||
|
const modsFolder = await getModsFolder()
|
||||||
|
|
||||||
|
if (!modsFolder) return null
|
||||||
|
|
||||||
|
const modEnabled = await invoke('dir_exists', {
|
||||||
|
path: modsFolder + modId,
|
||||||
|
})
|
||||||
|
const modDisabled = await invoke('dir_exists', {
|
||||||
|
path: modsFolder + 'DISABLED_' + modId,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!modEnabled && !modDisabled) return null
|
||||||
|
|
||||||
|
if (modEnabled) return modId
|
||||||
|
if (modDisabled) return 'DISABLED_' + modId
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function modIsEnabled(modId: string) {
|
||||||
|
return !(await getModFolderName(modId))?.includes('DISABLED_')
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user