From cf0afeb957a9d74bd6f235514af79587414657af Mon Sep 17 00:00:00 2001 From: Daniel Scalzi Date: Fri, 28 Apr 2017 03:59:28 -0400 Subject: [PATCH] Began working on launching minecraft process. Implementing hash validation for asset downloads. Full integration for the latter will be complete soon. --- app/assets/js/assetdownload.js | 47 +++++++++++++++++++------ app/assets/js/launchprocess.js | 64 ++++++++++++++++++++++++++++++++++ index.js | 11 +++--- package.json | 3 +- 4 files changed, 108 insertions(+), 17 deletions(-) create mode 100644 app/assets/js/launchprocess.js diff --git a/app/assets/js/assetdownload.js b/app/assets/js/assetdownload.js index 61c1d2fa..d6408900 100644 --- a/app/assets/js/assetdownload.js +++ b/app/assets/js/assetdownload.js @@ -3,11 +3,13 @@ const request = require('request') const path = require('path') const mkpath = require('mkdirp'); const async = require('async') +const crypto = require('crypto') -function Asset(from, to, size){ +function Asset(from, to, size, hash){ this.from = from this.to = to this.size = size + this.hash = hash } function AssetIndex(id, sha1, size, url, totalSize){ @@ -213,22 +215,26 @@ exports.downloadAssets = function(versionData, basePath){ const hash = String(ob['hash']) const assetName = path.join(hash.substring(0, 2), hash) const urlName = hash.substring(0, 2) + "/" + hash - const ast = new Asset(resourceURL + urlName, path.join(objectPath, assetName), ob['size']) + const ast = new Asset(resourceURL + urlName, path.join(objectPath, assetName), ob['size'], hash) assetArr.push(ast) }) let acc = 0; async.eachLimit(assetArr, 5, function(asset, cb){ mkpath.sync(path.join(asset.to, "..")) - let req = request(asset.from) - let writeStream = fs.createWriteStream(asset.to) - req.pipe(writeStream) - req.on('data', function(chunk){ - acc += chunk.length - //console.log('Progress', acc/datasize) - }) - writeStream.on('close', function(){ + if(!validateLocalIntegrity(asset.to, 'sha1', asset.hash)){ + let req = request(asset.from) + let writeStream = fs.createWriteStream(asset.to) + req.pipe(writeStream) + req.on('data', function(chunk){ + acc += chunk.length + //console.log('Progress', acc/datasize) + }) + writeStream.on('close', function(){ + cb() + }) + } else { cb() - }) + } }, function(err){ if(err){ console.log('An asset failed to process'); @@ -238,4 +244,23 @@ exports.downloadAssets = function(versionData, basePath){ }) }) }) +} + +validateLocalIntegrity = function(filePath, algo, hash){ + if(fs.existsSync(filePath)){ + let fileName = path.basename(filePath) + console.log('Validating integrity of local file', fileName) + let shasum = crypto.createHash(algo) + let content = fs.readFileSync(filePath) + shasum.update(content) + let localhash = shasum.digest('hex') + if(localhash === hash){ + console.log('Hash value of ' + fileName + ' matches the index hash, woo!') + return true + } else { + console.log('Hash value of ' + fileName + ' (' + localhash + ')' + ' does not match the index hash. Redownloading..') + return false + } + } + return false; } \ No newline at end of file diff --git a/app/assets/js/launchprocess.js b/app/assets/js/launchprocess.js new file mode 100644 index 00000000..7ac9eb67 --- /dev/null +++ b/app/assets/js/launchprocess.js @@ -0,0 +1,64 @@ +const mojang = require('mojang') +const uuidV4 = require('uuid/v4') +const path = require('path') +const child_process = require('child_process') + +exports.launchMinecraft = function(versionData, basePath){ + const authPromise = mojang.auth('EMAIL', 'PASS', uuidV4(), { + name: 'Minecraft', + version: 1 + }) + authPromise.then(function(data){ + const hardcodedargs = '' + const args = finalizeArguments(versionData, data, basePath) + console.log(args) + const child = child_process.execFile(basePath) + }) +} + +finalizeArguments = function(versionData, authData, basePath){ + const mcArgs = versionData['minecraftArguments'] + const regex = new RegExp('\\${*(.*)}') + const argArr = mcArgs.split(' ') + for(let i=0; i { win = null diff --git a/package.json b/package.json index f30244ce..a53333af 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "async": "^2.3.0", "electron": "^1.6.5", "mojang": "^0.4.0", - "promise": "^7.1.1" + "promise": "^7.1.1", + "uuid": "^3.0.1" } }