BetterDiscordApp-v2/v2/lib/core.js

109 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-10-31 16:36:03 +01:00
/* BetterDiscordApp Core
* Version: 2:1.0
* Author: Jiiks | https://jiiks.net
* Date: 31/10/2016
* Last Update: 31/10/2016
* Github: https://github.com/Jiiks/BetterDiscordApp
* Git: https://github.com/Jiiks/BetterDiscordApp.git
* License: MIT
*/
'use strict';
const
_fs = require('fs'),
_config = require('./config.json'),
{ _utils } = require('./utils'),
{ _logger } = require('./utils'),
{ EOL } = require('os'),
2016-10-31 16:57:12 +01:00
_os = require('os');
2016-10-31 16:36:03 +01:00
const _startDate = new Date();
var _cfg;
// Default configs if for some reason we don't receive config params
2016-10-31 16:57:12 +01:00
const _defaults = {
2017-02-05 14:12:16 +01:00
installPath: (_os.platform() === 'win32' ? process.env.APPDATA : _os.platform() === 'darwin' ? `${process.env.HOME}/Library/Preferences`: `/var/local`) + '/BetterDiscord/lib',
dataPath: (_os.platform() === 'win32' ? process.env.APPDATA : _os.platform() === 'darwin' ? `${process.env.HOME}/Library/Preferences`: `/var/local`) + '/BetterDiscord/data',
2016-10-31 16:57:12 +01:00
emotesEnabled: true
};
2016-10-31 16:36:03 +01:00
const _resources = {
"mainjs": {
"path": "js",
"filename": "main.js",
"var": "window.BetterDiscord"
},
"jQuery": {
"path": "vendor",
"filename": "jquery-2.2.4.min.js",
2016-11-02 06:38:32 +01:00
"var": "window.$ = window.jQuery"
}
};
var _self;
2016-10-31 16:36:03 +01:00
class Core {
constructor(args) {
_self = this;
this.continue = true;
_logger.log(`v${this.__version} Loading - OS: ${_os.platform()}`);
2016-10-31 16:57:12 +01:00
this.initConfig(args.cfg);
2016-10-31 16:36:03 +01:00
}
initConfig(args) {
2016-10-31 16:57:12 +01:00
_cfg = _config.cfg;
_cfg.installPath = args.installPath || _defaults.installPath;
_cfg.dataPath = args.dataPath || _defaults.dataPath;
_cfg.emotesEnabled = args.emotesEnabled || _defaults.emotesEnabled;
2016-10-31 16:36:03 +01:00
}
hook(mainWindow) {
if(mainWindow === undefined) {
this.exit("mainWindow is undefined!");
return;
}
this.mainWindow = mainWindow;
this.hookEvent('dom-ready', this.domReady);
}
hookEvent(event, callback) {
if(!this.continue) return;
try {
_logger.log(`Hooking ${event}`);
this.mainWindow.webContents.on(event, callback);
}catch(err) {
this.exit(`Failed to hook event ${event}. Reason: ${err}`);
}
}
domReady() {
for(var key in _resources) {
var resource = _resources[key];
2016-11-01 04:42:15 +01:00
_utils.requireJs(`${_cfg.installPath}/${resource.path}/${resource.filename}`, resource.var, _self.mainWindow);
}
2016-11-01 04:42:15 +01:00
_self.pluginLoader();
}
pluginLoader() {
}
exit(reason, severity) {
this.continue = false;
// TODO show an actual error dialog for user
_logger.log(`Quitting. Reason: ${reason}`, severity || 2);
_logger.save();
}
get __version() {
return _config.core.version;
}
2016-10-31 16:36:03 +01:00
}
exports.BetterDiscord = Core;