Added dynamic detection of default server.

This commit is contained in:
Daniel Scalzi
2017-12-02 02:59:25 -05:00
parent 52ab270ce3
commit 086bfc8593
8 changed files with 156 additions and 39 deletions

View File

@@ -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

View File

@@ -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.<Object>} - 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<servers.length; i++){
if(servers[i].default_selected){
return servers[i].id
}
}
// If no server declares default_selected, default to the first one declared.
return (servers.length > 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<servers.length; i++){
if(servers[i].id === serverID){
serv = servers[i]
}
}
return serv
}
/**
* Validates a file in the style used by forge's version index.
*
@@ -718,15 +799,16 @@ class AssetGuard extends EventEmitter {
validateDistribution(serverpackid, basePath){
const self = this
return new Promise(function(fulfill, reject){
self._chainValidateDistributionIndex(basePath).then((value) => {
let servers = value.servers
AssetGuard.retrieveDistributionData(false).then((value) => {
/*const servers = value.servers
let serv = null
for(let i=0; i<servers.length; i++){
if(servers[i].id === serverpackid){
serv = servers[i]
break
}
}
}*/
const serv = AssetGuard.getServerById(serverpackid)
self.forge = self._parseDistroModules(serv.modules, basePath, serv.mc_version)
//Correct our workaround here.
@@ -744,19 +826,18 @@ class AssetGuard extends EventEmitter {
})
}
//TODO The distro index should be downloaded in the 'pre-loader'. This is because
//we will eventually NEED the index to generate the server list on the ui.
/*//TODO The file should be hosted, the following code is for local testing.
_chainValidateDistributionIndex(basePath){
return new Promise(function(fulfill, reject){
//const distroURL = 'http://mc.westeroscraft.com/WesterosCraftLauncher/westeroscraft.json'
const targetFile = path.join(basePath, 'westeroscraft.json')
//const targetFile = path.join(basePath, 'westeroscraft.json')
//TEMP WORKAROUND TO TEST WHILE THIS IS NOT HOSTED
fs.readFile(path.join(__dirname, '..', 'westeroscraft.json'), 'utf-8', (err, data) => {
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

View File

@@ -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'))
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
}

View File

@@ -0,0 +1,6 @@
const {AssetGuard} = require('./assetguard.js')
console.log('Preloading')
// Ensure Distribution is downloaded and cached.
AssetGuard.retrieveDistributionDataSync(false)