mirror of
https://github.com/Grasscutters/Cultivation.git
synced 2025-12-14 16:14:48 +01:00
modify config functions
This commit is contained in:
@@ -4,17 +4,23 @@ import { dataDir } from '@tauri-apps/api/path'
|
||||
let configFilePath: string
|
||||
|
||||
export async function setConfigOption(key: string, value: any): Promise<void> {
|
||||
return
|
||||
const config = await getConfig()
|
||||
|
||||
config[key] = value
|
||||
|
||||
await saveConfig(config)
|
||||
}
|
||||
|
||||
export async function getConfigOption(key: string): Promise<any> {
|
||||
const config = await getConfig()
|
||||
|
||||
return config[key] || null
|
||||
}
|
||||
|
||||
export async function getConfig() {
|
||||
const raw = await readConfigFile()
|
||||
let parsed
|
||||
|
||||
|
||||
try {
|
||||
parsed = JSON.parse(raw)
|
||||
} catch(e) {
|
||||
@@ -26,7 +32,9 @@ export async function getConfig() {
|
||||
}
|
||||
|
||||
export async function saveConfig(obj: { [key: string]: any }) {
|
||||
return
|
||||
const raw = JSON.stringify(obj)
|
||||
|
||||
await writeConfigFile(raw)
|
||||
}
|
||||
|
||||
async function readConfigFile() {
|
||||
@@ -48,18 +56,21 @@ async function readConfigFile() {
|
||||
if (!dataFiles.find((fileOrDir) => fileOrDir?.name === 'configuration.json')) {
|
||||
// Create config file
|
||||
const file: fs.FsTextFileOption = {
|
||||
path: local + 'cultivation\\configuration.json',
|
||||
path: configFilePath,
|
||||
contents: '{}'
|
||||
}
|
||||
|
||||
await fs.writeFile(file)
|
||||
}
|
||||
|
||||
// Finally read the file
|
||||
|
||||
return await fs.readTextFile(local + 'cultivation\\configuration.json')
|
||||
// Finally read the file
|
||||
return await fs.readTextFile(configFilePath)
|
||||
}
|
||||
|
||||
async function writeConfigFile() {
|
||||
return
|
||||
async function writeConfigFile(raw: string) {
|
||||
// All external config functions call readConfigFile, which ensure files exists
|
||||
await fs.writeFile({
|
||||
path: configFilePath,
|
||||
contents: raw
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user