-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
197 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
export default interface InstanceSettings { | ||
manifest: string; | ||
memory: { | ||
min: 1024; | ||
max: 1024; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default interface Settings { | ||
selectedAccount: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import fs from 'fs/promises'; | ||
import fsSync from 'fs'; | ||
|
||
import Settings from '../../common/settings'; | ||
import { getLauncherFile } from '../utils/dir.utils'; | ||
import Logger from '../logger'; | ||
|
||
const DEFAULT_SETTINGS: Settings = { | ||
selectedAccount: -1, | ||
}; | ||
|
||
export default class SettingsProvider { | ||
private configFile: string; | ||
private settings: Settings | null; | ||
|
||
constructor() { | ||
this.configFile = getLauncherFile('settings.json'); | ||
this.settings = null; | ||
} | ||
|
||
async saveConfig(toSave?: Settings): Promise<void> { | ||
if (toSave) { | ||
this.settings = toSave; | ||
} | ||
|
||
if (this.settings != null) { | ||
const raw = JSON.stringify(this.settings); | ||
await fs.writeFile(this.configFile, raw, { encoding: 'utf-8' }); | ||
Logger.info(`Saved launcher configuration file.`); | ||
} else { | ||
Logger.warn(`Prvent config save due to null object.`); | ||
} | ||
} | ||
|
||
async createDefaultConfig(overwrites = false): Promise<boolean> { | ||
const exist = fsSync.existsSync(this.configFile); | ||
if (exist && !overwrites) { | ||
return false; | ||
} | ||
|
||
this.settings = DEFAULT_SETTINGS; | ||
await this.saveConfig(); | ||
return true; | ||
} | ||
|
||
async loadConfig(): Promise<Settings> { | ||
await this.createDefaultConfig(false); | ||
|
||
const raw = await fs.readFile(this.configFile, { encoding: 'utf-8' }); | ||
const loadedConfig = JSON.parse(raw) as Settings; | ||
this.settings = { ...DEFAULT_SETTINGS, ...loadedConfig }; | ||
Logger.info(`Loaded launcher configuration file.`); | ||
return this.settings; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react'; | ||
import Settings from 'common/settings'; | ||
import SettingsHook from './settings-hook'; | ||
|
||
const SettingsContext = React.createContext<SettingsHook>({ | ||
settings: null, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
setSettings: (settings: Settings): void => {}, | ||
}); | ||
|
||
export default SettingsContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Settings from 'common/settings'; | ||
|
||
export default interface SettingsHook { | ||
settings: Settings | null; | ||
setSettings: (settings: Settings) => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { PropsWithChildren, useEffect, useState } from 'react'; | ||
|
||
import Settings from 'common/settings'; | ||
import { loadSettings, saveSettings } from 'renderer/services/settings.service'; | ||
import useLogs from 'renderer/hooks/useLogs'; | ||
import VersionsContext from './settings-context'; | ||
|
||
export default function SettingsProvider({ children }: PropsWithChildren) { | ||
const [settings, setSettings] = useState<Settings | null>(null); | ||
const [firstLoad, setFirstLoad] = useState(true); | ||
const logger = useLogs(); | ||
|
||
useEffect(() => { | ||
loadSettings() | ||
// eslint-disable-next-line promise/always-return | ||
.then((loadedSettings: Settings) => { | ||
setSettings(loadedSettings); | ||
setFirstLoad(true); | ||
}) | ||
.catch(logger.crit); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (settings && !firstLoad) { | ||
saveSettings(settings); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [settings]); | ||
|
||
return ( | ||
<VersionsContext.Provider | ||
value={{ | ||
setSettings, | ||
settings, | ||
}} | ||
> | ||
{children} | ||
</VersionsContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Settings from 'common/settings'; | ||
|
||
export function loadSettings(): Promise<Settings> { | ||
return new Promise((resolve) => { | ||
window.electron.ipcRenderer.once('settings:load', (settings: unknown) => { | ||
resolve(settings as Settings); | ||
}); | ||
|
||
window.electron.ipcRenderer.sendMessage('settings:load', []); | ||
}); | ||
} | ||
|
||
export function saveSettings(settingstoSave: Settings): Promise<Settings> { | ||
return new Promise((resolve) => { | ||
window.electron.ipcRenderer.once('settings:save', (settings: unknown) => { | ||
resolve(settings as Settings); | ||
}); | ||
|
||
window.electron.ipcRenderer.sendMessage('settings:save', [settingstoSave]); | ||
}); | ||
} |