From dc908c54cc08cc1deda5ddc95e5e7a7f0d085b70 Mon Sep 17 00:00:00 2001 From: Oj Date: Tue, 5 Apr 2022 08:35:57 +0100 Subject: [PATCH] [AppSettings] Integrate Settings util within, make settings global to avoid unneeded reuse --- src/Constants.js | 1 - src/appSettings.js | 52 ++++++++++++++++++++++++++++++++++----- src/autoStart/win32.js | 3 --- src/bootstrap.js | 4 +-- src/config/index.js | 2 -- src/index.js | 3 ++- src/updater/appUpdater.js | 2 +- src/utils/Settings.js | 49 ------------------------------------ 8 files changed, 50 insertions(+), 66 deletions(-) delete mode 100644 src/utils/Settings.js diff --git a/src/Constants.js b/src/Constants.js index c25be3b..93c1e53 100644 --- a/src/Constants.js +++ b/src/Constants.js @@ -1,5 +1,4 @@ const { releaseChannel } = require('./utils/buildInfo'); -const settings = require('./appSettings').getSettings(); const titleCase = s => s[0].toUpperCase() + s.slice(1); diff --git a/src/appSettings.js b/src/appSettings.js index fcde384..f3dc430 100644 --- a/src/appSettings.js +++ b/src/appSettings.js @@ -1,7 +1,47 @@ -let settings; +const fs = require('fs'); -exports.getSettings = () => { - if (!settings) settings = new (require('./utils/Settings'))(require('./paths').getUserData()); - return settings; -}; -exports.init = () => {}; \ No newline at end of file +class Settings { // Heavily based on original for compat, but simplified and tweaked + constructor(path) { + this.path = path; + + try { + this.store = JSON.parse(fs.readFileSync(this.path)); + } catch (e) { + this.store = {}; + } + + this.mod = this.getMod(); + + log('AppSettings', this.path, this.store); + } + + getMod() { // Get when file was last modified + try { + return fs.statSync(this.path).mtime.getTime(); + } catch { } + } + + get(key, defaultValue) { + return this.store[key] ?? defaultValue; + } + + set(key, value) { + this.store[key] = value; + } + + save() { + if (this.mod && this.mod !== this.getMod()) return; // File was last modified after Settings was made, so was externally edited therefore we don't save over + + try { + fs.writeFileSync(this.path, JSON.stringify(this.store, null, 2)); + this.mod = this.getMod(); + + log('AppSettings', 'Saved'); + } catch (e) { + log('AppSettings', e); + } + } +} + +let inst; // Instance of class +exports.getSettings = () => inst = inst ?? new Settings(require('path').join(require('./paths').getUserData(), 'settings.json')); \ No newline at end of file diff --git a/src/autoStart/win32.js b/src/autoStart/win32.js index 91e68d1..439f94f 100644 --- a/src/autoStart/win32.js +++ b/src/autoStart/win32.js @@ -2,9 +2,6 @@ const { join, basename, dirname } = require('path'); const registry = require('../utils/registry'); -const appSettings = require('../appSettings'); -const settings = appSettings.getSettings(); - const appName = basename(process.execPath, '.exe'); const fullExeName = basename(process.execPath); const updatePath = join(dirname(process.execPath), '..', 'Update.exe'); diff --git a/src/bootstrap.js b/src/bootstrap.js index 41b1b3b..8eb2a02 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -18,13 +18,11 @@ const errorHandler = require('./errorHandler'); errorHandler.init(); const splashScreen = require('./splash'); -const appSettings = require('./appSettings'); const updater = require('./updater/updater'); const moduleUpdater = require('./updater/moduleUpdater'); const appUpdater = require('./updater/appUpdater'); -const settings = appSettings.getSettings(); if (!settings.get('enableHardwareAcceleration', true)) app.disableHardwareAcceleration(); let desktopCore; @@ -35,9 +33,9 @@ const startCore = () => { splashScreen, moduleUpdater, buildInfo, - appSettings, Constants, updater, + appSettings: require('./appSettings'), paths: require('./paths'), GPUSettings: require('./GPUSettings'), autoStart: require('./autoStart'), diff --git a/src/config/index.js b/src/config/index.js index 04f076c..6a1ae52 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -1,8 +1,6 @@ const { BrowserWindow, ipcMain, app } = require('electron'); const { join } = require('path'); -const settings = require('../appSettings').getSettings(); - ipcMain.on('DISCORD_UPDATED_QUOTES', (e, c) => { if (c === 'o') open(); }); diff --git a/src/index.js b/src/index.js index b31bac5..948bd49 100644 --- a/src/index.js +++ b/src/index.js @@ -9,7 +9,8 @@ process.resourcesPath = require('path').join(__dirname, '..'); // Force resource require('./paths').init(); -global.oaConfig = require('./appSettings').getSettings().get('openasar', {}); +global.settings = require('./appSettings').getSettings(); +global.oaConfig = settings.get('openasar', {}); require('./cmdSwitches')(); if (process.argv.includes('--overlay-host')) { // If overlay diff --git a/src/updater/appUpdater.js b/src/updater/appUpdater.js index 67d22a4..e6de963 100644 --- a/src/updater/appUpdater.js +++ b/src/updater/appUpdater.js @@ -18,7 +18,7 @@ exports.update = (startMin, done, show) => { require('../firstRun').do(inst); } else { - require('./moduleUpdater').init(Constants.UPDATE_ENDPOINT, require('../appSettings').getSettings(), buildInfo); + require('./moduleUpdater').init(Constants.UPDATE_ENDPOINT, settings, buildInfo); } splash.initSplash(startMin); diff --git a/src/utils/Settings.js b/src/utils/Settings.js deleted file mode 100644 index 4dec5b7..0000000 --- a/src/utils/Settings.js +++ /dev/null @@ -1,49 +0,0 @@ -const fs = require('fs'); -const { join } = require('path'); - -module.exports = class Settings { // Heavily based on original for compat, but simplified and tweaked - constructor(root) { - this.path = join(root, 'settings.json'); - - try { - this.settings = JSON.parse(fs.readFileSync(this.path)); - } catch (e) { - this.settings = {}; - } - - this.mod = this.getMod(); - - log('AppSettings', this.path, this.settings); - } - - getMod() { // Get when file was last modified - try { - return fs.statSync(this.path).mtime.getTime(); - } catch (e) { - return 0; - } - } - - get(key, defaultValue) { - return this.settings[key] ?? defaultValue; - } - - set(key, value) { - this.settings[key] = value; - } - - save() { - if (this.mod && this.mod !== this.getMod()) return; // File was last modified after Settings was made, so was externally edited therefore we don't save over - - try { - const str = JSON.stringify(this.settings, null, 2); - - fs.writeFileSync(this.path, str); - this.mod = this.getMod(); - - log('AppSettings', 'Saved'); - } catch (e) { - log('AppSettings', e); - } - } -} \ No newline at end of file