From 086bfc85937e1abb581d4eb06c977cff2acbb733 Mon Sep 17 00:00:00 2001 From: Daniel Scalzi Date: Sat, 2 Dec 2017 02:59:25 -0500 Subject: [PATCH] Added dynamic detection of default server. --- app/assets/js/actionbinder.js | 49 ++++++++++------- app/assets/js/assetguard.js | 99 +++++++++++++++++++++++++++++++---- app/assets/js/constants.js | 11 +++- app/assets/js/preloader.js | 6 +++ app/assets/westeroscraft.json | 1 + app/index.ejs | 2 +- docs/distro.md | 17 +++--- index.js | 10 +++- 8 files changed, 156 insertions(+), 39 deletions(-) create mode 100644 app/assets/js/preloader.js diff --git a/app/assets/js/actionbinder.js b/app/assets/js/actionbinder.js index efdaebf6..4b50a80d 100644 --- a/app/assets/js/actionbinder.js +++ b/app/assets/js/actionbinder.js @@ -8,10 +8,19 @@ document.addEventListener('readystatechange', function(){ if (document.readyState === 'interactive'){ // Bind launch button - document.getElementById("launch_button").addEventListener('click', function(e){ + document.getElementById('launch_button').addEventListener('click', function(e){ console.log('Launching game..') testdownloads() }) + + if(DEFAULT_CONFIG.getSelectedServer() == null){ + console.log('Determining default selected server..') + DEFAULT_CONFIG.setSelectedServer(AssetGuard.resolveSelectedServer()) + } + + // TODO convert this to dropdown menu. + // Bind selected server + document.getElementById('server_selection').innerHTML = '\u2022 ' + AssetGuard.getServerById(DEFAULT_CONFIG.getSelectedServer()).name } }, false) @@ -20,11 +29,11 @@ document.addEventListener('readystatechange', function(){ let tracker; testdownloads = async function(){ - const content = document.getElementById("launch_content") - const details = document.getElementById("launch_details") - const progress = document.getElementById("launch_progress") - const progress_text = document.getElementById("launch_progress_label") - const det_text = document.getElementById("launch_details_text") + const content = document.getElementById('launch_content') + const details = document.getElementById('launch_details') + const progress = document.getElementById('launch_progress') + const progress_text = document.getElementById('launch_progress_label') + const det_text = document.getElementById('launch_details_text') det_text.innerHTML = 'Please wait..' progress.setAttribute('max', '100') @@ -33,34 +42,34 @@ testdownloads = async function(){ tracker = new AssetGuard() - det_text.innerHTML = 'Loading version information..' - const versionData = await tracker.loadVersionData('1.11.2', GAME_DIRECTORY) + det_text.innerHTML = 'Loading server information..' + const serv = await tracker.validateDistribution(DEFAULT_CONFIG.getSelectedServer(), GAME_DIRECTORY) progress.setAttribute('value', 20) progress_text.innerHTML = '20%' + console.log('forge stuff done') + + det_text.innerHTML = 'Loading version information..' + const versionData = await tracker.loadVersionData(serv.mc_version, GAME_DIRECTORY) + progress.setAttribute('value', 40) + progress_text.innerHTML = '40%' det_text.innerHTML = 'Validating asset integrity..' await tracker.validateAssets(versionData, GAME_DIRECTORY) - progress.setAttribute('value', 40) - progress_text.innerHTML = '40%' + progress.setAttribute('value', 60) + progress_text.innerHTML = '60%' console.log('assets done') det_text.innerHTML = 'Validating library integrity..' await tracker.validateLibraries(versionData, GAME_DIRECTORY) - progress.setAttribute('value', 60) - progress_text.innerHTML = '60%' + progress.setAttribute('value', 80) + progress_text.innerHTML = '80%' console.log('libs done') det_text.innerHTML = 'Validating miscellaneous file integrity..' await tracker.validateMiscellaneous(versionData, GAME_DIRECTORY) - progress.setAttribute('value', 80) - progress_text.innerHTML = '80%' - console.log('files done') - - det_text.innerHTML = 'Validating server distribution files..' - const serv = await tracker.validateDistribution('WesterosCraft-1.11.2', GAME_DIRECTORY) progress.setAttribute('value', 100) progress_text.innerHTML = '100%' - console.log('forge stuff done') + console.log('files done') det_text.innerHTML = 'Downloading files..' tracker.on('totaldlprogress', function(data){ @@ -72,7 +81,7 @@ testdownloads = async function(){ tracker.on('dlcomplete', async function(){ det_text.innerHTML = 'Preparing to launch..' - const forgeData = await tracker.loadForgeData('WesterosCraft-1.11.2', GAME_DIRECTORY) + const forgeData = await tracker.loadForgeData(serv.id, GAME_DIRECTORY) const authUser = await mojang.auth('EMAIL', 'PASS', DEFAULT_CONFIG.getClientToken(), { name: 'Minecraft', version: 1 diff --git a/app/assets/js/assetguard.js b/app/assets/js/assetguard.js index 0c48dd98..0d41df83 100644 --- a/app/assets/js/assetguard.js +++ b/app/assets/js/assetguard.js @@ -26,7 +26,7 @@ const AdmZip = require('adm-zip') const async = require('async') const child_process = require('child_process') const crypto = require('crypto') -const {DEFAULT_CONFIG} = require('./constants') +const {DEFAULT_CONFIG, DISTRO_DIRECTORY} = require('./constants') const EventEmitter = require('events') const fs = require('fs') const mkpath = require('mkdirp'); @@ -152,6 +152,8 @@ class DLTracker { } +let distributionData = null + /** * Central object class used for control flow. This object stores data about * categories of downloads. Each category is assigned an identifier with a @@ -272,6 +274,85 @@ class AssetGuard extends EventEmitter { return false; } + /** + * Statically retrieve the distribution data. + * + * @param {Boolean} cached - optional. False if the distro should be freshly downloaded, else + * a cached copy will be returned. + * @returns {Promise.} - A promise which resolves to the distribution data object. + */ + static retrieveDistributionData(cached = true){ + return new Promise(function(fulfill, reject){ + if(!cached || distributionData == null){ + // TODO Download file from upstream. + //const distroURL = 'http://mc.westeroscraft.com/WesterosCraftLauncher/westeroscraft.json' + // TODO Save file to DISTRO_DIRECTORY + // TODO Fulfill with JSON.parse() + + // Workaround while file is not hosted. + fs.readFile(path.join(__dirname, '..', 'westeroscraft.json'), 'utf-8', (err, data) => { + distributionData = JSON.parse(data) + fulfill(distributionData) + }) + } else { + fulfill(distributionData) + } + }) + } + + /** + * Statically retrieve the distribution data. + * + * @param {Boolean} cached - optional. False if the distro should be freshly downloaded, else + * a cached copy will be returned. + * @returns {Object} - The distribution data object. + */ + static retrieveDistributionDataSync(cached = true){ + if(!cached || distributionData == null){ + distributionData = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'westeroscraft.json'), 'utf-8')) + } + return distributionData + } + + /** + * Resolve the default selected server from the distribution index. + * + * @returns {Object} - An object resolving to the default selected server. + */ + static resolveSelectedServer(){ + const distro = AssetGuard.retrieveDistributionDataSync() + const servers = distro.servers + for(let i=0; i 0) ? servers[0].id : null + } + + /** + * Gets a server from the distro index which maches the provided ID. + * Returns null if the ID could not be found or the distro index has + * not yet been loaded. + * + * @param {String} serverID - The id of the server to retrieve. + * @returns {Object} - The server object whose id matches the parameter. + */ + static getServerById(serverID){ + if(distributionData == null){ + AssetGuard.retrieveDistributionDataSync(false) + } + const servers = distributionData.servers + let serv = null + for(let i=0; i { - let servers = value.servers + AssetGuard.retrieveDistributionData(false).then((value) => { + /*const servers = value.servers let serv = null for(let i=0; i { fulfill(JSON.parse(data)) }) }) - } + }*/ _parseDistroModules(modules, basePath, version){ let alist = [] @@ -816,7 +897,7 @@ class AssetGuard extends EventEmitter { loadForgeData(serverpack, basePath){ const self = this return new Promise(async function(fulfill, reject){ - let distro = await self._chainValidateDistributionIndex(basePath) + let distro = AssetGuard.retrieveDistributionDataSync() const servers = distro.servers let serv = null diff --git a/app/assets/js/constants.js b/app/assets/js/constants.js index 3478b90b..9b4d33d5 100644 --- a/app/assets/js/constants.js +++ b/app/assets/js/constants.js @@ -2,5 +2,12 @@ const path = require('path') const ConfigManager = require('./configmanager') //TODO: Resolve game directory based on windows, linux, or mac.. -exports.GAME_DIRECTORY = path.join(__dirname, '..', '..', '..', 'target', 'test', 'mcfiles') -exports.DEFAULT_CONFIG = new ConfigManager(path.join(exports.GAME_DIRECTORY, 'config.json')) \ No newline at end of file +const GAME_DIRECTORY = path.join(__dirname, '..', '..', '..', 'target', 'test', 'mcfiles') +const DISTRO_DIRECTORY = path.join(GAME_DIRECTORY, 'westeroscraft.json') +const DEFAULT_CONFIG = new ConfigManager(path.join(GAME_DIRECTORY, 'config.json')) + +module.exports = { + GAME_DIRECTORY, + DISTRO_DIRECTORY, + DEFAULT_CONFIG +} \ No newline at end of file diff --git a/app/assets/js/preloader.js b/app/assets/js/preloader.js new file mode 100644 index 00000000..e7cc7272 --- /dev/null +++ b/app/assets/js/preloader.js @@ -0,0 +1,6 @@ +const {AssetGuard} = require('./assetguard.js') + +console.log('Preloading') + +// Ensure Distribution is downloaded and cached. +AssetGuard.retrieveDistributionDataSync(false) \ No newline at end of file diff --git a/app/assets/westeroscraft.json b/app/assets/westeroscraft.json index 4037407c..140f6611 100644 --- a/app/assets/westeroscraft.json +++ b/app/assets/westeroscraft.json @@ -9,6 +9,7 @@ "revision": "0.0.1", "server_ip": "mc.westeroscraft.com", "mc_version": "1.11.2", + "default_selected": true, "autoconnect": true, "modules": [ { diff --git a/app/index.ejs b/app/index.ejs index 0c9ca749..8fb17992 100644 --- a/app/index.ejs +++ b/app/index.ejs @@ -60,7 +60,7 @@
- • Westeroscraft Production Server + • No Server Selected
diff --git a/docs/distro.md b/docs/distro.md index 91acdf31..0930eb3a 100644 --- a/docs/distro.md +++ b/docs/distro.md @@ -14,6 +14,7 @@ The distribution index is written in JSON. The general format of the index is as "revision": "0.0.1", "server_ip": "mc.westeroscraft.com:1337", "mc_version": "1.11.2", + "default_selected": true, "autoconnect": true, "modules": [ ... @@ -25,6 +26,8 @@ The distribution index is written in JSON. The general format of the index is as You can declare an unlimited number of servers, however you must provide valid values for the fields listed above. In addition to that, the server can declare modules. +Only one server in the array should have the `default_selected` property enabled. This will tell the launcher that this is the default server to select if either the previously selected server is invalid, or there is no previously selected server. This field is not defined by any server (avoid this), the first server will be selected as the default. If multiple servers have `default_selected` enabled, the first one the launcher finds will be the effective value. Servers which are not the default may omit this property rather than explicitly setting it to false. + ## Modules A module is a generic representation of a file required to run the minecraft client. It takes the general form: @@ -35,8 +38,8 @@ A module is a generic representation of a file required to run the minecraft cli "name": "Artifact {version}", "type": "{a valid type}", "artifact": { - "size": {file size in bytes}, - "MD5": {MD5 hash for the file, string}, + "size": "{file size in bytes}", + "MD5": "{MD5 hash for the file, string}", "extension": ".jar", "url": "http://files.site.com/maven/group/id/artifact/version/artifact-version.jar" }, @@ -46,8 +49,8 @@ A module is a generic representation of a file required to run the minecraft cli "name": "Example File", "type": "file", "artifact": { - "size": {file size in bytes}, - "MD5": {MD5 hash for the file, string}, + "size": "{file size in bytes}", + "MD5": "{MD5 hash for the file, string}", "path": "examplefile.txt", "url": "http://files.site.com/examplefile.txt" } @@ -63,8 +66,8 @@ Modules may also declare a `required` object. ```json "required": { - "value": false, (if the module is required) - "def": false (if it's enabled by default, has no effect if value is true) + "value": false, "(if the module is required)" + "def": false "(if it's enabled by default, has no effect if value is true)" } ``` @@ -75,6 +78,8 @@ The format of the module's artifact depends on several things. The most importan Other times, you may want to store the files maven-style, such as with libraries and mods. In this case you must declare the module as the example artifact above. The `id` becomes more important as it will be used to resolve the final path. The `id` must be provided in maven format, that is `group.id.maybemore:artifact:version`. From there, you need to declare the `extension` of the file in the artifact object. This effectively replaces the `path` option we used above. +**It is EXTREMELY IMPORTANT that the file size is CORRECT. The launcher's download queue will not function properly otherwise.** + Ex. ```SHELL diff --git a/index.js b/index.js index a8224f1a..6593d737 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,15 @@ const ejse = require('ejs-electron') let win function createWindow() { - win = new BrowserWindow({ width: 980, height: 552, icon: getPlatformIcon('WesterosSealSquare'), frame: false}) + win = new BrowserWindow({ + width: 980, + height: 552, + icon: getPlatformIcon('WesterosSealSquare'), + frame: false, + webPreferences: { + preload: path.join(__dirname, 'app', 'assets', 'js', 'preloader.js') + } + }) ejse.data('bkid', Math.floor((Math.random() * fs.readdirSync(path.join(__dirname, 'app', 'assets', 'images', 'backgrounds')).length)))