From 51b1913ab1cb757e106f985fed1393d443df96b7 Mon Sep 17 00:00:00 2001 From: Jiiks Date: Wed, 30 Mar 2016 04:37:03 +0300 Subject: [PATCH] 0.2.7 --- lib/BetterDiscord.js | 204 ++++++++++++++++++++++++++++++------------- lib/Utils.js | 13 ++- lib/config.json | 2 +- package.json | 4 +- 4 files changed, 157 insertions(+), 66 deletions(-) diff --git a/lib/BetterDiscord.js b/lib/BetterDiscord.js index ac4e5ce..2d84b46 100644 --- a/lib/BetterDiscord.js +++ b/lib/BetterDiscord.js @@ -1,8 +1,8 @@ /* BetterDiscordApp Entry - * Version: 2.1 + * Version: 2.2 * Author: Jiiks | http://jiiks.net * Date: 27/08/2015 - 15:51 - * Last Update: 10/02/2015 - 02:05 GMT + * Last Update: 30/03/2016 * https://github.com/Jiiks/BetterDiscordApp */ @@ -36,7 +36,7 @@ var _dataPath; //IDE /*_config = { "Core": { - "Version": "0.2.5" + "Version": "0.2.7" } };*/ @@ -60,24 +60,24 @@ BetterDiscord.prototype.initLoaders = function(){ _dataPath += "/BetterDiscord/"; if (!_fs.existsSync(_dataPath)) { - console.log('BetterDiscord: Creating BD Dir'); + _this.getUtils().log('BetterDiscord: Creating BD Dir'); _fs.mkdirSync(_dataPath); } if (!_fs.existsSync(_dataPath + "plugins/")) { - console.log('BetterDiscord: Creating Plugins Dir'); + _this.getUtils().log('BetterDiscord: Creating Plugins Dir'); _fs.mkdirSync(_dataPath + "plugins/"); } if (!_fs.existsSync(_dataPath + "themes/")) { - console.log('BetterDiscord: Creating Themes Dir'); + _this.getUtils().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); + _this.getUtils().log(err); return; } _mainWindow.webContents.executeJavaScript('var bdplugins = {};'); @@ -85,13 +85,13 @@ BetterDiscord.prototype.initLoaders = function(){ 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 + ']'); + _this.getUtils().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); + _this.getUtils().log('BetterDiscord: Loading Plugin: ' + pluginName); _mainWindow.webContents.executeJavaScript(plugin); _mainWindow.webContents.executeJavaScript('(function() { var plugin = new ' + pluginName + '(); bdplugins[plugin.getName()] = { "plugin": plugin, "enabled": false } })();') }); @@ -99,7 +99,7 @@ BetterDiscord.prototype.initLoaders = function(){ _fs.readdir(_dataPath + 'themes/', function(err, files) { if (err) { - console.log(err); + _this.getUtils().log(err); return; } _mainWindow.webContents.executeJavaScript('var bdthemes = {};'); @@ -108,7 +108,7 @@ BetterDiscord.prototype.initLoaders = function(){ 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 + ']'); + _this.getUtils().log('BetterDiscord: ERROR[Theme META not found in file: ' + fileName + ']'); return; } var themeVar = meta.substring(meta.lastIndexOf('//META') + 6, meta.lastIndexOf('*//')); @@ -117,7 +117,7 @@ BetterDiscord.prototype.initLoaders = function(){ var themeAuthor = parse['author']; var themeDescription = parse['description']; var themeVersion = parse['version']; - console.log('BetterDiscord: Loading Theme: ' + themeName); + _this.getUtils().log('BetterDiscord: Loading Theme: ' + themeName); split.splice(0, 1); theme = split.join('\n'); theme = theme.replace(/(\r\n|\n|\r)/gm, ''); @@ -225,6 +225,7 @@ BetterDiscord.prototype.init = function() { self.getUtils().log("Latest Versions: " + _updater.LatestVersion); self.getUtils().log("Using CDN: " + _updater.CDN); self.getUtils().log("Starting up"); + self.updateData(); self.start(); }); @@ -239,7 +240,7 @@ BetterDiscord.prototype.start = function() { }; BetterDiscord.prototype.quit = function(reason) { - console.log("BetterDiscord ERR: " + reason); + _this.getUtils().log("BetterDiscord ERR: " + reason); }; var ipcHooked = false; @@ -300,12 +301,12 @@ BetterDiscord.prototype.load = function(reload) { }; var loadCount = 0; +var libCount; +var loadUs; -BetterDiscord.prototype.ipcAsyncMessage = function(event, arg) { - - var libCount = 9; - - var loadUs = { +BetterDiscord.prototype.updateData = function() { + libCount = 9; + loadUs = { 'load-jQueryUI': { 'type': 'javascript', 'resource': 'jQueryUI', @@ -358,8 +359,27 @@ BetterDiscord.prototype.ipcAsyncMessage = function(event, arg) { 'localpath': _dataPath + "/emotes_twitch_global.json", 'encoding': "utf8", 'https': true, - 'parse': true, - 'specialparser': 0 + 'parse': false, + 'specialparser': 0, + 'fallback': 'load-emoteData-twitchGlobal-fallback', + 'self': 'load-emoteData-twitchGlobal' + }, + 'load-emoteData-twitchGlobal-fallback': { + 'type': 'emotedata', + 'resource': 'Twitch Global Emotedata', + 'domain': _updater.CDN, + 'url': '/' + _repo + '/BetterDiscordApp/' + _hash + '/data/emotedata_twitch_global.json', + 'localurl': null, + 'message': 'load-emoteData-twitchSub', + 'cacheable': true, + 'variable': 'emotesTwitch', + 'localpath': _dataPath + "/emotes_twitch_global.json", + 'encoding': "utf8", + 'https': true, + 'parse': false, + 'specialparser': 0, + 'fallback': 'load-emoteData-twitchSub', + 'self': 'load-emoteData-twitchGlobal-fallback' }, 'load-emoteData-twitchSub': { 'type': 'emotedata', @@ -374,7 +394,26 @@ BetterDiscord.prototype.ipcAsyncMessage = function(event, arg) { 'encoding': "utf8", 'https': true, 'parse': true, - 'specialparser': 1 + 'specialparser': 1, + 'fallback': 'load-emoteData-twitchSub-fallback', + 'self': 'load-emoteData-twitchSub' + }, + 'load-emoteData-twitchSub-fallback': { + 'type': 'emotedata', + 'resource': 'Twitch Subscriber Emotedata', + 'domain': _updater.CDN, + 'url': '/' + _repo + '/BetterDiscordApp/' + _hash + '/data/emotedata_twitch_subscriber.json', + 'localurl': null, + 'message': 'load-emoteData-ffz', + 'cacheable': true, + 'variable': 'subEmotesTwitch', + 'localpath': _dataPath + "/emotes_twitch_subscriber.json", + 'encoding': "utf8", + 'https': true, + 'parse': true, + 'specialparser': 1, + 'fallback': 'load-emoteData-ffz', + 'self': 'load-emoteData-twitchSub-fallback' }, 'load-emoteData-ffz': { 'type': 'emotedata', @@ -389,7 +428,9 @@ BetterDiscord.prototype.ipcAsyncMessage = function(event, arg) { 'encoding': "utf8", 'https': true, 'parse': true, - 'specialparser': 2 + 'specialparser': 2, + 'fallback': 'load-emoteData-bttv', + 'self': 'load-emoteData-ffz' }, 'load-emoteData-bttv': { 'type': 'emotedata', @@ -404,7 +445,9 @@ BetterDiscord.prototype.ipcAsyncMessage = function(event, arg) { 'encoding': "utf8", 'https': true, 'parse': false, - 'specialparser': 3 + 'specialparser': 3, + 'fallback': 'load-emoteData-bttv-2', + 'self': 'load-emoteData-bttv' }, 'load-emoteData-bttv-2': { 'type': 'emotedata', @@ -419,11 +462,14 @@ BetterDiscord.prototype.ipcAsyncMessage = function(event, arg) { 'encoding': "utf8", 'https': true, 'parse': false, - 'specialparser': 4 + 'specialparser': 4, + 'fallback': 'start-bd', + 'self': 'load-emoteData-bttv-2' } }; +} - +BetterDiscord.prototype.ipcAsyncMessage = function(event, arg) { if(loadUs.hasOwnProperty(arg)) { loadCount++; @@ -445,36 +491,22 @@ BetterDiscord.prototype.ipcAsyncMessage = function(event, arg) { _this.getUtils().sendIcpAsync(loadMe.message); }); }else if(loadMe.type == 'emotedata') { - + if(loadMe.variable != "emotesTwitch") { + _this.getUtils().execJs('var ' + loadMe.variable + ' = {};');//jic + } var exists = _fs.existsSync(loadMe.localpath); if(exists && !_cacheExpired && loadMe.cacheable) { - _this.getUtils().log("Reading " + loadMe.resource + " from file"); - _this.injectEmoteData(loadMe, _fs.readFileSync(loadMe.localpath, loadMe.encoding)); + _this.loadLocalEData(loadMe); + //_this.injectEmoteData(loadMe, _fs.readFileSync(loadMe.localpath, loadMe.encoding)); } else { - _this.getUtils().log("Downloading " + loadMe.resource); - - if(loadMe.https) { - _this.getUtils().download(loadMe.domain, loadMe.url, function(data) { - var parsedEmoteData = _this.parseEmoteData(loadMe, data); - _this.saveEmoteData(loadMe, parsedEmoteData); - _this.injectEmoteData(loadMe, parsedEmoteData); - }); - - } else { - _this.getUtils().downloadHttp(loadMe.url, function(data) { - var parsedEmoteData = _this.parseEmoteData(loadMe, data); - _this.saveEmoteData(loadMe, parsedEmoteData); - _this.injectEmoteData(loadMe, parsedEmoteData); - }); - } + _this.loadRemoteEData(loadMe); } - - } } if(arg == "start-bd") { + _this.getUtils().saveLogs(_dataPath); _this.getUtils().updateLoading("Starting Up", 100, 100); _this.getUtils().execJs('var mainCore; var startBda = function() { mainCore = new Core(); mainCore.init(); }; startBda();'); @@ -485,6 +517,53 @@ BetterDiscord.prototype.ipcAsyncMessage = function(event, arg) { } }; +BetterDiscord.prototype.loadRemoteEData = function(loadMe) { + _this.getUtils().log("Downloading " + loadMe.resource + " from " + loadMe.domain + loadMe.url ); + if(loadMe.https) { + _this.getUtils().download(loadMe.domain, loadMe.url, function(data) { + var parsedEmoteData = _this.parseEmoteData(loadMe, data); + if(parsedEmoteData == null) { + _this.getUtils().sendIcpAsync(loadMe.fallback); + return true; + } + _this.saveEmoteData(loadMe, parsedEmoteData); + _this.injectEmoteData(loadMe, parsedEmoteData); + }); + return true; + } + _this.getUtils().downloadHttp(loadMe.url, function(data) { + var parsedEmoteData = _this.parseEmoteData(loadMe, data); + + _this.saveEmoteData(loadMe, parsedEmoteData); + _this.injectEmoteData(loadMe, parsedEmoteData); + }); +}; + +BetterDiscord.prototype.loadLocalEData = function(loadMe) { + _this.getUtils().log("Reading " + loadMe.resource + " from file"); + var data = _fs.readFileSync(loadMe.localpath, loadMe.encoding); + + if(_this.testJSON(loadMe, data)) { + _this.injectEmoteData(loadMe, data); + } else { + _this.getUtils().log("Deleting cached file " + loadMe.resource); + _fs.unlinkSync(loadMe.localpath); + _this.getUtils().sendIcpAsync(loadMe.self); + } +}; + +BetterDiscord.prototype.testJSON = function(loadMe, data) { + _this.getUtils().log("Validating " + loadMe.resource); + try { + var json = JSON.parse(data); + _this.getUtils().log(loadMe.resource + " is valid"); + return true; + }catch(err) { + _this.getUtils().log(loadMe.resource + " is invalid"); + return false; + } +}; + BetterDiscord.prototype.parseEmoteData = function(loadMe, emoteData) { _this.getUtils().log("Parsing: " + loadMe.resource); @@ -494,11 +573,17 @@ BetterDiscord.prototype.parseEmoteData = function(loadMe, emoteData) { switch(loadMe.specialparser) { case 0: //Twitch Global Emotes - returnData = emoteData.replace(/\$/g, "\\$").replace(/'/g, "\\'").replace(/"/g, "\\\""); + //returnData = emoteData.replace(/\$/g, "\\$").replace(/'/g, "\\'").replace(/"/g, "\\\""); We don't need this anymore + return emoteData; break; case 1: //Twitch Subscriber Emotes returnData = {}; + if(!_this.testJSON(loadMe, emoteData)) { + return null; + } + emoteData = JSON.parse(emoteData); + var channels = emoteData["channels"]; for(var channel in channels) { var emotes = channels[channel]["emotes"]; @@ -516,6 +601,11 @@ BetterDiscord.prototype.parseEmoteData = function(loadMe, emoteData) { break; case 3: //BTTV Emotes returnData = {}; + + if(!_this.testJSON(loadMe, emoteData)) { + return null; + } + emoteData = JSON.parse(emoteData); for(var emote in emoteData.emotes) { @@ -531,29 +621,23 @@ BetterDiscord.prototype.parseEmoteData = function(loadMe, emoteData) { case 4: returnData = emoteData; break; - } - return returnData; }; BetterDiscord.prototype.saveEmoteData = function(loadMe, emoteData) { - _fs.writeFileSync(loadMe.localpath, emoteData, loadMe.encoding); - }; BetterDiscord.prototype.injectEmoteData = function(loadMe, emoteData) { - - if(loadMe.parse) { - _this.getUtils().execJs('var ' + loadMe.variable + ' = JSON.parse(\'' + emoteData + '\');'); - } else { - _this.getUtils().execJs('var ' + loadMe.variable + ' = ' + emoteData + ';'); + if(emoteData != null) { + if(loadMe.parse) { + _this.getUtils().execJs('var ' + loadMe.variable + ' = JSON.parse(\'' + emoteData + '\');'); + } else { + _this.getUtils().execJs('var ' + loadMe.variable + ' = ' + emoteData + ';'); + } } - - - _this.getUtils().sendIcpAsync(loadMe.message); }; -exports.BetterDiscord = BetterDiscord; +exports.BetterDiscord = BetterDiscord; \ No newline at end of file diff --git a/lib/Utils.js b/lib/Utils.js index 37e47e0..41ad31c 100644 --- a/lib/Utils.js +++ b/lib/Utils.js @@ -1,14 +1,17 @@ /* BetterDiscordApp Utils and Helper functions - * Version: 1.3 + * Version: 1.4 * Author: Jiiks | http://jiiks.net * Date: 25/08/2015 - 09:19 - * Last Updated: 26/10/2015 06:11 + * Last Updated: 30/03/2016 * https://github.com/Jiiks/BetterDiscordApp */ var https = require('https'); var http = require('http'); +var _fs = require('fs'); var _mainWindow; +var eol = require('os').EOL; +var logs = ""; function Utils(mainWindow) { _mainWindow = mainWindow; @@ -57,7 +60,7 @@ Utils.prototype.downloadHttp = function(url, callback) { } Utils.prototype.getHash = function(beta, callback) { - var branch = beta ? "beta" : "master"; + var branch = beta ? "beta" : "master"; this.download("api.github.com", "/repos/Jiiks/BetterDiscordApp/commits/"+branch+"", function(data) { callback(JSON.parse(data).sha); }); @@ -97,6 +100,10 @@ Utils.prototype.updateLoading = function(message, cur, max) { //Logger Utils.prototype.log = function(message) { console.log("BetterDiscord: " + message); + logs += message + eol; +} +Utils.prototype.saveLogs = function(path) { + _fs.writeFileSync(path + "/logs.log", logs); } //Execute javascript diff --git a/lib/config.json b/lib/config.json index f5e2d3b..07f0513 100644 --- a/lib/config.json +++ b/lib/config.json @@ -1,6 +1,6 @@ { "Core": { - "Version": "0.2.6" + "Version": "0.2.7" }, "EmoteModule": { "Twitch":{ diff --git a/package.json b/package.json index 600ae5a..3f18caf 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "betterdiscord", "description": "Better Discord enhances Discord.", - "version": "0.2.6", + "version": "0.2.7", "homepage": "https://github.com/Jiiks/BetterDiscordApp", "license": "MIT", "main": "betterdiscord.js" -} +} \ No newline at end of file