Single module modding
This commit is contained in:
+33
-41
@@ -22,22 +22,18 @@ import { game_with_pool } from "./chunk_loading.ts";
|
||||
const [MAJOR, MINOR] = GAME_VERSION.split(".");
|
||||
const THIS_MINOR = `~${MAJOR}.${MINOR}.0`;
|
||||
|
||||
// a mod in dir with these scripts, as { side: [file, code] }
|
||||
// a mod in dir whose script is code
|
||||
function write_mod(
|
||||
dir: string,
|
||||
id: string,
|
||||
scripts: Record<string, [string, string]>,
|
||||
code: string,
|
||||
manifest: Record<string, unknown> = { engine_access: true, game_version: THIS_MINOR },
|
||||
) {
|
||||
Deno.mkdirSync(`${dir}/${id}/scripts`, { recursive: true });
|
||||
const paths: Record<string, string> = {};
|
||||
for (const [side, [file, code]] of Object.entries(scripts)) {
|
||||
Deno.writeTextFileSync(`${dir}/${id}/scripts/${file}`, code);
|
||||
paths[side] = `scripts/${file}`;
|
||||
}
|
||||
Deno.writeTextFileSync(`${dir}/${id}/scripts/main.ts`, code);
|
||||
Deno.writeTextFileSync(
|
||||
`${dir}/${id}/manifest.json`,
|
||||
JSON.stringify({ format_version: 1, id, name: id, version: "1.0.0", scripts: paths, ...manifest }),
|
||||
JSON.stringify({ format_version: 1, id, name: id, version: "1.0.0", main: "scripts/main.ts", ...manifest }),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -80,11 +76,11 @@ async function errors_during(fn: () => unknown): Promise<string[]> {
|
||||
|
||||
Deno.test("a server mod hooks GameServer to drop twice, like the plan's example", async () => {
|
||||
const dir = mods_dir((dir) =>
|
||||
write_mod(dir, "double_drops", {
|
||||
server: [
|
||||
"server.ts",
|
||||
`import { engine, hook } from "cubi/engine";
|
||||
export function setup() {
|
||||
write_mod(
|
||||
dir,
|
||||
"double_drops",
|
||||
`import { engine, hook } from "cubi/engine";
|
||||
export function server() {
|
||||
hook(engine.server.GameServer.prototype, "pop_item", {
|
||||
after(game, [x, y, z, stack], entity) {
|
||||
game.spawn_item(entity.x, entity.y, entity.z, stack.clone());
|
||||
@@ -92,8 +88,7 @@ Deno.test("a server mod hooks GameServer to drop twice, like the plan's example"
|
||||
},
|
||||
});
|
||||
}`,
|
||||
],
|
||||
})
|
||||
)
|
||||
);
|
||||
const { game } = await test_game(dir);
|
||||
const items = () => game.entities.entities_near(0.5, 100, 0.5, 4).length;
|
||||
@@ -105,16 +100,15 @@ Deno.test("a server mod hooks GameServer to drop twice, like the plan's example"
|
||||
|
||||
Deno.test("a worldgen mod hooks the terrain in engine.common", async () => {
|
||||
const dir = mods_dir((dir) =>
|
||||
write_mod(dir, "flat_sea", {
|
||||
worldgen: [
|
||||
"worldgen.ts",
|
||||
`import { engine, hook } from "cubi/engine";
|
||||
write_mod(
|
||||
dir,
|
||||
"flat_sea",
|
||||
`import { engine, hook } from "cubi/engine";
|
||||
// no ground anywhere, so it's all sea
|
||||
export function setup() {
|
||||
export function worldgen() {
|
||||
hook(engine.common.NoiseTerrain.prototype, "density", { replace: () => -1 });
|
||||
}`,
|
||||
],
|
||||
})
|
||||
)
|
||||
);
|
||||
const { game } = await test_game(dir, undefined, "engine-access-flat-sea");
|
||||
assertEquals(game.world.get_block_id(8, 10, 8), "bworld:water");
|
||||
@@ -126,20 +120,19 @@ Deno.test("a worldgen mod hooks the terrain in engine.common", async () => {
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "a worldgen mod's hook reaches the worldgen workers too, since each loads the worldgen scripts",
|
||||
name: "a worldgen mod's hook reaches the worldgen workers too, since each runs the worldgen phase",
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
fn: async () => {
|
||||
const dir = mods_dir((dir) =>
|
||||
write_mod(dir, "flat_sea", {
|
||||
worldgen: [
|
||||
"worldgen.ts",
|
||||
`import { engine, hook } from "cubi/engine";
|
||||
export function setup() {
|
||||
write_mod(
|
||||
dir,
|
||||
"flat_sea",
|
||||
`import { engine, hook } from "cubi/engine";
|
||||
export function worldgen() {
|
||||
hook(engine.common.NoiseTerrain.prototype, "density", { replace: () => -1 });
|
||||
}`,
|
||||
],
|
||||
})
|
||||
)
|
||||
);
|
||||
// it's all sea, so no spawn search
|
||||
const t = await game_with_pool(1, "engine-access-flat-sea-pool", dir, false);
|
||||
@@ -159,20 +152,19 @@ Deno.test({
|
||||
|
||||
Deno.test("a client mod hooks LocalPlayer from its .cubi", async () => {
|
||||
const dir = Deno.makeTempDirSync({ prefix: "bworld_engine_" });
|
||||
write_mod(dir, "floaty", {
|
||||
client: [
|
||||
"client.ts",
|
||||
`import { engine, hook } from "cubi/engine";
|
||||
write_mod(
|
||||
dir,
|
||||
"floaty",
|
||||
`import { engine, hook } from "cubi/engine";
|
||||
// everyone is a block higher than they think
|
||||
export function setup() {
|
||||
export function client() {
|
||||
hook(engine.client.LocalPlayer.prototype, "set_position", {
|
||||
before(_player, args) {
|
||||
args[1] += 1;
|
||||
},
|
||||
});
|
||||
}`,
|
||||
],
|
||||
});
|
||||
);
|
||||
const mod = (await load_and_check(dir))[0];
|
||||
assertEquals(mod.report.errors, []);
|
||||
const packed = read_mod(await pack_mod(mod), "floaty.cubi");
|
||||
@@ -340,18 +332,18 @@ Deno.test("hook needs a loading mod with engine access", async () => {
|
||||
Deno.test("cubi/engine without engine_access is an error in check-mods and in a .cubi", async () => {
|
||||
const dir = Deno.makeTempDirSync({ prefix: "bworld_engine_" });
|
||||
const script = `import { engine } from "cubi/engine";
|
||||
export function setup() {
|
||||
export function server() {
|
||||
console.log(engine.common.physics);
|
||||
}`;
|
||||
write_mod(dir, "sneaky", { server: ["server.ts", script] }, {});
|
||||
write_mod(dir, "sneaky", script, {});
|
||||
const [mod] = await load_and_check(dir);
|
||||
assertEquals(mod.report.errors, [
|
||||
`scripts/server.ts imports cubi/engine, which needs "engine_access": true in manifest.json`,
|
||||
`scripts/main.ts imports cubi/engine, which needs "engine_access": true in manifest.json`,
|
||||
]);
|
||||
|
||||
// packed anyway, the bundle gives it away
|
||||
const bytes = await pack_mod(mod);
|
||||
assertThrows(() => read_mod(bytes, "sneaky.cubi"), Error, "a script uses cubi/engine");
|
||||
assertThrows(() => read_mod(bytes, "sneaky.cubi"), Error, "its script uses cubi/engine");
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user