From 7eaf7a761027e96e2127ec6cf0883efc662dea4a Mon Sep 17 00:00:00 2001 From: DIKER0K Date: Mon, 7 Jul 2025 04:58:57 +0500 Subject: [PATCH] feat: saving files and folders when updating modpaks --- src/main/minecraft-launcher.ts | 53 +++++++++++++++++++++++++++++++ src/renderer/App.tsx | 7 ++++ src/renderer/pages/LaunchPage.tsx | 2 ++ 3 files changed, 62 insertions(+) diff --git a/src/main/minecraft-launcher.ts b/src/main/minecraft-launcher.ts index b043b6f..657edf6 100644 --- a/src/main/minecraft-launcher.ts +++ b/src/main/minecraft-launcher.ts @@ -348,6 +348,7 @@ export function initMinecraftHandlers() { apiReleaseUrl = 'https://api.github.com/repos/DIKER0K/Comfort/releases/latest', versionFileName = 'comfort_version.txt', packName = 'Comfort', + preserveFiles = [], // Новый параметр: список файлов/папок для сохранения } = options || {}; const appPath = path.dirname(app.getPath('exe')); @@ -375,6 +376,7 @@ export function initMinecraftHandlers() { } const tempDir = path.join(appPath, 'temp'); + const packDir = path.join(versionsDir, packName); // Директория пакета // Создаем/очищаем временную директорию if (fs.existsSync(tempDir)) { @@ -400,10 +402,61 @@ export function initMinecraftHandlers() { fs.mkdirSync(versionsDir, { recursive: true }); } + // Сохраняем файлы/папки, которые нужно оставить + const backupDir = path.join(tempDir, 'backup'); + fs.mkdirSync(backupDir, { recursive: true }); + + // Проверка и бэкап указанных файлов/папок + for (const filePath of preserveFiles) { + const fullPath = path.join(packDir, filePath); + + if (fs.existsSync(fullPath)) { + const backupPath = path.join(backupDir, filePath); + + // Создаем необходимые директории для бэкапа + const backupDirPath = path.dirname(backupPath); + if (!fs.existsSync(backupDirPath)) { + fs.mkdirSync(backupDirPath, { recursive: true }); + } + + // Копируем файл или директорию + if (fs.lstatSync(fullPath).isDirectory()) { + fs.cpSync(fullPath, backupPath, { recursive: true }); + console.log(`Директория ${filePath} сохранена во временный бэкап`); + } else { + fs.copyFileSync(fullPath, backupPath); + console.log(`Файл ${filePath} сохранен во временный бэкап`); + } + } + } + // Распаковываем архив напрямую в папку versions await extract(zipPath, { dir: versionsDir }); fs.unlinkSync(zipPath); + // Восстанавливаем файлы/папки из бэкапа + for (const filePath of preserveFiles) { + const backupPath = path.join(backupDir, filePath); + if (fs.existsSync(backupPath)) { + const targetPath = path.join(packDir, filePath); + + // Создаем необходимые директории для восстановления + const targetDirPath = path.dirname(targetPath); + if (!fs.existsSync(targetDirPath)) { + fs.mkdirSync(targetDirPath, { recursive: true }); + } + + // Копируем обратно файл или директорию + if (fs.lstatSync(backupPath).isDirectory()) { + fs.cpSync(backupPath, targetPath, { recursive: true }); + console.log(`Директория ${filePath} восстановлена из бэкапа`); + } else { + fs.copyFileSync(backupPath, targetPath); + console.log(`Файл ${filePath} восстановлен из бэкапа`); + } + } + } + // Сохраняем новую версию fs.writeFileSync(versionFilePath, latestVersion); diff --git a/src/renderer/App.tsx b/src/renderer/App.tsx index 38eaa14..e66d7cd 100644 --- a/src/renderer/App.tsx +++ b/src/renderer/App.tsx @@ -23,6 +23,13 @@ const launchOptions = { baseVersion: '1.21.4', serverIp: 'popa-popa.ru', fabricVersion: 'fabric0.16.14', + preserveFiles: [ + 'options.txt', + 'screenshots', + 'schematics', + 'syncmatics', + 'saves', + ], }; const AuthCheck = ({ children }: { children: ReactNode }) => { diff --git a/src/renderer/pages/LaunchPage.tsx b/src/renderer/pages/LaunchPage.tsx index 9e3733a..225bc17 100644 --- a/src/renderer/pages/LaunchPage.tsx +++ b/src/renderer/pages/LaunchPage.tsx @@ -33,6 +33,7 @@ interface LaunchPageProps { baseVersion: string; serverIp: string; fabricVersion: string; + preserveFiles: string[]; }; } @@ -115,6 +116,7 @@ const LaunchPage = ({ launchOptions }: LaunchPageProps) => { apiReleaseUrl: launchOptions.apiReleaseUrl, versionFileName: launchOptions.versionFileName, packName: launchOptions.packName, + preserveFiles: launchOptions.preserveFiles, }; // Передаем опции для скачивания