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