308 lines
12 KiB
TypeScript
308 lines
12 KiB
TypeScript
// checks between mods: dependencies, load order, and ids one mod uses from another. pure, so the build, check-mods
|
|
// and the server (on .bmod files) all use it
|
|
import {
|
|
type BlockJson,
|
|
choice_list,
|
|
type EffectJson,
|
|
type EntityJson,
|
|
type EntityModelJson,
|
|
type ItemJson,
|
|
type OreJson,
|
|
type RecipeJson,
|
|
SHARED_TAG_NAMESPACE,
|
|
type SpawnJson,
|
|
type TagJson,
|
|
type TagRegistry,
|
|
} from "./mod_data.ts";
|
|
import { PLAYER_PARTS } from "./everything_registry.ts";
|
|
import { BUILTIN_MODELS, type ModelJson } from "./block_models.ts";
|
|
import { resolve_tags } from "./tags.ts";
|
|
import type { TextureJson } from "./texture_animation.ts";
|
|
import { BIOMES } from "./worldgen/overworld.ts";
|
|
|
|
export interface ModReport {
|
|
id: string;
|
|
errors: string[];
|
|
warnings: string[];
|
|
}
|
|
|
|
export interface LoadedMod {
|
|
id: string;
|
|
dir: string;
|
|
report: ModReport;
|
|
manifest?: Record<string, unknown>;
|
|
blocks: { file: string; json: BlockJson }[];
|
|
models: { file: string; json: ModelJson }[];
|
|
items: { file: string; json: ItemJson }[];
|
|
recipes: { file: string; json: RecipeJson }[];
|
|
ores: { file: string; json: OreJson }[];
|
|
spawns: { file: string; json: SpawnJson }[];
|
|
entities: { file: string; json: EntityJson }[];
|
|
entity_models: { file: string; json: EntityModelJson }[];
|
|
effects: { file: string; json: EffectJson }[];
|
|
tags: { file: string; json: TagJson }[];
|
|
textures: string[];
|
|
// absolute paths by texture id
|
|
texture_files: Map<string, string>;
|
|
// textures/<name>.json by texture id, for the textures that have one (see texture_animation.ts)
|
|
texture_json: Map<string, TextureJson>;
|
|
}
|
|
|
|
// dependencies before the mods that need them, otherwise alphabetical
|
|
export function load_order(mods: LoadedMod[]): LoadedMod[] {
|
|
const by_id = new Map(mods.map((mod) => [mod.id, mod]));
|
|
const ordered: LoadedMod[] = [];
|
|
const state = new Map<string, "visiting" | "done">();
|
|
|
|
const visit = (mod: LoadedMod, path: string[]) => {
|
|
if (state.get(mod.id) === "done") return;
|
|
if (state.get(mod.id) === "visiting") {
|
|
throw new Error(`mods depend on each other in a circle: ${[...path, mod.id].join(" -> ")}`);
|
|
}
|
|
state.set(mod.id, "visiting");
|
|
const dependencies = ((mod.manifest?.dependencies ?? []) as { id: string }[]).map((d) => d.id).sort();
|
|
for (const dependency of dependencies) {
|
|
const other = by_id.get(dependency);
|
|
if (other) visit(other, [...path, mod.id]);
|
|
}
|
|
state.set(mod.id, "done");
|
|
ordered.push(mod);
|
|
};
|
|
|
|
for (const mod of [...mods].sort((a, b) => a.id.localeCompare(b.id))) {
|
|
visit(mod, []);
|
|
}
|
|
return ordered;
|
|
}
|
|
|
|
// engine_textures are the engine: texture ids, for telling whether a texture a mod uses exists
|
|
export function check_references(mods: LoadedMod[], engine_textures: Iterable<string>) {
|
|
const block_owners = new Map<string, string[]>();
|
|
const item_owners = new Map<string, string[]>();
|
|
const model_owners = new Map<string, string[]>();
|
|
const entity_owners = new Map<string, string[]>();
|
|
const entity_jsons = new Map<string, EntityJson>();
|
|
const entity_model_owners = new Map<string, string[]>();
|
|
const entity_model_jsons = new Map<string, EntityModelJson>();
|
|
const effect_owners = new Map<string, string[]>();
|
|
const tag_owners = new Map<TagRegistry, Set<string>>();
|
|
const liquids = new Set<string>();
|
|
const add = (map: Map<string, string[]>, id: string, mod: string) => map.set(id, [...(map.get(id) ?? []), mod]);
|
|
|
|
const textures = new Set<string>(engine_textures);
|
|
|
|
for (const mod of mods) {
|
|
for (const { json } of mod.blocks) {
|
|
add(block_owners, json.id, mod.id);
|
|
if (json.item !== false) add(item_owners, json.id, mod.id);
|
|
if (json.liquid) liquids.add(json.id);
|
|
}
|
|
// one without a texture is the item of the mod's block, which is already counted
|
|
for (const { json } of mod.items) if (json.texture !== undefined) add(item_owners, json.id, mod.id);
|
|
for (const { json } of mod.models) add(model_owners, json.id, mod.id);
|
|
for (const { json } of mod.entities) {
|
|
add(entity_owners, json.id, mod.id);
|
|
entity_jsons.set(json.id, json);
|
|
}
|
|
for (const { json } of mod.entity_models) {
|
|
add(entity_model_owners, json.id, mod.id);
|
|
entity_model_jsons.set(json.id, json);
|
|
}
|
|
for (const { json } of mod.effects) add(effect_owners, json.id, mod.id);
|
|
for (const { json } of mod.tags) {
|
|
if (!tag_owners.has(json.registry)) tag_owners.set(json.registry, new Set());
|
|
tag_owners.get(json.registry)!.add(json.id);
|
|
}
|
|
for (const texture of mod.textures) textures.add(texture);
|
|
}
|
|
|
|
const mod_ids = new Set(mods.map((mod) => mod.id));
|
|
|
|
for (const mod of mods) {
|
|
const { errors, warnings } = mod.report;
|
|
const dependencies = new Set(
|
|
((mod.manifest?.dependencies ?? []) as { id: string }[]).map((dep) => dep.id),
|
|
);
|
|
for (const dep of dependencies) {
|
|
if (!mod_ids.has(dep)) errors.push(`manifest.json: depends on "${dep}", which isn't installed`);
|
|
}
|
|
|
|
// the shared tag namespace belongs to no mod, so using it needs no dependency
|
|
const check_namespace = (file: string, id: string) => {
|
|
const tag = id.startsWith("#");
|
|
const namespace = id.replace("#", "").split(":")[0];
|
|
if (tag && namespace === SHARED_TAG_NAMESPACE) return;
|
|
if (namespace !== mod.id && namespace !== "engine" && !dependencies.has(namespace)) {
|
|
warnings.push(`${file}: uses ${id} but doesn't list "${namespace}" in dependencies`);
|
|
}
|
|
};
|
|
|
|
// block and item ids can be #tags, like in recipes and place_on
|
|
const uses = (file: string, id: string, what: "block" | "item" | "texture" | "model" | "entity model") => {
|
|
check_namespace(file, id);
|
|
if (id.startsWith("#") && (what === "block" || what === "item")) {
|
|
if (!tag_owners.get(what === "block" ? "blocks" : "items")?.has(id.slice(1))) {
|
|
errors.push(`${file}: ${what} tag ${id} doesn't exist`);
|
|
}
|
|
} else if (what === "model") {
|
|
if (!BUILTIN_MODELS[id] && !model_owners.has(id)) errors.push(`${file}: model ${id} doesn't exist`);
|
|
} else if (what === "entity model") {
|
|
if (!entity_model_owners.has(id)) errors.push(`${file}: entity model ${id} doesn't exist`);
|
|
} else if (what === "texture") {
|
|
if (!textures.has(id)) warnings.push(`${file}: texture ${id} doesn't exist, it will show as missing`);
|
|
} else if (!(what === "block" ? block_owners : item_owners).has(id)) {
|
|
errors.push(`${file}: ${what} ${id} doesn't exist`);
|
|
}
|
|
};
|
|
|
|
for (const { file, json } of mod.blocks) {
|
|
if ((block_owners.get(json.id)?.length ?? 0) > 1) {
|
|
errors.push(`${file}: block ${json.id} is also defined by ${block_owners.get(json.id)!.join(", ")}`);
|
|
}
|
|
for (const variant of [json, ...Object.values(json.variants ?? {})]) {
|
|
if (variant.model) uses(file, variant.model, "model");
|
|
const textures = variant.textures ?? {};
|
|
for (const texture of typeof textures === "string" ? [textures] : Object.values(textures)) {
|
|
uses(file, texture, "texture");
|
|
}
|
|
}
|
|
if (json.drops) uses(file, json.drops, "item");
|
|
if (json.waterloggable) {
|
|
uses(file, json.waterloggable, "block");
|
|
if (block_owners.has(json.waterloggable) && !liquids.has(json.waterloggable)) {
|
|
errors.push(`${file}: waterloggable ${json.waterloggable} isn't a liquid`);
|
|
}
|
|
}
|
|
for (const id of [...(json.place_on ?? []), ...(json.place_in ?? [])]) {
|
|
if (id !== "bworld:air") uses(file, id, "block");
|
|
}
|
|
}
|
|
for (const { file, json } of mod.models) {
|
|
if ((model_owners.get(json.id)?.length ?? 0) > 1 || BUILTIN_MODELS[json.id]) {
|
|
errors.push(`${file}: model ${json.id} is defined more than once`);
|
|
}
|
|
for (const texture of Object.values(json.textures ?? {})) {
|
|
if (!texture.startsWith("#")) uses(file, texture, "texture");
|
|
}
|
|
for (const element of json.elements) {
|
|
for (const face of Object.values(element.faces)) {
|
|
if (face && !face.texture.startsWith("#")) uses(file, face.texture, "texture");
|
|
}
|
|
}
|
|
}
|
|
for (const { file, json } of mod.items) {
|
|
if ((item_owners.get(json.id)?.length ?? 0) > 1) {
|
|
errors.push(`${file}: item ${json.id} is also defined by ${item_owners.get(json.id)!.join(", ")}`);
|
|
}
|
|
if (json.texture !== undefined) uses(file, json.texture, "texture");
|
|
else if (!mod.blocks.some((block) => block.json.id === json.id && block.json.item !== false)) {
|
|
errors.push(`${file}: item ${json.id} has no texture, and no block of this mod gives it an item`);
|
|
}
|
|
for (const texture of Object.values(json.data_textures ?? {})) uses(file, texture, "texture");
|
|
if (json.places) uses(file, json.places, "block");
|
|
if (json.food?.returns) uses(file, json.food.returns, "item");
|
|
if (json.worn) {
|
|
const { model, texture, slim_model, slim_texture } = json.worn;
|
|
for (const id of [model, slim_model]) {
|
|
if (id === undefined) continue;
|
|
uses(file, id, "entity model");
|
|
// nothing of it would move with the player
|
|
const bones = entity_model_jsons.get(id)?.bones;
|
|
if (bones && !bones.some((bone) => (PLAYER_PARTS as readonly string[]).includes(bone.name))) {
|
|
warnings.push(
|
|
`${file}: worn model ${id} has no bone named like a player part (${
|
|
PLAYER_PARTS.join(", ")
|
|
})`,
|
|
);
|
|
}
|
|
}
|
|
uses(file, texture, "texture");
|
|
if (slim_texture !== undefined) uses(file, slim_texture, "texture");
|
|
}
|
|
}
|
|
for (const { file, json } of mod.recipes) {
|
|
switch (json.type) {
|
|
case "shaped":
|
|
for (const id of Object.values(json.key).flatMap(choice_list)) uses(file, id, "item");
|
|
uses(file, json.result.id, "item");
|
|
break;
|
|
case "furnace":
|
|
for (const id of choice_list(json.input)) uses(file, id, "item");
|
|
uses(file, json.output.id, "item");
|
|
break;
|
|
case "fuel":
|
|
for (const id of choice_list(json.item)) uses(file, id, "item");
|
|
break;
|
|
case "smithing":
|
|
uses(file, json.tool, "item");
|
|
uses(file, json.material.id, "item");
|
|
if (json.addition) uses(file, json.addition, "item");
|
|
uses(file, json.result, "item");
|
|
break;
|
|
}
|
|
}
|
|
for (const { file, json } of mod.ores) {
|
|
uses(file, json.id, "block");
|
|
uses(file, json.replaces, "block");
|
|
}
|
|
for (const { file, json } of mod.entities) {
|
|
if ((entity_owners.get(json.id)?.length ?? 0) > 1) {
|
|
errors.push(`${file}: entity ${json.id} is also defined by ${entity_owners.get(json.id)!.join(", ")}`);
|
|
}
|
|
if (json.model !== undefined) uses(file, json.model, "entity model");
|
|
uses(file, json.texture, "texture");
|
|
}
|
|
for (const { file, json } of mod.effects) {
|
|
if ((effect_owners.get(json.id)?.length ?? 0) > 1) {
|
|
errors.push(`${file}: effect ${json.id} is also defined by ${effect_owners.get(json.id)!.join(", ")}`);
|
|
}
|
|
if (json.icon !== undefined) uses(file, json.icon, "texture");
|
|
}
|
|
for (const { file, json } of mod.spawns) {
|
|
check_namespace(file, json.entity);
|
|
const entity = entity_jsons.get(json.entity);
|
|
if (!entity) errors.push(`${file}: entity ${json.entity} doesn't exist`);
|
|
else if (entity.behavior !== "monster") {
|
|
errors.push(`${file}: ${json.entity} isn't a monster, only monsters spawn on their own for now`);
|
|
}
|
|
// biomes are the engine's, whatever their namespace
|
|
for (const biome of json.biomes ?? []) {
|
|
if (!(biome in BIOMES)) errors.push(`${file}: biome ${biome} doesn't exist`);
|
|
}
|
|
}
|
|
for (const { file, json } of mod.entity_models) {
|
|
if ((entity_model_owners.get(json.id)?.length ?? 0) > 1) {
|
|
errors.push(`${file}: entity model ${json.id} is defined more than once`);
|
|
}
|
|
}
|
|
// whether what a tag holds exists is up to resolve_tags below, since entries can be optional
|
|
for (const { file, json } of mod.tags) {
|
|
check_namespace(file, `#${json.id}`);
|
|
for (const entry of json.values) {
|
|
if (typeof entry === "string" || entry.required !== false) {
|
|
check_namespace(file, typeof entry === "string" ? entry : entry.id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// tags merge in load order. when there's no order, mods depending on each other in a circle is its own error
|
|
let ordered = mods;
|
|
try {
|
|
ordered = load_order(mods);
|
|
} catch {
|
|
// checked by the caller
|
|
}
|
|
const owners: Record<TagRegistry, Map<string, string[]>> = {
|
|
blocks: block_owners,
|
|
items: item_owners,
|
|
entities: entity_owners,
|
|
};
|
|
const { problems } = resolve_tags(
|
|
ordered.flatMap((mod) => mod.tags.map(({ file, json }) => ({ mod: mod.id, file, json }))),
|
|
(registry, id) => (registry === "blocks" && id === "bworld:air") || owners[registry].has(id),
|
|
);
|
|
const by_id = new Map(mods.map((mod) => [mod.id, mod]));
|
|
for (const { mod, file, message } of problems) by_id.get(mod)!.report.errors.push(`${file}: ${message}`);
|
|
}
|