From 97707f533d2b1945cf22fafbb0abebcc1721bf69 Mon Sep 17 00:00:00 2001 From: N0VAN0M4LY <224130499+N0VAN0M4LY@users.noreply.github.com> Date: Thu, 3 Dec 2026 01:51:43 +0800 Subject: [PATCH] feat(launcher): bundle example mod templates and seed fresh mods folder once --- .../src/main/services/mods/mods-service.ts | 42 ++++++++++++++++++- .../main/services/resources/resource-paths.ts | 4 ++ scripts/build-windows-installer.ts | 1 + 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/apps/launcher/src/main/services/mods/mods-service.ts b/apps/launcher/src/main/services/mods/mods-service.ts index fca9e9b..e92670b 100644 --- a/apps/launcher/src/main/services/mods/mods-service.ts +++ b/apps/launcher/src/main/services/mods/mods-service.ts @@ -1,5 +1,5 @@ import { randomUUID } from 'node:crypto'; -import { mkdir, readFile, stat, writeFile } from 'node:fs/promises'; +import { access, cp, mkdir, readdir, readFile, stat, writeFile } from 'node:fs/promises'; import { isAbsolute, join } from 'node:path'; import { app, BrowserWindow, dialog, shell } from 'electron'; import { @@ -19,8 +19,10 @@ import { ModPresetTransferSchema, type ModListItem, type ModsStateView } from '@ import { readJsonOr, writeJsonAtomic } from '../../util/atomic-json.ts'; import { getSettings, updateSettings } from '../config/settings-store.ts'; import { listCopies } from '../install/install-record.ts'; +import { resourcePaths } from '../resources/resource-paths.ts'; const CLIENT_MODS_CONFIG_FILE = 'mods-config.json'; +const EXAMPLES_SEEDED_MARKER = '.wto-examples-seeded'; export function defaultModsFolder(): string { return join(app.getPath('userData'), 'mods'); @@ -32,9 +34,47 @@ export async function resolveModsFolder(): Promise<{ readonly modsDir: string; r const isCustomModsDir = settings.modsFolder !== null; const modsDir = settings.modsFolder ?? defaultModsDir; await mkdir(modsDir, { recursive: true }); + await seedBundledExampleModsOnce(modsDir); return { modsDir, defaultModsDir, isCustomModsDir }; } +/** + * Copy the bundled example mod templates into a fresh mods folder once so players and authors + * have ready-to-inspect templates (disabled by default). Writing a marker ensures deleting the + * examples later does not re-create them. + */ +async function seedBundledExampleModsOnce(modsDir: string): Promise { + 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 }); + } + } + } + await writeFile(markerPath, '1\n', 'utf8'); + } catch (error) { + console.warn('[mods] seeding bundled example mods skipped:', error); + } +} + +async function pathExists(path: string): Promise { + try { + await access(path); + return true; + } catch { + return false; + } +} + export async function openModsFolder(): Promise { const { modsDir } = await resolveModsFolder(); const failure = await shell.openPath(modsDir); diff --git a/apps/launcher/src/main/services/resources/resource-paths.ts b/apps/launcher/src/main/services/resources/resource-paths.ts index ca43202..68ec76c 100644 --- a/apps/launcher/src/main/services/resources/resource-paths.ts +++ b/apps/launcher/src/main/services/resources/resource-paths.ts @@ -17,6 +17,8 @@ export interface ResourcePaths { readonly networkHookLibrary: string; /** Multi-resolution Windows icon for the window and system tray. */ readonly appIcon: string; + /** Bundled example mod templates seeded into an empty mods folder on first use. */ + readonly modTemplates: string; } export function resourcePaths(): ResourcePaths { @@ -32,6 +34,7 @@ export function bundledResourcePaths(): ResourcePaths { patches: join(process.resourcesPath, 'patches'), networkHookLibrary: join(process.resourcesPath, 'native', 'wto_ws.dll'), appIcon: join(process.resourcesPath, 'icon.ico'), + modTemplates: join(process.resourcesPath, 'mod-templates'), }; } // The app path is apps/launcher; the repository is two levels up. @@ -40,6 +43,7 @@ export function bundledResourcePaths(): ResourcePaths { patches: join(repository, 'patches'), networkHookLibrary: join(repository, 'build', 'native', 'wto_ws.dll'), appIcon: join(repository, 'apps', 'launcher', 'resources', 'icon.ico'), + modTemplates: join(repository, 'dev-docs', 'templates', 'mods'), }; } diff --git a/scripts/build-windows-installer.ts b/scripts/build-windows-installer.ts index 9bf3ff0..5219cf3 100644 --- a/scripts/build-windows-installer.ts +++ b/scripts/build-windows-installer.ts @@ -70,6 +70,7 @@ async function stageInstaller(stage: string, manifest: LauncherManifest): Promis await cp(join(launcher, 'resources', 'icon.png'), join(resources, 'icon.png')); await cp(join(repository, 'LICENSE'), join(resources, 'LICENSE-WTOffline')); await cp(join(native, 'wto_ws.dll'), join(resources, 'native', 'wto_ws.dll')); + await cp(join(repository, 'dev-docs', 'templates', 'mods'), join(resources, 'mod-templates'), { recursive: true }); for (const folder of ['exe', 'gui', 'native', 'versions']) { await cp(join(repository, 'patches', folder), join(resources, 'patches', folder), { recursive: true }); }