Single module modding
This commit is contained in:
+61
-33
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// deno task check-mods
|
||||
// validates every mod in mods/: manifests, data files, textures, references between them, and typechecks scripts
|
||||
// validates every mod in mods/: manifests, data files, textures, references between them, and typechecks their scripts
|
||||
import * as z from "zod";
|
||||
import {
|
||||
data_file,
|
||||
@@ -25,7 +25,7 @@ import {
|
||||
} from "@/common/mod_data.ts";
|
||||
import { json_object } from "@/common/schema.ts";
|
||||
import { check_references, load_order, type LoadedMod, type ModReport } from "@/common/mod_check.ts";
|
||||
import { png_size, texture_size_problem } from "@/common/mod.ts";
|
||||
import { data_url, png_size, texture_size_problem } from "@/common/mod.ts";
|
||||
import { type TextureJson, validate_texture_json } from "@/common/texture_animation.ts";
|
||||
import { type LootTableJson, validate_loot_table } from "@/common/loot.ts";
|
||||
import { validate_biome } from "@/common/worldgen/biomes.ts";
|
||||
@@ -67,23 +67,35 @@ export async function load_and_check(mods_dir = "mods"): Promise<LoadedMod[]> {
|
||||
return mods;
|
||||
}
|
||||
|
||||
// runs the mod's content script and adds the blocks it registers after its files' ones, named after the script, like
|
||||
// the server does with a .cubi's. packing leaves them out, the server registers them again
|
||||
// runs its script's content phase and adds the blocks it registers after its files' ones, named after the script,
|
||||
// like the server does with a .cubi's. packing leaves them out, the server registers them again
|
||||
async function add_registered_content(mod: LoadedMod) {
|
||||
const path = content_script(mod);
|
||||
const path = main_script(mod);
|
||||
if (path === undefined || !exists(`${mod.dir}/${path}`)) return;
|
||||
// only this folder's deno.json is in this realm's import map, so a mod with its own is bundled first
|
||||
let url = toFileUrl(resolve(mod.dir, path)).href;
|
||||
if (mod_deno_config(mod.dir)) {
|
||||
try {
|
||||
url = data_url(await bundle_script(mod.dir, `${mod.dir}/${path}`));
|
||||
} catch (e) {
|
||||
mod.report.errors.push((e as Error).message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
const { blocks, problems } = await run_content_script({
|
||||
mod: mod.id,
|
||||
version: String(mod.manifest?.version),
|
||||
url: toFileUrl(resolve(mod.dir, path)).href,
|
||||
engine_access: mod.manifest?.engine_access === true,
|
||||
url,
|
||||
});
|
||||
for (const json of blocks) mod.blocks.push({ file: path, json });
|
||||
for (const problem of problems) mod.report.errors.push(`${path}: ${problem}`);
|
||||
}
|
||||
|
||||
// the path of the mod's content script in its folder, which is also the file its blocks are listed under
|
||||
export function content_script(mod: LoadedMod): string | undefined {
|
||||
const path = (mod.manifest?.scripts as Record<string, unknown> | undefined)?.content;
|
||||
// the path of the mod's script in its folder, which is also the file the blocks its content phase registers are
|
||||
// listed under
|
||||
export function main_script(mod: LoadedMod): string | undefined {
|
||||
const path = mod.manifest?.main;
|
||||
return typeof path === "string" ? path : undefined;
|
||||
}
|
||||
|
||||
@@ -97,7 +109,7 @@ export async function check_mods(mods_dir = "mods", options = { typecheck: true
|
||||
|
||||
if (options.typecheck) {
|
||||
for (const mod of mods) {
|
||||
await typecheck_scripts(mod);
|
||||
await typecheck_script(mod);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,22 +156,14 @@ function load_mod(mods_dir: string, id: string): LoadedMod {
|
||||
error(`"cubi" is reserved for the engine itself`);
|
||||
}
|
||||
|
||||
const scripts = (mod.manifest?.scripts ?? {}) as Record<string, string>;
|
||||
for (const [side, path] of Object.entries(scripts)) {
|
||||
if (typeof path === "string" && !exists(`${dir}/${path}`)) {
|
||||
error(`manifest.json: scripts.${side} ${path} doesn't exist`);
|
||||
}
|
||||
}
|
||||
const main = main_script(mod);
|
||||
if (main !== undefined && !exists(`${dir}/${main}`)) error(`manifest.json: main ${main} doesn't exist`);
|
||||
// cubi/engine needs engine_access, it's checked again on the bundled code when a .cubi loads
|
||||
if (mod.manifest?.engine_access !== true) {
|
||||
const folders = new Set(Object.values(scripts).filter((p) => typeof p === "string").map((p) => dirname(p)));
|
||||
for (const folder of folders) {
|
||||
for (const file of [...walk(`${dir}/${folder}`, ".ts"), ...walk(`${dir}/${folder}`, ".js")]) {
|
||||
if (/["']cubi\/engine["']/.test(Deno.readTextFileSync(`${dir}/${folder}/${file}`))) {
|
||||
error(
|
||||
`${folder}/${file} imports cubi/engine, which needs "engine_access": true in manifest.json`,
|
||||
);
|
||||
}
|
||||
if (mod.manifest?.engine_access !== true && main !== undefined) {
|
||||
const folder = dirname(main);
|
||||
for (const file of [...walk(`${dir}/${folder}`, ".ts"), ...walk(`${dir}/${folder}`, ".js")]) {
|
||||
if (/["']cubi\/engine["']/.test(Deno.readTextFileSync(`${dir}/${folder}/${file}`))) {
|
||||
error(`${folder}/${file} imports cubi/engine, which needs "engine_access": true in manifest.json`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -308,22 +312,20 @@ function load_mod(mods_dir: string, id: string): LoadedMod {
|
||||
return mod;
|
||||
}
|
||||
|
||||
async function typecheck_scripts(mod: LoadedMod) {
|
||||
const scripts = Object.values((mod.manifest?.scripts ?? {}) as Record<string, string>)
|
||||
.filter((path) => typeof path === "string" && exists(`${mod.dir}/${path}`))
|
||||
.map((path) => `${mod.dir}/${path}`);
|
||||
if (scripts.length === 0) return;
|
||||
async function typecheck_script(mod: LoadedMod) {
|
||||
const path = main_script(mod);
|
||||
if (path === undefined || !exists(`${mod.dir}/${path}`)) return;
|
||||
|
||||
const config = mod_deno_config(mod.dir) ?? "deno.json";
|
||||
const output = await new Deno.Command(Deno.execPath(), {
|
||||
args: ["check", "--config", config, ...scripts],
|
||||
args: ["check", "--config", config, `${mod.dir}/${path}`],
|
||||
stdout: "piped",
|
||||
stderr: "piped",
|
||||
env: { NO_COLOR: "1" },
|
||||
}).output();
|
||||
if (!output.success) {
|
||||
const text = new TextDecoder().decode(output.stderr).trim();
|
||||
mod.report.errors.push(`scripts don't typecheck:\n${text}`);
|
||||
mod.report.errors.push(`its script doesn't typecheck:\n${text}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -366,7 +368,33 @@ function safe_read_dir(dir: string): Deno.DirEntry[] {
|
||||
}
|
||||
}
|
||||
|
||||
// a mod with its own deno.json is its own deno project, see pack_mod.ts
|
||||
// a mod is a deno project: with its own deno.json its script is bundled with it (its imports, npm packages and so
|
||||
// on), otherwise with the deno.json of the folder the build runs in. for browsers, since players run it too, and the
|
||||
// server's realms have no permissions, so deno's own apis are no use there
|
||||
export async function bundle_script(mod_dir: string, entry: string): Promise<string> {
|
||||
const platform = "browser";
|
||||
const config = mod_deno_config(mod_dir);
|
||||
if (!config) {
|
||||
const result = await Deno.bundle({ entrypoints: [entry], platform, write: false, minify: false });
|
||||
if (!result.success || !result.outputFiles?.length) {
|
||||
throw new Error(`Couldn't bundle ${entry}:\n${result.errors.map((e) => e.text).join("\n")}`);
|
||||
}
|
||||
return result.outputFiles[0].text();
|
||||
}
|
||||
|
||||
const result = await new Deno.Command(Deno.execPath(), {
|
||||
args: ["bundle", "--config", config, "--platform", platform, entry],
|
||||
stdout: "piped",
|
||||
stderr: "piped",
|
||||
env: { NO_COLOR: "1" },
|
||||
}).output();
|
||||
if (!result.success) {
|
||||
throw new Error(`Couldn't bundle ${entry}:\n${new TextDecoder().decode(result.stderr).trim()}`);
|
||||
}
|
||||
return new TextDecoder().decode(result.stdout);
|
||||
}
|
||||
|
||||
// a mod with its own deno.json is its own deno project, see bundle_script
|
||||
export function mod_deno_config(mod_dir: string): string | undefined {
|
||||
return ["deno.json", "deno.jsonc"].map((name) => `${mod_dir}/${name}`).find(exists);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user