gmll
Version:
A generic launcher core for building custom launchers
159 lines (158 loc) • 6.64 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.importModpack = exports.importCurseForge = exports.importGmllLink = void 0;
const index_1 = require("../../../index");
const config_js_1 = require("../../config.js");
const util_js_1 = require("../util.js");
const gfsl_1 = require("gfsl");
const downloader_js_1 = require("../../downloader.js");
const handler_js_1 = require("../../handler.js");
async function importGmllLink(url, name) {
const r = await fetch(url + "/.meta/api.json");
if (!r.ok)
throw "Could not find the api doc";
const v = (await r.json());
if (v.version > 2) {
throw "Incompatible version ID detected";
}
const manfile = (0, util_js_1.fsSanitizer)(v.name) + ".json";
const manifest = (await (0, config_js_1.getMeta)()
.manifests.getFile(manfile)
.download(url + "/.meta/manifest.json", { sha1: v.sha })).toJSON();
if (!name)
return manifest;
return new index_1.Instance({ version: manifest.id, name: name }).save();
}
exports.importGmllLink = importGmllLink;
async function importCurseForge(instance, urlorFile, forge) {
(0, config_js_1.emit)("debug.warn", "GMLL's support for curse modpacks is in an Alpha state!");
(0, config_js_1.emit)("debug.warn", "Use GMLLs native modpack api instead if you can");
(0, config_js_1.emit)("debug.warn", "Only fabric modpacks work properly atm.");
const tmp = (0, config_js_1.getMeta)().scratch.getDir("curse", instance.name).mkdir();
const metaInf = tmp.getFile("manifest.json");
const installedFile = instance.getDir().getFile("installed.txt");
const metaFile = (0, config_js_1.getMeta)().scratch.getFile("curse_meta.json");
const metaModData = metaFile.exists() ? metaFile.toJSON() : {};
if (installedFile.exists() && metaInf.exists()) {
const inf = metaInf.toJSON();
instance.version = "curse." + inf.name + "-" + inf.version;
(0, config_js_1.emit)("debug.info", "Installed files found, assuming file was installed!");
return instance;
}
let file;
if (typeof urlorFile == "string") {
file = instance.getDir().getFile("modpack.zip");
await file.download(urlorFile);
}
else {
file = urlorFile;
}
(0, config_js_1.emit)("debug.info", "Extracting achive");
await file.unzip(tmp);
const inf = metaInf.toJSON();
(0, config_js_1.emit)("debug.info", "Applying overides");
function copyFile(fToCopy, base) {
if (fToCopy instanceof gfsl_1.File) {
const file = base.getFile(fToCopy.getName()).rm();
fToCopy.moveTo(file);
}
else {
fToCopy.ls().forEach((e) => copyFile(e, base.getDir(fToCopy.getName())));
}
}
tmp
.getDir(inf.overrides)
.ls()
.forEach((e) => copyFile(e, instance.getDir()));
let mcVersion = inf.minecraft.version;
if (forge) {
mcVersion = await instance.installForge(forge);
}
else {
if (inf.minecraft.modLoaders.length > 1)
(0, config_js_1.emit)("debug.warn", "GMLL may not support multi modloader setups are currently not recommended!");
for (const e of inf.minecraft.modLoaders) {
const data = e.id.split("-");
const type = data[0];
const version = data[1];
if (["fabric", "quilt"].includes(type)) {
mcVersion = `${type}-loader-${version}-${inf.minecraft.version}`;
}
else if ("forge" == type) {
const forgeVersions = await (0, handler_js_1.getForgeVersions)();
const forgeVersion = forgeVersions[mcVersion].find((e) => e.forge.endsWith(version));
if (!forgeVersion)
throw "Forge version not found!";
await forgeVersion.install();
mcVersion = forgeVersion.game;
}
else {
(0, config_js_1.emit)("debug.warn", "Unsupported modloader type " +
e.id +
"\nGMLL is not natively compatible with the method Curse uses to install forge.");
}
}
}
const mods = instance.getDir().getDir("mods").mkdir();
const fileNames = [];
const files = [];
inf.files.forEach((f) => {
const name = `${f.projectID}-${f.fileID}`;
fileNames.push(name);
const meta = metaModData[name];
let fname = name;
if (meta && meta.id && meta.sha1 && meta.size)
fname = meta.id + "-" + meta.sha1.slice(0, 5) + meta.size.toString(36);
files.push({
name: fname + ".jar",
path: [instance.getDir().sysPath(), "mods"],
url: f.downloadUrl ||
`https://www.curseforge.com/api/v1/mods/${f.projectID}/files/${f.fileID}/download`,
key: meta?.id || name,
chk: meta || {},
});
});
await (0, downloader_js_1.download)(files);
instance.version = mcVersion;
const err = (await instance.getMetaPaths()).mods.getDir("unparsable").mkdir();
(await instance.getMods()).forEach((e) => {
const fileName = e.path.getName();
if (fileNames.includes(fileName)) {
if (e.error) {
if (e.loader == "unknown")
e.path.moveTo(err.getFile(e.path.getName()));
return;
}
const sha1 = e.path.getHash();
const size = e.path.getSize();
metaModData[fileName] = {
sha1,
size,
id: e.id + "-" + e.version,
};
const nfile = mods.getFile((0, util_js_1.fsSanitizer)(e.id + "-" + e.version + "-" + sha1.slice(0, 5) + size.toString(36)) + ".jar");
e.path.moveTo(nfile);
}
});
metaFile.write(metaModData);
}
exports.importCurseForge = importCurseForge;
async function importModpack(urlorFile, type, forge) {
switch (type) {
case "curseforge":
await importCurseForge(this, urlorFile, forge);
break;
case "gmll":
if (forge)
(0, config_js_1.emit)("debug.warn", "The forge property goes unused in this mode!");
if (typeof urlorFile == "string")
this.version = (await importGmllLink(urlorFile)).id;
else
(0, config_js_1.emit)("debug.warn", "Only URLS are supported");
break;
default:
(0, config_js_1.emit)("debug.error", "Unsupported modpack type!");
}
return this;
}
exports.importModpack = importModpack;
;