Added extractPackage, makePackage, and added more logic

This commit is contained in:
Pierce 2018-12-01 13:26:15 -05:00
parent 065e0befee
commit 3714c6582d
2 changed files with 51 additions and 18 deletions

View file

@ -2,7 +2,7 @@ const fs = require('fs');
const shelljs = require('shelljs'); const shelljs = require('shelljs');
const path = require('path'); const path = require('path');
const request = require('request'); const request = require('request');
const unzip = require('extract-zip'); const zip = require('adm-zip');
function downloadAsync (url, directory, name) { 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 => { 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"; const manifest = "https://launchermeta.mojang.com/mc/game/version_manifest.json";
request.get(manifest, function(error, response, body) { request.get(manifest, function(error, response, body) {
if (error) resolve(error); if (error) resolve(error);
@ -98,7 +100,13 @@ module.exports.getAssets = function (directory, version) {
module.exports.getNatives = function (root, version, os) { module.exports.getNatives = function (root, version, os) {
return new Promise(async(resolve) => { return new Promise(async(resolve) => {
const nativeDirectory = path.join(root, "natives", `${Math.floor(Math.random() * 1000000000)}`); let nativeDirectory;
if(fs.existsSync(path.join(root, 'natives', version.id))) {
nativeDirectory = path.join(root, 'natives', version.id);
} else {
nativeDirectory = path.join(root, "natives", version.id);
shelljs.mkdir('-p', nativeDirectory); shelljs.mkdir('-p', nativeDirectory);
const download = version.libraries.map(async function (lib) { const download = version.libraries.map(async function (lib) {
@ -108,16 +116,14 @@ module.exports.getNatives = function (root, version, os) {
if (native) { if (native) {
const name = native.path.split('/').pop(); const name = native.path.split('/').pop();
await downloadAsync(native.url, nativeDirectory, name); await downloadAsync(native.url, nativeDirectory, name);
new zip(path.join(nativeDirectory, name)).extractAllTo(nativeDirectory, true);
unzip(`${path.join(nativeDirectory, name)}`, {dir: nativeDirectory},e => {
shelljs.rm(path.join(nativeDirectory, name)); shelljs.rm(path.join(nativeDirectory, name));
})
} }
}); });
await Promise.all(download); await Promise.all(download);
}
resolve(nativeDirectory); 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();
});
};

View file

@ -8,9 +8,13 @@ const shelljs = require('shelljs');
module.exports = async function (options) { module.exports = async function (options) {
if (!fs.existsSync(options.root)) fs.mkdirSync(options.root); 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); const directory = path.join(options.root, 'versions', options.version.number);
options.directory = directory; options.directory = directory;
const versionFile = await handler.getVersion(options.version.number, options.directory);
const mcPath = path.join(directory, `${options.version.number}.jar`); const mcPath = path.join(directory, `${options.version.number}.jar`);
const nativePath = await handler.getNatives(options.root, versionFile, options.os); 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.stdout.on('data', (data) => {console.log(`[Minecraft] ${data}`)});
minecraft.stderr.on('data', (data) => {console.error(`[Error] ${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}`)});
}; };