From 9566beaf29dc2834cf30302cd9564c971e2f0c0c Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Mon, 25 Jul 2022 20:25:43 -0700 Subject: [PATCH] mod enabling and disabling --- src/ui/Mods.tsx | 11 ++-- src/ui/components/common/Checkbox.tsx | 1 - src/ui/components/common/MainProgressBar.tsx | 2 +- src/ui/components/mods/ModTile.tsx | 47 ++++++++++++++-- src/utils/mods.ts | 58 ++++++++++++++++++++ 5 files changed, 107 insertions(+), 12 deletions(-) diff --git a/src/ui/Mods.tsx b/src/ui/Mods.tsx index 22e8cf8..51fdbf8 100644 --- a/src/ui/Mods.tsx +++ b/src/ui/Mods.tsx @@ -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 { constructor(props: IProps) { super(props) @@ -49,10 +54,6 @@ export class Mods extends React.Component { this.addDownload = this.addDownload.bind(this) } - async componentDidMount() { - return - } - async addDownload(mod: ModData) { const modFolder = await getModsFolder() const modPath = `${modFolder}${mod.id}.zip` @@ -101,7 +102,7 @@ export class Mods extends React.Component {
- +
diff --git a/src/ui/components/common/Checkbox.tsx b/src/ui/components/common/Checkbox.tsx index 0a33505..c3b938e 100644 --- a/src/ui/components/common/Checkbox.tsx +++ b/src/ui/components/common/Checkbox.tsx @@ -35,7 +35,6 @@ export default class Checkbox extends React.Component { handleChange = () => { this.setState({ checked: !this.state.checked }) - console.log(this.props.onChange) this.props.onChange() } diff --git a/src/ui/components/common/MainProgressBar.tsx b/src/ui/components/common/MainProgressBar.tsx index 89b5407..ac69e48 100644 --- a/src/ui/components/common/MainProgressBar.tsx +++ b/src/ui/components/common/MainProgressBar.tsx @@ -71,7 +71,7 @@ export default class ProgressBar extends React.Component { > - {this.props.withStats && ( + {(this.props.withStats === undefined || this.props.withStats) && (
{this.state.files} ({this.state.speed})
diff --git a/src/ui/components/mods/ModTile.tsx b/src/ui/components/mods/ModTile.tsx index 2f453ae..8fe6cbf 100644 --- a/src/ui/components/mods/ModTile.tsx +++ b/src/ui/components/mods/ModTile.tsx @@ -8,6 +8,7 @@ import Download from '../../../resources/icons/download.svg' import Folder from '../../../resources/icons/folder.svg' import { shell } from '@tauri-apps/api' import Checkbox from '../common/Checkbox' +import { disableMod, enableMod, modIsEnabled } from '../../../utils/mods' interface IProps { mod: ModData | PartialModData @@ -33,15 +34,51 @@ export class ModTile extends React.Component { 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() { if (this.props.path) shell.open(this.props.path) } toggleMod() { - console.log('Mod toggled') - this.setState({ - modEnabled: !this.state.modEnabled, - }) + this.setState( + { + modEnabled: !this.state.modEnabled, + }, + () => { + if (this.state.modEnabled) { + enableMod(String(this.getModFolderName())) + return + } + + disableMod(String(this.getModFolderName())) + } + ) } render() { @@ -60,7 +97,7 @@ export class ModTile extends React.Component { }, })} > - {mod.name} + {mod.name.includes('DISABLED_') ? mod.name.split('DISABLED_')[1] : mod.name} {mod.submitter.name}
{this.state.hover && diff --git a/src/utils/mods.ts b/src/utils/mods.ts index 56caf00..a3c1825 100644 --- a/src/utils/mods.ts +++ b/src/utils/mods.ts @@ -1,3 +1,4 @@ +import { invoke } from '@tauri-apps/api' import { getConfigOption } from './configuration' export async function getModsFolder() { @@ -11,3 +12,60 @@ export async function getModsFolder() { 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_') +}