fix(mods): serialize initial example seeding and refresh on window focus
This commit is contained in:
1 parent
97707f533d
commit
8636d0b71d
2 files changed
+44
-23
No files matched your search
@@ -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<string, Promise<void>>();
|
||||
|
||||
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<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 });
|
||||
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<boolean> {
|
||||
|
||||
@@ -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<ModsStateView>, successNotice?: string): void => {
|
||||
|
||||
Reference in new issue
Block a user