From ac774930e5296daf3928efe86f8cdb168fa06436 Mon Sep 17 00:00:00 2001 From: Jiiks Date: Tue, 15 Dec 2015 16:03:39 +0200 Subject: [PATCH] Move loaders to bd module for 0.2.4 --- lib/BetterDiscord.js | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/lib/BetterDiscord.js b/lib/BetterDiscord.js index 0485492..645b65b 100644 --- a/lib/BetterDiscord.js +++ b/lib/BetterDiscord.js @@ -70,12 +70,87 @@ var _this; function BetterDiscord(mainWindow) { _this = this; _mainWindow = mainWindow; + this.initLoaders(); _version = _config.Core.Version; _utils = new _utils.Utils(mainWindow); this.createAndCheckData(this.init); } +BetterDiscord.prototype.initLoaders = function(){ + var os = process.platform; + var _dataPath = os == "win32" ? process.env.APPDATA : os == 'darwin' ? process.env.HOME + '/Library/Preferences' : '/var/local'; + _dataPath += "/BetterDiscord/"; + + _mainWindow.webContents.on('dom-ready', function() { + if (!_fs.existsSync(_dataPath)) { + console.log('BetterDiscord: Creating BD Dir'); + _fs.mkdirSync(_dataPath); + } + + if (!_fs.existsSync(_dataPath + "plugins/")) { + console.log('BetterDiscord: Creating Plugins Dir'); + _fs.mkdirSync(_dataPath + "plugins/"); + } + + if (!_fs.existsSync(_dataPath + "themes/")) { + console.log('BetterDiscord: Creating Themes Dir'); + _fs.mkdirSync(_dataPath + "themes/"); + } + _mainWindow.webContents.executeJavaScript('var themesupport2 = true'); + + _fs.readdir(_dataPath + "plugins/", function(err, files) { + if (err) { + console.log(err); + return; + } + _mainWindow.webContents.executeJavaScript('var bdplugins = {};'); + files.forEach(function(fileName) { + var plugin = _fs.readFileSync(_dataPath + "plugins/" + fileName, 'utf8'); + var meta = plugin.split('\n')[0]; + if (meta.indexOf('META') < 0) { + console.log('BetterDiscord: ERROR[Plugin META not found in file: ' + fileName + ']'); + return; + } + var pluginVar = meta.substring(meta.lastIndexOf('//META') + 6, meta.lastIndexOf('*//')); + var parse = JSON.parse(pluginVar); + var pluginName = parse['name']; + console.log('BetterDiscord: Loading Plugin: ' + pluginName); + _mainWindow.webContents.executeJavaScript(plugin); + _mainWindow.webContents.executeJavaScript('(function() { var plugin = new ' + pluginName + '(); bdplugins[plugin.getName()] = { "plugin": plugin, "enabled": false } })();') + }); + }); + + _fs.readdir(_dataPath + 'themes/', function(err, files) { + if (err) { + console.log(err); + return; + } + _mainWindow.webContents.executeJavaScript('var bdthemes = {};'); + files.forEach(function(fileName) { + var theme = _fs.readFileSync(_dataPath + 'themes/' + fileName, 'utf8'); + var split = theme.split('\n'); + var meta = split[0]; + if (meta.indexOf('META') < 0) { + console.log('BetterDiscord: ERROR[Theme META not found in file: ' + fileName + ']'); + return; + } + var themeVar = meta.substring(meta.lastIndexOf('//META') + 6, meta.lastIndexOf('*//')); + var parse = JSON.parse(themeVar); + var themeName = parse['name']; + var themeAuthor = parse['author']; + var themeDescription = parse['description']; + var themeVersion = parse['version']; + console.log('BetterDiscord: Loading Theme: ' + themeName); + split.splice(0, 1); + theme = split.join('\n'); + theme = theme.replace(/(\r\n|\n|\r)/gm, ''); + _mainWindow.webContents.executeJavaScript('(function() { bdthemes["' + themeName + '"] = { "enabled": false, "name": "' + themeName + '", "css": "' + escape(theme) + '", "description": "' + themeDescription + '", "author":"' + themeAuthor + '", "version":"' + themeVersion + '" } })();'); + }); + }); + }); +}; + BetterDiscord.prototype.getUtils = function() { return _utils; }