pimi-launcher-core/components/handler.js

559 lines
23 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');
const child = require('child_process');
let counter = 0;
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;
2019-08-07 16:25:49 -04:00
this.baseRequest = request.defaults({
pool: {maxSockets: this.options.overrides.maxSockets || 2},
timeout: this.options.timeout || 10000
});
2019-05-22 18:28:48 -04:00
}
2018-10-29 18:13:58 -04:00
checkJava(java) {
return new Promise(resolve => {
child.exec(`"${java}" -version`, (error, stdout, stderr) => {
if (error) {
resolve({
run: false,
2019-10-12 19:08:35 -04:00
message: error
})
} else {
this.client.emit('debug', `[MCLC]: Using Java version ${stderr.match(/"(.*?)"/).pop()} ${stderr.includes('64-Bit') ? '64-bit': '32-Bit'}`);
resolve({
run: true
});
}
});
});
}
downloadAsync(url, directory, name, retry, type) {
2019-05-22 18:28:48 -04:00
return new Promise(resolve => {
shelljs.mkdir('-p', directory);
2018-10-29 18:13:58 -04:00
2019-08-07 16:25:49 -04:00
const _request = this.baseRequest(url);
2018-10-29 18:13:58 -04:00
let received_bytes = 0;
let total_bytes = 0;
_request.on('response', (data) => {
if (data.statusCode === 404) {
this.client.emit('debug', `[MCLC]: Failed to download ${url} due to: File not found...`);
resolve(false);
}
total_bytes = parseInt(data.headers['content-length']);
});
_request.on('error', async (error) => {
this.client.emit('debug', `[MCLC]: Failed to download asset to ${path.join(directory, name)} due to\n${error}.` +
` Retrying... ${retry}`);
if (retry) await this.downloadAsync(url, directory, name, false, type);
resolve();
2018-10-29 18:13:58 -04:00
});
2019-05-22 18:28:48 -04:00
_request.on('data', (data) => {
received_bytes += data.length;
2019-05-22 18:28:48 -04:00
this.client.emit('download-status', {
"name": name,
"type": type,
"current": received_bytes,
"total": total_bytes
2019-05-22 18:28:48 -04:00
})
});
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-05-22 18:28:48 -04:00
});
2019-04-28 17:52:37 -04:00
});
file.on('error', async (e) => {
this.client.emit('debug', `[MCLC]: Failed to download asset to ${path.join(directory, name)} due to\n${e}.` +
` Retrying... ${retry}`);
if (fs.existsSync(path.join(directory, name))) shelljs.rm(path.join(directory, name));
if (retry) await this.downloadAsync(url, directory, name, false, type);
resolve();
});
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 => {
const versionJsonPath = this.options.overrides.versionJson || path.join(this.options.directory, `${this.options.version.number}.json`);
if (fs.existsSync(versionJsonPath)) {
this.version = JSON.parse(fs.readFileSync(versionJsonPath));
2019-05-22 18:28:48 -04:00
resolve(this.version);
return;
}
const manifest = `${this.options.overrides.url.meta}/mc/game/version_manifest.json`;
2019-05-22 18:28:48 -04:00
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) {
2019-05-22 18:28:48 -04:00
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, this.options.directory, `${this.options.version.number}.jar`, true, 'version-jar');
2018-10-29 18:13:58 -04:00
fs.writeFileSync(path.join(this.options.directory, `${this.options.version.number}.json`), JSON.stringify(this.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) => {
2019-05-26 12:23:34 -04:00
if(!fs.existsSync(path.join(this.options.root, 'assets', 'indexes', `${this.version.assetIndex.id}.json`))) {
await this.downloadAsync(this.version.assetIndex.url, path.join(this.options.root, 'assets', 'indexes'),
`${this.version.assetIndex.id}.json`, true, 'asset-json');
2018-10-29 18:13:58 -04:00
}
2020-01-19 23:46:31 +13:00
const index = JSON.parse(fs.readFileSync(path.join(this.options.root, 'assets', 'indexes',`${this.version.assetIndex.id}.json`), { encoding: 'utf8' }));
2018-10-29 18:13:58 -04:00
2019-08-28 15:47:04 +02:00
this.client.emit('progress', {
type: 'assets',
task: 0,
total: Object.keys(index.objects).length
});
await Promise.all(Object.keys(index.objects).map(async asset => {
const hash = index.objects[asset].hash;
const subhash = hash.substring(0,2);
const assetDirectory = this.options.overrides.assetRoot || path.join(this.options.root, 'assets');
const subAsset = path.join(assetDirectory, 'objects', subhash);
if(!fs.existsSync(path.join(subAsset, hash)) || !await this.checkSum(hash, path.join(subAsset, hash))) {
await this.downloadAsync(`${this.options.overrides.url.resource}/${subhash}/${hash}`, subAsset, hash,
true, 'assets');
counter = counter + 1;
this.client.emit('progress', {
type: 'assets',
task: counter,
total: Object.keys(index.objects).length
})
}
}));
counter = 0;
2018-10-29 18:13:58 -04:00
// Copy assets to legacy if it's an older Minecraft version.
2019-05-22 18:28:48 -04:00
if(this.version.assets === "legacy" || this.version.assets === "pre-1.6") {
const assetDirectory = this.options.overrides.assetRoot || path.join(this.options.root, 'assets');
this.client.emit('debug', `[MCLC]: Copying assets over to ${path.join(assetDirectory, 'legacy')}`);
2019-08-28 15:47:04 +02:00
this.client.emit('progress', {
type: 'assets-copy',
task: 0,
total: Object.keys(index.objects).length
});
2019-05-22 18:28:48 -04:00
await Promise.all(Object.keys(index.objects).map(async asset => {
const hash = index.objects[asset].hash;
const subhash = hash.substring(0,2);
const subAsset = path.join(assetDirectory, '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
if(!fs.existsSync(path.join(assetDirectory, 'legacy', legacyAsset.join('/')))) {
shelljs.mkdir('-p', path.join(assetDirectory, 'legacy', legacyAsset.join('/')));
}
2019-05-22 18:28:48 -04:00
if (!fs.existsSync(path.join(assetDirectory, 'legacy', asset))) {
fs.copyFileSync(path.join(subAsset, hash), path.join(assetDirectory, 'legacy', asset))
}
counter = counter + 1;
this.client.emit('progress', {
type: 'assets-copy',
task: counter,
total: Object.keys(index.objects).length
})
2019-05-22 18:28:48 -04:00
}));
}
counter = 0;
2019-05-22 18:28:48 -04:00
this.client.emit('debug', '[MCLC]: Downloaded assets');
resolve();
});
}
parseRule(lib) {
if(lib.rules) {
if (lib.rules.length > 1) {
if (lib.rules[0].action === 'allow' &&
lib.rules[1].action === 'disallow' &&
lib.rules[1].os.name === 'osx') {
return this.getOS() === 'osx';
} else {
return true;
}
} else {
if (lib.rules[0].action === 'allow' && lib.rules[0].os) return this.getOS() !== 'osx';
}
} else {
return false
}
}
2019-05-22 18:28:48 -04:00
getNatives() {
return new Promise(async(resolve) => {
const nativeDirectory = this.options.overrides.natives || path.join(this.options.root, 'natives', this.version.id);
2019-05-22 18:28:48 -04:00
if(!fs.existsSync(nativeDirectory) || !fs.readdirSync(nativeDirectory).length) {
2019-05-22 18:28:48 -04:00
shelljs.mkdir('-p', nativeDirectory);
const natives = () => {
return new Promise(async resolve => {
const natives = [];
await Promise.all(this.version.libraries.map(async (lib) => {
if (!lib.downloads.classifiers) return;
if (this.parseRule(lib)) return;
const native = this.getOS() === 'osx' ?
lib.downloads.classifiers['natives-osx'] || lib.downloads.classifiers['natives-macos'] :
lib.downloads.classifiers[`natives-${this.getOS()}`];
natives.push(native);
}));
resolve (natives);
})
};
const stat = await natives();
2019-08-28 15:47:04 +02:00
this.client.emit('progress', {
type: 'natives',
task: 0,
total: stat.length
});
await Promise.all(stat.map(async (native) => {
// Edge case on some systems where native is undefined and throws an error, this should fix it.
if(!native) return;
const name = native.path.split('/').pop();
await this.downloadAsync(native.url, nativeDirectory, name, true, 'natives');
if (!await this.checkSum(native.sha1, path.join(nativeDirectory, name))) {
await this.downloadAsync(native.url, nativeDirectory, name, true, 'natives');
2019-05-22 18:28:48 -04:00
}
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));
counter = counter + 1;
this.client.emit('progress', {
type: 'natives',
task: counter,
total: stat.length
})
2019-05-22 18:28:48 -04:00
}));
this.client.emit('debug', '[MCLC]: Downloaded and extracted natives');
}
2018-10-29 18:13:58 -04:00
counter = 0;
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
}
async getForgeDependenciesLegacy() {
2019-05-26 12:23:34 -04:00
if(!fs.existsSync(path.join(this.options.root, 'forge'))) {
shelljs.mkdir('-p', path.join(this.options.root, 'forge'));
2019-05-22 18:28:48 -04:00
}
try {
await new zip(this.options.forge).extractEntryTo('version.json', path.join(this.options.root, 'forge', `${this.version.id}`), false, true);
} catch(e) {
this.client.emit('debug', `[MCLC]: Unable to extract version.json from the forge jar due to ${e}`);
return null;
}
2019-05-22 18:28:48 -04:00
2020-01-19 23:46:31 +13:00
const forge = JSON.parse(fs.readFileSync(path.join(this.options.root, 'forge', `${this.version.id}`, 'version.json'), { encoding: 'utf8' }));
2019-05-22 18:28:48 -04:00
const paths = [];
2019-02-07 08:14:16 -05:00
2019-08-28 15:47:04 +02:00
this.client.emit('progress', {
type: 'forge',
task: 0,
total: forge.libraries.length
});
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
let url = this.options.overrides.url.mavenForge;
2019-05-26 12:23:34 -04:00
const jarPath = path.join(this.options.root, 'libraries', `${lib[0].replace(/\./g, '/')}/${lib[1]}/${lib[2]}`);
2019-05-22 18:28:48 -04:00
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 = this.options.overrides.url.defaultRepoForge;
2019-05-22 18:28:48 -04:00
} else {
return
}
2019-02-08 12:19:54 -05:00
}
const downloadLink = `${url}${lib[0].replace(/\./g, '/')}/${lib[1]}/${lib[2]}/${name}`;
2019-02-08 12:19:54 -05:00
2019-10-28 18:03:32 -04:00
2019-05-22 18:28:48 -04:00
if(fs.existsSync(path.join(jarPath, name))) {
paths.push(`${jarPath}${path.sep}${name}`);
counter = counter + 1;
this.client.emit('progress', { type: 'forge', task: counter, total: forge.libraries.length});
2019-05-22 18:28:48 -04:00
return;
}
if(!fs.existsSync(jarPath)) shelljs.mkdir('-p', jarPath);
2019-02-07 08:14:16 -05:00
const download = await this.downloadAsync(downloadLink, jarPath, name, true, 'forge');
2019-10-28 18:03:32 -04:00
if(!download) await this.downloadAsync(`${this.options.overrides.url.fallbackMaven}${lib[0].replace(/\./g, '/')}/${lib[1]}/${lib[2]}/${name}`, jarPath, name, true, 'forge');
2019-02-07 08:14:16 -05:00
2019-05-22 18:28:48 -04:00
paths.push(`${jarPath}${path.sep}${name}`);
counter = counter + 1;
this.client.emit('progress', {
2019-08-28 15:47:04 +02:00
type: 'forge',
task: counter,
total: forge.libraries.length
})
2019-05-22 18:28:48 -04:00
}));
2019-02-07 08:14:16 -05:00
counter = 0;
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
runInstaller(path) {
return new Promise(resolve => {
const installer = child.exec(path);
installer.on('close', (code) => resolve());
})
}
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) {
2020-01-29 13:54:50 +13:00
const customJarJson = JSON.parse(fs.readFileSync(path.join(this.options.root, 'versions', this.options.version.custom, `${this.options.version.custom}.json`), { encoding: 'utf8'}));
2019-08-28 15:47:04 +02:00
this.client.emit('progress', {
type: 'classes-custom',
task: 0,
total: customJarJson.libraries.length
});
2019-05-22 18:28:48 -04:00
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, true, 'classes-custom');
2019-05-22 18:28:48 -04:00
}
}
counter = counter + 1;
this.client.emit('progress', {
type: 'classes-custom',
task: counter,
total: customJarJson.libraries.length
});
libs.push(`${jarPath}${path.sep}${name}`);
2019-05-22 18:28:48 -04:00
}));
counter = 0;
2019-05-22 18:28:48 -04:00
}
const parsedClasses = () => {
return new Promise(async resolve => {
const classes = [];
await Promise.all(this.version.libraries.map(async (_lib) => {
if(!_lib.downloads.artifact) return;
if(this.parseRule(_lib)) return;
2018-10-29 18:13:58 -04:00
classes.push(_lib);
}));
resolve(classes);
})
};
const parsed = await parsedClasses();
2019-08-28 15:47:04 +02:00
this.client.emit('progress', {
type: 'classes',
task: 0,
total: parsed.length
});
await Promise.all(parsed.map(async (_lib) => {
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;
2019-08-07 16:25:49 -04:00
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
await this.downloadAsync(libraryUrl, directory, name, true, 'classes');
2019-05-22 18:28:48 -04:00
}
counter = counter + 1;
this.client.emit('progress', {
type: 'classes',
task: counter,
total: parsed.length
});
2019-05-22 18:28:48 -04:00
libs.push(libraryDirectory);
}));
counter = 0;
2019-05-22 18:28:48 -04:00
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) {
return new Promise(resolve => {
const newArray = [];
for(let classPath in array) {
if(newArray.includes(array[classPath])) continue;
newArray.push(array[classPath]);
}
resolve(newArray);
})
}
2019-05-22 18:28:48 -04:00
getLaunchOptions(modification) {
return new Promise(async resolve => {
2019-05-22 18:28:48 -04:00
let type = modification || this.version;
let args = type.minecraftArguments ? type.minecraftArguments.split(' ') : type.arguments.game;
const assetRoot = this.options.overrides.assetRoot || path.join(this.options.root, 'assets');
const assetPath = this.version.assets === "legacy" || this.version.assets === "pre-1.6" ? path.join(assetRoot, 'legacy') : path.join(assetRoot);
2019-05-22 18:28:48 -04:00
const minArgs = this.options.overrides.minArgs || 5;
if(args.length < minArgs) args = args.concat(this.version.minecraftArguments ? this.version.minecraftArguments.split(' ') : this.version.arguments.game);
2019-05-22 18:28:48 -04:00
this.options.authorization = await Promise.resolve(this.options.authorization);
2019-05-22 18:28:48 -04:00
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}': this.options.root,
2019-05-22 18:28:48 -04:00
'${assets_root}': assetPath,
'${game_assets}': assetPath,
'${version_type}': this.options.version.type
};
for (let index = 0; index < args.length; index++) {
if(typeof args[index] === 'object') args.splice(index, 2);
2019-05-22 18:28:48 -04:00
if (Object.keys(fields).includes(args[index])) {
args[index] = fields[args[index]];
}
2018-10-29 18:13:58 -04:00
}
if(this.options.window) this.options.window.fullscreen ? args.push('--fullscreen') : args.push('--width', this.options.window.width, '--height', this.options.window.height);
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() {
const opts = {
"windows": "-XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump",
"osx": "-XstartOnFirstThread",
"linux": "-Xss1M"
};
return opts[this.getOS()]
}
getOS() {
if(this.options.os) {
return this.options.os;
} else {
switch(process.platform) {
case "win32": return "windows";
case "darwin": return "osx";
2019-06-22 22:54:27 -04:00
default: return "linux";
2018-10-29 18:13:58 -04:00
}
}
}
extractPackage(options = this.options) {
return new Promise(async resolve => {
if(options.clientPackage.startsWith('http')) {
await this.downloadAsync(options.clientPackage, options.root, "clientPackage.zip", true, 'client-package');
options.clientPackage = path.join(options.root, "clientPackage.zip")
}
new zip(options.clientPackage).extractAllTo(options.root, true);
this.client.emit('package-extract', true);
if(options.removePackage) shelljs.rm(options.clientPackage);
resolve();
});
}
2019-05-22 18:28:48 -04:00
}
module.exports = Handler;