-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
277 lines (236 loc) · 9.69 KB
/
app.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
const { app, BrowserWindow, ipcMain } = require('electron');
const child = require('child-process-promise');
const { spawn, exec } = require('child_process');
const url = require("url");
const path = require("path");
const http = require('http');
const fs = require('fs');
const AdmZip = require("adm-zip");
const {
createFile,
createFolder,
deepMergeDir,
deleteFolderRecursively
} = require('./src/electron/file.service.js');
const {
getDownloadLinks,
downloadZIPFileByURL
} = require('./src/electron/remote-downloader.service.js');
let MAINWINDOW;
const DEFAULT_WINDOW_WIDTH = 750;
const DEFAULT_WINDOW_HEIGHT = 700;
function createWindow() {
MAINWINDOW = new BrowserWindow({
icon: 'favicon.ico',
width: DEFAULT_WINDOW_WIDTH,
height: DEFAULT_WINDOW_HEIGHT,
frame: false,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true
}
})
MAINWINDOW.loadURL(
url.format({
pathname: path.join(__dirname, `/dist/tf2-server-manager/index.html`),
protocol: "file:",
slashes: true
})
);
// MAINWINDOW.webContents.openDevTools();
MAINWINDOW.setMenu(null);
MAINWINDOW.setMinimumSize(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);
MAINWINDOW.setTitle('TF2 Server Launcher');
MAINWINDOW.on('closed', function () {
MAINWINDOW = null;
});
}
app.on('ready', createWindow);
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', function () {
if (MAINWINDOW === null) createWindow()
})
/* ------------------ IPC ------------------ */
ipcMain.on('close-window', async (event, arg) => {
MAINWINDOW.close();
});
ipcMain.on('minimize-window', async (event, arg) => {
MAINWINDOW.minimize();
});
ipcMain.on('navigate-to-folder', async (event, arg) => {
exec(`start "" "${arg.savePath}"`);
});
ipcMain.on('create-default-server-path', async (event, arg) => {
if (!fs.existsSync(arg.savePath)) {
await createFolder(arg.savePath);
}
if (!fs.existsSync(`${arg.savePath}\\config.json`)) {
await createFile(`${arg.savePath}\\config.json`, JSON.stringify(arg.config));
}
event.reply('create-default-server-path-reply', 'success');
});
ipcMain.on('create-server-directory', async (event, arg) => {
await createFolder(arg.savePath);
});
ipcMain.on('create-directory-config', async (event, arg) => {
await createFile(`${arg.savePath}\\config.json`, JSON.stringify(arg.config));
event.reply('create-directory-config-reply', 'success');
});
ipcMain.on('get-directories', async (event, arg) => {
const directories = fs.readdirSync(arg.savePath).filter(dir => fs.lstatSync(`${arg.savePath}\\${dir}`).isDirectory());
event.reply('get-directories-reply', directories);
});
ipcMain.on('download-file', async (event, arg) => {
const config = JSON.parse(fs.readFileSync(arg.savePath.replace('steamcmd.zip', 'config.json'), 'utf8'));
/**
* Thanks to https://github.com/mathphreak/node-steamcmd/blob/master/index.js#L68
* for the following implementation.
*/
const update = async () => {
return await new Promise((resolve, reject) => {
child.spawn(arg.savePath.replace('steamcmd.zip', 'run.bat'),
[],
{
capture: ['stdout', 'stderr'],
cwd: arg.savePath.replace('steamcmd.zip', '')
}
).then(function (x) {
resolve(x)
}).fail(function (x) {
// For some reason, steamcmd will occasionally exit with code 7 and be fine.
// This usually happens the first time touch() is called after download().
if (x.code === 7) {
resolve(x)
} else {
reject(x)
}
})
});
}
if (!fs.existsSync(arg.savePath.replace('steamcmd.zip', '\\steamapps'))) {
const file = fs.createWriteStream("steamcmd.zip");
const request = http.get(arg.url, function (response) {
response.pipe(file);
});
await new Promise((resolve, reject) => {
file.on('finish', resolve);
file.on('error', reject);
});
await new Promise((resolve, reject) => {
fs.rename('steamcmd.zip', arg.savePath, (err) => {
if (err) reject(err);
resolve();
});
});
await new Promise(resolve => {
const zip = new AdmZip(arg.savePath);
zip.extractAllTo(arg.savePath.replace('steamcmd.zip', ''), true);
resolve();
});
/**
* Thanks to https://github.com/mathphreak/node-steamcmd/blob/master/index.js#L68
* for the following implementation.
*/
const args = [].concat('quit').map(function (x) {
return '+' + x
}).join(' ').split(' ');
await new Promise(function (resolve, reject) {
child.spawn(arg.savePath.replace('steamcmd.zip', 'steamcmd.exe'),
args,
{
capture: ['stdout', 'stderr'],
cwd: arg.savePath.replace('steamcmd.zip', '')
}
).then(function (x) {
resolve(x)
}).fail(function (x) {
// For some reason, steamcmd will occasionally exit with code 7 and be fine.
// This usually happens the first time touch() is called after download().
if (x.code === 7) {
resolve(x)
} else {
reject(x)
}
})
});
await createFile(arg.savePath.replace('steamcmd.zip', 'tf2_ds.txt'), `
@ShutdownOnFailedCommand 1
@NoPromptForPassword 1
force_install_dir ${arg.savePath.replace('steamcmd.zip', '')}
login anonymous
app_update 232250
quit
`);
await createFile(arg.savePath.replace('steamcmd.zip', 'run.bat'), `
@echo off
steamcmd.exe +runscript tf2_ds.txt
`);
await update();
await createFile(arg.savePath.replace('steamcmd.zip', 'steamapps\\common\\Team Fortress 2 Dedicated Server\\run.bat'), `
@echo off
"${arg.savePath.replace('steamcmd.zip', 'steamapps\\common\\Team Fortress 2 Dedicated Server\\srcds.exe')}" ${config.args} ${config.ip ? '-ip ' + config.ip : ''} ${config.port ? '-port ' + config.port : ''}
`);
await createFile(arg.savePath.replace('steamcmd.zip', 'steamapps\\common\\Team Fortress 2 Dedicated Server\\tf\\cfg\\server.cfg'), `
${config.serverName ? 'hostname "' + config.serverName + '"' : ''}
${config.serverPassword ? 'sv_password "' + config.serverPassword + '"' : ''}
${config.configArgs ? config.configArgs : ''}
`.trim());
} else {
await update();
}
event.reply('download-file-reply', 'success');
});
ipcMain.on('run-server', async (event, arg) => {
const config = JSON.parse(fs.readFileSync(arg.savePath.replace('steamcmd.zip', 'config.json'), 'utf8'));
const child = spawn(arg.savePath.replace('steamcmd.zip', `\"steamapps\\common\\Team Fortress 2 Dedicated Server\\srcds.exe\" ${config.args} ${config.ip ? '-ip ' + config.ip : ''} ${config.port ? '-port ' + config.port : ''}`), {
shell: true,
detached: true,
stdio: ['ignore', 'ignore', 'ignore'],
});
child.unref();
event.reply('run-server-reply', {
status: 'success',
pid: child.pid
});
});
ipcMain.on('kill-server', async (event, arg) => {
spawn("taskkill", ["/pid", arg.pid, '/f', '/t']);
});
ipcMain.on('download-sourcemod', async (event, arg) => {
const pathToTFFolder = `${arg.savePath}\\steamapps\\common\\Team Fortress 2 Dedicated Server\\tf`;
if (fs.existsSync(pathToTFFolder) && !fs.existsSync(`${pathToTFFolder}\\addons`)) {
const sourcemodLinks = await getDownloadLinks(arg.downloadLinkSM);
const metamodLinks = await getDownloadLinks(arg.downloadLinkMM);
await createFolder(`${arg.savePath}\\sourcemod`);
await createFolder(`${arg.savePath}\\metamod`);
await downloadZIPFileByURL('sourcemod.zip', sourcemodLinks[0], `${arg.savePath}\\sourcemod`);
await downloadZIPFileByURL('metamod.zip', metamodLinks[0], `${arg.savePath}\\metamod`);
deepMergeDir(`${arg.savePath}\\metamod`, `${arg.savePath}\\sourcemod`);
deepMergeDir(`${arg.savePath}\\sourcemod`, pathToTFFolder);
}
event.reply('download-sourcemod-reply', 'success');
});
ipcMain.on('delete-server-files', async (event, arg) => {
await deleteFolderRecursively(arg.savePath).catch((err) => {
console.error(err);
});
event.reply('delete-server-files-reply', 'success');
});
ipcMain.on('get-config-file', async (event, arg) => {
const data = await new Promise((resolve, reject) => {
if (fs.existsSync(`${arg.savePath}/config.json`)) {
fs.readFile(`${arg.savePath}/config.json`, 'utf8', (err, data) => {
if (err) reject(err);
resolve(data);
});
}
});
event.reply('get-config-file-reply', data);
});
ipcMain.on('replace-config-file', async (event, arg) => {
await createFile(`${arg.savePath}/config.json`, JSON.stringify(arg.config));
event.reply('replace-config-file-reply', 'success');
});
/* ------------------ IPC ------------------ */