-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
180 lines (136 loc) · 4.86 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
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'
const vpkFiles = [
'resource/csgo_english.txt',
'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`);
vpkDir = new vpk(`${temp}/pak01_dir.vpk`);
vpkDir.load();
return vpkDir;
}
function getRequiredVPKFiles(vpkDir) {
const requiredIndices = [];
for (const fileName of vpkDir.files) {
for (const f of vpkFiles) {
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 index in requiredIndices) {
index = parseInt(index);
// pad to 3 zeroes
const archiveIndex = requiredIndices[index];
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 = `[${index+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")
for (const f of vpkFiles) {
let found = false;
for (const path of vpkDir.files) {
if (path.startsWith(f)) {
let file = vpkDir.getFile(path);
const filepath = f.split('/');
const fileName = filepath[filepath.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
file = trimBOM(file)
try {
fs.writeFileSync(`${dir}/${fileName}`, file)
} catch (err) {
throw err;
}
found = true;
break;
}
}
if (!found) {
throw `could not find ${f}`;
}
}
}
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}`);
} 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);
});