Skip to content

Commit

Permalink
feat(gamestates): implement automatic synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
GravityTwoG committed Mar 31, 2024
1 parent c2d6588 commit 8af3ed7
Show file tree
Hide file tree
Showing 22 changed files with 424 additions and 196 deletions.
104 changes: 101 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"dependencies": {
"adm-zip": "^0.5.10",
"clsx": "^2.1.0",
"electron-dl": "^3.5.2",
"electron-squirrel-startup": "^1.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
11 changes: 4 additions & 7 deletions src/@types/electron-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,14 @@ interface Window {
}>
>;

downloadSave: (archiveURL: string) => Promise<ElectronApiResponse<void>>;

downloadAndExtractSave: (
archiveURL: string,
path: string
) => Promise<ElectronApiResponse<void>>;

onGetSyncedSaves: (callback: () => void) => void;

sendSyncedSaves: (
args: import("../types").GameState[]
) => Promise<ElectronApiResponse<void>>;

downloadState: (gameState: import("../types").GameState) => Promise<void>;

downloadStateAs: (gameState: import("../types").GameState) => Promise<void>;
};
}
2 changes: 2 additions & 0 deletions src/Application.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from "path";
import { BrowserWindow, Menu, Tray, app, nativeImage, Event } from "electron";
import electronDl from "electron-dl";

import { setupIPC } from "./backend/electron-api";
import { SyncManager } from "./backend/SyncManager";
Expand All @@ -18,6 +19,7 @@ export class Application {
init() {
app.commandLine.appendSwitch("lang", "en-US");
this.registerProtocolClient();
electronDl();

this.syncManager.init(() => {
this.mainWindow?.webContents.send("getSyncedSaves");
Expand Down
73 changes: 0 additions & 73 deletions src/backend/GameStateAPI.ts

This file was deleted.

48 changes: 18 additions & 30 deletions src/backend/StatesManager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import AdmZip from "adm-zip";
import fs from "fs/promises";
import path from "path";
import os from "os";
import AdmZip from "adm-zip";

import { Game } from "@/types";
import { Game, GameState } from "@/types";
import { ValueExtractor } from "./game-state-parameters/ValueExtractor";
import { moveFolder } from "./fs/moveFolder";
import { downloadToFolder } from "./fs/downloadToFolder";
import { extractZIP } from "./fs/extractZIP";

export class StatesManager {
private readonly valueExtractor: ValueExtractor;
Expand All @@ -11,7 +16,7 @@ export class StatesManager {
this.valueExtractor = valueExtractor;
}

async uploadSave(folder: { path: string; name: string }, game?: Game) {
async uploadState(folder: { path: string; name: string }, game?: Game) {
const zip = new AdmZip();

const isDirectory = (await fs.lstat(folder.path)).isDirectory();
Expand All @@ -24,7 +29,7 @@ export class StatesManager {
// await zip.writeZipPromise(`${path}.zip`);
const gameStateValues = game
? await this.valueExtractor.extract(folder, game)
: { fields: [] };
: [];

const buffer = zip.toBuffer();
return {
Expand All @@ -33,34 +38,17 @@ export class StatesManager {
};
}

async downloadState(archiveURL: string) {
// TODO
console.log("Downloading state", archiveURL);
return { path: "" };
}

async downloadAndExtractSave(archiveURL: string, path: string) {
// TODO
const state = await this.downloadState(archiveURL);
// TODO
this.extractZIP(state.path);

console.log("Extracted to", path);

return state;
}

private extractZIP(filePath: string) {
const zip = new AdmZip(filePath);
async downloadState(gameState: GameState) {
const tempPath = os.tmpdir();
const archivePath = path.join(tempPath, "cloud-saves");
const filename = `${gameState.name}-archive.zip`;
const filePath = path.join(archivePath, filename);

const zipEntries = zip.getEntries();

for (const entry of zipEntries) {
console.log(entry.entryName);
}
await downloadToFolder(gameState.archiveURL, archivePath, filename);

zip.extractAllTo(filePath.replace(".zip", ".extracted"));
const extractedFolderPath = await extractZIP(filePath);

// TODO
// move extracted folder to game folder
await moveFolder(extractedFolderPath, gameState.localPath);
}
}
Loading

0 comments on commit 8af3ed7

Please sign in to comment.