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 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();
});
};

View file

@ -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}`)});
};