This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
GameDataManager.js
148 lines (132 loc) · 4.47 KB
/
GameDataManager.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
const fs = require("fs");
const path = require("path")
const gameFiles = [
"/js/pixi/fake/pixi.min.js",
"/js/libraries/combined.js",
"/js/codemirror/fake/codemirror.js",
"/js/common_functions.js",
"/js/functions.js",
"/js/game.js",
"/js/html.js",
"/js/payments.js",
"/js/keyboard.js",
"/data.js",
"/js/common_functions.js",
"/js/runner_functions.js",
"/js/runner_compat.js"
];
sleep = async function (num) {
return new Promise(function (resolve) {
setTimeout(resolve, num);
});
};
class GameDataManager {
constructor(httpWrapper) {
this.httpWrapper = httpWrapper;
this.currentGameVersion = -1
this.lastUpdated = 0;
}
async currentVersion() {
if (this.lastUpdated < Date.now() - 60 * 1000) {
this.lastUpdated = Date.now();
let version = 0;
do {
try {
version = await this.httpWrapper.getGameVersion();
} catch (e) {
console.log(e);
await sleep(15000);
}
} while (!version)
this.currentGameVersion = version;
return version;
} else {
return this.currentGameVersion;
}
}
async isUpToDate() {
const versions = this.getAvailableVersions();
let gameVersion = await this.currentVersion();
return versions.includes("" + gameVersion);
}
getAvailableVersions() {
let files = fs.readdirSync("./data", {withFileTypes: true});
let versions = [];
for (let file of files) {
if (file.isDirectory())
versions.push(+file.name)
}
return versions.sort((a, b) => b - a).map((a) => "" + a);
}
async updateGameData() {
let gameVersion = await this.currentVersion();
for (let gameFile of gameFiles) {
let data = await this.httpWrapper.getFile(gameFile);
let localPath = path.join("data", "" + gameVersion, gameFile)
fs.mkdirSync(path.parse(localPath).dir, {recursive: true})
fs.writeFileSync(localPath, data)
}
return gameVersion;
}
async readPatches(gameVersion) {
let localPath = path.join("data", "" + gameVersion, "/patches.json")
let patches = [];
try {
patches = JSON.parse(fs.readFileSync(localPath).toString());
} catch (e) {
if (e.code !== "ENOENT")
throw e;
}
return patches;
}
async writeAppliedPatches(gameVersion, patches) {
let localPath = path.join("data", "" + gameVersion, "/patches.json")
fs.writeFileSync(localPath, JSON.stringify(patches))
}
async readFiles(gameVersion) {
let files = {};
for (let gameFile of gameFiles) {
let localPath = path.join("data", "" + gameVersion, gameFile)
files[gameFile] = fs.readFileSync(localPath).toString();
}
return files;
}
async writeFiles(files, gameVersion) {
for (let filePath in files) {
let localPath = path.join("data", "" + gameVersion, filePath)
fs.writeFileSync(localPath, files[filePath]);
}
return files;
}
async applyPatches(gameVersion) {
let patches = fs.readdirSync("app/patches");
let appliedPatches = await this.readPatches(gameVersion);
let files = await this.readFiles(gameVersion);
console.log("Applying patches to " + gameVersion);
let failed = false;
for (let patchPath of patches) {
if (failed)
break;
let patch = require("./" + path.join("app/patches/", patchPath));
try {
if (!appliedPatches.includes(patchPath)) {
console.log(" " + patchPath);
patch.run(files);
appliedPatches.push(patchPath);
}
} catch (e) {
console.error("Failed to execute patch '" + patchPath + "'");
console.error(e);
failed = true;
}
}
if (failed) {
let error = new Error("Unable to apply all patches to game version:" + gameVersion);
error.code = "PATCH_FAIL";
throw error;
}
await this.writeAppliedPatches(gameVersion, appliedPatches)
await this.writeFiles(files, gameVersion)
}
}
module.exports = GameDataManager;