From 0873c8e244074ca7a8d3845ddb144aee59edacad Mon Sep 17 00:00:00 2001 From: Chubby Granny Chaser Date: Sat, 5 Oct 2024 03:27:40 +0100 Subject: [PATCH] fix: fixing windows path replacement --- .../cloud-save/download-game-artifact.ts | 35 +++++++++++++------ .../events/cloud-save/upload-save-game.ts | 2 +- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main/events/cloud-save/download-game-artifact.ts b/src/main/events/cloud-save/download-game-artifact.ts index d587daaa..289e5dec 100644 --- a/src/main/events/cloud-save/download-game-artifact.ts +++ b/src/main/events/cloud-save/download-game-artifact.ts @@ -8,6 +8,8 @@ import path from "node:path"; import { backupsPath } from "@main/constants"; import type { GameShop } from "@types"; +import os from "node:os"; + import YAML from "yaml"; export interface LudusaviBackup { @@ -20,14 +22,27 @@ export interface LudusaviBackup { } const replaceLudusaviBackupWithCurrentUser = ( - mappingPath: string, + gameBackupPath: string, backupHomeDir: string ) => { - const data = fs.readFileSync(mappingPath, "utf8"); - const manifest = YAML.parse(data); + const mappingYamlPath = path.join(gameBackupPath, "mapping.yaml"); + + const data = fs.readFileSync(mappingYamlPath, "utf8"); + const manifest = YAML.parse(data) as { + backups: LudusaviBackup[]; + drives: Record; + }; const currentHomeDir = app.getPath("home"); + // TODO: Only works on Windows + const usersDirPath = path.join(gameBackupPath, "drive-C", "Users"); + + fs.renameSync( + path.join(usersDirPath, path.basename(backupHomeDir)), + path.join(usersDirPath, os.userInfo().username) + ); + const backups = manifest.backups.map((backup: LudusaviBackup) => { const files = Object.entries(backup.files).reduce((prev, [key, value]) => { return { @@ -42,7 +57,9 @@ const replaceLudusaviBackupWithCurrentUser = ( }; }); - fs.writeFileSync(mappingPath, YAML.stringify({ ...manifest, backups })); + console.log(backups); + + fs.writeFileSync(mappingYamlPath, YAML.stringify({ ...manifest, backups })); }; const downloadGameArtifact = async ( @@ -89,15 +106,11 @@ const downloadGameArtifact = async ( const [game] = await Ludusavi.findGames(shop, objectId); if (!game) throw new Error("Game not found in Ludusavi manifest"); - const mappingPath = path.join( - backupsPath, - `${shop}-${objectId}`, - game, - "mapping.yaml" + replaceLudusaviBackupWithCurrentUser( + path.join(backupPath, game), + path.normalize(homeDir).replace(/\\/g, "/") ); - replaceLudusaviBackupWithCurrentUser(mappingPath, homeDir); - Ludusavi.restoreBackup(backupPath).then(() => { WindowManager.mainWindow?.webContents.send( `on-backup-download-complete-${objectId}-${shop}`, diff --git a/src/main/events/cloud-save/upload-save-game.ts b/src/main/events/cloud-save/upload-save-game.ts index 9948da31..919cfd9e 100644 --- a/src/main/events/cloud-save/upload-save-game.ts +++ b/src/main/events/cloud-save/upload-save-game.ts @@ -50,7 +50,7 @@ const uploadSaveGame = async ( shop, objectId, hostname: os.hostname(), - homeDir: app.getPath("home"), + homeDir: path.normalize(app.getPath("home")).replace(/\\/g, "/"), platform: os.platform(), });