[AppSettings] Integrate Settings util within, make settings global to avoid unneeded reuse

This commit is contained in:
Ducko 2022-04-05 08:35:57 +01:00
parent 7e9ede7fc8
commit dc908c54cc
8 changed files with 50 additions and 66 deletions

View File

@ -1,5 +1,4 @@
const { releaseChannel } = require('./utils/buildInfo');
const settings = require('./appSettings').getSettings();
const titleCase = s => s[0].toUpperCase() + s.slice(1);

View File

@ -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 = () => {};
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'));

View File

@ -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');

4
src/bootstrap.js vendored
View File

@ -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'),

View File

@ -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();
});

View File

@ -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

View File

@ -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);

View File

@ -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);
}
}
}