Implemented NeoForge metadata reader

Closes #85
This commit is contained in:
Kir_Antipov 2024-01-15 17:01:23 +03:00
parent 9cb5eb1085
commit 6adbc23acb
3 changed files with 66 additions and 1 deletions

View file

@ -5,6 +5,7 @@ import { ForgeMetadataReader } from "./forge/forge-metadata-reader";
import { LoaderMetadata } from "./loader-metadata"; import { LoaderMetadata } from "./loader-metadata";
import { LoaderType } from "./loader-type"; import { LoaderType } from "./loader-type";
import { QuiltMetadataReader } from "./quilt/quilt-metadata-reader"; import { QuiltMetadataReader } from "./quilt/quilt-metadata-reader";
import { NeoForgeMetadataReader } from "./neoforge/neoforge-metadata-reader";
/** /**
* Defines a structure for reading metadata files. * Defines a structure for reading metadata files.
@ -61,12 +62,14 @@ export function createLoaderMetadataReader(loader: LoaderType): LoaderMetadataRe
return new FabricMetadataReader(); return new FabricMetadataReader();
case LoaderType.FORGE: case LoaderType.FORGE:
case LoaderType.NEOFORGE:
return new ForgeMetadataReader(); return new ForgeMetadataReader();
case LoaderType.QUILT: case LoaderType.QUILT:
return new QuiltMetadataReader(); return new QuiltMetadataReader();
case LoaderType.NEOFORGE:
return new NeoForgeMetadataReader();
default: default:
throw new Error(`Unknown mod loader '${LoaderType.format(loader)}'.`); throw new Error(`Unknown mod loader '${LoaderType.format(loader)}'.`);
} }

View file

@ -0,0 +1,25 @@
import { PathLike } from "node:fs";
import { parse as parseToml } from "toml";
import { readAllZippedText } from "@/utils/io/file-info";
import { LoaderType } from "../loader-type";
import { LoaderMetadataReader } from "../loader-metadata-reader";
import { NeoForgeMetadata } from "./neoforge-metadata";
import { MODS_TOML } from "./raw-neoforge-metadata";
/**
* A metadata reader that is able to read NeoForge mod metadata from a zipped file.
*/
export class NeoForgeMetadataReader implements LoaderMetadataReader<NeoForgeMetadata> {
/**
* @inheritdoc
*/
async readMetadataFile(path: PathLike): Promise<NeoForgeMetadata> {
const metadataText = await readAllZippedText(path, MODS_TOML);
const metadata = NeoForgeMetadata.from(parseToml(metadataText));
if (!metadata.dependencies.some(x => x.id === LoaderType.NEOFORGE)) {
throw new Error("A NeoForge metadata file must contain a 'neoforge' dependency");
}
return metadata;
}
}

View file

@ -0,0 +1,37 @@
import { zipFile } from "@/../tests/utils/zip-utils";
import mockFs from "mock-fs";
import { NeoForgeMetadata } from "@/loaders/neoforge/neoforge-metadata";
import { NeoForgeMetadataReader } from "@/loaders/neoforge/neoforge-metadata-reader";
beforeEach(async () => {
mockFs({
"neoforge.mod.jar": await zipFile([__dirname, "../../../content/neoforge/mods.toml"], "META-INF/mods.toml"),
"text.txt": "",
});
});
afterEach(() => {
mockFs.restore();
});
describe("NeoForgeMetadataReader", () => {
test("successfully reads mods.toml", async () => {
const reader = new NeoForgeMetadataReader();
const metadata = await reader.readMetadataFile("neoforge.mod.jar");
expect(metadata).toBeInstanceOf(NeoForgeMetadata);
});
test("throws if file is not a NeoForge mod", async () => {
const reader = new NeoForgeMetadataReader();
await expect(reader.readMetadataFile("text.txt")).rejects.toThrow();
});
test("throws if file does not exist", async () => {
const reader = new NeoForgeMetadataReader();
await expect(reader.readMetadataFile("text.json")).rejects.toThrow();
});
});