// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // checks between mods: dependencies, load order, and ids one mod uses from another. pure, so the build, check-mods // and the server (on .cubi files) all use it import { type BlockJson, choice_list, type EffectJson, type EntityJson, type EntityModelJson, type FluidJson, 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 ModelFileJson, parent_loop } from "./block_models.ts"; import { resolve_tags } from "./tags.ts"; import type { TextureJson } from "./texture_animation.ts"; import { loot_references, type LootTableJson } from "./loot.ts"; import { biome_blocks, type BiomeJson } from "./worldgen/biomes.ts"; import { biome_source_refs, type BiomeSourceJson, density_function_refs, type DensityFunctionJson, dimension_shape, type DimensionJson, new_refs, noise_refs, noise_settings_refs, type NoiseJson, type NoiseSettingsJson, RULE_NAMES, type WorldgenRefs, } from "./worldgen/data.ts"; import { feature_refs, type FeatureJson } from "./worldgen/features.ts"; import { cell_height_problem } from "./world_shape.ts"; import type { ModData } from "./mod_loader.ts"; import type { Schematic } from "./schematic.ts"; export interface ModReport { id: string; errors: string[]; warnings: string[]; } export interface LoadedMod { id: string; dir: string; report: ModReport; manifest?: Record; blocks: { file: string; json: BlockJson }[]; models: { file: string; json: ModelFileJson }[]; 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 }[]; fluids: { file: string; json: FluidJson }[]; tags: { file: string; json: TagJson }[]; loot_tables: { file: string; json: LootTableJson }[]; biomes: { file: string; json: BiomeJson }[]; // the terrain as data, see common/worldgen/data.ts noises: { file: string; json: NoiseJson }[]; density_functions: { file: string; json: DensityFunctionJson }[]; noise_settings: { file: string; json: NoiseSettingsJson }[]; biome_sources: { file: string; json: BiomeSourceJson }[]; dimensions: { file: string; json: DimensionJson }[]; features: { file: string; json: FeatureJson }[]; // schematics/.cbsm, decoded and with their ids schematics: { file: string; json: Schematic }[]; // config.json's settings, see docs/modding/misc/config.md config?: Record; textures: string[]; // absolute paths by texture id texture_files: Map; // textures/.json by texture id, for the textures that have one (see texture_animation.ts) texture_json: Map; } // what a mod's data files hold, as packed into its .cubi export function mod_data(mod: LoadedMod): ModData { return { blocks: mod.blocks.map((b) => b.json), models: mod.models.map((m) => m.json), items: mod.items.map((i) => i.json), recipes: mod.recipes.map((r) => r.json), ores: mod.ores.map((o) => o.json), spawns: mod.spawns.map((s) => s.json), entities: mod.entities.map((e) => e.json), entity_models: mod.entity_models.map((m) => m.json), effects: mod.effects.map((e) => e.json), fluids: mod.fluids.map((f) => f.json), tags: mod.tags.map((t) => t.json), loot_tables: mod.loot_tables.map((t) => t.json), biomes: mod.biomes.map((b) => b.json), noises: mod.noises.map((n) => n.json), density_functions: mod.density_functions.map((d) => d.json), noise_settings: mod.noise_settings.map((n) => n.json), biome_sources: mod.biome_sources.map((b) => b.json), dimensions: mod.dimensions.map((d) => d.json), features: mod.features.map((f) => f.json), schematics: mod.schematics.map((s) => s.json), config: mod.config, }; } // 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(); 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; } // the blocks, items, entities and biomes the mods register, what tags can hold. a block is an item too unless it says // it has none, and an item file without a texture only adds to its block's item export function registered_ids(mods: LoadedMod[]): Record> { const ids: Record> = { blocks: new Set(["cubi:air"]), items: new Set(), entities: new Set(), biomes: new Set(), }; for (const mod of mods) { for (const { json } of mod.blocks) { ids.blocks.add(json.id); if (json.item !== false) ids.items.add(json.id); } for (const { json } of mod.items) if (json.texture !== undefined) ids.items.add(json.id); for (const { json } of mod.entities) ids.entities.add(json.id); for (const { json } of mod.biomes ?? []) ids.biomes.add(json.id); } return ids; } // engine_textures are the cubi: texture ids, for telling whether a texture a mod uses exists export function check_references(mods: LoadedMod[], engine_textures: Iterable) { // air is the engine's, so worldgen and place_on can name it like any block const block_owners = new Map([["cubi:air", ["cubi"]]]); const item_owners = new Map(); const model_owners = new Map(); const model_parents = new Map(); const entity_owners = new Map(); const entity_jsons = new Map(); const entity_model_owners = new Map(); const entity_model_jsons = new Map(); const effect_owners = new Map(); const fluid_owners = new Map(); const tag_owners = new Map>(); // blocks that flow, which waterloggable blocks can hold const fluid_blocks = new Set(); const loot_tables = new Set(); const biome_owners = new Map(); const add = (map: Map, id: string, mod: string) => map.set(id, [...(map.get(id) ?? []), mod]); const textures = new Set(engine_textures); // terrain data by kind, then id const worldgen_owners: Record> = { noise: new Map(), "density function": new Map(), "noise settings": new Map(), "biome source": new Map(), dimension: new Map(), }; const biome_rule_owners = new Map(); const all_noise_settings: NoiseSettingsJson[] = []; const feature_owners = new Map(); 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.fluid) fluid_blocks.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); if (json.parent !== undefined) model_parents.set(json.id, json.parent); } 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.fluids) add(fluid_owners, json.id, mod.id); for (const { json } of mod.loot_tables ?? []) loot_tables.add(json.id); for (const { json } of mod.biomes ?? []) add(biome_owners, json.id, mod.id); for (const { json } of mod.noises ?? []) add(worldgen_owners.noise, json.id, mod.id); for (const { json } of mod.density_functions ?? []) add(worldgen_owners["density function"], json.id, mod.id); for (const { json } of mod.features ?? []) add(feature_owners, json.id, mod.id); for (const { json } of mod.noise_settings ?? []) { add(worldgen_owners["noise settings"], json.id, mod.id); all_noise_settings.push(json); } for (const { json } of mod.dimensions ?? []) { add(worldgen_owners.dimension, json.id, mod.id); } for (const { json } of mod.biome_sources ?? []) { add(worldgen_owners["biome source"], json.id, mod.id); for (const rule of json.rules) add(biome_rule_owners, rule.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 !== "cubi" && !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" | "loot table", ) => { 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 === "loot table") { if (!loot_tables.has(id)) errors.push(`${file}: loot table ${id} doesn't exist`); } 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.item_model) uses(file, json.item_model, "model"); if (json.drops) uses(file, json.drops, "item"); if (json.loot_table) uses(file, json.loot_table, "loot table"); if (json.waterloggable) { uses(file, json.waterloggable, "block"); if (block_owners.has(json.waterloggable) && !fluid_blocks.has(json.waterloggable)) { errors.push(`${file}: waterloggable ${json.waterloggable} isn't a fluid`); } } for (const id of [...(json.place_on ?? []), ...(json.place_in ?? [])]) { 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"); } if (json.parent !== undefined) { uses(file, json.parent, "model"); const loop = parent_loop(json.id, (id) => model_parents.get(id)); if (loop) errors.push(`${file}: model ${json.id}'s parents loop: ${loop.join(" -> ")}`); } 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.model) uses(file, json.model, "model"); if (json.places) uses(file, json.places, "block"); if (json.food?.returns) uses(file, json.food.returns, "item"); for (const layer of json.layers ?? []) uses(file, layer.texture, "texture"); if (json.layers && json.model) { errors.push( `${file}: item ${json.id} has a model, so it isn't drawn flat and its layers wouldn't show`, ); } 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"); } } // mods' own types are checked by their parse when the mod loads for (const { file, json } of mod.recipes) { if (json.type !== "shaped") continue; for (const id of Object.values(json.key).flatMap(choice_list)) uses(file, id, "item"); uses(file, json.result.id, "item"); } for (const { file, json } of mod.ores) { uses(file, json.id, "block"); for (const replaces of [json.replaces].flat()) uses(file, 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"); if (json.loot_table) uses(file, json.loot_table, "loot table"); } for (const { file, json } of mod.loot_tables ?? []) { const refs = loot_references(json); for (const id of refs.items) uses(file, id, "item"); for (const id of refs.tags) uses(file, id, "item"); for (const id of refs.tables) uses(file, id, "loot table"); // mods' conditions and functions are registered by scripts, the server checks them when it starts for (const id of refs.custom) check_namespace(file, id); } // the terrain as data: every id defined once, and everything it uses there const check_worldgen = (kind: string, file: string, id: string, refs: WorldgenRefs) => { if ((worldgen_owners[kind].get(id)?.length ?? 0) > 1) { errors.push(`${file}: ${kind} ${id} is also defined by ${worldgen_owners[kind].get(id)!.join(", ")}`); } const exists = (ids: Set, what: string, owners: Map) => { for (const used of ids) { check_namespace(file, used); if (!owners.has(used)) errors.push(`${file}: ${what} ${used} doesn't exist`); } }; exists(refs.noises, "noise", worldgen_owners.noise); exists(refs.functions, "density function", worldgen_owners["density function"]); exists(refs.biome_sources, "biome source", worldgen_owners["biome source"]); exists(refs.biomes, "biome", biome_owners); for (const block of refs.blocks) uses(file, block, "block"); }; for (const { file, json } of mod.noises ?? []) { const refs = new_refs(); noise_refs(json, refs); check_worldgen("noise", file, json.id, refs); } for (const { file, json } of mod.density_functions ?? []) { const refs = new_refs(); density_function_refs(json, refs); check_worldgen("density function", file, json.id, refs); } for (const { file, json } of mod.noise_settings ?? []) { const refs = new_refs(); noise_settings_refs(json, refs); check_worldgen("noise settings", file, json.id, refs); } for (const { file, json } of mod.dimensions ?? []) { check_worldgen("dimension", file, json.id, new_refs()); check_namespace(file, json.generator); if (!worldgen_owners["noise settings"].has(json.generator)) { errors.push(`${file}: noise settings ${json.generator} doesn't exist`); } const settings = all_noise_settings.find((settings) => settings.id === json.generator); const problem = settings && cell_height_problem( dimension_shape(json), [settings.terrain.cell_height, settings.caves.cell_height], ); if (problem) errors.push(`${file}: can't be generated by ${json.generator}: ${problem}`); // whether exactly one is the default is up to the server, with the mods a world plays: a folder of mods // can hold several games, each with its own } for (const { file, json } of mod.biome_sources ?? []) { const refs = new_refs(); biome_source_refs(json, refs); check_worldgen("biome source", file, json.id, refs); // what its rules look at, in every generator that uses it const users = all_noise_settings.filter((settings) => settings.biome_source === (json.extends ?? json.id)); for (const settings of users) { const names = new Set([...Object.keys(settings.climate), ...RULE_NAMES]); for (const rule of json.rules) { for (const name of Object.keys(rule.when ?? {})) { if (!names.has(name)) { errors.push( `${file}: rule ${rule.id} looks at ${name}, which ${settings.id} doesn't have. it has ${ [...names].join(", ") }`, ); } } } } for (const rule of json.rules) { if ((biome_rule_owners.get(rule.id)?.length ?? 0) > 1) { errors.push( `${file}: biome rule ${rule.id} is also defined by ${ biome_rule_owners.get(rule.id)!.join(", ") }`, ); } const target = rule.before ?? rule.after; if (target !== undefined && !biome_rule_owners.has(target)) { errors.push(`${file}: rule ${rule.id} goes next to ${target}, which isn't a biome rule`); } } } for (const { file, json } of mod.features ?? []) { if ((feature_owners.get(json.id)?.length ?? 0) > 1) { errors.push( `${file}: feature ${json.id} is also defined by ${feature_owners.get(json.id)!.join(", ")}`, ); } const refs = { blocks: new Set(), biomes: new Set(), custom: new Set() }; feature_refs(json, refs); for (const block of refs.blocks) uses(file, block, "block"); for (const biome of refs.biomes) { check_namespace(file, biome); if (!biome_owners.has(biome)) errors.push(`${file}: biome ${biome} doesn't exist`); } // check-mods doesn't run scripts, the server checks these when it starts for (const custom of refs.custom) { check_namespace(file, custom); warnings.push(`${file}: ${custom} isn't built in, a mod's worldgen phase must register it`); } } for (const { file, json } of mod.biomes ?? []) { if ((biome_owners.get(json.id)?.length ?? 0) > 1) { errors.push(`${file}: biome ${json.id} is also defined by ${biome_owners.get(json.id)!.join(", ")}`); } for (const id of biome_blocks(json)) uses(file, id, "block"); } 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.fluids) { if ((fluid_owners.get(json.id)?.length ?? 0) > 1) { errors.push(`${file}: fluid ${json.id} is also defined by ${fluid_owners.get(json.id)!.join(", ")}`); } if (json.texture !== undefined) uses(file, json.texture, "texture"); } // cubi: entities, like dropped items, are the engine's for (const { file, json } of mod.schematics) { for (const { id } of json.palette) uses(file, id, "block"); for (const { id } of json.entities) { check_namespace(file, id); if (!id.startsWith("cubi:") && !entity_owners.has(id)) { errors.push(`${file}: entity ${id} doesn't exist`); } } } 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`); } for (const biome of json.biomes ?? []) { check_namespace(file, biome); if (!biome_owners.has(biome)) 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 registered = registered_ids(mods); const { problems } = resolve_tags( ordered.flatMap((mod) => mod.tags.map(({ file, json }) => ({ mod: mod.id, file, json }))), (registry, id) => registered[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}`); }