diff --git a/README.md b/README.md index febe0f0..a6d1aa7 100644 --- a/README.md +++ b/README.md @@ -59,10 +59,11 @@ launcher.on('data', (e) => console.log(e)); | Parameter | Type | Description | Required | |--------------------------|----------|-------------------------------------------------------------------------------------------|----------| -| `options.clientPackage` | String | Path or URL to a zip file, which will be extracted to the root directory. (Not recommended for produnction use)| False | +| `options.clientPackage` | String | Path or URL to a zip file, which will be extracted to the root directory. (Not recommended for production use)| False | | `options.removePackage` | Boolean | Option to remove the client package zip file after its finished extracting. | False | | `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.cache` | String | Path where launcher files will be cached in. like `C:/Users/user/AppData/Roaming/.mc/cache`, | False | | `options.os` | String | windows, osx or linux. MCLC will auto determine the OS if this field isn't provided. | False | | `options.customLaunchArgs`| Array | Array of custom Minecraft arguments you want to add. | False | | `options.customArgs` | Array | Array of custom Java arguments you want to add. | False | diff --git a/components/handler.js b/components/handler.js index 94a8057..796df17 100644 --- a/components/handler.js +++ b/components/handler.js @@ -112,18 +112,44 @@ class Handler { } const manifest = `${this.options.overrides.url.meta}/mc/game/version_manifest.json` + const cache = this.options.cache ? `${this.options.cache}/json` : `${this.options.root}/cache/json` request.get(manifest, (error, response, body) => { - if (error) return resolve(error) + if (error && error.code !== 'ENOTFOUND') return resolve(error) + if (!error) { + if (!fs.existsSync(cache)) { + fs.mkdirSync(cache, { recursive: true }) + this.client.emit('debug', '[MCLC]: Cache directory created.') + } + fs.writeFile(path.join(`${cache}/version_manifest.json`), body, (err) => { + if (err) return resolve(err) + this.client.emit('debug', '[MCLC]: Cached version_manifest.json') + }) + } - const parsed = JSON.parse(body) + let parsed + if (error && (error.code === 'ENOTFOUND')) { + parsed = JSON.parse(fs.readFileSync(`${cache}/version_manifest.json`)) + } else { + parsed = JSON.parse(body) + } 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) return resolve(error) + if (error && error.code !== 'ENOTFOUND') return resolve(error) + if (!error) { + fs.writeFile(path.join(`${cache}/${this.options.version.number}.json`), body, (err) => { + if (err) return resolve(err) + this.client.emit('debug', `[MCLC]: Cached ${this.options.version.number}.json`) + }) + } this.client.emit('debug', '[MCLC]: Parsed version from version manifest') - this.version = JSON.parse(body) + if (error && (error.code === 'ENOTFOUND')) { + this.version = JSON.parse(fs.readFileSync(`${cache}/${this.options.version.number}.json`)) + } else { + this.version = JSON.parse(body) + } return resolve(this.version) }) } diff --git a/index.d.ts b/index.d.ts index fafe4c5..ce63c06 100644 --- a/index.d.ts +++ b/index.d.ts @@ -152,6 +152,10 @@ declare module "minecraft-launcher-core" { }; overrides?: IOverrides; authorization: Promise; + /** + * Path of json cache. + */ + cache?: string; }