Skip to content

Commit

Permalink
Revert "Merge pull request #12 from select-studios/Client-side-refact…
Browse files Browse the repository at this point in the history
…oring"

This reverts commit 8a55f33, reversing
changes made to 4082623.
  • Loading branch information
Mellurboo committed Feb 20, 2024
1 parent 8a55f33 commit 3545b91
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 351 deletions.
30 changes: 0 additions & 30 deletions .eslintrc.js

This file was deleted.

114 changes: 54 additions & 60 deletions client/electron/api/gameManager.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,36 @@
import fs from "fs";
import path from "path";
import { execFile } from "child_process";
import download from "download";
import settings from "electron-settings";
import decompress from "decompress";
import { win } from "../main/index";

import Downloader from "nodejs-file-downloader";

/**
* Check if the games directory actually exists and creates it if it does not.
*
* @returns {void}
*/
export const checkIfGamesDirectoryExistsAndCreate = (): void => {
if (
!fs.existsSync(settings.getSync("locations.libraryLocation").toString())
) {
fs.mkdir(settings.getSync("locations.libraryLocation").toString(), () =>
console.log("created games folder"),
);
export const checkIfGamesDirectoryExists = (): boolean => {
if (fs.existsSync(settings.getSync("locations.libraryLocation").toString())) {
return true;
}
return false;
};

/**
* Downloads the provided game into the library directory.
*
* @param {string} gameName - The name of the game to be downloaded
*/
export const downloadGame = async (gameName: string) => {
checkIfGamesDirectoryExistsAndCreate();
if (!checkIfGamesDirectoryExists()) {
fs.mkdir(settings.getSync("locations.libraryLocation").toString(), () =>
console.log("created games folder")
);
}

const downloader = new Downloader({
url: `https://gitlab.com/akshit.singla.dps/launcher-games/-/raw/main/${gameName}.zip?inline=false`,
directory: settings.getSync("locations.libraryLocation").toString(),
maxAttempts: 3,
onError: (error) => {
url: `https://gitlab.com/akshit.singla.dps/launcher-games/-/raw/main/${gameName}.zip?inline=false`, //If the file name already exists, a new file with the name 200MB1.zip is created.
directory: settings.getSync("locations.libraryLocation").toString(), //This folder will be created, if it doesn't exist.
maxAttempts: 3, //Default is 1.
onError: function (error) {
//You can also hook into each failed attempt.
console.log("Error from attempt ", error);
},
onProgress: (percentage, chunk, remainingSize) => {
onProgress: function (percentage, chunk, remainingSize) {
console.log("% ", percentage);

win.webContents.send("downloading", {
Expand All @@ -49,6 +42,17 @@ export const downloadGame = async (gameName: string) => {
},
});

// download(
// `https://raw.githubusercontent.com/select-studios/LauncherGames/main/${gameName}.zip`,
// settings.getSync("locations.libraryLocation").toString(),
// { decompress: false }
// )
// .then(() => {
// installGame(gameName);
// return;
// })
// .catch((e) => );

try {
downloader
.download()
Expand All @@ -62,40 +66,38 @@ export const downloadGame = async (gameName: string) => {
.then(() => {
win.webContents.send("downloading", {
gameName,
msg: "",
msg: ``,
});
console.log("Finished installing the game.");
});
} catch (error) {
//TODO:Handle a possible error. An error is thrown in case of network errors, or status codes of 400 and above.
//IMPORTANT: Handle a possible error. An error is thrown in case of network errors, or status codes of 400 and above.
//Note that if the maxAttempts is set to higher than 1, the error is thrown only if all attempts fail.
console.log("Download failed", error);
}
};

/**
* Install the give downloaded game.
*
* @param {string} gameName - The game to be installed.
*/
export const installGame = (gameName: string) => {
checkIfGamesDirectoryExistsAndCreate();
if (!checkIfGamesDirectoryExists()) {
fs.mkdir(settings.getSync("locations.libraryLocation").toString(), () =>
console.log("created games folder")
);
}

if (
fs.existsSync(
path.join(
settings.getSync("locations.libraryLocation").toString(),
`${gameName}.zip`,
),
`${gameName}.zip`
)
)
) {
// Decompress the installed zip
decompress(
path.join(
settings.getSync("locations.libraryLocation").toString(),
`${gameName}.zip`,
`${gameName}.zip`
),
settings.getSync("locations.libraryLocation").toString(),
settings.getSync("locations.libraryLocation").toString()
)
.then(() => {
cleanupGame(gameName);
Expand All @@ -110,56 +112,48 @@ export const installGame = (gameName: string) => {
}
};

/**
* Deletes the zip file downloaded after it has been extracted.
*
* @param {string} gameName - The game to be cleaned up.
*/
export const cleanupGame = (gameName: string) => {
fs.rmSync(
path.join(
settings.getSync("locations.libraryLocation").toString(),
`${gameName}.zip`,
`${gameName}.zip`
),
{ force: true },
{ force: true }
);
win.webContents.send("downloading", {});
win.webContents.send("finish-download", `Finished installing ${gameName}!`);
};

/**
* Uninstall the given game.
*
* @param {string} gameName - The game to be uninstalled.
*/
export const uninstallGame = async (gameName: string) => {
checkIfGamesDirectoryExistsAndCreate();

if (!checkIfGamesDirectoryExists()) {
fs.mkdir(settings.getSync("locations.libraryLocation").toString(), () =>
console.log("created games folder")
);
}
fs.rmSync(
path.join(
settings.getSync("locations.libraryLocation").toString(),
gameName,
gameName
),
{ recursive: true, force: true },
{ recursive: true, force: true }
);

win.webContents.send("finish-uninstall", `Finished uninstalling ${gameName}`);
};

/**
* Start the given game's executable file.
*
* @param {string} gameName - The gam to be started
*/
export const startGame = async (gameName: string) => {
checkIfGamesDirectoryExistsAndCreate();
if (!checkIfGamesDirectoryExists()) {
fs.mkdir(settings.getSync("locations.libraryLocation").toString(), () =>
console.log("created games folder")
);
}

execFile(
path.join(
settings.getSync("locations.libraryLocation").toString(),
gameName,
`${gameName}.exe`,
),
`${gameName}.exe`
)
);
};

Expand Down
2 changes: 1 addition & 1 deletion client/electron/api/interfaces/release.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// The github response to a download request schema
// Generated by https://quicktype.io

export interface Release {
url: string;
Expand Down
8 changes: 4 additions & 4 deletions client/electron/electron-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare namespace NodeJS {
interface ProcessEnv {
VSCODE_DEBUG?: "true";
DIST_ELECTRON: string;
DIST: string;
VSCODE_DEBUG?: 'true'
DIST_ELECTRON: string
DIST: string
/** /dist/ or /public/ */
PUBLIC: string;
PUBLIC: string
}
}
111 changes: 0 additions & 111 deletions client/electron/main/eventHandlers.ts

This file was deleted.

Loading

0 comments on commit 3545b91

Please sign in to comment.