pimi-launcher-core/components/handler.js

369 lines
15 KiB
JavaScript
Raw Normal View History

2018-10-29 18:13:58 -04:00
const fs = require('fs');
const shelljs = require('shelljs');
const path = require('path');
const request = require('request');
const checksum = require('checksum');
const zip = require('adm-zip');
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
class Handler {
constructor(client) {
this.client = client;
this.options = client.options;
this.version = undefined;
}
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
downloadAsync(url, directory, name) {
return new Promise(resolve => {
shelljs.mkdir('-p', directory);
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
const _request = request(url, {timeout: this.options.timeout || 10000});
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
_request.on('error', function(error) {
resolve({
failed: true,
asset: {
url: url,
directory: directory,
name: name
}
});
2018-10-29 18:13:58 -04:00
});
2019-05-22 18:28:48 -04:00
_request.on('data', (data) => {
let size = 0;
if(fs.existsSync(path.join(directory, name))) size = fs.statSync(path.join(directory, name))["size"];
this.client.emit('download-status', {
"name": name,
"current": Math.round(size / 10000),
"total": data.length
})
});
2019-01-28 17:26:04 -05:00
2019-05-22 18:28:48 -04:00
const file = fs.createWriteStream(path.join(directory, name));
_request.pipe(file);
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
file.once('finish', () => {
this.client.emit('download', name);
resolve({failed: false, asset: null});
});
2019-04-28 17:52:37 -04:00
2019-05-22 18:28:48 -04:00
file.on('error', (e) => {
this.client.emit('debug', `[MCLC]: Failed to download asset to ${path.join(directory, name)} due to\n${e}`);
if(fs.existsSync(path.join(directory, name))) shelljs.rm(path.join(directory, name));
resolve({
failed: true,
asset: {
url: url,
directory: directory,
name: name
}
});
2019-04-28 17:52:37 -04:00
});
});
2019-05-22 18:28:48 -04:00
}
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
checkSum(hash, file) {
return new Promise(resolve => {
checksum.file(file, (err, sum) => resolve(hash === sum));
});
}
2019-05-22 18:28:48 -04:00
getVersion() {
return new Promise(resolve => {
if(fs.existsSync(path.join(this.options.directory, `${this.options.version}.json`))) {
this.version = require(path.join(this.options.directory, `${this.options.version}.json`));
resolve(this.version);
return;
}
2019-05-22 18:28:48 -04:00
const manifest = "https://launchermeta.mojang.com/mc/game/version_manifest.json";
request.get(manifest, (error, response, body) => {
if (error) resolve(error);
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
const parsed = JSON.parse(body);
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
for (const desiredVersion in parsed.versions) {
if(parsed.versions[desiredVersion].id === this.options.version.number) {
request.get(parsed.versions[desiredVersion].url, (error, response, body) => {
if (error) resolve(error);
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
this.client.emit('debug', `[MCLC]: Parsed version from version manifest`);
this.version = JSON.parse(body);
resolve(this.version);
});
}
2018-10-29 18:13:58 -04:00
}
2019-05-22 18:28:48 -04:00
});
2018-10-29 18:13:58 -04:00
});
2019-05-22 18:28:48 -04:00
}
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
getJar() {
return new Promise(async (resolve)=> {
await this.downloadAsync(this.version.downloads.client.url, directory, `${this.options.version.number}.jar`);
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
fs.writeFileSync(path.join(directory, `${this.options.version.number}.json`), JSON.stringify(this.options.version, null, 4));
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
this.client.emit('debug', '[MCLC]: Downloaded version jar and wrote version json');
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
resolve();
});
}
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
getAssets() {
return new Promise(async(resolve) => {
const assetsUrl = 'https://resources.download.minecraft.net';
const failed = [];
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
if(!fs.existsSync(path.join(this.options.directory, 'assets', 'indexes', `${this.version.assetIndex.id}.json`))) {
await this.downloadAsync(this.version.assetIndex.url, path.join(this.options.directory, 'assets', 'indexes'), `${this.version.assetIndex.id}.json`);
2018-10-29 18:13:58 -04:00
}
2019-05-22 18:28:48 -04:00
const index = require(path.join(this.options.directory, 'assets', 'indexes',`${this.version.assetIndex.id}.json`));
2018-10-29 18:13:58 -04:00
await Promise.all(Object.keys(index.objects).map(async asset => {
const hash = index.objects[asset].hash;
const subhash = hash.substring(0,2);
2019-05-22 18:28:48 -04:00
const assetDirectory = path.join(this.options.directory, 'assets', 'objects', subhash);
2019-05-22 18:28:48 -04:00
if(!fs.existsSync(path.join(assetDirectory, hash)) || !await this.checkSum(hash, path.join(assetDirectory, hash))) {
const download = await this.downloadAsync(`${assetsUrl}/${subhash}/${hash}`, assetDirectory, hash);
2019-05-22 18:28:48 -04:00
if(download.failed) failed.push(download.asset);
}
}));
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
// why do we have this? B/c sometimes Minecraft's resource site times out!
if(failed) {
await Promise.all(failed.map(async asset => await this.downloadAsync(asset.url, asset.directory, asset.name)))
}
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
// Copy assets to legacy if it's an older Minecarft version.
if(this.version.assets === "legacy" || this.version.assets === "pre-1.6") {
await Promise.all(Object.keys(index.objects).map(async asset => {
const hash = index.objects[asset].hash;
const subhash = hash.substring(0,2);
const assetDirectory = path.join(this.options.directory, 'assets', 'objects', subhash);
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
let legacyAsset = asset.split('/');
legacyAsset.pop();
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
if(!fs.existsSync(path.join(this.options.directory, 'assets', 'legacy', legacyAsset.join('/')))) {
shelljs.mkdir('-p', path.join(this.options.directory, 'assets', 'legacy', legacyAsset.join('/')));
}
2019-05-22 18:28:48 -04:00
if (!fs.existsSync(path.join(this.options.directory, 'assets', 'legacy', asset))) {
fs.copyFileSync(path.join(assetDirectory, hash), path.join(this.options.directory, 'assets', 'legacy', asset))
}
2019-05-22 18:28:48 -04:00
}));
}
this.client.emit('debug', '[MCLC]: Downloaded assets');
resolve();
});
}
getNatives() {
return new Promise(async(resolve) => {
let nativeDirectory;
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
if(fs.existsSync(path.join(this.options.root, 'natives', this.version.id))) {
nativeDirectory = path.join(this.options.root, 'natives', this.version.id);
} else {
nativeDirectory = path.join(this.options.root, "natives", this.version.id);
shelljs.mkdir('-p', nativeDirectory);
await Promise.all(version.libraries.map(async (lib) => {
if (!lib.downloads.classifiers) return;
const type = `natives-${this.options.os}`;
const native = lib.downloads.classifiers[type];
if (native) {
const name = native.path.split('/').pop();
await this.downloadAsync(native.url, nativeDirectory, name);
if(!await this.checkSum(native.sha1, path.join(nativeDirectory, name))) {
await this.downloadAsync(native.url, nativeDirectory, name);
}
try {new zip(path.join(nativeDirectory, name)).extractAllTo(nativeDirectory, true);} catch(e) {
// Only doing a console.warn since a stupid error happens. You can basically ignore this.
// if it says Invalid file name, just means two files were downloaded and both were deleted.
// All is well.
console.warn(e);
}
shelljs.rm(path.join(nativeDirectory, name));
}
}));
this.client.emit('debug', '[MCLC]: Downloaded and extracted natives');
}
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
this.client.emit('debug', `[MCLC]: Set native path to ${nativeDirectory}`);
resolve(nativeDirectory);
});
2019-02-07 08:14:16 -05:00
}
2019-05-22 18:28:48 -04:00
async getForgeDependencies() {
if(!fs.existsSync(path.join(root, 'forge'))) {
shelljs.mkdir('-p', path.join(root, 'forge'));
}
await new zip(this.options.forge).extractEntryTo('version.json', path.join(this.options.root, 'forge', `${this.version.id}`), false, true);
const forge = require(path.join(this.options.root, 'forge', `${this.version.id}`, 'version.json'));
const mavenUrl = 'http://files.minecraftforge.net/maven/';
const defaultRepo = 'https://libraries.minecraft.net/';
const paths = [];
2019-02-07 08:14:16 -05:00
2019-05-22 18:28:48 -04:00
await Promise.all(forge.libraries.map(async library => {
const lib = library.name.split(':');
2019-02-07 08:14:16 -05:00
2019-05-22 18:28:48 -04:00
if(lib[0] === 'net.minecraftforge' && lib[1].includes('forge')) return;
2019-02-07 08:14:16 -05:00
2019-05-22 18:28:48 -04:00
let url = mavenUrl;
const jarPath = path.join(root, 'libraries', `${lib[0].replace(/\./g, '/')}/${lib[1]}/${lib[2]}`);
const name = `${lib[1]}-${lib[2]}.jar`;
2019-02-07 08:14:16 -05:00
2019-05-22 18:28:48 -04:00
if(!library.url) {
if(library.serverreq || library.clientreq) {
url = defaultRepo;
} else {
return
}
2019-02-08 12:19:54 -05:00
}
2019-05-22 18:28:48 -04:00
const downloadLink = `${url}${lib[0].replace(/\./g, '/')}/${lib[1]}/${lib[2]}/${lib[1]}-${lib[2]}.jar`;
2019-02-08 12:19:54 -05:00
2019-05-22 18:28:48 -04:00
if(fs.existsSync(path.join(jarPath, name))) {
paths.push(`${jarPath}${path.sep}${name}`);
return;
}
if(!fs.existsSync(jarPath)) shelljs.mkdir('-p', jarPath);
2019-02-07 08:14:16 -05:00
2019-05-22 18:28:48 -04:00
await this.downloadAsync(downloadLink, jarPath, name);
2019-02-07 08:14:16 -05:00
2019-05-22 18:28:48 -04:00
paths.push(`${jarPath}${path.sep}${name}`);
}));
2019-02-07 08:14:16 -05:00
2019-05-22 18:28:48 -04:00
this.client.emit('debug', '[MCLC]: Downloaded Forge dependencies');
2019-04-28 17:52:37 -04:00
2019-05-22 18:28:48 -04:00
return {paths, forge};
}
2019-02-07 08:14:16 -05:00
2019-05-22 18:28:48 -04:00
getClasses() {
return new Promise(async (resolve) => {
const libs = [];
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
if(this.options.version.custom) {
const customJarJson = require(path.join(this.options.root, 'versions', this.options.version.custom, `${this.options.version.custom}.json`));
await Promise.all(customJarJson.libraries.map(async library => {
const lib = library.name.split(':');
2019-05-22 18:28:48 -04:00
const jarPath = path.join(this.options.root, 'libraries', `${lib[0].replace(/\./g, '/')}/${lib[1]}/${lib[2]}`);
const name = `${lib[1]}-${lib[2]}.jar`;
2019-05-22 18:28:48 -04:00
if(!fs.existsSync(path.join(jarPath, name))) {
if(library.url) {
const url = `${library.url}${lib[0].replace(/\./g, '/')}/${lib[1]}/${lib[2]}/${lib[1]}-${lib[2]}.jar`;
await this.downloadAsync(url, jarPath, name);
}
}
2019-05-22 18:28:48 -04:00
libs.push(`${jarPath}/${name}`);
}));
}
2019-05-22 18:28:48 -04:00
await Promise.all(this.version.libraries.map(async (_lib) => {
if(!_lib.downloads.artifact) return;
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
const libraryPath = _lib.downloads.artifact.path;
const libraryUrl = _lib.downloads.artifact.url;
const libraryHash = _lib.downloads.artifact.sha1;
const libraryDirectory = path.join(this.options.root, 'libraries', libraryPath);
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
if(!fs.existsSync(libraryDirectory) || !await this.checkSum(libraryHash, libraryDirectory)) {
let directory = libraryDirectory.split(path.sep);
const name = directory.pop();
directory = directory.join(path.sep);
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
await this.downloadAsync(libraryUrl, directory, name);
}
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
libs.push(libraryDirectory);
}));
this.client.emit('debug', '[MCLC]: Collected class paths');
resolve(libs)
});
}
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
static cleanUp(array) {
const newArray = [];
2018-10-29 18:13:58 -04:00
2019-05-22 18:28:48 -04:00
for(let argument in array) {
if(newArray.includes(array[argument])) continue;
newArray.push(array[argument]);
}
2019-05-22 18:28:48 -04:00
return newArray;
}
2019-05-22 18:28:48 -04:00
getLaunchOptions(modification) {
return new Promise(resolve => {
let type = modification || this.version;
let args = type.minecraftArguments ? type.minecraftArguments.split(' ') : type.arguments.game;
const assetPath = this.version.assets === "legacy" || this.version.assets === "pre-1.6" ? path.join(this.options.root, 'assets', 'legacy') : path.join(this.options.root, 'assets');
if(args.length < 5) args = args.concat(this.version.minecraftArguments ? this.version.minecraftArguments.split(' ') : this.version.arguments.game);
const fields = {
'${auth_access_token}': this.options.authorization.access_token,
'${auth_session}': this.options.authorization.access_token,
'${auth_player_name}': this.options.authorization.name,
'${auth_uuid}': this.options.authorization.uuid,
'${user_properties}': this.options.authorization.user_properties,
'${user_type}': 'mojang',
'${version_name}': this.options.version.number,
'${assets_index_name}': this.version.assetIndex.id,
'${game_directory}': path.join(this.options.root),
'${assets_root}': assetPath,
'${game_assets}': assetPath,
'${version_type}': this.options.version.type
};
for (let index = 0; index < args.length; index++) {
if (Object.keys(fields).includes(args[index])) {
args[index] = fields[args[index]];
}
2018-10-29 18:13:58 -04:00
}
2019-05-22 18:28:48 -04:00
if(this.options.server) args.push('--server', this.options.server.host, '--port', this.options.server.port || "25565");
if(this.options.proxy) args.push(
'--proxyHost',
this.options.proxy.host,
'--proxyPort',
this.options.proxy.port || "8080",
'--proxyUser',
this.options.proxy.username,
'--proxyPass',
this.options.proxy.password
);
this.client.emit('debug', '[MCLC]: Set launch options');
resolve(args);
});
}
async getJVM() {
switch(this.options.os) {
2018-10-29 18:13:58 -04:00
case "windows": {
2019-05-22 18:28:48 -04:00
return "-XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump"
2018-10-29 18:13:58 -04:00
}
case "osx": {
2019-05-22 18:28:48 -04:00
return "-XstartOnFirstThread"
2018-10-29 18:13:58 -04:00
}
case "linux": {
2019-05-22 18:28:48 -04:00
return "-Xss1M"
2018-10-29 18:13:58 -04:00
}
}
}
2019-05-22 18:28:48 -04:00
}
2019-05-22 18:28:48 -04:00
module.exports = Handler;