Files
bworld/common/mod_loader.ts
T

209 lines
7.6 KiB
TypeScript

// registers mods' data, the same way on the server and on clients
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "./everything_registry.ts";
import {
add_to_block_item,
block_from_json,
BlockJson,
choice_ids,
EffectJson,
EntityJson,
EntityModelJson,
grid_recipe_from_json,
GridRecipe,
is_mod_recipe,
item_from_json,
ItemJson,
ModRecipeJson,
OreJson,
RecipeJson,
SpawnJson,
TagJson,
TagRegistry,
} from "./mod_data.ts";
import { resolve_tags, Tags } from "./tags.ts";
import type { LootTableJson } from "./loot.ts";
import { type BiomeJson, register_biome } from "./worldgen/biomes.ts";
import type { BiomeSourceJson, DensityFunctionJson, NoiseJson, NoiseSettingsJson } from "./worldgen/data.ts";
import type { FeatureJson } from "./worldgen/features.ts";
import { register_block_item } from "./utils.ts";
import type { ModelJson } from "./block_models.ts";
// everything a mod's json files hold, merged into one file by the build (build/mods/<id>/<hash>/data.json)
export interface ModData {
blocks: BlockJson[];
// block models, see common/block_models.ts
models: ModelJson[];
items: ItemJson[];
recipes: RecipeJson[];
ores: OreJson[];
// natural spawns, see SpawnJson
spawns: SpawnJson[];
entities: EntityJson[];
entity_models: EntityModelJson[];
// status effects, see EffectJson
effects: EffectJson[];
// tag files, see common/tags.ts
tags: TagJson[];
// see common/loot.ts. missing from mods packed before there were loot tables
loot_tables?: LootTableJson[];
// see common/worldgen/biomes.ts. missing from mods packed before biomes were data
biomes?: BiomeJson[];
// the terrain as data, see common/worldgen/data.ts. server only, players' copies leave them out
noises?: NoiseJson[];
density_functions?: DensityFunctionJson[];
noise_settings?: NoiseSettingsJson[];
biome_sources?: BiomeSourceJson[];
// data features, see common/worldgen/features.ts. server only too
features?: FeatureJson[];
}
// a mod as the server lists it to clients, in load order: the .bmod players download, without its server script
export interface ModListing {
id: string;
name: string;
version: string;
// sha-256 in hex of the .bmod, clients check it before using anything in it
sha256: string;
// where it's served from, relative to the server's root. named by its hash, so it never changes
file: string;
size: number;
// it imports bworld/engine, see docs/modding/misc/engine.md. the confirm screen for other sites says so
engine_access?: boolean;
}
export class RecipeBook {
shaped: GridRecipe[] = [];
furnace = new Map<string, { output: { id: string; count: number }; cook_time: number }>();
fuel = new Map<string, number>();
smithing: Extract<RecipeJson, { type: "smithing" }>[] = [];
// recipes of mods' own types, by type
by_type = new Map<string, ModRecipeJson[]>();
// every recipe as its json, in load order, for recipe viewers
all: RecipeJson[] = [];
// each mod's ores, in load order. each mod's are their own pass, see generate_raw_chunk
ores: OreJson[][] = [];
spawns: SpawnJson[] = [];
}
export class ModLoadError extends Error {
constructor(mod: string, message: string) {
super(`${mod}: ${message}`);
this.name = "ModLoadError";
}
}
// whether a block, item or entity is registered, for tags. air isn't a registered block, but tags can hold it
export function is_registered(registry: TagRegistry, id: string): boolean {
return (registry === "blocks" && id === "bworld:air") || EverythingRegistry.get(registry, id) !== undefined;
}
// registers every mod's blocks, items, entities, effects, tags and recipes, in the order given (dependencies first). tags load
// once every mod's content has, since a tag can hold anything, and recipes after them, since they can use tags
export function register_mod_data(mods: { id: string; data: ModData }[]): RecipeBook {
const recipes = new RecipeBook();
for (const { id, data } of mods) {
try {
for (const model of data.models) {
EverythingRegistry.register<ModelJson>("models", model.id, model);
}
for (const json of data.blocks) {
const { block, has_item } = block_from_json(json);
EverythingRegistry.register<BlockRegistry>("blocks", block.id, block);
if (has_item) {
register_block_item(block);
}
}
for (const json of data.items) {
if (json.texture !== undefined) {
EverythingRegistry.register<ItemRegistry>("items", json.id, item_from_json(json));
continue;
}
const block_item = EverythingRegistry.get<BlockRegistry>("blocks", json.id) &&
EverythingRegistry.get<ItemRegistry>("items", json.id);
if (!block_item) throw new Error(`item ${json.id} has no texture and isn't a block's item`);
add_to_block_item(json, block_item);
}
for (const model of data.entity_models) {
EverythingRegistry.register<EntityModelJson>("entity_models", model.id, model);
}
for (const json of data.entities) {
EverythingRegistry.register<EntityJson>("entities", json.id, json);
}
for (const json of data.effects) {
EverythingRegistry.register<EffectJson>("effects", json.id, json);
}
for (const json of data.loot_tables ?? []) {
EverythingRegistry.register<LootTableJson>("loot_tables", json.id, json);
}
for (const json of data.biomes ?? []) {
register_biome(json);
}
// compiled into a generator once every mod's are in, see common/worldgen/generator.ts
for (const json of data.noises ?? []) EverythingRegistry.register("noises", json.id, json);
for (const json of data.density_functions ?? []) {
EverythingRegistry.register("density_functions", json.id, json);
}
for (const json of data.noise_settings ?? []) EverythingRegistry.register("noise_settings", json.id, json);
for (const json of data.biome_sources ?? []) EverythingRegistry.register("biome_sources", json.id, json);
// compiled with the types worldgen scripts register, see common/worldgen_loader.ts
for (const json of data.features ?? []) EverythingRegistry.register("features", json.id, json);
} catch (e) {
throw new ModLoadError(id, (e as Error).message);
}
}
const { tags, problems } = resolve_tags(
mods.flatMap(({ id, data }) =>
data.tags.map((json) => ({ mod: id, file: `tags/${json.registry}/${json.id}`, json }))
),
is_registered,
);
if (problems.length > 0) throw new ModLoadError(problems[0].mod, `${problems[0].file}: ${problems[0].message}`);
Tags.load(tags);
for (const { id, data } of mods) {
const fail = (message: string): never => {
throw new ModLoadError(id, message);
};
for (const recipe of data.recipes) {
recipes.all.push(recipe);
if (is_mod_recipe(recipe)) {
const list = recipes.by_type.get(recipe.type) ?? [];
recipes.by_type.set(recipe.type, list);
list.push(recipe);
continue;
}
switch (recipe.type) {
case "shaped":
recipes.shaped.push(grid_recipe_from_json(recipe));
break;
case "furnace":
for (const input of choice_ids(recipe.input)) {
if (recipes.furnace.has(input)) fail(`two furnace recipes for ${input}`);
recipes.furnace.set(input, { output: recipe.output, cook_time: recipe.cook_time });
}
break;
case "fuel":
for (const item of choice_ids(recipe.item)) recipes.fuel.set(item, recipe.burn_time);
break;
case "smithing":
if (
recipes.smithing.some((other) =>
other.tool === recipe.tool && other.material.id === recipe.material.id &&
other.addition === recipe.addition
)
) {
fail(`two smithing recipes for ${recipe.tool} with ${recipe.material.id}`);
}
recipes.smithing.push(recipe);
break;
}
}
if (data.ores.length > 0) recipes.ores.push(data.ores);
recipes.spawns.push(...data.spawns);
}
return recipes;
}