From 3714c6582d5c2e92667ae87f8e40c405268430c3 Mon Sep 17 00:00:00 2001 From: Pierce Date: Sat, 1 Dec 2018 13:26:15 -0500 Subject: [PATCH] Added extractPackage, makePackage, and added more logic --- components/handler.js | 61 +++++++++++++++++++++++++++++++----------- components/launcher.js | 8 ++++-- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/components/handler.js b/components/handler.js index 4fe8290..5d3b5fa 100644 --- a/components/handler.js +++ b/components/handler.js @@ -2,7 +2,7 @@ const fs = require('fs'); const shelljs = require('shelljs'); const path = require('path'); const request = require('request'); -const unzip = require('extract-zip'); +const zip = require('adm-zip'); function downloadAsync (url, directory, name) { @@ -33,8 +33,10 @@ function downloadAsync (url, directory, name) { }); } -module.exports.getVersion = function (version) { +module.exports.getVersion = function (version, directory) { return new Promise(resolve => { + if(fs.existsSync(path.join(directory, `${version}.json`))) resolve(require(path.join(directory, `${version}.json`))); + const manifest = "https://launchermeta.mojang.com/mc/game/version_manifest.json"; request.get(manifest, function(error, response, body) { if (error) resolve(error); @@ -98,26 +100,30 @@ module.exports.getAssets = function (directory, version) { module.exports.getNatives = function (root, version, os) { return new Promise(async(resolve) => { - const nativeDirectory = path.join(root, "natives", `${Math.floor(Math.random() * 1000000000)}`); - shelljs.mkdir('-p', nativeDirectory); + let nativeDirectory; - const download = version.libraries.map(async function (lib) { - if (!lib.downloads.classifiers) return; - const type = `natives-${os}`; - const native = lib.downloads.classifiers[type]; + if(fs.existsSync(path.join(root, 'natives', version.id))) { + nativeDirectory = path.join(root, 'natives', version.id); + } else { + nativeDirectory = path.join(root, "natives", version.id); - if (native) { - const name = native.path.split('/').pop(); + shelljs.mkdir('-p', nativeDirectory); - await downloadAsync(native.url, nativeDirectory, name); + const download = version.libraries.map(async function (lib) { + if (!lib.downloads.classifiers) return; + const type = `natives-${os}`; + const native = lib.downloads.classifiers[type]; - unzip(`${path.join(nativeDirectory, name)}`, {dir: nativeDirectory},e => { + if (native) { + const name = native.path.split('/').pop(); + await downloadAsync(native.url, nativeDirectory, name); + new zip(path.join(nativeDirectory, name)).extractAllTo(nativeDirectory, true); shelljs.rm(path.join(nativeDirectory, name)); - }) - } - }); + } + }); - await Promise.all(download); + await Promise.all(download); + } resolve(nativeDirectory); }); @@ -196,3 +202,26 @@ module.exports.getJVM = function (version, options) { } }); }; + +module.exports.makePackage = async function(versions, os) { + const directory = path.join(process.cwd(), 'clientpackage'); + + for(const version in versions) { + const versionFile = await this.getVersion(versions[version], directory); + await this.getNatives(`${directory}/natives/${versions[version]}`, versionFile, os, true); + await this.getJar(versionFile, versions[version], `${directory}/versions/${versions[version]}`); + await this.getClasses(directory, versionFile); + await this.getAssets(directory, versionFile); + } + + const archive = new zip(); + archive.addLocalFolder(directory); + archive.writeZip(`${directory}.zip`); +}; + +module.exports.extractPackage = function(root, clientPackage) { + return new Promise(resolve => { + new zip(clientPackage).extractAllTo(root, true); + resolve(); + }); +}; \ No newline at end of file diff --git a/components/launcher.js b/components/launcher.js index adc2b99..5b05858 100644 --- a/components/launcher.js +++ b/components/launcher.js @@ -8,9 +8,13 @@ const shelljs = require('shelljs'); module.exports = async function (options) { if (!fs.existsSync(options.root)) fs.mkdirSync(options.root); - const versionFile = await handler.getVersion(options.version.number); + if(options.clientPackage) { + await handler.extractPackage(options.root, options.clientPackage); + } + const directory = path.join(options.root, 'versions', options.version.number); options.directory = directory; + const versionFile = await handler.getVersion(options.version.number, options.directory); const mcPath = path.join(directory, `${options.version.number}.jar`); const nativePath = await handler.getNatives(options.root, versionFile, options.os); @@ -53,5 +57,5 @@ module.exports = async function (options) { minecraft.stdout.on('data', (data) => {console.log(`[Minecraft] ${data}`)}); minecraft.stderr.on('data', (data) => {console.error(`[Error] ${data}`)}); - minecraft.on('close', (code) => {shelljs.rm('-rf', nativePath); console.log(`Minecraft closed with code ${code}`)}); + minecraft.on('close', (code) => {console.log(`Minecraft closed with code ${code}`)}); }; \ No newline at end of file