-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgame_files.js
118 lines (108 loc) · 3.32 KB
/
game_files.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
const base_url = "https://adventure.land";
const fs = require("fs").promises;
const { createWriteStream } = require("fs");
const { pipeline } = require("stream");
const { promisify } = require("util");
const streamPipeline = promisify(pipeline);
const fetch = (...args) =>
import("node-fetch").then(({ default: fetch }) => fetch(...args));
const path = require("path");
const { console } = require("./src/LogUtils");
function get_runner_files() {
return [
"/js/common_functions.js",
"/js/runner_functions.js",
"/js/runner_compat.js",
];
}
function get_game_files() {
return [
"/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",
];
}
async function cull_versions(exclusions) {
const all_versions = await available_versions();
const target_culls = all_versions.filter(
(x, i) => i >= 2 && !exclusions.includes(x),
);
for (let cull of target_culls) {
try {
console.log("culling version " + cull);
await fs.rmdir("./game_files/" + cull, { recursive: true });
} catch (e) {
console.warn("failed to cull version " + cull, e);
}
}
}
async function available_versions() {
return (await fs.readdir("./game_files", { withFileTypes: true }))
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
.filter((x) => x.match(/^\d+$/))
.map((x) => parseInt(x))
.sort()
.reverse();
}
async function download_file(url, file_p) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`failed to download ${url}: ${response.statusText}`);
}
return await streamPipeline(response.body, createWriteStream(file_p));
}
async function get_latest_version() {
const raw = await fetch(base_url);
if (!raw.ok) {
throw new Error(`failed to check version: ${raw.statusText}`);
}
const html = await raw.text();
const match = /game\.js\?v=([0-9]+)"/.exec(html);
if (!match) {
throw new Error(`malformed version response`);
}
return parseInt(match[1]);
}
function locate_game_file(resource, version) {
return `./game_files/${version}/${path.posix.basename(resource)}`;
}
async function ensure_latest() {
const version = await get_latest_version();
if ((await available_versions()).includes(version)) {
console.log(`version ${version} is already downloaded`);
} else {
console.log(`downloading version ${version}`);
const fpath = "./game_files/" + version;
try {
await fs.mkdir(fpath);
const target_files = get_game_files()
.concat(get_runner_files())
//remove duplicates
.filter(function (item, pos, self) {
return self.indexOf(item) == pos;
});
const tasks = target_files.map((itm) =>
download_file(base_url + itm, locate_game_file(itm, version)),
);
await Promise.all(tasks);
} catch (e) {
await fs.rmdir(fpath, { recursive: true });
throw e;
}
}
return version;
}
exports.cull_versions = cull_versions;
exports.available_versions = available_versions;
exports.ensure_latest = ensure_latest;
exports.locate_game_file = locate_game_file;
exports.get_runner_files = get_runner_files;
exports.get_game_files = get_game_files;