feat: auto-resume download process & auto-retry if disconnected (#143)

This commit is contained in:
Fazri Gading
2026-01-25 01:36:20 +08:00
committed by GitHub
parent a6f716c61b
commit a7d0523186
7 changed files with 1503 additions and 247 deletions

320
main.js
View File

@@ -255,11 +255,11 @@ app.whenReady().then(async () => {
mainWindow.webContents.send('lock-play-button', true);
}
const progressCallback = (message, percent, speed, downloaded, total) => {
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('first-launch-progress', { message, percent, speed, downloaded, total });
}
};
const progressCallback = (message, percent, speed, downloaded, total, retryState) => {
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('first-launch-progress', { message, percent, speed, downloaded, total, retryState });
}
};
const firstLaunchResult = await Promise.race([
handleFirstLaunchCheck(progressCallback),
@@ -346,20 +346,21 @@ app.on('window-all-closed', () => {
});
ipcMain.handle('launch-game', async (event, playerName, javaPath, installPath, gpuPreference) => {
try {
const progressCallback = (message, percent, speed, downloaded, total) => {
if (mainWindow && !mainWindow.isDestroyed()) {
const data = {
message: message || null,
percent: percent !== null && percent !== undefined ? Math.min(100, Math.max(0, percent)) : null,
speed: speed !== null && speed !== undefined ? speed : null,
downloaded: downloaded !== null && downloaded !== undefined ? downloaded : null,
total: total !== null && total !== undefined ? total : null
};
mainWindow.webContents.send('progress-update', data);
}
};
ipcMain.handle('launch-game', async (event, playerName, javaPath, installPath, gpuPreference) => {
try {
const progressCallback = (message, percent, speed, downloaded, total, retryState) => {
if (mainWindow && !mainWindow.isDestroyed()) {
const data = {
message: message || null,
percent: percent !== null && percent !== undefined ? Math.min(100, Math.max(0, percent)) : null,
speed: speed !== null && speed !== undefined ? speed : null,
downloaded: downloaded !== null && downloaded !== undefined ? downloaded : null,
total: total !== null && total !== undefined ? total : null,
retryState: retryState || null
};
mainWindow.webContents.send('progress-update', data);
}
};
const result = await launchGameWithVersionCheck(playerName, progressCallback, javaPath, installPath, gpuPreference);
@@ -389,47 +390,108 @@ ipcMain.handle('launch-game', async (event, playerName, javaPath, installPath, g
}
});
ipcMain.handle('install-game', async (event, playerName, javaPath, installPath, branch) => {
try {
console.log(`[IPC] install-game called with branch: ${branch || 'default'}`);
// Signal installation start
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('installation-start');
}
const progressCallback = (message, percent, speed, downloaded, total) => {
if (mainWindow && !mainWindow.isDestroyed()) {
const data = {
message: message || null,
percent: percent !== null && percent !== undefined ? Math.min(100, Math.max(0, percent)) : null,
speed: speed !== null && speed !== undefined ? speed : null,
downloaded: downloaded !== null && downloaded !== undefined ? downloaded : null,
total: total !== null && total !== undefined ? total : null
};
mainWindow.webContents.send('progress-update', data);
}
};
const result = await installGame(playerName, progressCallback, javaPath, installPath, branch);
// Signal installation end
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('installation-end');
}
return result;
} catch (error) {
console.error('Install error:', error);
const errorMessage = error.message || error.toString();
// Signal installation end on error too
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('installation-end');
}
return { success: false, error: errorMessage };
}
ipcMain.handle('install-game', async (event, playerName, javaPath, installPath, branch) => {
try {
console.log(`[IPC] install-game called with branch: ${branch || 'default'}`);
// Signal installation start
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('installation-start');
}
const progressCallback = (message, percent, speed, downloaded, total, retryState) => {
if (mainWindow && !mainWindow.isDestroyed()) {
const data = {
message: message || null,
percent: percent !== null && percent !== undefined ? Math.min(100, Math.max(0, percent)) : null,
speed: speed !== null && speed !== undefined ? speed : null,
downloaded: downloaded !== null && downloaded !== undefined ? downloaded : null,
total: total !== null && total !== undefined ? total : null,
retryState: retryState || null
};
mainWindow.webContents.send('progress-update', data);
}
};
const result = await installGame(playerName, progressCallback, javaPath, installPath, branch);
// Signal installation end
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('installation-end');
}
// Ensure we always return a result for the IPC handler
const successResponse = result || { success: true };
console.log('[Main] Returning success response for install-game:', successResponse);
return successResponse;
} catch (error) {
console.error('Install error:', error);
const errorMessage = error.message || error.toString();
// Enhanced error data extraction for both download and Butler errors
let errorData = {
message: errorMessage,
error: true,
canRetry: true,
retryData: null
};
// Handle Butler-specific errors
if (error.butlerError) {
console.log('[Main] Processing Butler error with retry context');
errorData.retryData = {
branch: error.branch || 'release',
fileName: error.fileName || '4.pwr',
cacheDir: error.cacheDir
};
errorData.canRetry = error.canRetry !== undefined ? error.canRetry : true;
// Add Butler-specific error details
if (error.stderr) {
console.error('[Main] Butler stderr:', error.stderr);
}
if (error.stdout) {
console.log('[Main] Butler stdout:', error.stdout);
}
if (error.errorCode) {
console.log('[Main] Butler error code:', error.errorCode);
}
}
// Handle PWR download errors
else if (error.branch && error.fileName) {
console.log('[Main] Processing PWR download error with retry context');
errorData.retryData = {
branch: error.branch,
fileName: error.fileName,
cacheDir: error.cacheDir
};
errorData.canRetry = error.canRetry !== undefined ? error.canRetry : true;
}
// Default fallback for other errors
else {
console.log('[Main] Processing generic error, creating default retry data');
errorData.retryData = {
branch: 'release',
fileName: '4.pwr'
};
}
// Send enhanced error info for retry UI
if (mainWindow && !mainWindow.isDestroyed()) {
console.log('[Main] Sending error data to renderer:', errorData);
mainWindow.webContents.send('progress-update', errorData);
}
// Signal installation end on error too
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('installation-end');
}
// Always return a proper response to prevent timeout
const errorResponse = { success: false, error: errorMessage };
console.log('[Main] Returning error response for install-game:', errorResponse);
return errorResponse;
}
});
ipcMain.handle('save-username', (event, username) => {
@@ -519,18 +581,19 @@ ipcMain.handle('select-install-path', async () => {
ipcMain.handle('accept-first-launch-update', async (event, existingGame) => {
try {
const progressCallback = (message, percent, speed, downloaded, total) => {
if (mainWindow && !mainWindow.isDestroyed()) {
const data = {
message: message || null,
percent: percent !== null && percent !== undefined ? Math.min(100, Math.max(0, percent)) : null,
speed: speed !== null && speed !== undefined ? speed : null,
downloaded: downloaded !== null && downloaded !== undefined ? downloaded : null,
total: total !== null && total !== undefined ? total : null
};
mainWindow.webContents.send('first-launch-progress', data);
}
};
const progressCallback = (message, percent, speed, downloaded, total, retryState) => {
if (mainWindow && !mainWindow.isDestroyed()) {
const data = {
message: message || null,
percent: percent !== null && percent !== undefined ? Math.min(100, Math.max(0, percent)) : null,
speed: speed !== null && speed !== undefined ? speed : null,
downloaded: downloaded !== null && downloaded !== undefined ? downloaded : null,
total: total !== null && total !== undefined ? total : null,
retryState: retryState || null
};
mainWindow.webContents.send('first-launch-progress', data);
}
};
const result = await proposeGameUpdate(existingGame, progressCallback);
@@ -573,28 +636,94 @@ ipcMain.handle('uninstall-game', async () => {
}
});
ipcMain.handle('repair-game', async () => {
try {
const progressCallback = (message, percent, speed, downloaded, total) => {
if (mainWindow && !mainWindow.isDestroyed()) {
const data = {
message: message || null,
percent: percent !== null && percent !== undefined ? Math.min(100, Math.max(0, percent)) : null,
speed: speed !== null && speed !== undefined ? speed : null,
downloaded: downloaded !== null && downloaded !== undefined ? downloaded : null,
total: total !== null && total !== undefined ? total : null
};
mainWindow.webContents.send('progress-update', data);
}
};
const result = await repairGame(progressCallback);
return result;
} catch (error) {
console.error('Repair error:', error);
return { success: false, error: error.message };
}
});
ipcMain.handle('repair-game', async () => {
try {
const progressCallback = (message, percent, speed, downloaded, total, retryState) => {
if (mainWindow && !mainWindow.isDestroyed()) {
const data = {
message: message || null,
percent: percent !== null && percent !== undefined ? Math.min(100, Math.max(0, percent)) : null,
speed: speed !== null && speed !== undefined ? speed : null,
downloaded: downloaded !== null && downloaded !== undefined ? downloaded : null,
total: total !== null && total !== undefined ? total : null,
retryState: retryState || null
};
mainWindow.webContents.send('progress-update', data);
}
};
const result = await repairGame(progressCallback);
return result;
} catch (error) {
console.error('Repair error:', error);
const errorMessage = error.message || error.toString();
return { success: false, error: errorMessage };
}
});
ipcMain.handle('retry-download', async (event, retryData) => {
try {
console.log('[IPC] retry-download called with data:', retryData);
// Handle null retry data gracefully
if (!retryData || !retryData.branch || !retryData.fileName) {
console.log('[IPC] Invalid retry data, using defaults');
retryData = {
branch: 'release',
fileName: '4.pwr'
};
}
const progressCallback = (message, percent, speed, downloaded, total, retryState) => {
if (mainWindow && !mainWindow.isDestroyed()) {
const data = {
message: message || null,
percent: percent !== null && percent !== undefined ? Math.min(100, Math.max(0, percent)) : null,
speed: speed !== null && speed !== undefined ? speed : null,
downloaded: downloaded !== null && downloaded !== undefined ? downloaded : null,
total: total !== null && total !== undefined ? total : null,
retryState: retryState || null
};
mainWindow.webContents.send('progress-update', data);
}
};
// Extract PWR download info from retryData
const branch = retryData.branch;
const fileName = retryData.fileName;
const cacheDir = retryData.cacheDir;
console.log(`[IPC] Retrying PWR download: branch=${branch}, fileName=${fileName}`);
// Perform the retry with enhanced context
await retryPWRDownload(branch, fileName, progressCallback, cacheDir);
return { success: true };
} catch (error) {
console.error('Retry download error:', error);
const errorMessage = error.message || error.toString();
// Send error update to frontend with context
if (mainWindow && !mainWindow.isDestroyed()) {
const data = {
message: errorMessage,
error: true,
canRetry: true,
retryData: {
branch: retryData?.branch || 'release',
fileName: retryData?.fileName || '4.pwr',
cacheDir: retryData?.cacheDir
}
};
mainWindow.webContents.send('progress-update', data);
}
// Always return a proper response to prevent timeout
const errorResponse = { success: false, error: errorMessage };
console.log('[Main] Returning error response for retry-download:', errorResponse);
return errorResponse;
}
});
ipcMain.handle('get-hytale-news', async () => {
try {
@@ -710,7 +839,8 @@ ipcMain.handle('load-settings', async () => {
}
});
const { getModsPath, loadInstalledMods, downloadMod, uninstallMod, toggleMod, getCurrentUuid, getAllUuidMappings, setUuidForUser, generateNewUuid, deleteUuidForUser, resetCurrentUserUuid } = require('./backend/launcher');
const { getModsPath, loadInstalledMods, downloadMod, uninstallMod, toggleMod, getCurrentUuid, getAllUuidMappings, setUuidForUser, generateNewUuid, deleteUuidForUser, resetCurrentUserUuid } = require('./backend/launcher');
const { retryPWRDownload } = require('./backend/managers/gameManager');
const os = require('os');
ipcMain.handle('get-local-app-data', async () => {