From 8636d0b71d38875e109460c5681ace902b20a7a1 Mon Sep 17 00:00:00 2001 From: N0VAN0M4LY <224130499+N0VAN0M4LY@users.noreply.github.com> Date: Thu, 3 Dec 2026 01:55:20 +0800 Subject: [PATCH] fix(mods): serialize initial example seeding and refresh on window focus --- .../src/main/services/mods/mods-service.ts | 52 ++++++++++++------- .../src/renderer/src/pages/mods/ModsPage.tsx | 15 ++++-- 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/apps/launcher/src/main/services/mods/mods-service.ts b/apps/launcher/src/main/services/mods/mods-service.ts index e92670b..e374bd3 100644 --- a/apps/launcher/src/main/services/mods/mods-service.ts +++ b/apps/launcher/src/main/services/mods/mods-service.ts @@ -23,6 +23,7 @@ import { resourcePaths } from '../resources/resource-paths.ts'; const CLIENT_MODS_CONFIG_FILE = 'mods-config.json'; const EXAMPLES_SEEDED_MARKER = '.wto-examples-seeded'; +const inFlightSeeds = new Map>(); export function defaultModsFolder(): string { return join(app.getPath('userData'), 'mods'); @@ -40,30 +41,43 @@ 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). Writing a marker ensures deleting the - * examples later does not re-create them. + * 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. */ 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 }); + const key = modsDir.toLowerCase(); + const pending = inFlightSeeds.get(key); + if (pending) { + await pending; + return; + } + 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 }); + } } } + await writeFile(markerPath, '1\n', 'utf8'); + } catch (error) { + console.warn('[mods] seeding bundled example mods skipped:', error); } - await writeFile(markerPath, '1\n', 'utf8'); - } catch (error) { - console.warn('[mods] seeding bundled example mods skipped:', error); - } + })().finally(() => { + inFlightSeeds.delete(key); + }); + inFlightSeeds.set(key, task); + await task; } async function pathExists(path: string): Promise { diff --git a/apps/launcher/src/renderer/src/pages/mods/ModsPage.tsx b/apps/launcher/src/renderer/src/pages/mods/ModsPage.tsx index 120cfc7..d0e9de0 100644 --- a/apps/launcher/src/renderer/src/pages/mods/ModsPage.tsx +++ b/apps/launcher/src/renderer/src/pages/mods/ModsPage.tsx @@ -47,10 +47,17 @@ export function ModsPage({ copy, section, readOnly, onError }: Props) { useEffect(() => { let mounted = true; - ipcInvoke('mods:getState', { copyId: copy.id }) - .then((result) => { if (mounted) setState(result); }) - .catch(onError); - return () => { mounted = false; }; + const sync = (): void => { + ipcInvoke('mods:getState', { copyId: copy.id }) + .then((result) => { if (mounted) setState(result); }) + .catch(onError); + }; + sync(); + window.addEventListener('focus', sync); + return () => { + mounted = false; + window.removeEventListener('focus', sync); + }; }, [copy.id, onError]); const runAction = (action: Promise, successNotice?: string): void => {