@kiwigdc/kiwilaunch
Version:
Launch minecraft client
173 lines (172 loc) • 8.7 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());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ForgeUpdater = void 0;
const __1 = require("..");
const fs = require("fs");
const download = require("download");
const path = require("path");
const hasha = require("hasha");
const StreamZip = require("node-stream-zip");
const LibsInformations_1 = require("../Utils/LibsInformations");
const child_process_1 = require("child_process");
const FormatColor_1 = require("../Logger/FormatColor");
const fetch = (...args) => Promise.resolve().then(() => require('node-fetch')).then(({ default: fetch }) => fetch(...args));
class ForgeUpdater {
constructor(gameVersion, dir) {
this.gameVersion = gameVersion;
this.dir = dir;
this.FORGE_URL = "https://maven.minecraftforge.net/net/minecraftforge/forge/";
this.patchedClient = false;
this.allFiles = [];
this.mods = [];
}
addMods(mods) {
this.mods = this.mods.concat(mods);
}
addMod(mod) {
this.mods.push(mod);
}
addModWithUrl(url) {
return __awaiter(this, void 0, void 0, function* () {
let json = yield fetch("https://node1.kiwigdc.fr/test.json", { method: "get" }).then((response) => {
return response.json();
});
this.mods = this.mods.concat(json);
});
}
setManisfest() {
return __awaiter(this, void 0, void 0, function* () {
this.gameProperties = [];
let fileProperties = path.join(this.dir.getGameDirectory(), 'version.json');
if (!fs.existsSync(fileProperties)) {
throw new Error("Updater Error : Version not found !");
}
this.forgeProperties = require(fileProperties);
});
}
isForgeInstalled() {
return __awaiter(this, void 0, void 0, function* () {
return fs.existsSync(path.join(this.dir.getLibsDirectory(), "forge-" + this.gameVersion.getMcVer() + "-" + this.gameVersion.getForgeVer() + ".jar"));
});
}
updateGame() {
return __awaiter(this, void 0, void 0, function* () {
if (yield this.isForgeInstalled()) {
__1.Logger.getLogger().print("Forge installed, verification...");
yield this.setManisfest();
}
yield this.downloadsForgeFiles();
yield this.downloadsLibrariesFiles();
yield this.downloadMods();
//await this.patchClient(); not working, use the forge .jar ?
});
}
downloadMods() {
return __awaiter(this, void 0, void 0, function* () {
__1.Logger.getLogger().print("Download mods files : ");
yield Promise.all(this.mods.map((mod) => __awaiter(this, void 0, void 0, function* () {
yield this.checkDownloadFiles(mod.url, mod.sha1, path.join(this.dir.getGameDirectory(), "mods", path.basename(mod.url)));
})));
});
}
downloadAssetsFiles() {
return;
}
downloadsForgeFiles() {
var e_1, _a;
return __awaiter(this, void 0, void 0, function* () {
__1.Logger.getLogger().print("Download forge file");
var url = this.FORGE_URL + this.gameVersion.getMcVer() + "-" + this.gameVersion.getForgeVer() + "/forge-" + this.gameVersion.getMcVer() + "-" + this.gameVersion.getForgeVer() + "-installer.jar";
var file = path.join(this.dir.getGameDirectory(), "forge-installer.jar");
yield this.checkDownloadFiles(url, "", file);
__1.Logger.getLogger().print("Forge file :" + FormatColor_1.TextColor.GREEN + file + FormatColor_1.TextFormat.RESET + "| Url : " + FormatColor_1.TextColor.GREEN + url);
(0, child_process_1.spawn)("java", ["-jar", file, "--extract", this.dir.getLibsDirectory()], { cwd: this.dir.gameDir });
__1.Logger.getLogger().print("Forge extraction");
this.allFiles.push(path.join(this.dir.getLibsDirectory(), "forge-" + this.gameVersion.getMcVer() + "-" + this.gameVersion.getForgeVer() + ".jar"));
const zip = new StreamZip.async({ file: file });
const entries = yield zip.entries();
try {
for (var _b = __asyncValues(Object.values(entries)), _c; _c = yield _b.next(), !_c.done;) {
const entry = _c.value;
if (entry.isFile && entry.name == "version.json") {
yield zip.extract(entry.name, this.dir.getGameDirectory());
__1.Logger.getLogger().print("Extraction : " + FormatColor_1.TextColor.GREEN + entry.name);
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
//const count = await zip.extract(null, this.dir.getNativesDirectory());
yield zip.close();
this.setManisfest();
});
}
downloadsLibrariesFiles() {
return __awaiter(this, void 0, void 0, function* () {
var downloadFilesList = [];
yield this.forgeProperties.libraries.forEach((val) => {
var LibsInfo = new LibsInformations_1.LibsInformations(val.name);
if (val.downloads.artifact != undefined) {
if (val.downloads.artifact.url != "")
downloadFilesList.push({ url: val.downloads.artifact.url, name: val.name, hash: val.downloads.artifact.sha1 });
}
});
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)));
})));
});
}
extractNatives(filePath) {
return;
}
downloadClientJarFiles() {
return;
}
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 (hash != "" && 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.ForgeUpdater = ForgeUpdater;