Added ability to override paths. Fixed docs and typos

This commit is contained in:
Pierce 2019-07-22 19:29:36 -04:00
parent 45f3c95da1
commit 4a30dd96d5
5 changed files with 46 additions and 17 deletions

View file

@ -55,6 +55,7 @@ launcher.on('error', (e) => console.log(e.toString('utf-8')));
| `options.installer` | String | Path to installer being executed. | False |
| `options.root` | String | Path where you want the launcher to work in. like `C:/Users/user/AppData/Roaming/.mc`, | True |
| `options.os` | String | windows, osx or linux. MCLC with auto determine the OS if this field isn't provided. | False |
| `options.customArgs` | Array | Array of custom java arguments you want to add. | False |
| `options.version.number` | String | Minecraft version that is going to be launched. | True |
| `options.version.type` | String | Any string. The actual Minecraft launcher uses `release` and `snapshot`. | True |
| `options.version.custom` | String | The name of the folder, jar file, and version json in the version folder. | False |
@ -71,6 +72,20 @@ launcher.on('error', (e) => console.log(e.toString('utf-8')));
| `options.timeout` | Integer | Timeout on download requests. | False |
| `options.window.width` | String | Width of the Minecraft Client | False |
| `options.window.height` | String | Height of the Minecraft Client. | False |
| `options.overrides` | Object | Json object redefining paths for better customization. Example below. | False |
```js
let opts = {
otherOps...,
overrides: {
minecraftJar: "",
versionJson: "",
directory: "",
libraries: "",
natives: "",
assetRoot: ""
}
}
```
#### Notes
##### Custom

View file

@ -119,7 +119,7 @@ module.exports.invalidate = function(accessToken, clientToken) {
module.exports.signOut = function(username, password) {
return new Promise(resolve => {
const requestObject = {
url: api_url + "/invalidate",
url: api_url + "/signout",
json: {
"username": username,
"password": password
@ -132,4 +132,4 @@ module.exports.signOut = function(username, password) {
if(!body) resolve(true); else resolve(false);
});
});
};
};

View file

@ -72,8 +72,9 @@ class Handler {
getVersion() {
return new Promise(resolve => {
if(fs.existsSync(path.join(this.options.directory, `${this.options.version.number}.json`))) {
this.version = require(path.join(this.options.directory, `${this.options.version.number}.json`));
const versionJsonPath = this.options.overrides.versionJson || path.join(this.options.directory, `${this.options.version.number}.json`);
if(fs.existsSync(versionJsonPath)) {
this.version = require(versionJsonPath);
resolve(this.version);
return;
}
@ -125,10 +126,11 @@ class Handler {
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.root, 'assets', 'objects', subhash);
const assetDirectory = this.options.overrides.assetRoot || path.join(this.options.root, 'assets');
const subAsset = path.join(assetDirectory, 'objects', subhash);
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);
if(!fs.existsSync(path.join(subAsset, hash)) || !await this.checkSum(hash, path.join(subAsset, hash))) {
const download = await this.downloadAsync(`${assetsUrl}/${subhash}/${hash}`, subAsset, hash);
if(download.failed) failed.push(download.asset);
}
@ -140,12 +142,13 @@ class Handler {
await Promise.all(failed.map(async asset => await this.downloadAsync(asset.url, asset.directory, asset.name)))
}
// Copy assets to legacy if it's an older Minecarft version.
// Copy assets to legacy if it's an older Minecraft 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.root, 'assets', 'objects', subhash);
const assetDirectory = this.options.overrides.assetRoot || path.join(this.options.root, 'assets');
const subAsset = path.join(assetDirectory, 'objects', subhash);
let legacyAsset = asset.split('/');
legacyAsset.pop();
@ -155,7 +158,7 @@ class Handler {
}
if (!fs.existsSync(path.join(this.options.root, 'assets', 'legacy', asset))) {
fs.copyFileSync(path.join(assetDirectory, hash), path.join(this.options.root, 'assets', 'legacy', asset))
fs.copyFileSync(path.join(subAsset, hash), path.join(assetDirectory, 'legacy', asset))
}
}));
}
@ -167,7 +170,7 @@ class Handler {
getNatives() {
return new Promise(async(resolve) => {
const nativeDirectory = path.join(this.options.root, 'natives', this.version.id);
const nativeDirectory = this.options.overrides.natives || path.join(this.options.root, 'natives', this.version.id);
if(!fs.existsSync(nativeDirectory) || !fs.readdirSync(nativeDirectory).length) {
shelljs.mkdir('-p', nativeDirectory);
@ -281,7 +284,7 @@ class Handler {
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);
const libraryDirectory = this.options.overrides.libraries || path.join(this.options.root, 'libraries', libraryPath);
if(!fs.existsSync(libraryDirectory) || !await this.checkSum(libraryHash, libraryDirectory)) {
let directory = libraryDirectory.split(path.sep);
@ -316,7 +319,8 @@ class Handler {
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');
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);
if(args.length < 11) args = args.concat(this.version.minecraftArguments ? this.version.minecraftArguments.split(' ') : this.version.arguments.game);

View file

@ -13,7 +13,17 @@ class MCLCore extends EventEmitter {
async launch(options) {
this.options = options;
this.options.root = path.resolve(this.options.root);
if(!this.options.overrides) this.options.overrides = {};
this.options.overrides = {
minecraftJar: this.options.overrides.minecraftJar ? path.join(this.options.root, this.options.overrides.minecraftJar): null,
versionJson: this.options.overrides.versionJson ? path.join(this.options.root, this.options.overrides.versionJson): null,
directory: this.options.overrides.directory ? path.join(this.options.root, this.options.overrides.directory): null,
libraries: this.options.overrides.libraries ? path.join(this.options.root, this.options.overrides.libraries): null,
natives: this.options.overrides.natives ? path.join(this.options.root, this.options.overrides.natives): null,
assetRoot: this.options.overrides.assetRoot ? path.join(this.options.root, this.options.overrides.assetRoot): null,
};
this.handler = new handler(this);
const override = this.options.overrides;
if(!fs.existsSync(this.options.root)) {
this.emit('debug', '[MCLC]: Attempting to create root folder');
@ -31,13 +41,13 @@ class MCLCore extends EventEmitter {
await this.handler.runInstaller(this.options.installer)
}
const directory = path.join(this.options.root, 'versions', this.options.version.number);
const directory = override.directory || path.join(this.options.root, 'versions', this.options.version.number);
this.options.directory = directory;
// Version JSON for the main launcher folder
const versionFile = await this.handler.getVersion();
const mcPath = this.options.version.custom ? path.join(this.options.root, 'versions', this.options.version.custom , `${this.options.version.custom}.jar`):
path.join(directory, `${this.options.version.number}.jar`);
const mcPath = override.minecraftJar || (this.options.version.custom ? path.join(this.options.root, 'versions', this.options.version.custom , `${this.options.version.custom}.jar`):
path.join(directory, `${this.options.version.number}.jar`));
const nativePath = await this.handler.getNatives();
if (!fs.existsSync(mcPath)) {

View file

@ -1,6 +1,6 @@
{
"name": "minecraft-launcher-core",
"version": "3.6.2",
"version": "3.7.0",
"description": "Lightweight module that downloads and runs Minecraft using javascript / NodeJS",
"main": "index.js",
"dependencies": {