feat(launcher): bundle example mod templates and seed fresh mods folder once

This commit is contained in:
N0VAN0M4LY committed 2026-12-03 01:51:43 +08:00
1 parent ada635ecc7
commit 97707f533d
3 files changed
+46 -1

No files matched your search

@@ -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<void> {
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<boolean> {
try {
await access(path);
return true;
} catch {
return false;
}
}
export async function openModsFolder(): Promise<void> {
const { modsDir } = await resolveModsFolder();
const failure = await shell.openPath(modsDir);
@@ -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'),
};
}
+1
View File
@@ -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 });
}