feat(mods): add battle.onFilterMissions hook and per-template bundled seeding

This commit is contained in:
N0VAN0M4LY committed 2026-12-03 02:40:38 +08:00
1 parent 8636d0b71d
commit 5983e3f3e2
6 files changed
+127 -20

No files matched your search

@@ -40,10 +40,10 @@ export async function resolveModsFolder(): Promise<{ readonly modsDir: string; r
}
/**
* Copy the bundled example mod templates into a fresh mods folder once so players and authors
* have ready-to-inspect templates (disabled by default). Serialized per directory so concurrent
* startup calls (such as React StrictMode double mounts) wait for the copy to finish before
* scanning mods.
* Copy newly shipped bundled template mods into the mods folder once each (disabled by default).
* Tracks which template folder names have already been seeded in `.wto-examples-seeded` so
* deleting a bundled template never restores it, while newly added official templates still
* appear for existing users. Serialized per directory to prevent concurrent seeding races.
*/
async function seedBundledExampleModsOnce(modsDir: string): Promise<void> {
const key = modsDir.toLowerCase();
@@ -54,22 +54,25 @@ async function seedBundledExampleModsOnce(modsDir: string): Promise<void> {
}
const task = (async () => {
const markerPath = join(modsDir, EXAMPLES_SEEDED_MARKER);
if (await pathExists(markerPath)) return;
try {
const existingEntries = await readdir(modsDir);
if (existingEntries.length === 0) {
const templatesRoot = resourcePaths().modTemplates;
if (await pathExists(templatesRoot)) {
const templates = await readdir(templatesRoot, { withFileTypes: true });
for (const entry of templates) {
if (!entry.isDirectory()) continue;
const dest = join(modsDir, entry.name);
if (await pathExists(dest)) continue;
await cp(join(templatesRoot, entry.name), dest, { recursive: true });
}
const templatesRoot = resourcePaths().modTemplates;
if (!(await pathExists(templatesRoot))) return;
const seeded = await readSeededTemplateNames(markerPath, modsDir);
const templates = await readdir(templatesRoot, { withFileTypes: true });
let changed = false;
for (const entry of templates) {
if (!entry.isDirectory()) continue;
if (seeded.has(entry.name)) continue;
const dest = join(modsDir, entry.name);
if (!(await pathExists(dest))) {
await cp(join(templatesRoot, entry.name), dest, { recursive: true });
}
seeded.add(entry.name);
changed = true;
}
if (changed || !(await pathExists(markerPath))) {
await writeFile(markerPath, JSON.stringify({ seeded: [...seeded].sort() }, null, 2) + '\n', 'utf8');
}
await writeFile(markerPath, '1\n', 'utf8');
} catch (error) {
console.warn('[mods] seeding bundled example mods skipped:', error);
}
@@ -80,6 +83,26 @@ async function seedBundledExampleModsOnce(modsDir: string): Promise<void> {
await task;
}
async function readSeededTemplateNames(markerPath: string, modsDir: string): Promise<Set<string>> {
if (!(await pathExists(markerPath))) return new Set();
try {
const raw = (await readFile(markerPath, 'utf8')).trim();
if (raw === '1') {
// Legacy marker from initial 2-template version: mark existing folders as already seeded
// so newly added official templates (e.g. official-map-selector) are still delivered once.
const existing = await readdir(modsDir);
const set = new Set(['example-vehicle-tweak', 'example-server-script']);
for (const name of existing) set.add(name);
return set;
}
const parsed = JSON.parse(raw) as { seeded?: unknown };
if (Array.isArray(parsed.seeded)) {
return new Set(parsed.seeded.filter((item): item is string => typeof item === 'string'));
}
} catch { /* ignore */ }
return new Set();
}
async function pathExists(path: string): Promise<boolean> {
try {
await access(path);