Shared state between client and server
This commit is contained in:
@@ -4,6 +4,8 @@ import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everyt
|
||||
import { Container as EngineContainer, ItemStack as EngineItemStack } from "$/common/inventory.ts";
|
||||
import { get_state_value, set_state_value } from "$/common/utils.ts";
|
||||
import { covers_face } from "$/common/block_shapes.ts";
|
||||
import { BlockRules } from "$/common/block_rules.ts";
|
||||
import type { SharedContext } from "$/common/mod_api/shared.ts";
|
||||
import { tag_api } from "$/common/tags.ts";
|
||||
import type {
|
||||
BlockComponent,
|
||||
@@ -112,6 +114,8 @@ interface Timer {
|
||||
|
||||
export class ModRuntime {
|
||||
block_components = new Map<string, { mod: string; component: BlockComponent }>();
|
||||
// what shared scripts registered, see common/block_rules.ts
|
||||
rules = new BlockRules(run_guarded);
|
||||
item_components = new Map<string, { mod: string; component: ItemComponent }>();
|
||||
effect_components = new Map<string, { mod: string; component: EffectComponent }>();
|
||||
// every command, the engine's and mods', see common/commands.ts
|
||||
@@ -208,6 +212,44 @@ export class ModRuntime {
|
||||
game!: GameServer;
|
||||
|
||||
// imports each server script and calls its setup, in load order
|
||||
// shared scripts, the block rules clients run too. their world is the server's
|
||||
async load_shared_scripts(game: GameServer, scripts: { mod: string; version: string; url: string }[]) {
|
||||
this.game = game;
|
||||
for (const { mod, version, url } of scripts) {
|
||||
const module = await import(url);
|
||||
if (typeof module.setup !== "function") {
|
||||
throw new ModLoadError(mod, "the shared script doesn't export a setup function");
|
||||
}
|
||||
const { world } = this.#context(mod, version);
|
||||
const ctx: SharedContext = {
|
||||
mod: { id: mod, version },
|
||||
side: "server",
|
||||
tags: tag_api(),
|
||||
world: {
|
||||
get_block: world.get_block,
|
||||
get_state: world.get_state,
|
||||
set_state: world.set_state,
|
||||
is_face_sturdy: world.is_face_sturdy,
|
||||
},
|
||||
components: {
|
||||
register_block: (id, rules) => {
|
||||
if (!this.#setting_up) throw new ModLoadError(mod, "rules can only be registered during setup");
|
||||
if (!id.startsWith(`${mod}:`)) {
|
||||
throw new ModLoadError(mod, `component ${id} must be in the namespace "${mod}"`);
|
||||
}
|
||||
this.rules.register(mod, id, rules);
|
||||
},
|
||||
},
|
||||
};
|
||||
running_mod = mod;
|
||||
try {
|
||||
await module.setup(ctx);
|
||||
} finally {
|
||||
running_mod = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async load_scripts(game: GameServer, scripts: { mod: string; version: string; url: string }[]) {
|
||||
this.game = game;
|
||||
for (const { mod, version, url } of scripts) {
|
||||
@@ -286,7 +328,7 @@ export class ModRuntime {
|
||||
|
||||
for (const [id, block] of EverythingRegistry.entries<{ components?: Record<string, unknown> }>("blocks")) {
|
||||
for (const component of Object.keys(block.components ?? {})) {
|
||||
if (!this.block_components.has(component)) {
|
||||
if (!this.block_components.has(component) && !this.rules.has(component)) {
|
||||
throw new ModLoadError(
|
||||
id.split(":")[0],
|
||||
`block ${id} uses component ${component}, which nothing registered`,
|
||||
@@ -366,11 +408,11 @@ export class ModRuntime {
|
||||
// components on a block, with their params from its json
|
||||
components_of(block_id: string): { mod: string; id: string; component: BlockComponent; params: unknown }[] {
|
||||
const block = EverythingRegistry.get<{ components?: Record<string, unknown> }>("blocks", block_id);
|
||||
return Object.entries(block?.components ?? {}).map(([id, params]) => ({
|
||||
...this.block_components.get(id)!,
|
||||
id,
|
||||
params,
|
||||
}));
|
||||
// components that only have shared rules have nothing here
|
||||
return Object.entries(block?.components ?? {}).flatMap(([id, params]) => {
|
||||
const registered = this.block_components.get(id);
|
||||
return registered ? [{ ...registered, id, params }] : [];
|
||||
});
|
||||
}
|
||||
|
||||
// components on an item, with their params from its json
|
||||
|
||||
Reference in New Issue
Block a user