BetterDiscordApp-v2/lib/BetterDiscord.devp.js

460 lines
15 KiB
JavaScript
Raw Normal View History

2016-04-30 11:19:49 +02:00
'use strict';
2016-04-20 21:54:25 +02:00
var _fs = require("fs");
var _config = require("./config.json");
var _utils = require("./utils");
var _utils2;
var _bdIpc = require('electron').ipcMain;
var _error = false;
var _eol = require('os').EOL;
var _mainWindow;
var _cfg = {};
var _extData = {};
function BetterDiscord(mainWindow) {
2016-04-30 11:19:49 +02:00
console.log("WTHFHUFHEIUWHFEUIHFWIUHFEIU");
2016-04-20 21:54:25 +02:00
_mainWindow = mainWindow;
_cfg = _config.cfg;
_cfg.version = _config.Core.Version;
_cfg.os = process.platform;
_utils2 = new _utils.Utils(mainWindow);
createAndCheckData();
2016-04-30 11:19:49 +02:00
}
2016-04-20 21:54:25 +02:00
function createAndCheckData() {
getUtils().log("Checking data/cache");
_cfg.dataPath = (_cfg.os == 'win32' ? process.env.APPDATA : _cfg.os == 'darwin' ? process.env.HOME + '/Library/Preferences' : '/var/local') + '/BetterDiscordTest/';
_cfg.userFile = _cfg.dataPath + 'user.json';
try {
getUtils().mkdirSync(_cfg.dataPath);
if(_fs.existsSync(_cfg.userFile)) {
_cfg.userCfg = JSON.parse(_fs.readFileSync(_cfg.userFile));
}
if(_cfg.userCfg.cache == null) {
_cfg.userCfg.cache = new Date();
} else {
var currentDate = new Date();
var cacheDate = new Date(_cfg.userCfg.cache);
//Check if cache is expired
if(Math.abs(currentDate.getDate() - cacheDate.getDate()) > _cfg.cache.days) {
_cfg.userCfg.cache = currentDate;
} else {
_cfg.cache.expired = false;
}
}
//Write new cache date if expired
if(_cfg.cache.expired) {
getUtils().log("Cache expired or null");
_fs.writeFileSync(_cfg.userFile, JSON.stringify(_cfg.userCfg));
}
init();
} catch(err) {
getUtils().err(err);
exit(err.message);
}
2016-04-30 11:19:49 +02:00
}
2016-04-20 21:54:25 +02:00
function init() {
getUtils().log("Initializing");
getUtils().log("Getting latest hash");
if(_cfg.branch == null) {
_cfg.branch = _cfg.beta ? "beta" : "master";
}
getUtils().log("Using repo: " + _cfg.repo + " with branch: " + _cfg.branch);
getUtils().download("api.github.com", "/repos/" + _cfg.repo + "/BetterDiscordApp/commits/" + _cfg.branch, function(data) {
try {
_cfg.hash = JSON.parse(data).sha;
getUtils().injectVar("_bdhash", _cfg.hash);
}catch(err) {
getUtils().err(err);
exit("Failed to load hash");
return;
}
if(_cfg.hash == undefined) {
exit("Failed to load hash");
return;
}
getUtils().log("Hash: " + _cfg.hash);
initUpdater();
});
2016-04-30 11:19:49 +02:00
}
2016-04-20 21:54:25 +02:00
function initUpdater() {
getUtils().log("Getting updater");
getUtils().download("raw.githubusercontent.com", "/" + _cfg.repo + "/BetterDiscordApp/" + _cfg.hash + "/data/updater.json", function(data) {
try {
_cfg.updater = JSON.parse(data);
} catch(err) {
exit("Failed to load updater");
return;
}
if(_cfg.updater == undefined) {
exit("Failed to load updater");
return;
}
if(_cfg.updater.LatestVersion == undefined || _cfg.updater.CDN == undefined) {
exit("Failed to load updater");
return;
}
getUtils().log("Latest Version: " + _cfg.updater.LatestVersion);
getUtils().log("Using CDN: " + _cfg.updater.CDN);
updateExtData();
start();
});
2016-04-30 11:19:49 +02:00
}
2016-04-20 21:54:25 +02:00
function updateExtData() {
getUtils().log("Updating ext data");
_extData = {
'load-jQueryUI': {
'type': 'javascript',
'resource': 'jQueryUI',
'domain': 'cdnjs.cloudflare.com',
'url': '//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js',
'localurl': null,
'message': 'load-mainCSS',
'cacheable': false,
'variable': null
},
'load-mainCSS': {
'type': 'css',
'resource': 'Main CSS',
'domain': _cfg.updater.CDN,
'url': '//' + _cfg.updater.CDN + '/' + _cfg.repo + '/BetterDiscordApp/' + _cfg.hash + '/css/main.min.css',
'localurl': _cfg.localServer + '/BetterDiscordApp/css/main.css',
'message': 'load-mainJS',
'cacheable': false,
'variable': null
},
'load-mainJS': {
'type': 'javascript',
'resource': 'Main JS',
'domain': _cfg.updater.CDN,
'url': '//' + _cfg.updater.CDN + '/' + _cfg.repo + '/BetterDiscordApp/' + _cfg.hash + '/js/main.min.js',
'localurl': _cfg.localServer + '/BetterDiscordApp/js/main.js',
'message': 'load-publicServers',
'cacheable': false,
'variable': null
},
'load-publicServers': {
'type': 'json',
'resource': 'Public Servers',
'domain': _cfg.updater.CDN,
'url': '/' + _cfg.repo + '/BetterDiscordApp/' + _cfg.hash + '/data/serverlist.json',
'localurl': null,
'message': 'load-emoteData-twitchGlobal',
'cacheable': false,
'variable': 'publicServers'
},
'load-emoteData-twitchGlobal': {
'type': 'emotedata',
'resource': 'Twitch Global Emotedata',
'domain': 'twitchemotes.com',
'url': '/api_cache/v2/global.json',
'localurl': null,
'message': 'load-emoteData-twitchSub',
'cacheable': true,
'variable': 'emotesTwitch',
'localpath': _cfg.dataPath + "/emotes_twitch_global.json",
'encoding': "utf8",
'https': true,
'parse': false,
'specialparser': 0,
'fallback': 'load-emoteData-twitchGlobal-fallback',
'self': 'load-emoteData-twitchGlobal'
},
'load-emoteData-twitchGlobal-fallback': {
'type': 'emotedata',
'resource': 'Twitch Global Emotedata',
'domain': _cfg.updater.CDN,
'url': '/' + _cfg.repo + '/BetterDiscordApp/' + _cfg.hash + '/data/emotedata_twitch_global.json',
'localurl': null,
'message': 'load-emoteData-twitchSub',
'cacheable': true,
'variable': 'emotesTwitch',
'localpath': _cfg.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',
'resource': 'Twitch Subscriber Emotedata',
'domain': 'twitchemotes.com',
'url': '/api_cache/v2/subscriber.json',
'localurl': null,
'message': 'load-emoteData-ffz',
'cacheable': true,
'variable': 'subEmotesTwitch',
'localpath': _cfg.dataPath + "/emotes_twitch_subscriber.json",
'encoding': "utf8",
'https': true,
'parse': true,
'specialparser': 1,
'fallback': 'load-emoteData-twitchSub-fallback',
'self': 'load-emoteData-twitchSub'
},
'load-emoteData-twitchSub-fallback': {
'type': 'emotedata',
'resource': 'Twitch Subscriber Emotedata',
'domain': _cfg.updater.CDN,
'url': '/' + _cfg.repo + '/BetterDiscordApp/' + _cfg.hash + '/data/emotedata_twitch_subscriber.json',
'localurl': null,
'message': 'load-emoteData-ffz',
'cacheable': true,
'variable': 'subEmotesTwitch',
'localpath': _cfg.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',
'resource': 'FrankerFaceZ Emotedata',
'domain': _cfg.updater.CDN,
'url': '/' + _cfg.repo + '/BetterDiscordApp/' + _cfg.hash + '/data/emotedata_ffz.json',
'localurl': null,
'message': 'load-emoteData-bttv',
'cacheable': true,
'variable': 'emotesFfz',
'localpath': _cfg.dataPath + "/emotes_ffz.json",
'encoding': "utf8",
'https': true,
'parse': true,
'specialparser': 2,
'fallback': 'load-emoteData-bttv',
'self': 'load-emoteData-ffz'
},
'load-emoteData-bttv': {
'type': 'emotedata',
'resource': 'BTTV Emotedata',
'domain': 'api.betterttv.net',
'url': '/emotes',
'localurl': null,
'message': 'load-emoteData-bttv-2',
'cacheable': true,
'variable': 'emotesBTTV',
'localpath': _cfg.dataPath + "/emotes_bttv.json",
'encoding': "utf8",
'https': true,
'parse': false,
'specialparser': 3,
'fallback': 'load-emoteData-bttv-2',
'self': 'load-emoteData-bttv'
},
'load-emoteData-bttv-2': {
'type': 'emotedata',
'resource': 'BTTV Emotedata',
'domain': _cfg.updater.CDN,
'url': '/' + _cfg.repo + '/BetterDiscordApp/' + _cfg.hash + '/data/emotedata_bttv.json',
'localurl': null,
'message': 'start-bd',
'cacheable': true,
'variable': 'emotesBTTV2',
'localpath': _cfg.dataPath + "/emotes_bttv_2.json",
'encoding': "utf8",
'https': true,
'parse': false,
'specialparser': 4,
'fallback': 'start-bd',
'self': 'load-emoteData-bttv-2'
}
};
2016-04-30 11:19:49 +02:00
}
2016-04-20 21:54:25 +02:00
function start() {
getUtils().log("Starting up");
try {
var webContents = getUtils().getWebContents();
2016-04-30 11:19:49 +02:00
2016-04-20 21:54:25 +02:00
getUtils().log("Hooking dom-ready");
webContents.on('dom-ready', domReady);
getUtils().log("Hooked dom-ready");
webContents.on('did-finish-loading', function() {
2016-04-30 11:19:49 +02:00
if(domReadyHooked) {
return;
}
2016-04-20 21:54:25 +02:00
getUtils().log("Hooking did-finish-loading failsafe");
domReady();
getUtils().log("Hooked did-finish-loading failsafe");
});
}catch(err) {
exit(err);
}
2016-04-30 11:19:49 +02:00
}
2016-04-20 21:54:25 +02:00
var domReadyHooked = false;
var ipcHooked = false;
function domReady() {
domReadyHooked = true;
if(ipcHooked) {
load(true);
return;
}
ipcHooked = true;
load(false);
2016-04-30 11:19:49 +02:00
}
2016-04-20 21:54:25 +02:00
function load(reload) {
getUtils().log(reload ? "Reloading" : "Loading");
initLoaders();
2016-04-30 11:19:49 +02:00
}
2016-04-20 21:54:25 +02:00
function initLoaders() {
try {
getUtils().mkdirSync(_cfg.dataPath);
getUtils().mkdirSync(_cfg.dataPath + "plugins/");
getUtils().mkdirSync(_cfg.dataPath + "themes/");
getUtils().execJs('var themesupport2 = true');
loadPlugins();
loadThemes();
}catch(err) {
exit(err);
}
2016-04-30 11:19:49 +02:00
}
2016-04-20 21:54:25 +02:00
function loadPlugins() {
var pluginPath = _cfg.dataPath + "plugins/";
_fs.readdir(pluginPath, function(err, files) {
if(err) {
getUtils().log(err);
getUtils().alert(err);
return;
}
var pluginErrors = [];
getUtils().injectVar("bdplugins", "{}");
2016-04-30 11:19:49 +02:00
2016-04-20 21:54:25 +02:00
files.forEach(function(fileName) {
2016-04-30 11:19:49 +02:00
if(!fileName.endsWith(".plugin.js")) {
getUtils().log("Invalid plugin detected: " + fileName);
return;
}
2016-04-20 21:54:25 +02:00
var plugin = _fs.readFileSync(pluginPath + fileName, 'utf8');
var meta = plugin.split(_eol)[0];
if (meta.indexOf('META') < 0) {
getUtils().warn('Plugin META not found in file: ' + fileName);
pluginErrors.push(fileName + " Reason: Plugin META not found");
return;
}
});
if(pluginErrors.length > 0) {
2016-04-30 11:19:49 +02:00
getUtils().alert("The following plugin(s) could not be loaded", pluginErrors.join("<br>"));
2016-04-20 21:54:25 +02:00
}
});
2016-04-30 11:19:49 +02:00
}
2016-04-20 21:54:25 +02:00
function loadThemes() {
2016-04-30 11:19:49 +02:00
var themePath = _cfg.dataPath + "themes/";
_fs.readdir(themePath, function(err, files) {
if(err) {
getUtils().log(err);
getUtils().alert(err);
return;
}
var themeErrors = [];
getUtils().injectVar("bdthtmes", "{}");
2016-04-20 21:54:25 +02:00
2016-04-30 11:19:49 +02:00
files.forEach(function(fileName) {
if(!fileName.endsWith(".theme.css")) {
getUtils().log("Invalid theme detected " + fileName);
return;
}
var theme = _fs.readFileSync(themePath + fileName, 'utf8');
var split = theme.split(_eol);
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");
return;
}
var themeVar = meta.substring(meta.lastIndexOf('//META') + 6, meta.lastIndexOf('*//'));
var themeInfo;
try {
themeInfo = JSON.parse(themeVar);
}catch(err) {
getUtils().warn("Failed to parse theme META in file ("+err+"): " + fileName);
themeErrors.push(fileName + " Reason: Failed to parse theme META (" + err + ")");
return;
}
if(themeInfo['name'] == undefined) {
getUtils().warn("Missing theme name in file: " + fileName);
themeErrors.push(fileName + " Reason: Missing theme name");
return;
}
if(themeInfo['author'] == undefined) {
getUtils().warn("Missing author name in file: " + fileName);
}
if(themeInfo['description'] == undefined) {
getUtils().warn("Missing description in file: " + fileName);
}
if(themeInfo['version'] == undefined) {
getUtils().warn("Missing version in file: " + fileName);
}
getUtils().log("Loading theme: " + themeInfo['name']);
split.splice(0, 1);
theme = split.join('\n');
theme = theme.replace(/(\r\n|\n|\r)/gm, '');
});
if(themeErrors.length > 0) {
getUtils().alert("The following theme(s) could not be loaded", themeErrors.join("<br>"));
}
});
}
2016-04-20 21:54:25 +02:00
function getUtils() {
return _utils2;
2016-04-30 11:19:49 +02:00
}
2016-04-20 21:54:25 +02:00
function exit(reason) {
error = true;
getUtils().log("Exiting. Reason: " + reason);
getUtils().saveLogs(_cfg.dataPath);
getUtils().alert("Something went wrong :(", reason);
2016-04-30 11:19:49 +02:00
}
2016-04-20 21:54:25 +02:00
exports.BetterDiscord = BetterDiscord;