Add core settings storage

This commit is contained in:
Samuel Elliott 2018-02-12 15:19:11 +00:00
parent 0eddaf86eb
commit 4dfe349802
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
5 changed files with 74 additions and 5 deletions

3
.gitignore vendored
View File

@ -21,4 +21,5 @@ Installers/**/*/obj
Installers/**/*/packages Installers/**/*/packages
.vs .vs
dist/ dist/
user.config.json user.config.json
tests/data

View File

@ -10,7 +10,7 @@
import { DOM, BdUI } from 'ui'; import { DOM, BdUI } from 'ui';
import BdCss from './styles/index.scss'; import BdCss from './styles/index.scss';
import { Events, CssEditor, Globals, PluginManager, ThemeManager, ModuleManager, WebpackModules } from 'modules'; import { Events, CssEditor, Globals, PluginManager, ThemeManager, ModuleManager, WebpackModules, Settings } from 'modules';
import { ClientLogger as Logger, ClientIPC } from 'common'; import { ClientLogger as Logger, ClientIPC } from 'common';
class BetterDiscord { class BetterDiscord {
@ -21,11 +21,13 @@ class BetterDiscord {
window.pm = PluginManager; window.pm = PluginManager;
window.events = Events; window.events = Events;
window.wpm = WebpackModules; window.wpm = WebpackModules;
window.bdsettings = Settings;
DOM.injectStyle(BdCss, 'bdmain'); DOM.injectStyle(BdCss, 'bdmain');
Events.on('global-ready', this.globalReady.bind(this)); Events.on('global-ready', this.globalReady.bind(this));
} }
async init() { async init() {
await Settings.loadSettings();
await ModuleManager.initModules(); await ModuleManager.initModules();
await PluginManager.loadAllPlugins(); await PluginManager.loadAllPlugins();
await ThemeManager.loadAllThemes(); await ThemeManager.loadAllThemes();

View File

@ -9,9 +9,69 @@
*/ */
import defaultSettings from '../data/user.settings.default'; import defaultSettings from '../data/user.settings.default';
import { default as Globals } from './globals';
import { FileUtils, ClientLogger as Logger } from 'common';
import path from 'path';
export default class { export default class {
static async loadSettings() {
try {
await FileUtils.ensureDirectory(this.dataPath);
const settingsPath = path.resolve(this.dataPath, 'user.settings.json');
const user_config = await FileUtils.readJsonFromFile(settingsPath);
const { settings } = user_config;
this.settings = defaultSettings;
for (let newCategory of settings) {
let category = this.settings.find(c => c.id === newCategory.id);
if (!category) continue;
for (let newSetting of newCategory.settings) {
let setting = category.settings.find(s => s.id === newSetting.id);
if (!setting) continue;
setting.enabled = newSetting.enabled;
}
}
} catch (err) {
// There was an error loading settings
// This probably means that the user doesn't have any settings yet
Logger.err('Settings', err);
}
}
static async saveSettings() {
try {
await FileUtils.ensureDirectory(this.dataPath);
const settingsPath = path.resolve(this.dataPath, 'user.settings.json');
await FileUtils.writeJsonToFile(settingsPath, {
settings: this.getSettings.map(category => {
return {
id: category.id,
settings: category.settings.map(setting => {
return {
id: setting.id,
enabled: setting.enabled
};
})
};
})
});
} catch (err) {
// There was an error loading settings
// This probably means that the user doesn't have any settings yet
Logger.err('Settings', err);
}
}
static get getSettings() { static get getSettings() {
return defaultSettings; return this.settings ? this.settings : defaultSettings;
}
static get dataPath() {
return this._dataPath ? this._dataPath : (this._dataPath = Globals.getObject('paths').find(p => p.id === 'data').path);
} }
} }

View File

@ -126,14 +126,18 @@
enableSetting(cat, id) { enableSetting(cat, id) {
switch (cat) { switch (cat) {
case 'core': case 'core':
return this.coreSettings.find(setting => setting.id === id).enabled = true; this.coreSettings.find(setting => setting.id === id).enabled = true; break;
} }
Settings.saveSettings();
}, },
disableSetting(cat, id) { disableSetting(cat, id) {
switch (cat) { switch (cat) {
case 'core': case 'core':
return this.coreSettings.find(setting => setting.id === id).enabled = false; this.coreSettings.find(setting => setting.id === id).enabled = false; break;
} }
Settings.saveSettings();
}, },
openGithub() { openGithub() {
console.log('open github?'); console.log('open github?');

View File

@ -21,6 +21,7 @@ const __DEV = {
clientScriptPath: `${clientScriptPath}/betterdiscord.client.js` clientScriptPath: `${clientScriptPath}/betterdiscord.client.js`
} }
const __dataPath = path.resolve(__dirname, '..', '..', 'tests', 'data');
const __pluginPath = path.resolve(__dirname, '..', '..', 'tests', 'plugins'); const __pluginPath = path.resolve(__dirname, '..', '..', 'tests', 'plugins');
const __themePath = path.resolve(__dirname, '..', '..', 'tests', 'themes'); const __themePath = path.resolve(__dirname, '..', '..', 'tests', 'themes');
@ -33,6 +34,7 @@ const dummyArgs = {
'version': '0.3.1', 'version': '0.3.1',
'paths': [ 'paths': [
{ 'id': 'base', 'path': 'basePath' }, { 'id': 'base', 'path': 'basePath' },
{ 'id': 'data', 'path': __dataPath },
{ 'id': 'plugins', 'path': __pluginPath }, { 'id': 'plugins', 'path': __pluginPath },
{ 'id': 'themes', 'path': __themePath } { 'id': 'themes', 'path': __themePath }
] ]