-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
268 lines (243 loc) · 8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
const fs = require("fs");
const zlib = require("zlib");
const path = require("path");
const GameEngine = require("./core/GameEngine");
const game = new GameEngine();
const { log } = require("./lib/Console");
const readline = require("readline");
const { exec } = require("child_process");
// Create necessary folders if they do not exist
["game", "save", "cache"].forEach((dir) => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
});
const rl = game.rl;
// Function to list all games in the "game" folder
function listGames() {
console.clear();
const games = fs.readdirSync("game");
log(
`
ARGA GAME LAUNCHER
█████╗ ██████╗ ██████╗ █████╗
██╔══██╗██╔══██╗██╔════╝ ██╔══██╗
███████║██████╔╝██║ ███╗███████║
██╔══██║██╔══██╗██║ ██║██╔══██║
██║ ██║██║ ██║╚██████╔╝██║ ██║
╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
ARGA GAME ENGINE 2.0.0
`,
"fgCyan"
);
log("Available games:");
log("========================================");
log("0. Download new game");
log("u. Update all games");
log("d. Delete a game");
log("========================================");
games.forEach((game, index) => {
log(`${index + 1}. ${game}`, "fgGreen");
});
}
// Function to load a game based on user input
async function loadGame(gameIndex) {
const games = fs.readdirSync("game");
if (gameIndex >= 1 && gameIndex <= games.length) {
const selectedGame = games[gameIndex - 1];
await game.loadGame(`game/${selectedGame}/`);
game.gameLoop(); // Start the game loop after loading the game
} else {
console.log("Invalid game selection.");
// List games again if the selection is invalid
askQuestions();
}
}
// Function to get a list of repositories with the tag 'arga-game-production'
async function getGameList() {
const cacheFile = path.join("cache", "game_list.json");
return fetch(
"https://api.github.com/search/repositories?q=topic:arga-game-production",
{
headers: {
"User-Agent": "node.js",
},
}
)
.then((res) => res.json())
.then(async (response) => {
if (!response.items) {
throw new Error("Invalid response from GitHub API");
}
const gameList = [];
for (const repo of response.items) {
gameList.push({
name: repo.name,
repo_url: repo.html_url,
});
}
fs.writeFileSync(cacheFile, JSON.stringify(gameList));
return gameList;
})
.catch((error) => {
log("Error fetching game list from GitHub:", "fgWhite", "bgRed");
log(error, "fgWhite", "bgRed");
log("Loading games from cache...", "fgWhite", "bgRed");
return JSON.parse(fs.readFileSync(cacheFile, "utf-8")).then(
(gameList) => {
return gameList;
}
);
});
}
function deleteGame(gameIndex) {
const games = fs.readdirSync("game");
if (gameIndex >= 1 && gameIndex <= games.length) {
const gameToDelete = games[gameIndex - 1];
fs.rmdirSync(path.join("game", gameToDelete), { recursive: true });
return console.log(`Game ${gameToDelete} deleted successfully.`);
} else {
console.log("Invalid game selection.");
}
}
async function cloneGame(repoURL, dest) {
if (fs.existsSync(dest)) {
const update = await askQuestion(
"The game already exists. Do you want to update it? (y/n): "
);
if (update.toLowerCase() === "y") {
try {
await new Promise((resolve, reject) => {
exec(`git -C ${dest} pull`, (error, stdout, stderr) => {
if (error) {
console.error(`Error updating repository: ${error.message}`);
reject(error);
return;
}
if (stderr) {
console.error(`Updating warning: ${stderr}`);
}
console.log(`Repository updated successfully: ${repoURL}`);
resolve();
});
});
} catch (error) {
console.error("Error updating the game:", error);
throw error;
}
}
} else {
try {
await new Promise((resolve, reject) => {
exec(`git clone ${repoURL} ${dest}`, (error, stdout, stderr) => {
if (error) {
console.error(`Error cloning repository: ${error.message}`);
reject(error);
return;
}
if (stderr) {
console.error(`Cloning warning: ${stderr}`);
}
console.log(`Repository cloned successfully: ${repoURL}`);
resolve();
});
});
} catch (error) {
console.error("Error cloning the game:", error);
throw error;
}
}
}
// List all games
listGames();
function askQuestion(question) {
return new Promise((resolve) =>
rl.question(question, (answer) => {
resolve(answer);
})
).catch((error) => {
log("Error during user input:", error, "fgWhite", "bgRed");
throw error; // Rethrow the error after logging it.
});
}
function askQuestions() {
askQuestion(
"Enter the number of the game you want to play or 0 to clone a new game: "
).then(async (answer) => {
if (answer === "0") {
const gameList = await getGameList();
gameList.forEach((game, index) => {
console.log(`${index + 1}. ${game.name}`);
});
const gameIndex = await askQuestion(
"Enter the number of the game you want to clone: "
);
const selectedGame = gameList[gameIndex - 1];
const url = selectedGame.repo_url;
const dest = `game/${selectedGame.name}`;
console.log("Cloning the game repository, please wait...");
await cloneGame(url, dest);
console.log(`Game repository cloned to ${dest}`);
listGames(); // List games after cloning
askQuestions();
} else if (answer.toLowerCase() === "u") {
console.log("Updating all game repositories, please wait...");
await updateAllGames();
console.log(`All game repositories updated`);
listGames(); // List games after updating
askQuestions();
} else if (answer.toLowerCase() === "d") {
const gameIndex = await askQuestion(
"Enter the number of the game you want to delete: "
);
deleteGame(parseInt(gameIndex, 10));
listGames(); // List games after deleting
askQuestions();
} else {
loadGame(parseInt(answer, 10));
}
});
}
askQuestions();
function updateAllGames() {
const games = fs.readdirSync("game");
return Promise.all(
games.map((game) => {
const dest = `game/${game}`;
return new Promise((resolve, reject) => {
exec(`git -C ${dest} pull`, (error, stdout, stderr) => {
if (error) {
console.error(`Error updating repository: ${error.message}`);
reject(error);
return;
}
if (stderr) {
console.error(`Updating warning: ${stderr}`);
}
console.log(`Repository updated successfully: ${game}`);
resolve();
});
});
})
);
}
process.on("uncaughtException", (err) => {
log("Game Engine error:", "fgWhite", "bgRed");
// at
log(err.stack, "fgWhite", "bgRed");
game.quit();
});
process.on("unhandledRejection", (reason, promise) => {
log("========================================", "fgWhite", "bgRed");
log("Game Engine : Unhandled Rejection at:", "fgWhite", "bgRed");
log(`Reason: ${reason}`, "fgWhite", "bgRed");
log(`Promise: ${promise}`, "fgWhite", "bgRed");
log("========================================", "fgWhite", "bgRed");
game.quit();
});
// handle process exit
process.on("exit", async (code) => {
log(`Kernel Rechive Game Engine Exit with code ${code}`, "fgWhite", "bgRed");
// make exit block
await new Promise((resolve) => setTimeout(resolve, 1000));
});