add error checking locally

This commit is contained in:
Zack Rauen 2017-11-04 00:32:20 -04:00
parent 28174e59c9
commit d906826fc0
1 changed files with 105 additions and 39 deletions

View File

@ -9,6 +9,7 @@
'use strict';
var _fs = require("fs");
var _vm = require("vm")
var _config = require("./config.json");
var _utils = require("./utils");
var _utils2;
@ -38,7 +39,7 @@ function initStorage() {
bdStorage.data = bdStorage.defaults.data;
_fs.writeFileSync(_cfg.dataPath + "/bdstorage.json", JSON.stringify(bdStorage, null, 4));
} else {
bdStorage.data = JSON.parse(_fs.readFileSync(_cfg.dataPath + "/bdStorage.json"));
bdStorage.data = JSON.parse(_fs.readFileSync(_cfg.dataPath + "/bdstorage.json"));
}
};
@ -129,7 +130,7 @@ function init() {
}
if(_cfg.repo == null) {
_cfg.repo = "Jiiks";
_cfg.repo = "rauenzi";
}
getUtils().log("Using repository: " + _cfg.repo + " and branch: " + _cfg.branch);
@ -141,6 +142,7 @@ function init() {
initStorage();
}
function getHash(callback) {
getUtils().download("api.github.com", "/repos/" + _cfg.repo + "/BetterDiscordApp/commits/" + _cfg.branch, function(data) {
try {
@ -217,7 +219,7 @@ function updateExtData() {
'type': 'css',
'resource': 'Main CSS',
'domain': _cfg.updater.CDN,
'url': '//' + _cfg.updater.CDN + '/' + _cfg.repo + '/BetterDiscordApp/' + _cfg.hash + '/css/main.min.css',
'url': '//' + _cfg.updater.CDN + '/' + _cfg.repo + '/BetterDiscordApp/' + _cfg.hash + '/css/main.css',
'localurl': _cfg.localServer + '/BetterDiscordApp/css/main.css',
'message': 'load-mainJS',
'cacheable': false,
@ -227,7 +229,7 @@ function updateExtData() {
'type': 'javascript',
'resource': 'Main JS',
'domain': _cfg.updater.CDN,
'url': '//' + _cfg.updater.CDN + '/' + _cfg.repo + '/BetterDiscordApp/' + _cfg.hash + '/js/main.min.js',
'url': '//' + _cfg.updater.CDN + '/' + _cfg.repo + '/BetterDiscordApp/' + _cfg.hash + '/js/main.js',
'localurl': _cfg.localServer + '/BetterDiscordApp/js/main.js?v=1.1',
'message': 'load-emoteData-twitchGlobal',
'cacheable': false,
@ -418,10 +420,10 @@ function load(reload) {
_bdIpc.on('synchronous-message', function(event, arg) { ipcSyncMessage(event, arg); });
getUtils().log("Hooked ipc async");
}
initLoaders();
initLoaders(reload);
}
function initLoaders() {
function initLoaders(reload) {
try {
getUtils().mkdirSync(_cfg.dataPath);
getUtils().mkdirSync(_cfg.dataPath + "plugins/");
@ -448,9 +450,10 @@ function loadPlugins() {
var pluginErrors = [];
getUtils().injectVarRaw("bdplugins", "{}");
getUtils().injectVarRaw("bdpluginErrors", "[]");
files.forEach(function(fileName) {
if (!_fs.lstatSync(pluginPath + fileName).isFile() || fileName.endsWith(".config.json")) return;
if (!_fs.lstatSync(pluginPath + fileName).isFile() || fileName.endsWith(".config.json")) return;
if(!fileName.endsWith(".plugin.js")) {
getUtils().log("Invalid plugin detected: " + fileName);
return;
@ -461,32 +464,93 @@ function loadPlugins() {
if (meta.indexOf('META') < 0) {
getUtils().warn('Plugin META not found in file: ' + fileName);
pluginErrors.push(fileName + " Reason: Plugin META not found");
pluginErrors.push({name: null, file: fileName, reason: "META not found.", error: null});
return;
}
var pluginVar = meta.substring(meta.lastIndexOf('//META') + 6, meta.lastIndexOf('*//'));
var parse;
try {
parse = JSON.parse(pluginVar);
}catch(err) {
try { parse = JSON.parse(pluginVar); }
catch(err) {
getUtils().warn("Failed to parse plugin META in file: " + fileName + "("+err+")");
pluginErrors.push(fileName + " Reason: Failed to parse plugin META (" + err + ")");
pluginErrors.push({name: null, file: fileName, reason: "META could not be parsed.", error: {message: err.message, stack: err.stack}});
return;
}
if(parse["name"] == undefined) {
getUtils().warn("Undefined plugin name in file: " + fileName);
pluginErrors.push(fileName + " Reason: invalid plugin name");
pluginErrors.push({name: null, file: fileName, reason: "No name defined.", error: null});
return;
}
getUtils().log("Loading plugin: " + parse["name"]);
try { new _vm.Script(plugin, {displayErrors: true}); }
catch(err) {
pluginErrors.push({name: parse["name"], file: fileName, reason: "Plugin could not be compiled.", error: {message: err.message, stack: err.stack}});
getUtils().execJs(`bdplugins["${parse["name"]}"] = {"plugin": {
start: () => {},
load: () => {},
getName: () => {return "${parse["name"]}";},
getAuthor: () => {return "???";},
getDescription: () => {return "This plugin was unable to be loaded. Check the author's page for updates.";},
getVersion: () => {return "???";}
},
"name": "${parse["name"]}",
"filename": "${fileName}",
"source": "${parse["source"] ? parse["source"] : ""}",
"website": "${parse["website"] ? parse["website"] : ""}"
};`);
return;
}
getUtils().execJs(plugin);
getUtils().execJs('(function() { var plugin = new ' + parse["name"] + '(); bdplugins[plugin.getName()] = { "plugin": plugin, "enabled": false } })();')
try {new _vm.Script(`new ${parse["name"]}();`, {displayErrors: true});}
catch(err) {
pluginErrors.push({name: parse["name"], file: fileName, reason: "Plugin could not be constructed", error: {message: err.message, stack: err.stack}});
getUtils().execJs(`bdplugins["${parse["name"]}"] = {"plugin": {
start: () => {},
load: () => {},
getName: () => {return "${parse["name"]}";},
getAuthor: () => {return "???";},
getDescription: () => {return "This plugin was unable to be loaded. Check the author's page for updates.";},
getVersion: () => {return "???";}
},
"name": "${parse["name"]}",
"filename": "${fileName}",
"source": "${parse["source"] ? parse["source"] : ""}",
"website": "${parse["website"] ? parse["website"] : ""}"
};`);
return;
}
getUtils().execJs(`(function() {
try {
var plugin = new ${parse["name"]}();
bdplugins[plugin.getName()] = {"plugin": plugin, "name": "${parse["name"]}", "filename": "${fileName}", "source": "${parse["source"] ? parse["source"] : ""}", "website": "${parse["website"] ? parse["website"] : ""}" };
}
catch (e) {
bdpluginErrors.push({name: "${parse["name"]}", file: "${fileName}", reason: "Plugin could not be constructed.", error: {message: e.message, stack: e.stack}})
bdplugins["${parse["name"]}"] = {"plugin": {
start: () => {},
load: () => {},
getName: () => {return "${parse["name"]}";},
getAuthor: () => {return "???";},
getDescription: () => {return "This plugin was unable to be loaded. Check the author's page for updates.";},
getVersion: () => {return "???";}
},
"name": "${parse["name"]}",
"filename": "${fileName}",
"source": "${parse["source"] ? parse["source"] : ""}",
"website": "${parse["website"] ? parse["website"] : ""}"
};
}
})();`)
});
if(pluginErrors.length > 0) {
getUtils().alert("The following plugin(s) could not be loaded", pluginErrors.join("<br>"));
for (var i = 0; i < pluginErrors.length; i++) {
getUtils().execJs(`bdpluginErrors.push(${JSON.stringify(pluginErrors[i])});`);
}
});
@ -506,7 +570,7 @@ function loadThemes() {
getUtils().injectVarRaw("bdthemes", "{}");
files.forEach(function(fileName) {
if (!_fs.lstatSync(themePath + fileName).isFile()) return;
if (!_fs.lstatSync(themePath + fileName).isFile()) return;
if(!fileName.endsWith(".theme.css")) {
getUtils().log("Invalid theme detected " + fileName);
return;
@ -516,22 +580,23 @@ function loadThemes() {
var meta = split[0];
if(meta.indexOf('META') < 0) {
getUtils().warn("Theme META not found in file: " + fileName);
themeErrors.push(fileName + " Reason: Theme META not found");
themeErrors.push({name: null, file: fileName, reason: "META not found.", error: null});
return;
}
var themeVar = meta.substring(meta.lastIndexOf('//META') + 6, meta.lastIndexOf('*//'));
var themeInfo;
try {
themeInfo = JSON.parse(themeVar);
}catch(err) {
}
catch(err) {
getUtils().warn("Failed to parse theme META in file: " + fileName + "("+err+")");
themeErrors.push(fileName + " Reason: Failed to parse theme META (" + err + ")");
themeErrors.push({name: null, file: fileName, reason: "META could not be parsed.", error: {message: err.message, stack: err.stack}});
return;
}
if(themeInfo['name'] == undefined) {
getUtils().warn("Missing theme name in file: " + fileName);
themeErrors.push(fileName + " Reason: Missing theme name");
themeErrors.push({name: null, file: fileName, reason: "No name defined.", error: null});
return;
}
if(themeInfo['author'] == undefined) {
@ -539,7 +604,7 @@ function loadThemes() {
getUtils().warn("Missing author name in file: " + fileName);
}
if(themeInfo['description'] == undefined) {
themeInfo['description'] = "No_Description";
themeInfo['description'] = "No Description";
getUtils().warn("Missing description in file: " + fileName);
}
if(themeInfo['version'] == undefined) {
@ -552,19 +617,23 @@ function loadThemes() {
theme = split.join("\n");
theme = theme.replace(/(\r\n|\n|\r)/gm, '');
_mainWindow.webContents.executeJavaScript('(function() { bdthemes["' + themeInfo['name'] + '"] = { "enabled": false, "name": "' + themeInfo['name'] + '", "css": "' + escape(theme) + '", "description": "' + themeInfo['description'] + '", "author":"' + themeInfo['author'] + '", "version":"' + themeInfo['version'] + '" } })();');
getUtils().execJs(`(function() {
bdthemes["${themeInfo['name']}"] = {
name: "${themeInfo['name']}",
css: "${escape(theme)}",
description: "${themeInfo['description']}",
author:"${themeInfo['author']}",
version:"${themeInfo['version']}",
"source": "${themeInfo["source"] ? themeInfo["source"] : ""}",
"website": "${themeInfo["website"] ? themeInfo["website"] : ""}"
}
})();`);
});
if(themeErrors.length > 0) {
getUtils().alert("The following theme(s) could not be loaded", themeErrors.join("<br>"));
}
getUtils().injectVarRaw("bdthemeErrors", JSON.stringify(themeErrors));
});
}
function loadApp() {
getUtils().execJs('var loadingNode = document.createElement("DIV");');
getUtils().execJs('loadingNode.innerHTML = \' <div style="height:30px;width:100%;background:#282B30;"><div style="padding-right:10px; float:right"> <span id="bd-status" style="line-height:30px;color:#E8E8E8;">BetterDiscord - Loading Libraries : </span><progress id="bd-pbar" value="10" max="100"></progress></div></div> \'');
getUtils().execJs('var flex = document.getElementsByClassName("flex-vertical flex-spacer")[0]; flex.appendChild(loadingNode);');
getUtils().injectVar('bdVersion', _cfg.version);
getUtils().injectVar('bdCdn', _cfg.CDN);
@ -637,11 +706,6 @@ function ipcAsyncMessage(event, arg) {
if(arg == "start-bd") {
getUtils().updateLoading("Starting Up", 100, 100);
getUtils().execJs('var mainCore; var startBda = function() { mainCore = new Core(); mainCore.init(); }; startBda();');
//Remove loading node
setTimeout(function() {
getUtils().execJs('$("#bd-status").parent().parent().hide();');
}, 2000);
getUtils().saveLogs(_cfg.dataPath);
}
}
@ -689,6 +753,7 @@ function loadExtData(extData) {
}
function loadEmoteData(extData, local) {
//getUtils().log(extData.self);
if(local) {
getUtils().log("Reading " + extData.resource + " from file");
var data = _fs.readFileSync(extData.localpath, extData.encoding);
@ -706,6 +771,7 @@ function loadEmoteData(extData, local) {
if(extData.https) {
getUtils().download(extData.domain, extData.url, function(data) {
var parsedEmoteData = parseEmoteData(extData, data);
if(parsedEmoteData == null) {
getUtils().sendIcpAsync(extData.fallback);
return true;
@ -824,9 +890,9 @@ function parseEmoteData(extData, data) {
returnData = JSON.stringify(returnData);
break;
case 4:
returnData = data;
break;
case 4:
returnData = data;
break;
}
return returnData;
}
@ -844,4 +910,4 @@ function exit(reason) {
BetterDiscord.prototype.init = function() {}//Compatibility
exports.BetterDiscord = BetterDiscord;
exports.BetterDiscord = BetterDiscord;