From 87b168dd4cb22731f6b9cfe5c6293aed7ff69d3b Mon Sep 17 00:00:00 2001 From: AMIAY Date: Sat, 24 Jan 2026 12:22:15 +0100 Subject: [PATCH] fix --- backend/managers/gameManager.js | 36 ++++++++++++++++----------------- backend/utils/clientPatcher.js | 35 +++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/backend/managers/gameManager.js b/backend/managers/gameManager.js index c2be5d7..1673704 100644 --- a/backend/managers/gameManager.js +++ b/backend/managers/gameManager.js @@ -155,7 +155,22 @@ async function updateGameFiles(newVersion, progressCallback, gameDir = GAME_DIR, try { if (progressCallback) { - progressCallback('Updating game files...', 0, null, null, null); + progressCallback('Backing up user data...', 5, null, null, null); + } + + // Backup UserData AVANT de télécharger/installer (critical for same-branch updates) + try { + console.log(`[UpdateGameFiles] Attempting to backup UserData from old branch: ${oldBranch}`); + backupPath = await userDataBackup.backupUserData(installPath, oldBranch, hasVersionConfig); + if (backupPath) { + console.log(`[UpdateGameFiles] ✓ UserData backed up from ${oldBranch}: ${backupPath}`); + } + } catch (backupError) { + console.warn('[UpdateGameFiles] ✗ UserData backup failed:', backupError.message); + } + + if (progressCallback) { + progressCallback('Updating game files...', 10, null, null, null); } console.log(`Updating game files to version: ${newVersion} (branch: ${branch})`); @@ -167,32 +182,17 @@ async function updateGameFiles(newVersion, progressCallback, gameDir = GAME_DIR, fs.mkdirSync(tempUpdateDir, { recursive: true }); if (progressCallback) { - progressCallback('Downloading new game version...', 10, null, null, null); + progressCallback('Downloading new game version...', 20, null, null, null); } const pwrFile = await downloadPWR(branch, newVersion, progressCallback, cacheDir); if (progressCallback) { - progressCallback('Extracting new files...', 50, null, null, null); + progressCallback('Extracting new files...', 60, null, null, null); } await applyPWR(pwrFile, progressCallback, tempUpdateDir, toolsDir); - if (progressCallback) { - progressCallback('Backing up user data...', 70, null, null, null); - } - - // Backup UserData from OLD branch (before switching) - try { - console.log(`[UpdateGameFiles] Attempting to backup UserData from old branch: ${oldBranch}`); - backupPath = await userDataBackup.backupUserData(installPath, oldBranch, hasVersionConfig); - if (backupPath) { - console.log(`[UpdateGameFiles] ✓ UserData backed up from ${oldBranch}: ${backupPath}`); - } - } catch (backupError) { - console.warn('[UpdateGameFiles] ✗ UserData backup failed:', backupError.message); - } - if (progressCallback) { progressCallback('Replacing game files...', 80, null, null, null); } diff --git a/backend/utils/clientPatcher.js b/backend/utils/clientPatcher.js index cbbcee3..7be05df 100644 --- a/backend/utils/clientPatcher.js +++ b/backend/utils/clientPatcher.js @@ -323,19 +323,19 @@ class ClientPatcher { // FORCE PATCHING: Always patch, never skip console.log(`Force patching server for ${newDomain}`); - if (progressCallback) { - progressCallback('Preparing to patch server...', 10); - } - - console.log('Creating backup...'); - this.backupClient(serverPath); - if (progressCallback) { progressCallback('Extracting server JAR...', 20); } console.log('Opening server JAR...'); - const zip = new AdmZip(serverPath); + let zip; + try { + zip = new AdmZip(serverPath); + } catch (zipError) { + console.error('Failed to read server JAR:', zipError.message); + return { success: false, error: `Failed to read JAR: ${zipError.message}` }; + } + const entries = zip.getEntries(); console.log(`JAR contains ${entries.length} entries`); @@ -375,7 +375,24 @@ class ClientPatcher { } console.log('Writing patched JAR...'); - zip.writeZip(serverPath); + const tempPath = serverPath + '.patched.tmp'; + + // Write to temp file first to avoid corruption + try { + zip.writeZip(tempPath); + + // Replace original with patched version + if (fs.existsSync(serverPath)) { + fs.unlinkSync(serverPath); + } + fs.renameSync(tempPath, serverPath); + } catch (writeError) { + // Cleanup temp file if it exists + if (fs.existsSync(tempPath)) { + fs.unlinkSync(tempPath); + } + throw writeError; + } this.markAsPatched(serverPath);