-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
200 lines (149 loc) · 5.49 KB
/
index.js
File metadata and controls
200 lines (149 loc) · 5.49 KB
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
const SteamUser = require('steam-user');
const fs = require('fs');
const vpk = require('vpk');
const iconv = require('iconv-lite');
const appId = 730;
const depotId = 2347770;
const dir = `./static`;
const temp = "./temp";
const manifestIdFile = 'manifestId.txt'
// use regex to match language filename
const langRegex = /^resource\/csgo_[a-z]+\.txt$/;
const itemsGameFile = 'scripts/items/items_game.txt';
async function downloadVPKDir(user, manifest) {
const dirFile = manifest.manifest.files.find((file) => file.filename.endsWith("csgo\\pak01_dir.vpk"));
console.log(`Downloading vpk dir`)
await user.downloadFile(appId, depotId, dirFile, `${temp}/pak01_dir.vpk`);
// Persist in static directory
fs.copyFileSync(`${temp}/pak01_dir.vpk`, `${dir}/pak01_dir.vpk`);
const vpkDir = new vpk(`${temp}/pak01_dir.vpk`);
vpkDir.load();
return vpkDir;
}
function getVPKFilePaths(vpkDir) {
const paths = [itemsGameFile];
// get language file paths
for (const fileName of vpkDir.files) {
if (langRegex.test(fileName)) {
paths.push(fileName);
}
}
return paths;
}
function getRequiredVPKFiles(vpkDir) {
const paths = getVPKFilePaths(vpkDir);
const requiredIndices = [];
for (const fileName of vpkDir.files) {
for (const f of paths) {
if (fileName.startsWith(f)) {
console.log(`Found vpk for ${f}: ${fileName}`)
const archiveIndex = vpkDir.tree[fileName].archiveIndex;
if (!requiredIndices.includes(archiveIndex)) {
requiredIndices.push(archiveIndex);
}
break;
}
}
}
return requiredIndices.sort();
}
async function downloadVPKArchives(user, manifest, vpkDir) {
const requiredIndices = getRequiredVPKFiles(vpkDir);
console.log(`Required VPK files ${requiredIndices}`);
for (let i = 0; i < requiredIndices.length; i++) {
const archiveIndex = requiredIndices[i];
// pad to 3 zeroes
const paddedIndex = '0'.repeat(3-archiveIndex.toString().length) + archiveIndex;
const fileName = `pak01_${paddedIndex}.vpk`;
const file = manifest.manifest.files.find((f) => f.filename.endsWith(fileName));
const filePath = `${temp}/${fileName}`;
const status = `[${i + 1}/${requiredIndices.length}]`;
console.log(`${status} Downloading ${fileName}`);
await user.downloadFile(appId, depotId, file, filePath);
}
}
function trimBOM(buffer) {
// Check if the Buffer starts with the BOM character
if (buffer.length >= 3 && buffer[0] === 0xEF && buffer[1] === 0xBB && buffer[2] === 0xBF) {
// Trim the first two bytes (BOM)
return buffer.slice(3);
} else {
// No BOM, return the original Buffer
return buffer;
}
}
function extractVPKFiles(vpkDir) {
console.log("Extracting vpk files")
const filePaths = getVPKFilePaths(vpkDir);
for (const targetPath of filePaths) {
let found = false;
for (const vpkPath of vpkDir.files) {
if (vpkPath.startsWith(targetPath)) {
console.log(`Extracting ${targetPath}: ${vpkPath}`);
const file = vpkDir.getFile(vpkPath);
const pathParts = targetPath.split('/');
const fileName = pathParts[pathParts.length - 1];
// Remove BOM from file (https://en.wikipedia.org/wiki/Byte_order_mark)
// Convenience so down stream users don't have to worry about decoding with BOM
const trimmedFile = trimBOM(file)
try {
fs.writeFileSync(`${dir}/${fileName}`, trimmedFile)
} catch (err) {
throw err;
}
found = true;
break;
}
}
if (!found) {
throw new Error(`could not find ${targetPath}`);
}
}
}
if (process.argv.length !== 4) {
console.error(`Missing input arguments, expected 4 got ${process.argv.length}`);
process.exit(1);
}
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
if (!fs.existsSync(temp)){
fs.mkdirSync(temp);
}
const user = new SteamUser();
console.log('Logging into Steam....');
user.logOn({
accountName: process.argv[2],
password: process.argv[3],
rememberPassword: true,
logonID: 2121,
});
user.once('loggedOn', async () => {
const cs = (await user.getProductInfo([appId], [], true)).apps[appId].appinfo;
const commonDepot = cs.depots[depotId];
const latestManifestId = commonDepot.manifests.public.gid;
console.log(`Obtained latest manifest ID: ${latestManifestId}`);
let existingManifestId = "";
try {
existingManifestId = fs.readFileSync(`${dir}/${manifestIdFile}`, 'utf-8');
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
if (existingManifestId === latestManifestId) {
console.log("Latest manifest Id matches existing manifest Id, exiting");
process.exit(0);
}
console.log("Latest manifest Id does not match existing manifest Id, downloading game files")
const manifest = await user.getManifest(appId, depotId, latestManifestId, 'public');
const vpkDir = await downloadVPKDir(user, manifest);
await downloadVPKArchives(user, manifest, vpkDir);
extractVPKFiles(vpkDir);
try {
fs.writeFileSync(`${dir}/${manifestIdFile}`, latestManifestId);
} catch (err) {
throw err;
}
process.exit(0);
});