This commit is contained in:
AMIAY
2026-01-24 12:22:15 +01:00
parent 679f065e24
commit 87b168dd4c
2 changed files with 44 additions and 27 deletions

View File

@@ -155,7 +155,22 @@ async function updateGameFiles(newVersion, progressCallback, gameDir = GAME_DIR,
try { try {
if (progressCallback) { 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})`); 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 }); fs.mkdirSync(tempUpdateDir, { recursive: true });
if (progressCallback) { 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); const pwrFile = await downloadPWR(branch, newVersion, progressCallback, cacheDir);
if (progressCallback) { if (progressCallback) {
progressCallback('Extracting new files...', 50, null, null, null); progressCallback('Extracting new files...', 60, null, null, null);
} }
await applyPWR(pwrFile, progressCallback, tempUpdateDir, toolsDir); 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) { if (progressCallback) {
progressCallback('Replacing game files...', 80, null, null, null); progressCallback('Replacing game files...', 80, null, null, null);
} }

View File

@@ -323,19 +323,19 @@ class ClientPatcher {
// FORCE PATCHING: Always patch, never skip // FORCE PATCHING: Always patch, never skip
console.log(`Force patching server for ${newDomain}`); console.log(`Force patching server for ${newDomain}`);
if (progressCallback) {
progressCallback('Preparing to patch server...', 10);
}
console.log('Creating backup...');
this.backupClient(serverPath);
if (progressCallback) { if (progressCallback) {
progressCallback('Extracting server JAR...', 20); progressCallback('Extracting server JAR...', 20);
} }
console.log('Opening server JAR...'); 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(); const entries = zip.getEntries();
console.log(`JAR contains ${entries.length} entries`); console.log(`JAR contains ${entries.length} entries`);
@@ -375,7 +375,24 @@ class ClientPatcher {
} }
console.log('Writing patched JAR...'); 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); this.markAsPatched(serverPath);