UNPKG

@kiwigdc/kiwilaunch

Version:
151 lines (150 loc) 7.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OfficialUpdater = void 0; const path = require("path"); const fs = require("fs"); const download = require("download"); const StreamZip = require("node-stream-zip"); const hasha = require("hasha"); const RecursiveFolderFile_1 = require("../Utils/RecursiveFolderFile"); const crc32_1 = require("crc/crc32"); const LibsInformations_1 = require("../Utils/LibsInformations"); const fetch = (...args) => Promise.resolve().then(() => require('node-fetch')).then(({ default: fetch }) => fetch(...args)); class OfficialUpdater { gameVersion; dir; MANIFEST_URL = "https://launchermeta.mojang.com/mc/game/version_manifest.json"; ASSETS_URL = "https://resources.download.minecraft.net/"; totalDownloadedFiles = 0; gameProperties; assetIndex; constructor(gameVersion, dir) { this.gameVersion = gameVersion; this.dir = dir; } getGameProperties() { return this.gameProperties; } /** * get */ async setManisfest() { let settings = { method: "get" }; const resJson = await fetch(this.MANIFEST_URL, settings).then(res => res.json()); this.gameProperties = await Promise.all(resJson.versions .filter(version => version.id === this.gameVersion.versionManisfest) .map(ver => fetch(ver.url, settings).then(res => res.json()))); this.assetIndex = await fetch(this.gameProperties[0].assetIndex.url, settings).then(res => res.json()); } async updateGame() { if (!fs.existsSync(this.dir.getGameDirectory())) { fs.mkdirSync(this.dir.getGameDirectory(), { recursive: true }); } console.log("Nombre de fichier ? : " + RecursiveFolderFile_1.RecursiveFolderFile.getAllFiles(this.dir.getGameDirectory()).length); await this.setManisfest(); await this.downloadsLibrariesFiles(); await this.downloadAssetsFiles(); await this.downloadClientJarFiles(); console.log("Telechargé ? : " + this.totalDownloadedFiles); } getMainClass() { return this.gameProperties[0].mainClass; } async downloadAssetsFiles() { var downloadFilesList = []; for (var i in this.assetIndex.objects) { let val = this.assetIndex.objects[i]; let hash = val.hash; let url = this.ASSETS_URL + hash.substring(0, 2) + "/" + hash; downloadFilesList.push({ url: (this.ASSETS_URL + hash.substring(0, 2) + "/" + hash), hash: val.hash }); } await Promise.all(downloadFilesList.map(async (fileInfo) => { const filePath = fileInfo.url.split('/').pop(); const passedPath = path.join(this.dir.getAssetDirectory(), 'objects', filePath.substring(0, 2), filePath); //fs.mkdirSync(passedPath, { recursive: true }) //fs.writeFileSync(path.join(this.dir.getAssetDirectory(), 'objects', filePath.substring(0,2), filePath), //await download(url)) //fs.writeFileSync(this.dir.getmainJar(), await download(this.gameProperties[0].downloads.client.url)); await this.checkDownloadFiles(fileInfo.url, fileInfo.hash, passedPath); })); //await Promise.all(downloadFilesList.map(url => download(url, path.join(this.dir.getAssetDirDirectory(), 'objects', url.split('/').pop().substring(0,2))))); await download(this.gameProperties[0].assetIndex.url, path.join(this.dir.getAssetDirectory(), "indexes")); } async downloadsLibrariesFiles() { var downloadFilesList = []; await this.gameProperties[0].libraries.forEach(async (val) => { var LibsInfo = new LibsInformations_1.LibsInformations(val.name); if (val['natives'] != undefined) { if (val['natives']['windows'] != undefined) { var nativesUsed = val['natives']['windows']; var filePath = path.join(this.dir.getLibsDirectory(), path.basename(val['downloads']['classifiers'][nativesUsed]['url'])); //fs.writeFileSync(filePath, await download(val['downloads']['classifiers']['natives-windows']['url'])); var downloaded = await this.checkDownloadFiles(val['downloads']['classifiers'][nativesUsed]['url'], val['downloads']['classifiers'][nativesUsed]['sha1'], filePath); if (downloaded) await this.extractNatives(filePath); } } if (val.downloads.artifact != undefined) { var index = downloadFilesList.findIndex(file => new LibsInformations_1.LibsInformations(file.name).getName() == LibsInfo.getName()); if (index == -1) { downloadFilesList.push({ url: val.downloads.artifact.url, name: val.name, hash: val.downloads.artifact.sha1 }); } else { var index = downloadFilesList.findIndex(file => new LibsInformations_1.LibsInformations(file.name).getName() == LibsInfo.getName()); if (LibsInfo.compareVersion(new LibsInformations_1.LibsInformations(downloadFilesList[index].name).getVersion())) { downloadFilesList[index] = { url: val.downloads.artifact.url, name: val.name, hash: val.downloads.artifact.sha1 }; } } } }); await Promise.all(downloadFilesList.map(fileInfo => { this.checkDownloadFiles(fileInfo.url, fileInfo.hash, path.join(this.dir.getLibsDirectory(), path.basename(fileInfo.url))); })); } async downloadClientJarFiles() { //fs.writeFileSync(this.dir.getmainJar(), await download(this.gameProperties[0].downloads.client.url)); await this.checkDownloadFiles(this.gameProperties[0].downloads.client.url, this.gameProperties[0].downloads.client.sha1, this.dir.getmainJar()); } async extractNatives(filePath) { const zip = new StreamZip.async({ file: filePath }); const entries = await zip.entries(); for (const entry of Object.values(entries)) { if (entry.isDirectory || path.parse(entry.name).ext == '.sha1' || path.parse(entry.name).ext == '.git' || path.parse(entry.name).ext == '.class' || entry.name.includes('META-INF')) continue; if (fs.existsSync(path.join(this.dir.getNativesDirectory(), entry.name)) && (0, crc32_1.default)(fs.readFileSync(path.join(this.dir.getNativesDirectory(), entry.name))) == entry.crc) continue; if (!fs.existsSync(this.dir.getNativesDirectory())) { fs.mkdirSync(this.dir.getNativesDirectory(), { recursive: true }); } await zip.extract(entry.name, this.dir.getNativesDirectory()); console.log("Extraction : " + entry.name); } //const count = await zip.extract(null, this.dir.getNativesDirectory()); await zip.close(); } async checkDownloadFiles(url, hash, dist) { var isChanged = false; if (!fs.existsSync(dist)) { fs.mkdirSync(path.dirname(dist), { recursive: true }); fs.writeFileSync(dist, await download(url)); isChanged = true; console.log(dist); this.totalDownloadedFiles++; } else { if (hasha.fromFileSync(dist, { algorithm: 'sha1' }) != hash) { fs.writeFileSync(dist, await download(url)); isChanged = true; console.log(dist); this.totalDownloadedFiles++; } } return isChanged; } } exports.OfficialUpdater = OfficialUpdater;