gmll
Version:
A generic launcher core for building custom launchers
254 lines (253 loc) • 10.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const gfsl_1 = require("gfsl");
const path_1 = require("path");
const config_js_1 = require("../config.js");
const handler_js_1 = require("../handler.js");
const launchHandler = tslib_1.__importStar(require("../internal/handlers/launch.js"));
const metaHandler = tslib_1.__importStar(require("../internal/handlers/meta.js"));
const modpacks_js_1 = require("../internal/handlers/modpacks.js");
const modsHandler = tslib_1.__importStar(require("../internal/handlers/mods.js"));
const util_js_1 = require("../internal/util.js");
const version_js_1 = tslib_1.__importDefault(require("./version.js"));
/**
* An instance is what the name entails. An instance of the game Minecraft containing Minecraft specific data.
* This information on where the game is stored and the like. The mods installed and what not.
*/
class Instance {
constructor(opt = {}) {
/**Gets the load order of minecraft jars in jar mod loader. */
this.getJarModPriority = modsHandler.getJarModPriority;
/**Install forge in this instance. */
this.installForge = modsHandler.installForge;
this.getForgeVersions = modsHandler.getForgeVersions;
/**An version of the wrap function that takes an object as a variable instead of the mess the base function takes. */
this.pack = modsHandler.pack;
/**Wraps up an instance in a prepackaged format that can be easily uploaded to a server for distribution
* @param baseUrl The base URL the generated files will be stored within on your server. For example http\:\/\/yourawesomdomain.net\/path\/to\/files\/
* @param save The file GMLL will generate the final files on.
* @param name The name that should be used to identify the generated version files
* @param forge The path to a forge installation jar
* @param trimMisc Gets rid of any unnecessary miscellaneous files
* @deprecated Use {@link pack} instead
*/
this.wrap = modsHandler.wrap;
/**
* @returns Some low level meta paths used to obtain some key files of this instance.
*/
this.getMetaPaths = metaHandler.getMetaPaths;
/**
* Gets information about mods in this instance. This includes the loader version plus some general
* information about the mod author and mod itself. This will also provide you the icon for a set mod if it can be obtained.\
*
* Works with Legacy forge, forge, fabric, riftloader and liteloader
*/
this.getMods = metaHandler.getMods;
/**
* Gets information about the installed resource and texture packs of this instance.
* This includes information like the pack icon, name, description, legal documents and credits.
*/
this.getResourcePacks = metaHandler.getResourcePacks;
/**
* Gets some general information about all the world files in this instance.
* It also decodes the level.DAT file for you and returns the decoded file as a JSON file.
*
* It also decodes the player data stored in the "playerdata" and "stats" subfolder in newer versions of the game.
*/
this.getWorlds = metaHandler.getWorlds;
/**
* This function is used to launch the game. It also runs the install script for you.
* This essentially does an integrity check.
* @param token The player login token
* @param resolution Optional information defining the game's resolution
* @returns The game's child process
*/
this.launch = launchHandler.launch;
/**
* Runs the installer script without launching MC
* @returns The instance's version object.
* @see {@link getVersion} if you just want the instance's version
*/
this.install = launchHandler.install;
//https://www.curseforge.com/api/v1/mods/829758/files/4661651/download
this.import = modpacks_js_1.importModpack;
this.version = opt.version || (0, handler_js_1.getLatest)().release;
this.name = opt.name || this.version;
this.path = opt.path || (0, path_1.join)("<instance>", (0, util_js_1.fsSanitizer)(this.name));
this.ram = opt.ram || 2;
this.meta = opt.meta || undefined;
this.assets = opt.assets || {};
this.javaPath = opt.javaPath || "default";
this.env = opt.env || {};
this.noLegacyFix = opt.noLegacyFix || false;
this.detach = opt.detach || false;
this.getDir().mkdir();
const MESA = "MESA_GL_VERSION_OVERRIDE";
if (!["x64", "arm64", "ppc64"].includes((0, util_js_1.getCpuArch)()) && this.ram > 1.4) {
(0, config_js_1.emit)("debug.warn", "Setting ram limit to 1.4GB due to running on a 32-bit version of java!");
this.ram = 1.4;
}
if (!(MESA in this.env) && process.platform == "linux") {
this.env[MESA] = "4.6";
}
}
getID() {
return this.id;
}
/**
*
* @returns An object containing the version data this instance is based on
* @see {@link install} if you want to initiate that version object first!
*/
async getVersion() {
return await version_js_1.default.get(this.version);
}
getDir() {
return new gfsl_1.Dir((0, config_js_1.resolvePath)(this.path));
}
/**Gets a list of profiles that where saved previously */
static getProfiles() {
const profiles = new Map();
(0, config_js_1.getMeta)()
.profiles.ls()
.forEach((e) => {
if (e instanceof gfsl_1.File && e.getName().endsWith(".json")) {
const profile = e.toJSON();
profiles.set(profile.name, {
...profile,
get: () => this.get(e.getName()),
});
}
});
return profiles;
}
/**Gets a set profile based on the name of that profile */
static get(profile) {
if (!profile.endsWith(".json"))
profile += ".json";
const _file = (0, config_js_1.getMeta)().profiles.getFile((0, util_js_1.fsSanitizer)(profile));
const json = _file.exists() ? _file.toJSON() : {};
return new Instance(json);
}
/**
* Deletes a profile based on the profileID
* @param profile
* @returns
*/
static rm(profile) {
if (!profile.endsWith(".json"))
profile += ".json";
const _file = (0, config_js_1.getMeta)().profiles.getFile((0, util_js_1.fsSanitizer)(profile));
return _file.rm();
}
/**
* Delete the saved information for this instance.
* @returns
*/
rmSelf() {
return Instance.rm(this.getID());
}
/**
* Saves the instance data. Can be used to automatically get the instance again by using it's name
* @see {@link get} for more info
*/
save() {
(0, config_js_1.getMeta)()
.profiles.getFile((0, util_js_1.fsSanitizer)(this.name + ".json"))
.write(this);
return this;
}
/**
* This will tell GMLL to rerun some of the install scripts it normally skips upon a second "install" call.
* This won't reset worlds or rewrite dynamic files. Use this if, for instance, forge failed to install.
*/
reinstall() {
this.getDir().getFile(".installed.txt").rm();
}
/**Injects a set selection of images into the asset files and sets them as the icon for this instance */
setIcon(x32, x16, mac) {
if (x32) {
const x32Icon = this.injectAsset("icons/icon_32x32.png", x32);
this.assets.objects["minecraft/icons/icon_32x32.png"] = x32Icon;
}
if (x16) {
const x16Icon = this.injectAsset("icons/icon_16x16.png", x16);
this.assets.objects["minecraft/icons/icon_16x16.png"] = x16Icon;
}
if (mac) {
const macIcon = this.injectAsset("icons/minecraft.icns", mac);
this.assets.objects["minecraft/icons/minecraft.icns"] = macIcon;
}
}
/**
* Inject custom assets into the game.
* @param key The asset key
* @param path The path to the asset file in questions...it must exist!
*/
injectAsset(key, path) {
if (typeof path == "string")
path = new gfsl_1.File(path);
if (!path.exists())
(0, util_js_1.throwErr)("Cannot find file");
const hash = path.getHash();
path.copyTo((0, util_js_1.assetTag)((0, config_js_1.getAssets)().getDir("objects"), hash).getFile(hash));
if (!this.assets.objects)
this.assets.objects = {};
const asset = { hash: hash, size: path.getSize(), ignore: true };
this.assets.objects[key] = asset;
return asset;
}
getName() {
return this.name;
}
static async import(name, urlorFile, type, forge) {
return new this({ name }).import(urlorFile, type, forge);
}
}
exports.default = Instance;
/**Additional arguments added for legacy versions */
Instance.oldJVM = [
"-Djava.util.Arrays.useLegacyMergeSort=true",
"-Dminecraft.applet.TargetDirectory=${game_directory}",
];
/**The default game arguments, don't mess with these unless you know what you are doing */
Instance.defaultGameArguments = [
"-Xmx${ram}M",
"-XX:+UnlockExperimentalVMOptions",
"-XX:+UseG1GC",
"-XX:G1NewSizePercent=20",
"-XX:G1ReservePercent=20",
"-XX:MaxGCPauseMillis=50",
"-XX:G1HeapRegionSize=32M",
"-Dlog4j2.formatMsgNoLookups=true",
];
/**Do not mess with unless you know what you're doing. Some older versions may not launch if information from this file is missing. */
Instance.defJVM = [
{
rules: [{ action: "allow", os: { name: "windows" } }],
value: "-XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump",
},
{
rules: [{ action: "allow", os: { name: "windows", version: "^10\\." } }],
value: ["-Dos.name=Windows 10", "-Dos.version=10.0"],
},
{ rules: [{ action: "allow", os: { arch: "x86" } }], value: "-Xss1M" },
"-Djava.library.path=${natives_directory}",
"-Dminecraft.launcher.brand=${launcher_name}",
"-Dminecraft.launcher.version=${launcher_version}",
"-Dminecraft.client.jar=${game_jar}",
"-cp",
"${classpath}",
{
rules: [{ action: "allow", os: { name: "osx" } }],
value: ["-Xdock:name=${mac_name}", "-Xdock:icon=${mac_icon}"],
},
];
/**
* Used to modify minecraft's jar file (Low level)
* @param metaPaths
* @param version
* @returns
*/
Instance.jarMod = modsHandler.jarMod;