diff --git a/client/src/index.js b/client/src/index.js index e774f928..eb4f5f34 100644 --- a/client/src/index.js +++ b/client/src/index.js @@ -10,12 +10,13 @@ import { DOM, BdUI, Modals } from 'ui'; import BdCss from './styles/index.scss'; -import { Events, CssEditor, Globals, ExtModuleManager, PluginManager, ThemeManager, ModuleManager, WebpackModules, Settings } from 'modules'; +import { Events, CssEditor, Globals, ExtModuleManager, PluginManager, ThemeManager, ModuleManager, WebpackModules, Settings, Database } from 'modules'; import { ClientLogger as Logger, ClientIPC } from 'common'; class BetterDiscord { constructor() { + window.bddb = Database; window.bdglobals = Globals; window.ClientIPC = ClientIPC; window.css = CssEditor; @@ -32,14 +33,19 @@ class BetterDiscord { } async init() { - await Settings.loadSettings(); - await ModuleManager.initModules(); - await ExtModuleManager.loadAllModules(true); - await PluginManager.loadAllPlugins(true); - await ThemeManager.loadAllThemes(true); - Modals.showContentManagerErrors(); - Events.emit('ready'); - Events.emit('discord-ready'); + try { + await Database.init(); + await Settings.loadSettings(); + await ModuleManager.initModules(); + await ExtModuleManager.loadAllModules(true); + await PluginManager.loadAllPlugins(true); + await ThemeManager.loadAllThemes(true); + Modals.showContentManagerErrors(); + Events.emit('ready'); + Events.emit('discord-ready'); + } catch (err) { + console.log('FAILED TO LOAD!', err); + } } globalReady() { diff --git a/client/src/modules/content.js b/client/src/modules/content.js index cba21ed6..0517c78b 100644 --- a/client/src/modules/content.js +++ b/client/src/modules/content.js @@ -10,6 +10,7 @@ import { Utils, FileUtils, ClientLogger as Logger, AsyncEventEmitter } from 'common'; import { Modals } from 'ui'; +import Database from './database'; export default class Content { @@ -72,12 +73,21 @@ export default class Content { */ async saveConfiguration() { try { + /* await FileUtils.writeFile(`${this.contentPath}/user.config.json`, JSON.stringify({ enabled: this.enabled, config: this.settings.strip().settings, data: this.data })); - + */ + Database.insertOrUpdate({ type: 'contentconfig', $or: [{ id: this.id }, { name: this.name }] }, { + type: 'contentconfig', + id: this.id, + name: this.name, + enabled: this.enabled, + config: this.settings.strip().settings, + data: this.data + }); this.settings.setSaved(); } catch (err) { Logger.err(this.name, ['Failed to save configuration', err]); diff --git a/client/src/modules/contentmanager.js b/client/src/modules/contentmanager.js index 789a7975..c35c4b89 100644 --- a/client/src/modules/contentmanager.js +++ b/client/src/modules/contentmanager.js @@ -15,6 +15,7 @@ import path from 'path'; import { Events } from 'modules'; import { SettingsSet, ErrorEvent } from 'structs'; import { Modals } from 'ui'; +import Database from './database'; /** * Base class for external content managing @@ -189,13 +190,16 @@ export default class { }; try { - const readUserConfig = await this.readUserConfig(contentPath); - userConfig.enabled = readUserConfig.enabled || false; - // await userConfig.config.merge({ settings: readUserConfig.config }); - // userConfig.config.setSaved(); - // userConfig.config = userConfig.config.clone({ settings: readUserConfig.config }); - userConfig.config = readUserConfig.config; - userConfig.data = readUserConfig.data || {}; + //const readUserConfig = await this.readUserConfig(contentPath); + const readUserConfig = await Database.find({ type: 'contentconfig', name: readConfig.info.name }); + if (readUserConfig.length) { + userConfig.enabled = readUserConfig[0].enabled || false; + // await userConfig.config.merge({ settings: readUserConfig.config }); + // userConfig.config.setSaved(); + // userConfig.config = userConfig.config.clone({ settings: readUserConfig.config }); + userConfig.config = readUserConfig[0].config; + userConfig.data = readUserConfig[0].data || {}; + } } catch (err) { /*We don't care if this fails it either means that user config doesn't exist or there's something wrong with it so we revert to default config*/ Logger.info(this.moduleName, `Failed reading config for ${this.contentType} ${readConfig.info.name} in ${dirName}`); Logger.err(this.moduleName, err); diff --git a/client/src/modules/database.js b/client/src/modules/database.js new file mode 100644 index 00000000..9b6e475d --- /dev/null +++ b/client/src/modules/database.js @@ -0,0 +1,34 @@ +/** + * BetterDiscord Database Module + * Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks + * All rights reserved. + * https://betterdiscord.net + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. +*/ + +import { ClientIPC } from 'bdipc'; + +export default class { + + static async init() { + return true; + } + + static async insertOrUpdate(args, data) { + try { + return ClientIPC.send('bd-dba', { action: 'update', args, data }); + } catch (err) { + throw err; + } + } + + static async find(args) { + try { + return ClientIPC.send('bd-dba', { action: 'find', args }); + } catch (err) { + throw err; + } + } +} diff --git a/client/src/modules/modules.js b/client/src/modules/modules.js index db1b5b7c..b93216a3 100644 --- a/client/src/modules/modules.js +++ b/client/src/modules/modules.js @@ -12,3 +12,4 @@ export { default as EventListener } from './eventlistener'; export { default as SocketProxy } from './socketproxy'; export { default as EventHook } from './eventhook'; export { default as Permissions } from './permissionmanager'; +export { default as Database } from './database'; diff --git a/core/src/main.js b/core/src/main.js index cca1689c..3e1c4ceb 100644 --- a/core/src/main.js +++ b/core/src/main.js @@ -26,7 +26,7 @@ const __pluginPath = path.resolve(__dirname, '..', '..', 'tests', 'plugins'); const __themePath = path.resolve(__dirname, '..', '..', 'tests', 'themes'); const __modulePath = path.resolve(__dirname, '..', '..', 'tests', 'modules'); -const { Utils, FileUtils, BDIpc, Config, WindowUtils, CSSEditor } = require('./modules'); +const { Utils, FileUtils, BDIpc, Config, WindowUtils, CSSEditor, Database } = require('./modules'); const { BrowserWindow, dialog } = require('electron'); const Common = {}; @@ -42,6 +42,8 @@ const dummyArgs = { ] }; +const dbInstance = new Database(dummyArgs.paths.find(path => path.id === 'data').path + '/storage'); + console.log(dummyArgs); @@ -87,6 +89,17 @@ class Comms { o.reply(result); }); }); + + BDIpc.on('bd-dba', o => { + (async () => { + try { + const ret = await dbInstance.exec(o.args); + o.reply(ret); + } catch (err) { + o.reply({err}); + } + })(); + }); } async readFile(o, json) { diff --git a/core/src/modules/database.js b/core/src/modules/database.js new file mode 100644 index 00000000..6e114698 --- /dev/null +++ b/core/src/modules/database.js @@ -0,0 +1,53 @@ +/** + * BetterDiscord Database Module + * Copyright (c) 2015-present JsSucks - https://github.com/JsSucks + * All rights reserved. + * https://github.com/JsSucks - https://betterdiscord.net + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. +*/ + +const Datastore = require('nedb'); + +class Database { + + constructor(dbPath) { + this.exec = this.exec.bind(this); + this.update = this.update.bind(this); + this.db = new Datastore({ filename: dbPath, autoload: true }); + } + + async update(cmd) { + return new Promise((resolve, reject) => { + this.db.update(cmd.args, cmd.data, { upsert: true }, (err, docs) => { + if (err) return reject(err); + this.db.persistence.compactDatafile(); + resolve(docs); + }); + }); + } + + async find(cmd) { + console.log('FIND', cmd); + return new Promise((resolve, reject) => { + this.db.find(cmd.args, (err, docs) => { + if (err) return reject(err); + resolve(docs); + }); + }); + } + + async exec(cmd) { + switch (cmd.action) { + case 'update': + return this.update(cmd); + case 'find': + return this.find(cmd); + } + throw 'Invalid Command'; + } + +} + +module.exports = { Database }; diff --git a/core/src/modules/index.js b/core/src/modules/index.js index a2e0d7bc..5f2029d9 100644 --- a/core/src/modules/index.js +++ b/core/src/modules/index.js @@ -2,3 +2,4 @@ export { BDIpc } from './bdipc'; export { Utils, FileUtils, WindowUtils } from './utils'; export { Config } from './config'; export { CSSEditor } from './csseditor'; +export { Database } from './database'; diff --git a/package.json b/package.json index 7cd77276..55e0f401 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "gulp-watch": "^5.0.0", "jquery": "^3.2.1", "lodash": "^4.17.4", + "nedb": "^1.8.0", "node-gyp": "^3.6.2", "node-sass": "^4.7.2", "pump": "^2.0.0",