adds forge support, fixes some critical errors

This commit is contained in:
2023-10-08 13:38:00 -06:00
parent 6944cbb9f7
commit ad4629576c
21 changed files with 3484 additions and 132 deletions

View File

@@ -1,35 +1,41 @@
import { ensureFile } from "$std/fs/ensure_file.ts";
/**
*
* @param src url of file
* @param dest destination file. If `useFileName` is true, this should be the destination directory
* @param [useFileName] whether to use the inferred file name from `src`
*/
export async function downloadFile(src: string, dest: string, useFileName?:boolean) {
export async function downloadFile(
src: string,
dest: string,
useFileName?: boolean,
) {
if (!(src.startsWith("http://") || src.startsWith("https://"))) {
throw new TypeError("URL must start with be http:// or https://");
}
const fileName = src.split('/').at(-1);
const resp = await fetch(src);
if (!resp.ok) {
const fileName = src.split("/").at(-1);
const res = await fetch(src);
if (!res.ok) {
throw new Deno.errors.BadResource(
`Request failed with status ${resp.status}`,
`Request failed with status ${res.status}`,
);
} else if (!resp.body) {
} else if (!res.body) {
throw new Deno.errors.UnexpectedEof(
`The download url ${src} doesn't contain a file to download`,
);
} else if (resp.status === 404) {
} else if (res.status === 404) {
throw new Deno.errors.NotFound(
`The requested url "${src}" could not be found`,
);
}
const blob = await res.blob();
const buffer = await blob.arrayBuffer();
const uint8array = new Uint8Array(buffer);
await ensureFile(useFileName ? dest + fileName : dest);
const file = await Deno.open(dest, { truncate: true, write: true });
resp.body.pipeTo(file.writable);
}
await Deno.writeFile(dest, uint8array);
}

15
util/forge.ts Normal file
View File

@@ -0,0 +1,15 @@
export const getForgeDownload = async (version: string, latest?: boolean) => {
const res = await fetch(
`https://files.minecraftforge.net/net/minecraftforge/forge/promotions_slim.json`,
);
const { promos } = await res.json();
// https://maven.minecraftforge.net/net/minecraftforge/forge/1.20.1-47.2.1/forge-1.20.1-47.2.1-installer.jar
let loaderVersion = promos[`${version}-${latest ? "latest" : "recommended"}`];
if (!loaderVersion) loaderVersion = promos[`${version}-latest`];
return `https://maven.minecraftforge.net/net/minecraftforge/forge/${version}-${loaderVersion}/forge-${version}-${loaderVersion}-installer.jar`;
};

18
util/gameVersions.ts Normal file
View File

@@ -0,0 +1,18 @@
import { ensureFile } from "$std/fs/ensure_file.ts";
import { Modrinth, ModrinthGameVersion } from "../lib/modrinth.ts";
export const getGameVersions = async (releaseOnly?: boolean) => {
const cacheFile = "./cache/gameVersions.json";
await ensureFile(cacheFile);
let versions: ModrinthGameVersion[] = JSON.parse(
await Deno.readTextFile(cacheFile) || "[]",
);
if (!versions.length) {
versions = await Modrinth.getGameVersions();
await Deno.writeTextFile(cacheFile, JSON.stringify(versions));
}
return versions.filter(v => !releaseOnly || v.major);
};