From 15a83a7736679ffa9871f19ea453bd740b789502 Mon Sep 17 00:00:00 2001 From: Daniel Scalzi Date: Tue, 8 May 2018 20:10:46 -0400 Subject: [PATCH] Attempting to make references to the distribution index more streamlined. WIP --- app/assets/js/assetexec.js | 2 +- app/assets/js/assetguard.js | 117 ++++++++++---------- app/assets/js/preloader.js | 10 +- app/assets/js/scripts/landing.js | 178 ++++++++++++++++++------------ app/assets/js/scripts/overlay.js | 4 +- app/assets/js/scripts/uibinder.js | 21 +++- app/assets/westeroscraft.json | 34 +++--- 7 files changed, 207 insertions(+), 159 deletions(-) diff --git a/app/assets/js/assetexec.js b/app/assets/js/assetexec.js index ee3c25d..97d51b7 100644 --- a/app/assets/js/assetexec.js +++ b/app/assets/js/assetexec.js @@ -42,7 +42,7 @@ process.on('message', (msg) => { res.then((v) => { process.send({result: v, content: msg.content}) }).catch((err) => { - process.send({result: v, content: msg.content}) + process.send({result: err, content: msg.content}) }) } else { process.send({result: res, content: msg.content}) diff --git a/app/assets/js/assetguard.js b/app/assets/js/assetguard.js index 4257938..f75ea8a 100644 --- a/app/assets/js/assetguard.js +++ b/app/assets/js/assetguard.js @@ -162,6 +162,7 @@ class DLTracker { } let distributionData = null +let launchWithLocal = false /** * Central object class used for control flow. This object stores data about @@ -378,7 +379,7 @@ class AssetGuard extends EventEmitter { * @param {string} launcherPath The root launcher directory. * @returns {Promise.} A promise which resolves to the distribution data object. */ - static retrieveDistributionDataFresh(launcherPath){ + static refreshDistributionDataRemote(launcherPath){ return new Promise((resolve, reject) => { const distroURL = 'http://mc.westeroscraft.com/WesterosCraftLauncher/westeroscraft.json' const distroDest = path.join(launcherPath, 'westeroscraft.json') @@ -404,24 +405,18 @@ class AssetGuard extends EventEmitter { * Retrieve a local copy of the distribution index asynchronously. * * @param {string} launcherPath The root launcher directory. - * @param {boolean} cached Optional. False if the distro file should be read from the - * disk and re-cached, otherwise a cached copy will be returned. * @returns {Promise.} A promise which resolves to the distribution data object. */ - static retrieveDistributionData(launcherPath, cached = true){ + static refreshDistributionDataLocal(launcherPath){ return new Promise((resolve, reject) => { - if(!cached || distributionData == null){ - fs.readFile(path.join(launcherPath, 'westeroscraft.json'), 'utf-8', (err, data) => { - if(!err){ - distributionData = JSON.parse(data) - resolve(distributionData) - } else { - reject(err) - } - }) - } else { - resolve(distributionData) - } + fs.readFile(path.join(launcherPath, 'westeroscraft.json'), 'utf-8', (err, data) => { + if(!err){ + distributionData = JSON.parse(data) + resolve(distributionData) + } else { + reject(err) + } + }) }) } @@ -429,25 +424,27 @@ class AssetGuard extends EventEmitter { * Retrieve a local copy of the distribution index synchronously. * * @param {string} launcherPath The root launcher directory. - * @param {boolean} cached Optional. False if the distro file should be read from the - * disk and re-cached, otherwise a cached copy will be returned. * @returns {Object} The distribution data object. */ - static retrieveDistributionDataSync(launcherPath, cached = true){ - if(!cached || distributionData == null){ - distributionData = JSON.parse(fs.readFileSync(path.join(launcherPath, 'westeroscraft.json'), 'utf-8')) - } + static refreshDistributionDataLocalSync(launcherPath){ + distributionData = JSON.parse(fs.readFileSync(path.join(launcherPath, 'westeroscraft.json'), 'utf-8')) + return distributionData + } + + /** + * Get a cached copy of the distribution index. + */ + static getDistributionData(){ return distributionData } /** * Resolve the default selected server from the distribution index. * - * @param {string} launcherPath The root launcher directory. * @returns {Object} An object resolving to the default selected server. */ - static resolveSelectedServer(launcherPath){ - const distro = AssetGuard.retrieveDistributionDataSync(launcherPath) + static resolveSelectedServer(){ + const distro = AssetGuard.getDistributionData() const servers = distro.servers for(let i=0; i { - - const cbFunc = function(){ - const serv = AssetGuard.getServerById(self.launcherPath, serverpackid) + AssetGuard.refreshDistributionDataLocal(self.launcherPath).then((v) => { + const serv = AssetGuard.getServerById(serverpackid) if(serv == null) { console.error('Invalid server pack id:', serverpackid) @@ -1319,32 +1340,6 @@ class AssetGuard extends EventEmitter { } } resolve(serv) - } - - AssetGuard.retrieveDistributionDataFresh(self.launcherPath).then((value) => { - - console.log('Loaded fresh copy of the distribution index.') - - cbFunc() - - }).catch((err) => { - - console.log('Failed to load fresh copy of the distribution index.') - console.log('Attempting to load an older copy of the distribution index.') - - AssetGuard.retrieveDistributionData(self.launcherPath, false).then((value) => { - - console.log('Successfully loaded an older copy of the distribution index.') - - cbFunc() - - }).catch((err) => { - - console.log('Failed to load an older copy of the distribution index. Cannot launch.') - - reject(err) - - }) }) }) } @@ -1405,7 +1400,7 @@ class AssetGuard extends EventEmitter { loadForgeData(serverpack){ const self = this return new Promise(async (resolve, reject) => { - let distro = AssetGuard.retrieveDistributionDataSync(self.launcherPath, true) + let distro = AssetGuard.getDistributionData() const servers = distro.servers let serv = null diff --git a/app/assets/js/preloader.js b/app/assets/js/preloader.js index 2028048..f324cdf 100644 --- a/app/assets/js/preloader.js +++ b/app/assets/js/preloader.js @@ -14,9 +14,9 @@ function onDistroLoad(data){ if(data != null){ // Resolve the selected server if its value has yet to be set. - if(ConfigManager.getSelectedServer() == null || AssetGuard.getServerById(ConfigManager.getLauncherDirectory(), ConfigManager.getSelectedServer()) == null){ - console.log('Determining default selected server..') - ConfigManager.setSelectedServer(AssetGuard.resolveSelectedServer(ConfigManager.getLauncherDirectory()).id) + if(ConfigManager.getSelectedServer() == null || AssetGuard.getServerById(ConfigManager.getSelectedServer()) == null){ + console.log('%c[Preloader]', 'color: #a02d2a; font-weight: bold', 'Determining default selected server..') + ConfigManager.setSelectedServer(AssetGuard.resolveSelectedServer().id) ConfigManager.save() } } @@ -24,7 +24,7 @@ function onDistroLoad(data){ } // Ensure Distribution is downloaded and cached. -AssetGuard.retrieveDistributionDataFresh(ConfigManager.getLauncherDirectory()).then((data) => { +AssetGuard.refreshDistributionDataRemote(ConfigManager.getLauncherDirectory()).then((data) => { console.log('%c[Preloader]', 'color: #a02d2a; font-weight: bold', 'Loaded distribution index.') onDistroLoad(data) @@ -35,7 +35,7 @@ AssetGuard.retrieveDistributionDataFresh(ConfigManager.getLauncherDirectory()).t console.log('%c[Preloader]', 'color: #a02d2a; font-weight: bold', 'Attempting to load an older version of the distribution index.') // Try getting a local copy, better than nothing. - AssetGuard.retrieveDistributionData(ConfigManager.getLauncherDirectory(), false).then((data) => { + AssetGuard.refreshDistributionDateLocal(ConfigManager.getLauncherDirectory()).then((data) => { console.log('%c[Preloader]', 'color: #a02d2a; font-weight: bold', 'Successfully loaded an older version of the distribution index.') onDistroLoad(data) diff --git a/app/assets/js/scripts/landing.js b/app/assets/js/scripts/landing.js index db925a3..62620f7 100644 --- a/app/assets/js/scripts/landing.js +++ b/app/assets/js/scripts/landing.js @@ -6,7 +6,6 @@ const cp = require('child_process') const {URL} = require('url') // Internal Requirements -const {AssetGuard} = require('./assets/js/assetguard.js') const AuthManager = require('./assets/js/authmanager.js') const DiscordWrapper = require('./assets/js/discordwrapper.js') const Mojang = require('./assets/js/mojang.js') @@ -168,7 +167,7 @@ const refreshMojangStatuses = async function(){ const refreshServerStatus = async function(fade = false){ console.log('Refreshing Server Status') - const serv = AssetGuard.getServerById(ConfigManager.getGameDirectory(), ConfigManager.getSelectedServer()) + const serv = AssetGuard.getServerById(ConfigManager.getSelectedServer()) let pLabel = 'SERVER' let pVal = 'OFFLINE' @@ -418,30 +417,13 @@ function dlAsync(login = true){ aEx.on('message', (m) => { if(m.content === 'validateDistribution'){ - if(m.result instanceof Error){ + setLaunchPercentage(20, 100) + serv = m.result + console.log('Validated distibution index.') - setOverlayContent( - 'Fatal Error', - 'Could not load a copy of the distribution index. See the console for more details.', - 'Okay' - ) - setOverlayHandler(null) - - toggleOverlay(true) - toggleLaunchArea(false) - - // Disconnect from AssetExec - aEx.disconnect() - - } else { - setLaunchPercentage(20, 100) - serv = m.result - console.log('Forge Validation Complete.') - - // Begin version load. - setLaunchDetails('Loading version information..') - aEx.send({task: 0, content: 'loadVersionData', argsArr: [serv.mc_version]}) - } + // Begin version load. + setLaunchDetails('Loading version information..') + aEx.send({task: 0, content: 'loadVersionData', argsArr: [serv.mc_version]}) } else if(m.content === 'loadVersionData'){ @@ -596,7 +578,7 @@ function dlAsync(login = true){ proc.stdout.on('data', gameStateChange) // Init Discord Hook - const distro = AssetGuard.retrieveDistributionDataSync(ConfigManager.getLauncherDirectory(), true) + const distro = AssetGuard.getDistributionData() if(distro.discord != null && serv.discord != null){ DiscordWrapper.initRPC(distro.discord, serv.discord) hasRPC = true @@ -633,7 +615,62 @@ function dlAsync(login = true){ // Validate Forge files. setLaunchDetails('Loading server information..') - aEx.send({task: 0, content: 'validateDistribution', argsArr: [ConfigManager.getSelectedServer()]}) + + if(AssetGuard.isLocalLaunch()){ + + refreshDistributionIndex(false, (data) => { + onDistroRefresh(data) + aEx.send({task: 0, content: 'validateDistribution', argsArr: [ConfigManager.getSelectedServer()]}) + }, (err) => { + console.error('Unable to refresh distribution index.', err) + if(AssetGuard.getDistributionData() == null){ + setOverlayContent( + 'Fatal Error', + 'Could not load a copy of the distribution index. See the console for more details.', + 'Okay' + ) + setOverlayHandler(null) + + toggleOverlay(true) + toggleLaunchArea(false) + + // Disconnect from AssetExec + aEx.disconnect() + } else { + aEx.send({task: 0, content: 'validateDistribution', argsArr: [ConfigManager.getSelectedServer()]}) + } + }) + + } else { + + refreshDistributionIndex(true, (data) => { + onDistroRefresh(data) + aEx.send({task: 0, content: 'validateDistribution', argsArr: [ConfigManager.getSelectedServer()]}) + }, (err) => { + refreshDistributionIndex(false, (data) => { + onDistroRefresh(data) + }, (err) => { + console.error('Unable to refresh distribution index.', err) + if(AssetGuard.getDistributionData() == null){ + setOverlayContent( + 'Fatal Error', + 'Could not load a copy of the distribution index. See the console for more details.', + 'Okay' + ) + setOverlayHandler(null) + + toggleOverlay(true) + toggleLaunchArea(false) + + // Disconnect from AssetExec + aEx.disconnect() + } else { + aEx.send({task: 0, content: 'validateDistribution', argsArr: [ConfigManager.getSelectedServer()]}) + } + }) + }) + + } } /** @@ -825,57 +862,54 @@ function displayArticle(articleObject, index){ */ function loadNews(){ return new Promise((resolve, reject) => { - AssetGuard.retrieveDistributionData(ConfigManager.getLauncherDirectory(), true).then((v) => { - const newsFeed = v['news_feed'] - const newsHost = new URL(newsFeed).origin + '/' - $.get(newsFeed, (data) => { - const items = $(data).find('item') - const articles = [] + const distroData = AssetGuard.getDistributionData() + const newsFeed = distroData['news_feed'] + const newsHost = new URL(newsFeed).origin + '/' + $.get(newsFeed, (data) => { + const items = $(data).find('item') + const articles = [] - for(let i=0; i { - reject(err) + + let link = el.find('link').text() + let title = el.find('title').text() + let author = el.find('dc\\:creator').text() + + // Generate article. + articles.push( + { + link, + title, + date, + author, + content, + comments, + commentsLink: link + '#comments' + } + ) + } + resolve({ + articles }) - }).catch((err) => { - console.log('Error Loading News', err) + }).catch(err => { + reject(err) }) }) } \ No newline at end of file diff --git a/app/assets/js/scripts/overlay.js b/app/assets/js/scripts/overlay.js index c509bde..90aa215 100644 --- a/app/assets/js/scripts/overlay.js +++ b/app/assets/js/scripts/overlay.js @@ -118,7 +118,7 @@ document.getElementById('serverSelectConfirm').addEventListener('click', () => { const listings = document.getElementsByClassName('serverListing') for(let i=0; i { document.getElementById('frameBar').style.backgroundColor = 'rgba(1, 2, 1, 0.5)' @@ -51,6 +52,24 @@ function showFatalStartupError(){ }, 750) } +function onDistroRefresh(data){ + updateSelectedServer(AssetGuard.getServerById(ConfigManager.getSelectedServer()).name) + refreshServerStatus() + initNews() +} + +function refreshDistributionIndex(remote, onSuccess, onError){ + if(remote){ + AssetGuard.refreshDistributionDataRemote(ConfigManager.getLauncherDirectory()) + .then(onSuccess) + .catch(onError) + } else { + AssetGuard.refreshDistributionDataLocal(ConfigManager.getLauncherDirectory()) + .then(onSuccess) + .catch(onError) + } +} + // Synchronous Listener document.addEventListener('readystatechange', function(){ diff --git a/app/assets/westeroscraft.json b/app/assets/westeroscraft.json index 795a3e1..5f50db8 100644 --- a/app/assets/westeroscraft.json +++ b/app/assets/westeroscraft.json @@ -15,7 +15,7 @@ "name": "WesterosCraft Production Server", "description": "Main WesterosCraft server. Connect to enter the Realm.", "icon_url": "http://mc.westeroscraft.com/WesterosCraftLauncher/files/server-prod.png", - "revision": "3.4.17", + "revision": "3.7.1", "server_ip": "mc.westeroscraft.com", "mc_version": "1.11.2", "discord": { @@ -347,8 +347,8 @@ "name": "DynamicSurroundings General Configuration File", "type": "file", "artifact": { - "size": 19736, - "MD5": "4c64fc6cbbb83b18012ed4820b0b496e", + "size": 20258, + "MD5": "3df81248db151750b7d0a0193b327b47", "path": "/config/dsurround/dsurround.cfg", "url": "http://mc.westeroscraft.com/WesterosCraftLauncher/prod-1.11.2/config/dsurround/dsurround.cfg" } @@ -367,23 +367,23 @@ ] }, { - "id": "com.westeroscraft:westerosblocks:3.0.0-beta-6-133", - "name": "WesterosBlocks (3.0.0-beta-6-133)", + "id": "com.westeroscraft:westerosblocks:3.1.0-alpha-2-135", + "name": "WesterosBlocks (3.1.0-alpha-2-135)", "type": "forgemod", "artifact": { - "size": 16321712, - "MD5": "5a89e2ab18916c18965fc93a0766cc6e", + "size": 16854431, + "MD5": "ed5b2349d1ce2496895a5e8839b77f74", "extension": ".jar", "url": "http://mc.westeroscraft.com/WesterosCraftLauncher/prod-1.11.2/mods/WesterosBlocks.jar" } }, { - "id": "com.westeroscraft:westeroscraftrp:2017-08-16", - "name": "WesterosCraft Resource Pack (2017-08-16)", + "id": "com.westeroscraft:westeroscraftrp:2018-05-05", + "name": "WesterosCraft Resource Pack (2018-05-05)", "type": "file", "artifact": { - "size": 45241339, - "MD5": "ec2d9fdb14d5c2eafe5975a240202f1a", + "size": 46067606, + "MD5": "0e08b0bcf44c9d266bfe067d865ffc1e", "path": "resourcepacks/WesterosCraft.zip", "url": "http://mc.westeroscraft.com/WesterosCraftLauncher/prod-1.11.2/resourcepacks/WesterosCraft.zip" } @@ -416,7 +416,7 @@ "name": "WesterosCraft Test Server", "description": "Main testing server. Experimental changes are live here.", "icon_url": "http://mc.westeroscraft.com/WesterosCraftLauncher/files/server-test.png", - "revision": "3.7.0", + "revision": "3.7.1", "server_ip": "mc.westeroscraft.com:4444", "mc_version": "1.11.2", "discord": { @@ -748,8 +748,8 @@ "name": "DynamicSurroundings General Configuration File", "type": "file", "artifact": { - "size": 19736, - "MD5": "4c64fc6cbbb83b18012ed4820b0b496e", + "size": 20849, + "MD5": "8d6c08c158aa846162e2a179d6228181", "path": "/config/dsurround/dsurround.cfg", "url": "http://mc.westeroscraft.com/WesterosCraftLauncher/test-1.11.2/config/dsurround/dsurround.cfg" } @@ -817,7 +817,7 @@ "name": "WesterosCraft 1.12.2 Test Server", "description": "Tests for our version change to 1.12.2 are live here.", "icon_url": "http://mc.westeroscraft.com/WesterosCraftLauncher/files/server-test.png", - "revision": "4.1.0", + "revision": "4.1.1", "server_ip": "mc.westeroscraft.com:4445", "mc_version": "1.12.2", "discord": { @@ -1146,8 +1146,8 @@ "name": "DynamicSurroundings General Configuration File", "type": "file", "artifact": { - "size": 19736, - "MD5": "4c64fc6cbbb83b18012ed4820b0b496e", + "size": 21195, + "MD5": "850f1103765f45698954b4e3b0b0369d", "path": "/config/dsurround/dsurround.cfg", "url": "http://mc.westeroscraft.com/WesterosCraftLauncher/test-1.12.2/config/dsurround/dsurround.cfg" }