mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-03-09 15:40:26 +00:00
feat: removing documents mapping
This commit is contained in:
parent
16cd5b43d8
commit
446b03eeff
3 changed files with 78 additions and 109 deletions
|
@ -20,5 +20,3 @@ export const seedsPath = app.isPackaged
|
||||||
export const backupsPath = path.join(app.getPath("userData"), "Backups");
|
export const backupsPath = path.join(app.getPath("userData"), "Backups");
|
||||||
|
|
||||||
export const appVersion = app.getVersion();
|
export const appVersion = app.getVersion();
|
||||||
|
|
||||||
export const artifactMetadataFileName = "hydra-launcher-metadata.json";
|
|
||||||
|
|
|
@ -3,9 +3,10 @@ import fs from "node:fs";
|
||||||
import * as tar from "tar";
|
import * as tar from "tar";
|
||||||
import { registerEvent } from "../register-event";
|
import { registerEvent } from "../register-event";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
import os from "node:os";
|
||||||
import { app } from "electron";
|
import { app } from "electron";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { artifactMetadataFileName, backupsPath } from "@main/constants";
|
import { backupsPath } from "@main/constants";
|
||||||
import type { GameShop } from "@types";
|
import type { GameShop } from "@types";
|
||||||
|
|
||||||
import YAML from "yaml";
|
import YAML from "yaml";
|
||||||
|
@ -22,7 +23,8 @@ export interface LudusaviBackup {
|
||||||
|
|
||||||
const replaceLudusaviBackupWithCurrentUser = (
|
const replaceLudusaviBackupWithCurrentUser = (
|
||||||
backupPath: string,
|
backupPath: string,
|
||||||
title: string
|
title: string,
|
||||||
|
homeDir: string
|
||||||
) => {
|
) => {
|
||||||
const gameBackupPath = path.join(backupPath, title);
|
const gameBackupPath = path.join(backupPath, title);
|
||||||
const mappingYamlPath = path.join(gameBackupPath, "mapping.yaml");
|
const mappingYamlPath = path.join(gameBackupPath, "mapping.yaml");
|
||||||
|
@ -33,50 +35,26 @@ const replaceLudusaviBackupWithCurrentUser = (
|
||||||
drives: Record<string, string>;
|
drives: Record<string, string>;
|
||||||
};
|
};
|
||||||
|
|
||||||
const metadataPath = path.join(backupPath, artifactMetadataFileName);
|
|
||||||
|
|
||||||
if (!fs.existsSync(metadataPath)) {
|
|
||||||
logger.error(`metadata not found in backup ${gameBackupPath}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const metadata = JSON.parse(fs.readFileSync(metadataPath, "utf8")) as {
|
|
||||||
home: string;
|
|
||||||
documents: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
const currentHomeDir = normalizePath(app.getPath("home"));
|
const currentHomeDir = normalizePath(app.getPath("home"));
|
||||||
const currentDocumentsDir = normalizePath(app.getPath("documents"));
|
|
||||||
|
|
||||||
/* Renaming logic */
|
/* Renaming logic */
|
||||||
|
if (os.platform() === "win32") {
|
||||||
const mappedHomeDir = path.join(
|
const mappedHomeDir = path.join(
|
||||||
gameBackupPath,
|
gameBackupPath,
|
||||||
metadata.home.replace("C:", "drive-C")
|
path.join("drive-C", homeDir.replace("C:", ""))
|
||||||
);
|
|
||||||
const mappedDocumentsDir = path.join(
|
|
||||||
gameBackupPath,
|
|
||||||
metadata.documents.replace("C:", "drive-C")
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (fs.existsSync(mappedHomeDir)) {
|
if (fs.existsSync(mappedHomeDir)) {
|
||||||
fs.renameSync(
|
fs.renameSync(
|
||||||
mappedHomeDir,
|
mappedHomeDir,
|
||||||
path.join(gameBackupPath, currentHomeDir.replace("C:", "drive-C"))
|
path.join(gameBackupPath, "drive-C", currentHomeDir.replace("C:", ""))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fs.existsSync(mappedDocumentsDir)) {
|
|
||||||
fs.renameSync(
|
|
||||||
mappedDocumentsDir,
|
|
||||||
path.join(gameBackupPath, currentDocumentsDir.replace("C:", "drive-C"))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const backups = manifest.backups.map((backup: LudusaviBackup) => {
|
const backups = manifest.backups.map((backup: LudusaviBackup) => {
|
||||||
const files = Object.entries(backup.files).reduce((prev, [key, value]) => {
|
const files = Object.entries(backup.files).reduce((prev, [key, value]) => {
|
||||||
const updatedKey = key
|
const updatedKey = key.replace(homeDir, currentHomeDir);
|
||||||
.replace(metadata.documents, currentDocumentsDir)
|
|
||||||
.replace(metadata.home, currentHomeDir);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...prev,
|
...prev,
|
||||||
|
@ -99,9 +77,11 @@ const downloadGameArtifact = async (
|
||||||
shop: GameShop,
|
shop: GameShop,
|
||||||
gameArtifactId: string
|
gameArtifactId: string
|
||||||
) => {
|
) => {
|
||||||
const { downloadUrl, objectKey } = await HydraApi.post<{
|
try {
|
||||||
|
const { downloadUrl, objectKey, homeDir } = await HydraApi.post<{
|
||||||
downloadUrl: string;
|
downloadUrl: string;
|
||||||
objectKey: string;
|
objectKey: string;
|
||||||
|
homeDir: string;
|
||||||
}>(`/profile/games/artifacts/${gameArtifactId}/download`);
|
}>(`/profile/games/artifacts/${gameArtifactId}/download`);
|
||||||
|
|
||||||
const zipLocation = path.join(app.getPath("userData"), objectKey);
|
const zipLocation = path.join(app.getPath("userData"), objectKey);
|
||||||
|
@ -145,7 +125,7 @@ const downloadGameArtifact = async (
|
||||||
const [game] = await Ludusavi.findGames(shop, objectId);
|
const [game] = await Ludusavi.findGames(shop, objectId);
|
||||||
if (!game) throw new Error("Game not found in Ludusavi manifest");
|
if (!game) throw new Error("Game not found in Ludusavi manifest");
|
||||||
|
|
||||||
replaceLudusaviBackupWithCurrentUser(backupPath, game);
|
replaceLudusaviBackupWithCurrentUser(backupPath, game, homeDir);
|
||||||
|
|
||||||
Ludusavi.restoreBackup(backupPath).then(() => {
|
Ludusavi.restoreBackup(backupPath).then(() => {
|
||||||
WindowManager.mainWindow?.webContents.send(
|
WindowManager.mainWindow?.webContents.send(
|
||||||
|
@ -155,6 +135,12 @@ const downloadGameArtifact = async (
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
} catch (err) {
|
||||||
|
WindowManager.mainWindow?.webContents.send(
|
||||||
|
`on-backup-download-complete-${objectId}-${shop}`,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
registerEvent("downloadGameArtifact", downloadGameArtifact);
|
registerEvent("downloadGameArtifact", downloadGameArtifact);
|
||||||
|
|
|
@ -7,7 +7,7 @@ import crypto from "node:crypto";
|
||||||
import { GameShop } from "@types";
|
import { GameShop } from "@types";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import { artifactMetadataFileName, backupsPath } from "@main/constants";
|
import { backupsPath } from "@main/constants";
|
||||||
import { app } from "electron";
|
import { app } from "electron";
|
||||||
import { normalizePath } from "@main/helpers";
|
import { normalizePath } from "@main/helpers";
|
||||||
|
|
||||||
|
@ -21,22 +21,7 @@ const bundleBackup = async (shop: GameShop, objectId: string) => {
|
||||||
|
|
||||||
await Ludusavi.backupGame(shop, objectId, backupPath);
|
await Ludusavi.backupGame(shop, objectId, backupPath);
|
||||||
|
|
||||||
const tarLocation = path.join(backupsPath, `${crypto.randomUUID()}.zip`);
|
const tarLocation = path.join(backupsPath, `${crypto.randomUUID()}.tar`);
|
||||||
|
|
||||||
fs.writeFileSync(
|
|
||||||
path.join(backupPath, artifactMetadataFileName),
|
|
||||||
JSON.stringify({
|
|
||||||
home: normalizePath(app.getPath("home")),
|
|
||||||
documents: normalizePath(app.getPath("documents")),
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log(
|
|
||||||
JSON.stringify({
|
|
||||||
home: normalizePath(app.getPath("home")),
|
|
||||||
documents: normalizePath(app.getPath("documents")),
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
await tar.create(
|
await tar.create(
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue