[AppSettings] Integrate Settings util within, make settings global to avoid unneeded reuse
This commit is contained in:
parent
7e9ede7fc8
commit
dc908c54cc
8 changed files with 50 additions and 66 deletions
|
@ -1,5 +1,4 @@
|
||||||
const { releaseChannel } = require('./utils/buildInfo');
|
const { releaseChannel } = require('./utils/buildInfo');
|
||||||
const settings = require('./appSettings').getSettings();
|
|
||||||
|
|
||||||
const titleCase = s => s[0].toUpperCase() + s.slice(1);
|
const titleCase = s => s[0].toUpperCase() + s.slice(1);
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,47 @@
|
||||||
let settings;
|
const fs = require('fs');
|
||||||
|
|
||||||
exports.getSettings = () => {
|
class Settings { // Heavily based on original for compat, but simplified and tweaked
|
||||||
if (!settings) settings = new (require('./utils/Settings'))(require('./paths').getUserData());
|
constructor(path) {
|
||||||
return settings;
|
this.path = path;
|
||||||
};
|
|
||||||
exports.init = () => {};
|
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'));
|
|
@ -2,9 +2,6 @@ const { join, basename, dirname } = require('path');
|
||||||
|
|
||||||
const registry = require('../utils/registry');
|
const registry = require('../utils/registry');
|
||||||
|
|
||||||
const appSettings = require('../appSettings');
|
|
||||||
const settings = appSettings.getSettings();
|
|
||||||
|
|
||||||
const appName = basename(process.execPath, '.exe');
|
const appName = basename(process.execPath, '.exe');
|
||||||
const fullExeName = basename(process.execPath);
|
const fullExeName = basename(process.execPath);
|
||||||
const updatePath = join(dirname(process.execPath), '..', 'Update.exe');
|
const updatePath = join(dirname(process.execPath), '..', 'Update.exe');
|
||||||
|
|
4
src/bootstrap.js
vendored
4
src/bootstrap.js
vendored
|
@ -18,13 +18,11 @@ const errorHandler = require('./errorHandler');
|
||||||
errorHandler.init();
|
errorHandler.init();
|
||||||
|
|
||||||
const splashScreen = require('./splash');
|
const splashScreen = require('./splash');
|
||||||
const appSettings = require('./appSettings');
|
|
||||||
|
|
||||||
const updater = require('./updater/updater');
|
const updater = require('./updater/updater');
|
||||||
const moduleUpdater = require('./updater/moduleUpdater');
|
const moduleUpdater = require('./updater/moduleUpdater');
|
||||||
const appUpdater = require('./updater/appUpdater');
|
const appUpdater = require('./updater/appUpdater');
|
||||||
|
|
||||||
const settings = appSettings.getSettings();
|
|
||||||
if (!settings.get('enableHardwareAcceleration', true)) app.disableHardwareAcceleration();
|
if (!settings.get('enableHardwareAcceleration', true)) app.disableHardwareAcceleration();
|
||||||
|
|
||||||
let desktopCore;
|
let desktopCore;
|
||||||
|
@ -35,9 +33,9 @@ const startCore = () => {
|
||||||
splashScreen,
|
splashScreen,
|
||||||
moduleUpdater,
|
moduleUpdater,
|
||||||
buildInfo,
|
buildInfo,
|
||||||
appSettings,
|
|
||||||
Constants,
|
Constants,
|
||||||
updater,
|
updater,
|
||||||
|
appSettings: require('./appSettings'),
|
||||||
paths: require('./paths'),
|
paths: require('./paths'),
|
||||||
GPUSettings: require('./GPUSettings'),
|
GPUSettings: require('./GPUSettings'),
|
||||||
autoStart: require('./autoStart'),
|
autoStart: require('./autoStart'),
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
const { BrowserWindow, ipcMain, app } = require('electron');
|
const { BrowserWindow, ipcMain, app } = require('electron');
|
||||||
const { join } = require('path');
|
const { join } = require('path');
|
||||||
|
|
||||||
const settings = require('../appSettings').getSettings();
|
|
||||||
|
|
||||||
ipcMain.on('DISCORD_UPDATED_QUOTES', (e, c) => {
|
ipcMain.on('DISCORD_UPDATED_QUOTES', (e, c) => {
|
||||||
if (c === 'o') open();
|
if (c === 'o') open();
|
||||||
});
|
});
|
||||||
|
|
|
@ -9,7 +9,8 @@ process.resourcesPath = require('path').join(__dirname, '..'); // Force resource
|
||||||
|
|
||||||
require('./paths').init();
|
require('./paths').init();
|
||||||
|
|
||||||
global.oaConfig = require('./appSettings').getSettings().get('openasar', {});
|
global.settings = require('./appSettings').getSettings();
|
||||||
|
global.oaConfig = settings.get('openasar', {});
|
||||||
require('./cmdSwitches')();
|
require('./cmdSwitches')();
|
||||||
|
|
||||||
if (process.argv.includes('--overlay-host')) { // If overlay
|
if (process.argv.includes('--overlay-host')) { // If overlay
|
||||||
|
|
|
@ -18,7 +18,7 @@ exports.update = (startMin, done, show) => {
|
||||||
|
|
||||||
require('../firstRun').do(inst);
|
require('../firstRun').do(inst);
|
||||||
} else {
|
} else {
|
||||||
require('./moduleUpdater').init(Constants.UPDATE_ENDPOINT, require('../appSettings').getSettings(), buildInfo);
|
require('./moduleUpdater').init(Constants.UPDATE_ENDPOINT, settings, buildInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
splash.initSplash(startMin);
|
splash.initSplash(startMin);
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue