-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteam.js
244 lines (230 loc) · 9.81 KB
/
steam.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
import stripAnsi from 'strip-ansi';
import {mkdirSync, existsSync, createWriteStream } from 'fs';
import {spawn} from 'child_process';
import * as VDF from '@node-steam/vdf';
import https from 'https';
import pty from 'node-pty';
import { resolve } from 'path';
const platform = process.platform;
/**
* Tries to download and install SteamCMD
* @remarks: This is a blocking function.
* @param {string} steamCmdPath - Path to steamcmd folder
* @example
* installSteamCmd('C:\\steamcmd\\');
* installSteamCmd('/steamcmd/');
*
*/
export function installSteamCmd(steamCmdPath) {
return new Promise((resolve, reject) => {
try {
mkdirSync(steamCmdPath);
console.log('Created directory: ' + steamCmdPath);
} catch(err) {
console.error('Failed to make steamcmd directory: ' + steamCmdPath)
console.error(err);
}
console.log('SteamCMD is not installed. Downloading...');
switch (platform) {
case 'win32':
//Download SteamCMD archive for windows.
https.get('https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip', (res) => {
res.pipe(createWriteStream(steamCmdPath + 'steamcmd.zip'));
res.on('end', () => {
console.log('Downloaded SteamCMD for windows')
//Extract downloaded archive
spawn('PowerShell', ['-Command', 'Expand-Archive -Path "' + steamCmdPath + 'steamcmd.zip" -Destination "' + steamCmdPath + '"'])
.on('exit', () => {
if(!existsSync(steamCmdPath + 'steamcmd.exe')){
console.error('Failed to extract SteamCMD');
process.exit(1);
}
console.log('SteamCMD extracted successfully')
//Todo refactor runSteamCmd
runSteamCmd(steamCmdPath, ['login anonymous', 'quit']).then(()=>{
resolve();
}).catch(err => {
console.error(err);
reject(err);
});
})
})
})
break;
case 'linux':
//Download SteamCMD archive for linux.
https.get('https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz', (res, err) => {
res.pipe(createWriteStream(steamCmdPath + 'steamcmd_linux.tar.gz'));
res.on('end', () => {
console.log('Downloaded SteamCMD for linux')
spawn('tar', ['-xzvf', steamCmdPath + 'steamcmd.tar.gz', '-C', steamCmdPath])
.on('exit', () => {
if(!existsSync(steamCmdPath + 'steamcmd.sh')){
console.error('Failed to extract SteamCMD');
process.exit(1);
}
console.log('SteamCMD extracted successfully')
//Todo refactor/fix runSteamCmd
runSteamCmd(steamCmdPath, ['login anonymous', 'quit']).then(() =>{
resolve();
}).catch(err => {
console.error(err);
reject(err);
})
})
})
})
break;
case 'darwin':
//Download SteamCMD archive for mac.
https.get('https://steamcdn-a.akamaihd.net/client/installer/steamcmd_osx.tar.gz', (res, err) => {
res.pipe(createWriteStream(steamCmdPath + 'steamcmd_osx.tar.gz'));
res.on('end', () => {
console.log('Downloaded SteamCMD for darwin')
spawn('tar', ['-xzvf', steamCmdPath + 'steamcmd.tar.gz', '-C', steamCmdPath])
.on('exit', () => {
if(!existsSync(steamCmdPath + 'steamcmd.sh')){
console.error('Failed to extract SteamCMD')
process.exit(1)
}
console.log('SteamCMD extracted successfully')
//Todo refactor runSteamCmd
runSteamCmd(steamCmdPath, ['login anonymous', 'quit']).then(() =>{
resolve();
}).catch(err => {
console.error(err);
reject(err);
})
})
})
})
break;
default:
console.log('Unsupported platform')
break;
}
})
}
/**
* Run new steamcmd process with specific command, one time.
*
* @param {string} steamCmdPath - Path to steamcmd folder
* @param {string} command - Command to run
* @example
* runSteamCmd('C:\\steamcmd\\', ['login anonymous', 'app_info_update 1', 'app_info_print 740', 'quit']); //Get app info for steam_appid 740 windows
* runSteamCmd('/steamcmd/', ['login anonymous', 'app_info_update 1', 'app_info_print 740', 'quit']); //Get app info for steam_appid 740 linux/macos
**/
export function runSteamCmd(steamCmdPath, args) {
return new Promise((resolve, reject) => {
let steamcmdExe;
switch (platform) {
case 'win32': steamcmdExe = steamCmdPath + 'steamcmd.exe'; break;
case 'linux' || 'darwin': steamcmdExe = steamCmdPath + 'steamcmd.sh'; break;
}
console.log(steamcmdExe, [' +' + args]);
let stringArgs;
args.forEach(element => {
stringArgs += ' +' + element;
});
const steamCmdShell = spawn(steamcmdExe + stringArgs);
steamCmdShell.stdout.on('data', (data) => { console.log(data.toString()) });
steamCmdShell.stderr.on('data', (data) => { console.error(data.toString()) });
steamCmdShell.on('exit', (code) => {
console.log('SteamCMD exited with code: ' + code);
resolve(code)
});
});
}
/**
*
* @param {string} steamCmdPath
*
*/
export function createSteamProcess(steamCmdPath) {
return new Promise((resolve, reject) => {
let steamcmdExe;
switch (platform) {
case 'win32': steamcmdExe = steamCmdPath + 'steamcmd.exe'; break;
case 'linux' || 'darwin': steamcmdExe = steamCmdPath + 'steamcmd.sh'; break;
}
console.log('Creating new SteamCMD Process');
console.log('platform: ' + platform);
// Set rows high to avoid newline funkyness in output.
const steamCmdProcess = pty.spawn(steamcmdExe, ['+login anonymous'], {cols: 9999, rows: 80});
let processToReturn;
const sOnData = steamCmdProcess.onData(function(data) {
console.log(data);
if(data.includes('Steam>')) {
console.log('SteamCMD process created');
processToReturn = steamCmdProcess;
}
})
steamCmdProcess.onExit((code) => {
console.log('SteamCMD exited with code: ' + code)
reject(code);
});
// Loop till processToReturn is set.
const sOnExit = setInterval(() => {
if(processToReturn) {
clearInterval(sOnExit);
sOnData.dispose();
resolve(processToReturn);
}
})
})
}
/**
* Gets app info for a specific appid.
* @param {pty.IPty} steamCmdProcess
* @param {string} appid
* @returns
*/
export function getAppInfo(steamCmdProcess, appid) {
let incomingDataBuffer = [];
return new Promise((resolve, reject) => {
let resolvedData = null;
steamCmdProcess.write('app_info_print ' + appid + '\n');
const steamProcessInterface = steamCmdProcess.onData(function(data) {
incomingDataBuffer.push(Buffer.from(data));
if (data.includes('Steam>')){
let incomingData = Buffer.concat(incomingDataBuffer);
// Check if output contains strings that denote update in progress then resend the command.
if(incomingData.includes('change number : 0') || incomingData.includes('requesting...')) {
console.log('App info not received resend');
incomingDataBuffer = [];
steamCmdProcess.write('app_info_print ' + appid + '\n');
} else {
console.log('App info received');
let strippedData = stripAnsi(incomingData.toString());
// Remove the first two lines and last two lines of the output.
let strippedAndSplicedData = strippedData.split('\n').slice(2, -1).join('\n');
let jsonData;
try {
jsonData = VDF.parse(strippedAndSplicedData)
console.log('Parsed VDF to JSON successfully.');
} catch(e) {
console.log('Error parsing VDF: ' + e);
// Regex to get line number from error, Strip error to only relelvant lineNumber
// let lineNumber = e.toString().match(/(?:line\s)([0-9]*)/g);
// let erroredLine = strippedData.split('\n').slice(lineNumber, lineNumber+1).join('\n');
// console.log('Errored line: ' + lineNumber + ': ' + erroredLine);
// writeFileSync('C:\\steamcmd\\incomingData.txt', incomingData.toString());
// writeFileSync('C:\\steamcmd\\strippedAndSplicedData.txt', strippedAndSplicedData);
// writeFileSync('C:\\steamcmd\\strippedData.txt', strippedData);
}
resolvedData = JSON.stringify(jsonData, null, 2);
return;
}
}
})
// Loop till resolvedData is set.
const sOnExit = setInterval(() => {
if(resolvedData) {
clearInterval(sOnExit);
// Dispose of onData EventListener
steamProcessInterface.dispose();
resolve(resolvedData);
}
})
})
}