mirror of
https://github.com/Grasscutters/Cultivation.git
synced 2025-12-14 08:04:52 +01:00
Configuration interface
This commit is contained in:
@@ -2,36 +2,53 @@ import { fs } from '@tauri-apps/api'
|
|||||||
import { dataDir } from '@tauri-apps/api/path'
|
import { dataDir } from '@tauri-apps/api/path'
|
||||||
|
|
||||||
let configFilePath: string
|
let configFilePath: string
|
||||||
|
let roamingAppData: string
|
||||||
|
let defaultConfig: Configuration
|
||||||
|
|
||||||
|
(async() => {
|
||||||
|
roamingAppData = await dataDir()
|
||||||
|
|
||||||
|
defaultConfig = {
|
||||||
|
toggle_grasscutter: false,
|
||||||
|
game_install_path: 'C:\\Program Files\\Genshin Impact\\Genshin Impact game\\Genshin Impact.exe',
|
||||||
|
grasscutter_with_game: false,
|
||||||
|
grasscutter_path: roamingAppData + '\\cultivation\\grasscutter.jar',
|
||||||
|
close_action: 0,
|
||||||
|
startup_launch: false
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
|
||||||
export async function setConfigOption(key: string, value: any): Promise<void> {
|
export async function setConfigOption(key: string, value: any): Promise<void> {
|
||||||
const config = await getConfig()
|
const config = await getConfig()
|
||||||
|
|
||||||
config[key] = value
|
Object.assign(config, { [key]: value })
|
||||||
|
|
||||||
await saveConfig(config)
|
await saveConfig(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getConfigOption(key: string): Promise<any> {
|
export async function getConfigOption(key: string): Promise<any> {
|
||||||
const config = await getConfig()
|
const config: any = await getConfig()
|
||||||
|
|
||||||
return config[key] || null
|
return config[key] || null
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getConfig() {
|
export async function getConfig() {
|
||||||
const raw = await readConfigFile()
|
const raw = await readConfigFile()
|
||||||
let parsed
|
let parsed: Configuration = defaultConfig
|
||||||
|
|
||||||
try {
|
try {
|
||||||
parsed = JSON.parse(raw)
|
parsed = <Configuration> JSON.parse(raw)
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
// We could not open the file
|
// We could not open the file
|
||||||
console.log(e)
|
console.log(e)
|
||||||
|
|
||||||
|
// TODO: Create a popup saying the config file is corrupted.
|
||||||
}
|
}
|
||||||
|
|
||||||
return parsed
|
return parsed
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function saveConfig(obj: { [key: string]: any }) {
|
export async function saveConfig(obj: Configuration) {
|
||||||
const raw = JSON.stringify(obj)
|
const raw = JSON.stringify(obj)
|
||||||
|
|
||||||
await writeConfigFile(raw)
|
await writeConfigFile(raw)
|
||||||
@@ -63,7 +80,7 @@ async function readConfigFile() {
|
|||||||
await fs.writeFile(file)
|
await fs.writeFile(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally read the file
|
// Finally, read the file
|
||||||
return await fs.readTextFile(configFilePath)
|
return await fs.readTextFile(configFilePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,4 +90,16 @@ async function writeConfigFile(raw: string) {
|
|||||||
path: configFilePath,
|
path: configFilePath,
|
||||||
contents: raw
|
contents: raw
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 'close_action': 0 = close, 1 = tray
|
||||||
|
*/
|
||||||
|
export interface Configuration {
|
||||||
|
toggle_grasscutter: boolean
|
||||||
|
game_install_path: string
|
||||||
|
grasscutter_with_game: boolean
|
||||||
|
grasscutter_path: string
|
||||||
|
close_action: number
|
||||||
|
startup_launch: boolean
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user