9bafd981d7
Some games include a language.txt marker in the unpacked local tree, similar in spirit to account_name.txt. Installs and updates now carry the launcher language alongside the account name so those game-provided marker files are rewritten before staged files are promoted into local/. The Tauri command boundary keeps the UI setting vocabulary as de/en, then maps it to the file vocabulary expected by games: german or english. Unknown values continue through the existing DEFAULT_LANGUAGE path, so the marker file falls back to english just like script launch arguments fall back to en. The transaction layer deliberately reuses the same first-match traversal helper for both marker files. The searches stay independent, so games may place account_name.txt and language.txt in different directories if their archive layout requires that. Test Plan: - just fmt - just test - just frontend-test - just clippy - deno task build - git diff --check Refs: none
117 lines
3.8 KiB
TypeScript
117 lines
3.8 KiB
TypeScript
import { useCallback } from 'react';
|
|
import { invoke } from '@tauri-apps/api/core';
|
|
|
|
import { type UseGamesResult } from './useGames';
|
|
import { type UISettings } from './useSettings';
|
|
|
|
export interface GameActions {
|
|
play: (id: string) => Promise<void>;
|
|
startServer: (id: string) => Promise<void>;
|
|
install: (id: string) => Promise<void>;
|
|
update: (id: string) => Promise<void>;
|
|
uninstall: (id: string) => Promise<void>;
|
|
removeDownload: (id: string) => Promise<void>;
|
|
cancelDownload: (id: string) => Promise<void>;
|
|
viewFiles: (id: string) => Promise<void>;
|
|
}
|
|
|
|
/**
|
|
* Thin wrappers over the backend `run_game` / `install_game` / `update_game`
|
|
* / `uninstall_game` / `remove_downloaded_game` commands. Peer-backed downloads
|
|
* are marked as "checking peers" until the backend emits an authoritative
|
|
* operation snapshot; cancellation waits for the backend to clear that snapshot.
|
|
*/
|
|
export const useGameActions = (
|
|
games: UseGamesResult,
|
|
settings: Pick<UISettings, 'language' | 'username'>,
|
|
): GameActions => {
|
|
const play = useCallback(async (id: string) => {
|
|
try {
|
|
await invoke('run_game', {
|
|
id,
|
|
language: settings.language,
|
|
username: settings.username,
|
|
});
|
|
} catch (err) {
|
|
console.error('run_game failed:', err);
|
|
}
|
|
}, [settings.language, settings.username]);
|
|
|
|
const startServer = useCallback(async (id: string) => {
|
|
try {
|
|
await invoke('start_server', {
|
|
id,
|
|
language: settings.language,
|
|
username: settings.username,
|
|
});
|
|
} catch (err) {
|
|
console.error('start_server failed:', err);
|
|
}
|
|
}, [settings.language, settings.username]);
|
|
|
|
const install = useCallback(async (id: string) => {
|
|
try {
|
|
const success = await invoke<boolean>('install_game', {
|
|
id,
|
|
language: settings.language,
|
|
username: settings.username,
|
|
});
|
|
if (!success) return;
|
|
|
|
const game = games.games.find(item => item.id === id);
|
|
if (!game?.downloaded) {
|
|
games.markChecking(id);
|
|
}
|
|
} catch (err) {
|
|
console.error('install_game failed:', err);
|
|
}
|
|
}, [games, settings.language, settings.username]);
|
|
|
|
const update = useCallback(async (id: string) => {
|
|
try {
|
|
const success = await invoke<boolean>('update_game', {
|
|
id,
|
|
language: settings.language,
|
|
username: settings.username,
|
|
});
|
|
if (success) games.markChecking(id);
|
|
} catch (err) {
|
|
console.error('update_game failed:', err);
|
|
}
|
|
}, [games, settings.language, settings.username]);
|
|
|
|
const uninstall = useCallback(async (id: string) => {
|
|
try {
|
|
await invoke('uninstall_game', { id });
|
|
} catch (err) {
|
|
console.error('uninstall_game failed:', err);
|
|
}
|
|
}, []);
|
|
|
|
const removeDownload = useCallback(async (id: string) => {
|
|
try {
|
|
await invoke('remove_downloaded_game', { id });
|
|
} catch (err) {
|
|
console.error('remove_downloaded_game failed:', err);
|
|
}
|
|
}, []);
|
|
|
|
const cancelDownload = useCallback(async (id: string) => {
|
|
try {
|
|
await invoke('cancel_download', { id });
|
|
} catch (err) {
|
|
console.error('cancel_download failed:', err);
|
|
}
|
|
}, []);
|
|
|
|
const viewFiles = useCallback(async (id: string) => {
|
|
try {
|
|
await invoke('open_game_files', { id });
|
|
} catch (err) {
|
|
console.error('open_game_files failed:', err);
|
|
}
|
|
}, []);
|
|
|
|
return { play, startServer, install, update, uninstall, removeDownload, cancelDownload, viewFiles };
|
|
};
|