@kiwigdc/kiwilaunch
Version:
Launch minecraft client
184 lines (183 loc) • 10.9 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VanillaUpdater = void 0;
const path = require("path");
const __1 = require("..");
const fs = require("fs");
const download = require("download");
const hasha = require("hasha");
const StreamZip = require("node-stream-zip");
const RecursiveFolderFile_1 = require("../Utils/RecursiveFolderFile");
const crc32_1 = require("crc/crc32");
const LibsInformations_1 = require("../Utils/LibsInformations");
const FormatColor_1 = require("../Logger/FormatColor");
const fetch = (...args) => Promise.resolve().then(() => require('node-fetch')).then(({ default: fetch }) => fetch(...args));
class VanillaUpdater {
constructor(gameVersion, dir) {
this.gameVersion = gameVersion;
this.dir = dir;
this.MANIFEST_URL = "https://launchermeta.mojang.com/mc/game/version_manifest.json";
this.ASSETS_URL = "https://resources.download.minecraft.net/";
this.totalDownloadedFiles = 0;
this.allFiles = [];
this.libsLoad = [];
}
getGameProperties() {
return this.gameProperties;
}
/**
* get
*/
setManisfest() {
return __awaiter(this, void 0, void 0, function* () {
let settings = { method: "get" };
const resJson = yield fetch(this.MANIFEST_URL, settings).then(res => res.json());
this.gameProperties = yield Promise.all(resJson.versions
.filter(version => version.id === this.gameVersion.versionManifest)
.map(ver => fetch(ver.url, settings).then(res => res.json())));
this.assetIndex = yield fetch(this.gameProperties[0].assetIndex.url, settings).then(res => res.json());
});
}
updateGame() {
return __awaiter(this, void 0, void 0, function* () {
if (!fs.existsSync(this.dir.getGameDirectory())) {
fs.mkdirSync(this.dir.getGameDirectory(), { recursive: true });
}
__1.Logger.getLogger().print("Total File : " + FormatColor_1.TextColor.GREEN + RecursiveFolderFile_1.RecursiveFolderFile.getAllFiles(this.dir.getGameDirectory()).length);
yield this.setManisfest();
yield this.downloadsLibrariesFiles();
yield this.downloadAssetsFiles();
yield this.downloadClientJarFiles();
__1.Logger.getLogger().print("Downloaded : " + FormatColor_1.TextColor.GREEN + this.totalDownloadedFiles);
});
}
getMainClass() {
return this.gameProperties[0].mainClass;
}
downloadAssetsFiles() {
return __awaiter(this, void 0, void 0, function* () {
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 });
}
yield Promise.all(downloadFilesList.map((fileInfo) => __awaiter(this, void 0, void 0, function* () {
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));
yield 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)))));
yield download(this.gameProperties[0].assetIndex.url, path.join(this.dir.getAssetDirectory(), "indexes"));
});
}
downloadsLibrariesFiles() {
return __awaiter(this, void 0, void 0, function* () {
var downloadFilesList = [];
yield Promise.all(this.gameProperties[0].libraries.map((val) => __awaiter(this, void 0, void 0, function* () {
var LibsInfo = new LibsInformations_1.LibsInformations(val.name);
if (val['natives'] != undefined) {
if (val['natives']['windows'] != undefined) {
var nativesUsed = val['natives']['windows'];
nativesUsed = nativesUsed.replace("${arch}", "64");
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 index = downloadFilesList.findIndex(file => new LibsInformations_1.LibsInformations(file.name).getName() == LibsInfo.getName());
//if(index == -1 || LibsInfo.compareVersion(new LibsInformations(downloadFilesList[index].name).getVersion())){
var downloaded = yield this.checkDownloadFiles(val['downloads']['classifiers'][nativesUsed]['url'], val['downloads']['classifiers'][nativesUsed]['sha1'], filePath);
if (downloaded)
yield 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 });
this.libsLoad.push(path.join(this.dir.getLibsDirectory(), (val.downloads.artifact.url).substring(val.downloads.artifact.url.lastIndexOf("/") + 1)));
}
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 };
this.libsLoad.push(path.join(this.dir.getLibsDirectory(), (val.downloads.artifact.url).substring(val.downloads.artifact.url.lastIndexOf("/") + 1)));
}
}
}
})));
yield Promise.all(downloadFilesList.map((fileInfo) => __awaiter(this, void 0, void 0, function* () {
yield this.checkDownloadFiles(fileInfo.url, fileInfo.hash, path.join(this.dir.getLibsDirectory(), path.basename(fileInfo.url)));
})));
});
}
downloadClientJarFiles() {
return __awaiter(this, void 0, void 0, function* () {
//fs.writeFileSync(this.dir.getmainJar(), await download(this.gameProperties[0].downloads.client.url));
yield this.checkDownloadFiles(this.gameProperties[0].downloads.client.url, this.gameProperties[0].downloads.client.sha1, this.dir.getmainJar());
});
}
extractNatives(filePath) {
return __awaiter(this, void 0, void 0, function* () {
const zip = new StreamZip.async({ file: filePath });
const entries = yield 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 });
}
yield zip.extract(entry.name, this.dir.getNativesDirectory());
__1.Logger.getLogger().print("Extraction : " + FormatColor_1.TextColor.GREEN + entry.name);
}
//const count = await zip.extract(null, this.dir.getNativesDirectory());
yield zip.close();
});
}
checkDownloadFiles(url, hash, dist) {
return __awaiter(this, void 0, void 0, function* () {
this.allFiles.push(dist);
var isChanged = false;
if (!fs.existsSync(dist)) {
fs.mkdirSync(path.dirname(dist), { recursive: true });
fs.writeFileSync(dist, yield download(url));
isChanged = true;
__1.Logger.getLogger().print(FormatColor_1.TextColor.GREEN + dist);
this.totalDownloadedFiles++;
}
else {
if (hasha.fromFileSync(dist, { algorithm: 'sha1' }) != hash) {
fs.writeFileSync(dist, yield download(url));
isChanged = true;
__1.Logger.getLogger().print(FormatColor_1.TextColor.GREEN + dist);
this.totalDownloadedFiles++;
}
}
return isChanged;
});
}
getAllFiles() {
return this.allFiles;
}
}
exports.VanillaUpdater = VanillaUpdater;