import { app } from "electron" ; import { mkdirsSync } from "fs-extra" ; import { existsSync as fileExists , readFileSync , writeFileSync } from "fs" ; import { join as joinPath , sep as pathSep } from "path" ; import Logger from "./Logger" ; export default class Config { param key param noDefault returns public static readConfigValue ( key , noDefault ? ) { try { if ( fileExists ( this . configPath ) ) { const contents = readFileSync ( this . configPath ) . toString ( "utf8" ) ; const config = JSON . parse ( contents ) ; if ( config . hasOwnProperty ( key ) ) { return config [ key ] ; } else if ( ! noDefault && this . defaultConfig [ key ] ) { return this . defaultConfig [ key ] ; } else { return undefined ; } } else { if ( ! noDefault && this . defaultConfig [ key ] ) { return this . defaultConfig [ key ] ; } else { return undefined ; } } } catch ( e ) { writeFileSync ( this . configPath , "{}" ) ; if ( ! noDefault && this . defaultConfig [ key ] ) { return this . defaultConfig [ key ] ; } else { return undefined ; } } } param key param value public static saveConfigValue ( key , value ) { let config = { } ; if ( fileExists ( this . configPath ) ) { let contents ; try { contents = readFileSync ( this . configPath ) . toString ( "utf8" ) ; } catch ( e ) { contents = "{}" ; writeFileSync ( this . configPath , "{}" ) ; } config = JSON . parse ( contents ) ; } Logger . info ( "Config" , key + " was set to " + JSON . stringify ( value ) ) ; config [ key ] = value ; mkdirsSync ( this . configPath . split ( pathSep ) . slice ( 0 , - 1 ) . join ( pathSep ) ) ; writeFileSync ( this . configPath , JSON . stringify ( config ) ) ; } /**
     * The default config values
     */ private static defaultConfig = { installFolder : joinPath ( app . getPath ( "userData" ) , "GameData" ) , background : "default.png" , discordEnabled : true , sdkMode : "always" , updateChannel : "latest" , language : global . ddmm_constants . default_language , localUI : null , modBackgrounds : true , renpy : { skipSplash : false , skipMenu : false , screenVariant : null } } ; private static configPath = joinPath ( app . getPath ( "userData" ) , "config.json" ) ; }