feat: allow to save version's json and jar

This commit is contained in:
HerozDotExe 2025-11-11 12:36:33 +01:00
parent f91f3dd4c8
commit d47c546a4a
10 changed files with 74 additions and 32 deletions

View file

@ -73,12 +73,16 @@ function parseArg(
return this;
}
function generateClassPaths(versionManifest: Version, librariesRoot: string) {
function generateClassPaths(
versionManifest: Version,
librariesRoot: string,
versionJar: string,
) {
const libs = getLibraries(versionManifest, librariesRoot).map(
(lib) => lib.path,
);
libs.push(path.join(librariesRoot, "..", "versions", `${versionManifest.id}.jar`))
libs.push(versionJar);
return libs.join(path.delimiter);
}
@ -87,6 +91,7 @@ export async function generateLaunchArguments(
versionManifest: Version,
javaRoot: string,
gameRoot: string,
versionJar: string,
auth: Auth,
options = {
minRam: "2G",
@ -103,6 +108,7 @@ export async function generateLaunchArguments(
const classPaths = generateClassPaths(
versionManifest,
path.join(gameRoot, "libraries"),
versionJar,
);
function p(args: string[], arg: Argument) {
@ -116,7 +122,7 @@ export async function generateLaunchArguments(
p(game, arg);
}
const log4j = await getArgument(versionManifest, gameRoot)
const log4j = await getArgument(versionManifest, gameRoot);
return `${getJavaExecutable(javaRoot)} ${jvm.join(" ")}${options.customJvm === "" ? "" : ` ${options.customJvm}`} -Xms${options.minRam} -Xmx${options.maxRam} -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M ${log4j} ${versionManifest.mainClass} ${game.join(" ")}`;
}

View file

@ -1,9 +1,9 @@
import { downloadFile, fetchJson } from "../utils/fetch";
import { exists } from "../utils/fs";
import { exists, readJson } from "../utils/fs";
import { Versions, Version } from "../utils/types";
import path from "path";
export async function getVersionManifest(versionString: string) {
export async function getVersionManifestUrl(versionString: string) {
const versions = await (
await fetchJson<Versions>(
"https://piston-meta.mojang.com/mc/game/version_manifest_v2.json",
@ -20,20 +20,36 @@ export async function getVersionManifest(versionString: string) {
}
}
const versionManifest = await fetchJson<Version>(versionManifestURL);
return versionManifest;
return versionManifestURL;
}
export async function downloadJar(versionManifest: Version, gameRoot: string) {
const destination = path.join(
gameRoot,
"versions",
`${versionManifest.id}.jar`,
);
export async function downloadJson(versionString: string, to: string) {
const destination = path.join(to, `${versionString}.json`);
if (!(await exists(destination)))
await downloadFile({
url: await getVersionManifestUrl(versionString),
path: destination,
});
}
export async function downloadJar(versionManifest: Version, to: string) {
const destination = path.join(to, `${versionManifest.id}.jar`);
if (!(await exists(destination)))
await downloadFile({
url: versionManifest.downloads.client.url,
path: destination,
});
}
export async function getVersionManifest(
versionString: string,
to: string,
): Promise<Version> {
const destination = path.join(to, `${versionString}.json`);
if (await exists(destination)) {
return await readJson(destination);
}
await downloadJson(versionString, to);
return await readJson(destination);
}

View file

@ -15,3 +15,7 @@ export async function ensureDir(path: string, recursive = false) {
return true;
} else return false;
}
export async function readJson(destination: string) {
return JSON.parse(await fs.readFile(destination, { encoding: "utf-8" }));
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -4,6 +4,7 @@ import path from "path";
import fs from "fs/promises";
const assetsPath = path.join(import.meta.dirname, "temp/assets");
const root = path.join(import.meta.dirname, "temp");
await fs.rm(assetsPath, { recursive: true, force: true });
await fs.mkdir(assetsPath, { recursive: true });
@ -23,7 +24,7 @@ test(
"download assets for 1.21.8 correctly",
{ timeout: 0 },
async () => {
const versionManifest = await nlk.core.version.getVersionManifest("1.21.8");
const versionManifest = await nlk.core.version.getVersionManifest("1.21.8", root);
const assetsDownloader = await nlk.core.AssetsDownloader(assetsPath, versionManifest);
assetsDownloader.on("completed", () => {
console.log(

View file

@ -4,6 +4,7 @@ import path from "path";
import fs from "fs/promises";
const librariesPath = path.join(import.meta.dirname, "temp/libraries");
const root = path.join(import.meta.dirname, "temp");
await fs.rm(librariesPath, { recursive: true, force: true });
await fs.mkdir(librariesPath, { recursive: true });
@ -23,7 +24,7 @@ test(
"download libraries for 1.21.8 correctly",
{ timeout: 0 },
async () => {
const versionManifest = await nlk.core.version.getVersionManifest("1.21.8");
const versionManifest = await nlk.core.version.getVersionManifest("1.21.8", root);
const librariesDownloader = await nlk.core.LibrariesDownloader(librariesPath, versionManifest);
librariesDownloader.on("completed", () => {
console.log(

View file

@ -19,7 +19,7 @@ test(
"download xml file and return correct arguments",
{ timeout: 0 },
async () => {
const versionManifest = await nlk.core.version.getVersionManifest("1.21.8");
const versionManifest = await nlk.core.version.getVersionManifest("1.21.8", root);
const arg = await nlk.core.log4j.getArgument(versionManifest, root);
expect(arg, "correct arg returned").toBe(

View file

@ -4,6 +4,7 @@ import path from "path";
import fs from "fs/promises";
const nativesPath = path.join(import.meta.dirname, "temp/natives");
const root = path.join(import.meta.dirname, "temp");
await fs.rm(nativesPath, { recursive: true, force: true });
await fs.mkdir(nativesPath, { recursive: true });
@ -21,7 +22,7 @@ vi.stubGlobal("process", { platform: "linux" });
// old version because newer versions doesn't have natives
test("download natives for 1.15 correctly", { timeout: 0 }, async () => {
const versionManifest = await nlk.core.version.getVersionManifest("1.15");
const versionManifest = await nlk.core.version.getVersionManifest("1.15", root);
const nativesDownloader = await nlk.core.NativesDownloader(
nativesPath,
versionManifest,

View file

@ -3,9 +3,13 @@ import * as nlk from "../dist/index.js";
import path from "path";
import fs from "fs/promises";
const versionsPath = path.join(import.meta.dirname, "temp/versions");
await fs.rm(versionsPath, { recursive: true });
await fs.mkdir(versionsPath, { recursive: true });
const root = path.join(import.meta.dirname, "temp");
try {
await fs.rm(path.join(root, "1.21.8.json"));
await fs.rm(path.join(root, "1.21.8.jar"));
} catch (e) {
console.warn(e)
}
async function exists(path: string) {
try {
@ -20,14 +24,18 @@ async function exists(path: string) {
vi.stubGlobal("process", { platform: "linux" });
test("parse arguments correctly for 1.21.8", { timeout: 0 }, async () => {
const versionManifest = await nlk.core.version.getVersionManifest("1.21.8");
await nlk.core.version.downloadJar(
versionManifest,
path.join(versionsPath, ".."),
const versionManifest = await nlk.core.version.getVersionManifest(
"1.21.8",
root,
);
await nlk.core.version.downloadJar(versionManifest, root);
expect(
await exists(path.join(versionsPath, `${versionManifest.id}.jar`)),
await exists(path.join(root, `${versionManifest.id}.jar`)),
"check jar file",
).toBe(true);
expect(
await exists(path.join(root, `${versionManifest.id}.json`)),
"check json file",
).toBe(true);
});