[client] unpack game works!

This commit is contained in:
2024-11-15 11:20:35 +01:00
parent f9cd8471b4
commit bc70d6300b
6 changed files with 156 additions and 18 deletions

View File

@ -1,6 +1,6 @@
import {useEffect, useState} from 'react';
import {invoke} from '@tauri-apps/api/core';
import {listen} from '@tauri-apps/api/event';
import { useEffect, useState } from 'react';
import { invoke } from '@tauri-apps/api/core';
import { listen } from '@tauri-apps/api/event';
import { open } from '@tauri-apps/plugin-dialog';
import { load } from '@tauri-apps/plugin-store';
@ -48,6 +48,32 @@ const App = () => {
}
};
useEffect(() => {
// Listen for game-unpack-finished events specifically
const setupUnpackListener = async () => {
const unlisten = await listen('game-unpack-finished', (event) => {
const game_id = event.payload as string;
console.log(`🗲 game-unpack-finished ${game_id} event received`);
console.log('Current gameDir in listener:', gameDir); // Add this log
setGameItems(prev => prev.map(item => item.id === game_id
? {...item, install_status: InstallStatus.Installed}
: item));
// Convert to string explicitly and verify it's not empty
const pathString = String(gameDir);
if (!pathString) {
console.error('gameDir is empty before invoke!');
return;
}
invoke('update_game_directory', { path: pathString })
.catch(error => console.error('❌ Error updating game directory:', error));
});
return unlisten;
};
setupUnpackListener();
}, [gameDir]);
useEffect(() => {
if (gameDir) {
// store game directory in persistent storage
@ -88,7 +114,7 @@ const App = () => {
const game_id = event.payload as string;
console.log(`🗲 game-download-begin ${game_id} event received`);
setGameItems(prev => prev.map(item => item.id === game_id
? {...item, install_status: InstallStatus.Downloading}
? { ...item, install_status: InstallStatus.Downloading }
: item));
});
@ -97,7 +123,7 @@ const App = () => {
const game_id = event.payload as string;
console.log(`🗲 game-download-finished ${game_id} event received`);
setGameItems(prev => prev.map(item => item.id === game_id
? {...item, install_status: InstallStatus.Unpacking}
? { ...item, install_status: InstallStatus.Unpacking }
: item));
});
@ -128,7 +154,7 @@ const App = () => {
const runGame = async (id: string) => {
console.log(`🎯 Running game with id=${id}`);
try {
const result = await invoke('run_game', {id});
const result = await invoke('run_game', { id });
console.log(`✅ Game started, result=${result}`);
} catch (error) {
console.error('❌ Error running game:', error);
@ -138,12 +164,12 @@ const App = () => {
const installGame = async (id: string) => {
console.log(`🎯 Installing game with id=${id}`);
try {
const success = await invoke('install_game', {id});
const success = await invoke('install_game', { id });
if (success) {
console.log(`✅ Game install for id=${id} started...`);
// update install status in gameItems for this game
setGameItems(prev => prev.map(item => item.id === id
? {...item, install_status: InstallStatus.CheckingServer}
? { ...item, install_status: InstallStatus.CheckingServer }
: item));
} else {
// game is already being installed
@ -203,14 +229,14 @@ const App = () => {
<span className="size-text">{item.size.toString()}</span>
</div>
<div className="play-button"
onClick={() => item.installed
? runGame(item.id)
: installGame(item.id)}>
onClick={() => item.installed
? runGame(item.id)
: installGame(item.id)}>
{item.installed ? 'Play'
: item.install_status === InstallStatus.CheckingServer ? 'Checking server...'
: item.install_status === InstallStatus.Downloading ? 'Downloading...'
: item.install_status === InstallStatus.Unpacking ? 'Unpacking...'
: 'Install'}
: item.install_status === InstallStatus.Downloading ? 'Downloading...'
: item.install_status === InstallStatus.Unpacking ? 'Unpacking...'
: 'Install'}
</div>
</div>
);