Better NeoForge support

Closes #85
This commit is contained in:
Kir_Antipov 2024-01-03 21:30:24 +03:00
parent b34393cfcd
commit 0a3874adee
9 changed files with 299 additions and 177 deletions

View file

@ -101,6 +101,7 @@ const IGNORED_DEPENDENCIES: readonly string[] = [
"minecraft",
"java",
"forge",
"neoforge",
];
/**

View file

@ -5,6 +5,7 @@ import { PlatformType } from "@/platforms";
import { PartialRecord } from "@/utils/types";
import { deprecate } from "node:util";
import { RawForgeMetadata } from "./raw-forge-metadata";
import { getForgeDependencies } from "./forge-dependency";
import { asString } from "@/utils/string-utils";
// _ TODO: Remove the deprecated stuff in v4.0.
@ -81,7 +82,12 @@ const getLegacyForgeMetadataCustomPayload = deprecate(
/**
* A list of default mod loaders associated with the Forge loader.
*/
const DEFAULT_LOADERS = [LoaderType.FORGE] as const;
const DEFAULT_FORGE_LOADERS = [LoaderType.FORGE] as const;
/**
* A list of default mod loaders associated with the NeoForge loader.
*/
const DEFAULT_NEOFORGE_LOADERS = [LoaderType.NEOFORGE] as const;
/**
* Gets an array of supported mod loaders from the custom payload attached to the Forge metadata.
@ -90,8 +96,14 @@ const DEFAULT_LOADERS = [LoaderType.FORGE] as const;
*
* @returns An array of supported mod loaders.
*/
export function getLoadersFromForgeMetadataCustomPayload(payload: ForgeMetadataCustomPayload): string[] {
return payload?.loaders || [...DEFAULT_LOADERS];
export function getLoadersFromForgeMetadataCustomPayload(metadata: RawForgeMetadata): string[] {
const payload = getForgeMetadataCustomPayload(metadata);
if (payload?.loaders) {
return payload.loaders;
}
const isNeoForge = getForgeDependencies(metadata).some(x => x.modId === LoaderType.NEOFORGE);
return isNeoForge ? [...DEFAULT_NEOFORGE_LOADERS] : [...DEFAULT_FORGE_LOADERS];
}
/**

View file

@ -63,7 +63,7 @@ export class ForgeMetadata implements LoaderMetadata {
* @inheritdoc
*/
get loaders(): string[] {
return getLoadersFromForgeMetadataCustomPayload(this.customPayload);
return getLoadersFromForgeMetadataCustomPayload(this._raw);
}
/**

View file

@ -59,6 +59,7 @@ export function createLoaderMetadataReader(loader: LoaderType): LoaderMetadataRe
return new FabricMetadataReader();
case LoaderType.FORGE:
case LoaderType.NEOFORGE:
return new ForgeMetadataReader();
case LoaderType.QUILT:

View file

@ -20,6 +20,11 @@ enum LoaderTypeValues {
* Quilt mod loader.
*/
QUILT = "quilt",
/**
* NeoForge mod loader.
*/
NEOFORGE = "neoforge",
}
/**

View file

@ -0,0 +1,73 @@
modLoader="javafml"
loaderVersion="[34,)"
issueTrackerURL="https://github.com/"
displayURL="https://github.com/"
authors="Author"
license="MIT"
[[mods]]
modId="example-mod"
version="0.1.0"
displayName="Example Mod"
description='''
Example mod
'''
[[dependencies.example-mod]]
modId="minecraft"
mandatory=true
versionRange="[1.17, 1.18)"
side="BOTH"
[[dependencies.example-mod]]
modId="neoforge"
mandatory=true
versionRange="[34,)"
ordering="NONE"
side="BOTH"
[[dependencies.example-mod]]
modId="java"
mandatory=true
versionRange="[16,)"
ordering="NONE"
side="BOTH"
[[dependencies.example-mod]]
modId="suggested-mod"
mandatory=false
versionRange="*"
ordering="NONE"
side="BOTH"
[dependencies.example-mod.mc-publish]
modrinth="BBBB"
curseforge=43
github="v0.3.0"
ignore=["curseforge"]
[[dependencies.example-mod]]
modId="included-mod"
mandatory=false
embedded=true
versionRange="*"
ordering="NONE"
side="BOTH"
[[dependencies.example-mod]]
modId="breaking-mod"
mandatory=false
incompatible=true
versionRange="*"
ordering="NONE"
side="BOTH"
[mc-publish]
modrinth="AANobbMI"
curseforge=394468
github="mc1.18-0.4.0-alpha5"
loaders=["forge", "forge2"]
dependencies=[
"recommended-mod@0.2.0(recommended){modrinth:AAAA}{curseforge:42}{github:v0.2.0}#(ignore)",
"conflicting-mod@<0.40.0(conflicting)",
]

View file

@ -6,6 +6,7 @@ import { ForgeMetadataReader } from "@/loaders/forge/forge-metadata-reader";
beforeEach(async () => {
mockFs({
"forge.mod.jar": await zipFile([__dirname, "../../../content/forge/mods.toml"], "META-INF/mods.toml"),
"neoforge.mod.jar": await zipFile([__dirname, "../../../content/neoforge/mods.toml"], "META-INF/mods.toml"),
"text.txt": "",
});
});
@ -15,7 +16,7 @@ afterEach(() => {
});
describe("ForgeMetadataReader", () => {
test("successfully reads mods.toml", async () => {
test("successfully reads forge/mods.toml", async () => {
const reader = new ForgeMetadataReader();
const metadata = await reader.readMetadataFile("forge.mod.jar");
@ -23,6 +24,14 @@ describe("ForgeMetadataReader", () => {
expect(metadata).toBeInstanceOf(ForgeMetadata);
});
test("successfully reads neoforge/mods.toml", async () => {
const reader = new ForgeMetadataReader();
const metadata = await reader.readMetadataFile("neoforge.mod.jar");
expect(metadata).toBeInstanceOf(ForgeMetadata);
});
test("returns undefined if file is not a Forge mod", async () => {
const reader = new ForgeMetadataReader();

View file

@ -5,217 +5,237 @@ import { DependencyType } from "@/dependencies/dependency-type";
import { PlatformType } from "@/platforms/platform-type";
import { RawForgeMetadata } from "@/loaders/forge/raw-forge-metadata";
import { ForgeMetadata } from "@/loaders/forge/forge-metadata";
import { LoaderType } from "@/loaders/loader-type";
const RAW_METADATA: RawForgeMetadata = Object.freeze(parseToml(
readFileSync(resolvePath(__dirname, "../../../content/forge/mods.toml"), "utf8")
));
function createRawMetadataEntry(loader: LoaderType): { loader: LoaderType, raw: RawForgeMetadata } {
const raw = Object.freeze(parseToml(
readFileSync(resolvePath(__dirname, `../../../content/${loader}/mods.toml`), "utf8")
));
return Object.freeze({ loader, raw });
}
const RAW_METADATA_ENTRIES = Object.freeze([
createRawMetadataEntry(LoaderType.FORGE),
createRawMetadataEntry(LoaderType.NEOFORGE),
]);
describe("ForgeMetadata", () => {
describe("from", () => {
test("constructs new ForgeMetadata instance using given raw metadata", () => {
const metadata = ForgeMetadata.from(RAW_METADATA);
describe.each(RAW_METADATA_ENTRIES)("$loader", ({ loader, raw }) => {
describe("from", () => {
test("constructs new ForgeMetadata instance using given raw metadata", () => {
const metadata = ForgeMetadata.from(raw);
expect(metadata).toBeInstanceOf(ForgeMetadata);
expect(metadata.raw).toBe(RAW_METADATA);
});
});
describe("id", () => {
test("returns id of the mod", () => {
const metadata = ForgeMetadata.from(RAW_METADATA);
expect(metadata.id).toBe("example-mod");
});
});
describe("name", () => {
test("returns name of the mod", () => {
const metadata = ForgeMetadata.from(RAW_METADATA);
expect(metadata.name).toBe("Example Mod");
});
});
describe("version", () => {
test("returns version of the mod", () => {
const metadata = ForgeMetadata.from(RAW_METADATA);
expect(metadata.version).toBe("0.1.0");
});
});
describe("loaders", () => {
test("returns 'forge' by default", () => {
const metadata = ForgeMetadata.from({} as RawForgeMetadata);
expect(metadata.loaders).toEqual(["forge"]);
expect(metadata).toBeInstanceOf(ForgeMetadata);
expect(metadata.raw).toBe(raw);
});
});
test("returns the same value as the 'loaders' field in the custom payload", () => {
const metadata = ForgeMetadata.from(RAW_METADATA);
describe("id", () => {
test("returns id of the mod", () => {
const metadata = ForgeMetadata.from(raw);
expect(metadata.loaders).toEqual(["forge", "forge2"]);
});
});
describe("gameName", () => {
test("always returns 'minecraft'", () => {
expect(ForgeMetadata.from({} as RawForgeMetadata).gameName).toBe("minecraft");
expect(ForgeMetadata.from(RAW_METADATA).gameName).toBe("minecraft");
});
});
describe("gameVersions", () => {
test("returns an empty array if no dependencies were specified", () => {
const metadata = ForgeMetadata.from({} as RawForgeMetadata);
expect(metadata.gameVersions).toEqual([]);
expect(metadata.id).toBe("example-mod");
});
});
test("returns the same value as the 'minecraft' dependency", () => {
const metadata = ForgeMetadata.from({ dependencies: { "example-mod": [{ modId: "minecraft", versionRange: "[1.16.5,)" }] } } as unknown as RawForgeMetadata);
describe("name", () => {
test("returns name of the mod", () => {
const metadata = ForgeMetadata.from(raw);
expect(metadata.gameVersions).toEqual(["[1.16.5,)"]);
expect(metadata.name).toBe("Example Mod");
});
});
test("returns the same values as the 'minecraft' dependency", () => {
const metadata = ForgeMetadata.from(RAW_METADATA);
describe("version", () => {
test("returns version of the mod", () => {
const metadata = ForgeMetadata.from(raw);
expect(metadata.gameVersions).toEqual(["[1.17, 1.18)"]);
});
});
describe("dependencies", () => {
test("returns an empty array if no dependencies were specified", () => {
const metadata = ForgeMetadata.from({} as RawForgeMetadata);
expect(metadata.dependencies).toEqual([]);
expect(metadata.version).toBe("0.1.0");
});
});
test("returns dependencies if they were specified", () => {
const metadata = ForgeMetadata.from({ dependencies: { "example-mod": [{ modId: "breaking-mod", versionRange: "*", incompatible: true }] } } as unknown as RawForgeMetadata);
describe("loaders", () => {
test(`returns '${loader}' by default`, () => {
const rawWithoutLoadersField = {
...raw,
"mc-publish": {
...raw["mc-publish"],
loaders: undefined,
},
};
const dependencies = metadata.dependencies;
const metadata = ForgeMetadata.from(rawWithoutLoadersField);
expect(dependencies).toHaveLength(1);
expect(dependencies[0]).toMatchObject({ id: "breaking-mod", versions: ["*"], type: DependencyType.INCOMPATIBLE });
expect(metadata.loaders).toEqual([loader]);
});
test("returns the same value as the 'loaders' field in the custom payload", () => {
const metadata = ForgeMetadata.from(raw);
expect(metadata.loaders).toEqual(["forge", "forge2"]);
});
});
test("regular dependencies have no aliases", () => {
const metadata = ForgeMetadata.from(RAW_METADATA);
describe("gameName", () => {
test("always returns 'minecraft'", () => {
expect(ForgeMetadata.from({} as RawForgeMetadata).gameName).toBe("minecraft");
expect(ForgeMetadata.from(raw).gameName).toBe("minecraft");
});
});
const dependencies = metadata.dependencies;
const regularDependencies = ["included-mod", "conflicting-mod", "breaking-mod"].map(id => dependencies.find(x => x.id === id));
describe("gameVersions", () => {
test("returns an empty array if no dependencies were specified", () => {
const metadata = ForgeMetadata.from({} as RawForgeMetadata);
for (const dependency of regularDependencies) {
for (const platform of PlatformType.values()) {
expect(dependency?.getProjectId(platform)).toBe(dependency.id);
expect(metadata.gameVersions).toEqual([]);
});
test("returns the same value as the 'minecraft' dependency", () => {
const metadata = ForgeMetadata.from({ dependencies: { "example-mod": [{ modId: "minecraft", versionRange: "[1.16.5,)" }] } } as unknown as RawForgeMetadata);
expect(metadata.gameVersions).toEqual(["[1.16.5,)"]);
});
test("returns the same values as the 'minecraft' dependency", () => {
const metadata = ForgeMetadata.from(raw);
expect(metadata.gameVersions).toEqual(["[1.17, 1.18)"]);
});
});
describe("dependencies", () => {
test("returns an empty array if no dependencies were specified", () => {
const metadata = ForgeMetadata.from({} as RawForgeMetadata);
expect(metadata.dependencies).toEqual([]);
});
test("returns dependencies if they were specified", () => {
const metadata = ForgeMetadata.from({ dependencies: { "example-mod": [{ modId: "breaking-mod", versionRange: "*", incompatible: true }] } } as unknown as RawForgeMetadata);
const dependencies = metadata.dependencies;
expect(dependencies).toHaveLength(1);
expect(dependencies[0]).toMatchObject({ id: "breaking-mod", versions: ["*"], type: DependencyType.INCOMPATIBLE });
});
test("regular dependencies have no aliases", () => {
const metadata = ForgeMetadata.from(raw);
const dependencies = metadata.dependencies;
const regularDependencies = ["included-mod", "conflicting-mod", "breaking-mod"].map(id => dependencies.find(x => x.id === id));
for (const dependency of regularDependencies) {
for (const platform of PlatformType.values()) {
expect(dependency?.getProjectId(platform)).toBe(dependency.id);
}
}
}
});
test(`special dependencies ('${loader}', 'minecraft', 'java') are ignored by default`, () => {
const metadata = ForgeMetadata.from(raw);
const dependencies = metadata.dependencies;
expect(dependencies.find(x => x.id === loader)?.isIgnored()).toBe(true);
expect(dependencies.find(x => x.id === "minecraft")?.isIgnored()).toBe(true);
expect(dependencies.find(x => x.id === "java")?.isIgnored()).toBe(true);
});
test("regular dependencies are not ignored by default", () => {
const metadata = ForgeMetadata.from(raw);
const dependencies = metadata.dependencies;
expect(dependencies.find(x => x.id === "included-mod")?.isIgnored()).toBe(false);
expect(dependencies.find(x => x.id === "suggested-mod")?.isIgnored()).toBe(false);
expect(dependencies.find(x => x.id === "conflicting-mod")?.isIgnored()).toBe(false);
expect(dependencies.find(x => x.id === "breaking-mod")?.isIgnored()).toBe(false);
});
test("returns dependencies merged with the 'dependencies' declaration from the custom payload", () => {
const metadata = ForgeMetadata.from(raw);
const dependencies = metadata.dependencies;
expect(dependencies).toHaveLength(8);
expect(dependencies.find(x => x.id === loader)).toMatchObject({ versions: ["[34,)"], type: DependencyType.REQUIRED });
expect(dependencies.find(x => x.id === "minecraft")).toMatchObject({ versions: ["[1.17, 1.18)"], type: DependencyType.REQUIRED });
expect(dependencies.find(x => x.id === "java")).toMatchObject({ versions: ["[16,)"], type: DependencyType.REQUIRED });
expect(dependencies.find(x => x.id === "recommended-mod")).toMatchObject({ versions: ["0.2.0"], type: DependencyType.RECOMMENDED });
expect(dependencies.find(x => x.id === "included-mod")).toMatchObject({ versions: ["*"], type: DependencyType.EMBEDDED });
expect(dependencies.find(x => x.id === "suggested-mod")).toMatchObject({ versions: ["*"], type: DependencyType.OPTIONAL });
expect(dependencies.find(x => x.id === "conflicting-mod")).toMatchObject({ versions: ["<0.40.0"], type: DependencyType.CONFLICTING });
expect(dependencies.find(x => x.id === "breaking-mod")).toMatchObject({ versions: ["*"], type: DependencyType.INCOMPATIBLE });
const merged = dependencies.find(x => x.id === "recommended-mod");
expect(merged.getProjectId(PlatformType.MODRINTH)).toBe("AAAA");
expect(merged.getProjectId(PlatformType.CURSEFORGE)).toBe("42");
expect(merged.getProjectId(PlatformType.GITHUB)).toBe("v0.2.0");
expect(merged.isIgnored()).toBe(true);
const withMetadata = dependencies.find(x => x.id === "suggested-mod");
expect(withMetadata.getProjectId(PlatformType.MODRINTH)).toBe("BBBB");
expect(withMetadata.getProjectId(PlatformType.CURSEFORGE)).toBe("43");
expect(withMetadata.getProjectId(PlatformType.GITHUB)).toBe("v0.3.0");
expect(withMetadata.isIgnored()).toBe(false);
expect(withMetadata.isIgnored(PlatformType.CURSEFORGE)).toBe(true);
});
});
test("special dependencies ('forge', 'minecraft', 'java') are ignored by default", () => {
const metadata = ForgeMetadata.from(RAW_METADATA);
describe("mod", () => {
test("returns the main mod entry in the metadata", () => {
const metadata = ForgeMetadata.from(raw);
const dependencies = metadata.dependencies;
expect(metadata.mod?.modId).toBe("example-mod");
});
expect(dependencies.find(x => x.id === "forge")?.isIgnored()).toBe(true);
expect(dependencies.find(x => x.id === "minecraft")?.isIgnored()).toBe(true);
expect(dependencies.find(x => x.id === "java")?.isIgnored()).toBe(true);
test("returns an empty mod entry if no mods were specified in the metadata", () => {
const metadata = ForgeMetadata.from({} as RawForgeMetadata);
expect(metadata.mod).toEqual({});
});
});
test("regular dependencies are not ignored by default", () => {
const metadata = ForgeMetadata.from(RAW_METADATA);
describe("raw", () => {
test("returns the raw metadata oject this instance was created from", () => {
const metadata = ForgeMetadata.from(raw);
const dependencies = metadata.dependencies;
expect(dependencies.find(x => x.id === "included-mod")?.isIgnored()).toBe(false);
expect(dependencies.find(x => x.id === "suggested-mod")?.isIgnored()).toBe(false);
expect(dependencies.find(x => x.id === "conflicting-mod")?.isIgnored()).toBe(false);
expect(dependencies.find(x => x.id === "breaking-mod")?.isIgnored()).toBe(false);
expect(metadata.raw).toBe(raw);
});
});
test("returns dependencies merged with the 'dependencies' declaration from the custom payload", () => {
const metadata = ForgeMetadata.from(RAW_METADATA);
describe("customPayload", () => {
test("returns an empty object by default", () => {
const metadata = ForgeMetadata.from({} as RawForgeMetadata);
const dependencies = metadata.dependencies;
expect(metadata.customPayload).toEqual({});
});
expect(dependencies).toHaveLength(8);
expect(dependencies.find(x => x.id === "forge")).toMatchObject({ versions: ["[34,)"], type: DependencyType.REQUIRED });
expect(dependencies.find(x => x.id === "minecraft")).toMatchObject({ versions: ["[1.17, 1.18)"], type: DependencyType.REQUIRED });
expect(dependencies.find(x => x.id === "java")).toMatchObject({ versions: ["[16,)"], type: DependencyType.REQUIRED });
expect(dependencies.find(x => x.id === "recommended-mod")).toMatchObject({ versions: ["0.2.0"], type: DependencyType.RECOMMENDED });
expect(dependencies.find(x => x.id === "included-mod")).toMatchObject({ versions: ["*"], type: DependencyType.EMBEDDED });
expect(dependencies.find(x => x.id === "suggested-mod")).toMatchObject({ versions: ["*"], type: DependencyType.OPTIONAL });
expect(dependencies.find(x => x.id === "conflicting-mod")).toMatchObject({ versions: ["<0.40.0"], type: DependencyType.CONFLICTING });
expect(dependencies.find(x => x.id === "breaking-mod")).toMatchObject({ versions: ["*"], type: DependencyType.INCOMPATIBLE });
test("return the custom payload if it was specified", () => {
const metadata = ForgeMetadata.from(raw);
const merged = dependencies.find(x => x.id === "recommended-mod");
expect(merged.getProjectId(PlatformType.MODRINTH)).toBe("AAAA");
expect(merged.getProjectId(PlatformType.CURSEFORGE)).toBe("42");
expect(merged.getProjectId(PlatformType.GITHUB)).toBe("v0.2.0");
expect(merged.isIgnored()).toBe(true);
const withMetadata = dependencies.find(x => x.id === "suggested-mod");
expect(withMetadata.getProjectId(PlatformType.MODRINTH)).toBe("BBBB");
expect(withMetadata.getProjectId(PlatformType.CURSEFORGE)).toBe("43");
expect(withMetadata.getProjectId(PlatformType.GITHUB)).toBe("v0.3.0");
expect(withMetadata.isIgnored()).toBe(false);
expect(withMetadata.isIgnored(PlatformType.CURSEFORGE)).toBe(true);
});
});
describe("mod", () => {
test("returns the main mod entry in the metadata", () => {
const metadata = ForgeMetadata.from(RAW_METADATA);
expect(metadata.mod?.modId).toBe("example-mod");
expect(metadata.customPayload?.loaders).toEqual(["forge", "forge2"]);
});
});
test("returns an empty mod entry if no mods were specified in the metadata", () => {
const metadata = ForgeMetadata.from({} as RawForgeMetadata);
describe("getProjectId", () => {
test("returns the mod id by default", () => {
const metadata = ForgeMetadata.from({ mods: [{ modId: "example-mod" }] } as RawForgeMetadata);
expect(metadata.mod).toEqual({});
});
});
for (const platform of PlatformType.values()) {
expect(metadata.getProjectId(platform)).toBe("example-mod");
}
});
describe("raw", () => {
test("returns the raw metadata oject this instance was created from", () => {
const metadata = ForgeMetadata.from(RAW_METADATA);
test("returns the same value as one specified in the custom payload", () => {
const metadata = ForgeMetadata.from(raw);
expect(metadata.raw).toBe(RAW_METADATA);
});
});
describe("customPayload", () => {
test("returns an empty object by default", () => {
const metadata = ForgeMetadata.from({} as RawForgeMetadata);
expect(metadata.customPayload).toEqual({});
});
test("return the custom payload if it was specified", () => {
const metadata = ForgeMetadata.from(RAW_METADATA);
expect(metadata.customPayload?.loaders).toEqual(["forge", "forge2"]);
});
});
describe("getProjectId", () => {
test("returns the mod id by default", () => {
const metadata = ForgeMetadata.from({ mods: [{ modId: "example-mod" }] } as RawForgeMetadata);
for (const platform of PlatformType.values()) {
expect(metadata.getProjectId(platform)).toBe("example-mod");
}
});
test("returns the same value as one specified in the custom payload", () => {
const metadata = ForgeMetadata.from(RAW_METADATA);
expect(metadata.getProjectId(PlatformType.MODRINTH)).toBe("AANobbMI");
expect(metadata.getProjectId(PlatformType.CURSEFORGE)).toBe("394468");
expect(metadata.getProjectId(PlatformType.GITHUB)).toBe("mc1.18-0.4.0-alpha5");
expect(metadata.getProjectId(PlatformType.MODRINTH)).toBe("AANobbMI");
expect(metadata.getProjectId(PlatformType.CURSEFORGE)).toBe("394468");
expect(metadata.getProjectId(PlatformType.GITHUB)).toBe("mc1.18-0.4.0-alpha5");
});
});
});
});

View file

@ -13,6 +13,7 @@ beforeEach(async () => {
"fabric.jar": await zipFile([__dirname, "../../content/fabric/fabric.mod.json"]),
"quilt.jar": await zipFile([__dirname, "../../content/quilt/quilt.mod.json"]),
"forge.jar": await zipFile([__dirname, "../../content/forge/mods.toml"], "META-INF/mods.toml"),
"neoforge.jar": await zipFile([__dirname, "../../content/neoforge/mods.toml"], "META-INF/mods.toml"),
"text.txt": "",
});
});