diff --git a/src/ui/App.tsx b/src/ui/App.tsx index 38bd875..3709faa 100644 --- a/src/ui/App.tsx +++ b/src/ui/App.tsx @@ -4,15 +4,14 @@ import './App.css' import './custom.css' // Config -import { getConfigOption } from '../utils/configuration' +import { getConfigOption, getConfig, saveConfig, setConfigOption } from '../utils/configuration' // Major Components import Topbar from './components/TopBar' import BigButton from './components/common/BigButton' function playGame() { - alert('cum') - getConfigOption('test') + } function App() { diff --git a/src/utils/configuration.ts b/src/utils/configuration.ts index d50a8d2..9ddf8e9 100644 --- a/src/utils/configuration.ts +++ b/src/utils/configuration.ts @@ -4,17 +4,23 @@ import { dataDir } from '@tauri-apps/api/path' let configFilePath: string export async function setConfigOption(key: string, value: any): Promise { - return + const config = await getConfig() + + config[key] = value + + await saveConfig(config) } export async function getConfigOption(key: string): Promise { 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 + }) } \ No newline at end of file