-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
496 lines (430 loc) · 18.6 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
const fs = require('fs')
const ws = require('ws')
// const axios = require('axios')
// const axios = require(process.execPath + '/node_modules/axios')
// let axios
// import(process.cwd() + '/node_modules/axios').then((module) => {
// axios = module.default
// }).catch((error) => {
// console.error("Failed to import axios:", error)
// })
// import "axios"
const https = require('https')
const jsZip = require('jszip')
const Config = require('./classes/config/Config')
const DrawManager = require('./classes/DrawManager')
const Setting = require('./classes/config/Setting')
const Positions = require('./classes/config/Positions')
const GuiBuilder = require('./classes/gui/GuiBuilder')
const starBoxGen = require('./lib/starBoxGen')
console.log(starBoxGen("This is the server\nYou can start the gui by opening the gui.exe"))
let package = {} // DON'T CHANGE THIS LINE OTHERWISE THE BUILD WILL NOT SUCCEED
let versions = {
latest: "unknown", // latest version
latestStable: "unknown", // latest stable version. latest version not marked as pre-release
}
let isCheckingForUpdates = false
let updateCheckFailed = false
let isUpdateAvailable = {
latest: false,
latestStable: false
}
let tellGui
try {
package = require('./package.json')
}
catch (e) {
console.log("Couldn't get package.json.")
}
let config_ = new Config(undefined, true)
try {
config_.fromFile('./config.json')
} catch (e) {
console.error("Error loading config:", e)
console.log(`\n\ncreating diagnostic data`)
makeDiagnostics(undefined, true, true)
// throw new Error("Error loading config")
}
config_.save('./config.json')
let config = config_.data
if (config.debug.enabled) console.warn('Debug mode enabled')
// console.log(config)
// crash handling
process.on('uncaughtException', (err, origin) => {
console.error('uncaughtException', err, origin)
if (tellGui) {
tellGui.buildMessage(`An error has occurred that crashed the server: ${err.message}`).serve()
}
process.exit(1)
})
if (fs.existsSync(config.temp + config.abortingFile)) {
fs.unlinkSync(config.temp + config.abortingFile)
}
// check if temp exists
if (!fs.existsSync(config.temp)) {
fs.mkdirSync(config.temp)
}
if (config.checkForUpdates) {
console.log('checking for updates')
isCheckingForUpdates = true
let resData = ""
https.get('https://api.github.com/repos/1Euro7Cent/Mrballou-drawbot/releases', {
headers: {
'User-Agent': 'Mrballou-drawbot'
}
})
.on('response', (res) => {
res.on('data', (data) => {
resData += data
})
res.on('end', () => {
if (resData.length == 0 || res.statusCode != 200) {
console.error('Error getting update information. Status code: ' + res.statusCode)
// console.error(resData, res.statusCode)
if (resData.startsWith('{')) {
let err = new Error(JSON.parse(resData).message)
console.error(err)
}
// isCheckingForUpdates = false
updateCheckFailed = true
tellGuiUpdateInfos()
return
}
if (resData.startsWith("[") || resData.startsWith("{")) {
res = {
data: JSON.parse(resData)
}
}
// console.log('got response: ' + resData)
let latest = res.data[0]
let latestStable = res.data.find((release) => !release.prerelease)
versions.latest = latest.tag_name
versions.latestStable = latestStable.tag_name
isCheckingForUpdates = false
isUpdateAvailable.latest = latest.tag_name != package.version
isUpdateAvailable.latestStable = latestStable.tag_name != package.version
console.log(`latest version: ${versions.latest} ${isUpdateAvailable.latest ? "update available" : "no update available"}`)
console.log(`latest stable version: ${versions.latestStable} ${isUpdateAvailable.latestStable ? "update available" : "no update available"}`)
tellGuiUpdateInfos()
})
})
/*
.then((res) => {
let latest = res.data[0]
let latestStable = res.data.find((release) => !release.prerelease)
versions.latest = latest.tag_name
versions.latestStable = latestStable.tag_name
isCheckingForUpdates = false
isUpdateAvailable.latest = latest.tag_name != package.version
isUpdateAvailable.latestStable = latestStable.tag_name != package.version
console.log(`latest version: ${versions.latest} ${isUpdateAvailable.latest ? "update available" : "no update available"}`)
console.log(`latest stable version: ${versions.latestStable} ${isUpdateAvailable.latestStable ? "update available" : "no update available"}`)
tellGuiUpdateInfos()
})
.catch((e) => {
console.error(e)
isCheckingForUpdates = false
updateCheckFailed = true
tellGuiUpdateInfos()
})
//*/
}
if (!fs.existsSync('./settings.json')) {
fs.writeFileSync('./settings.json', "{}")
}
//*
let setting = new Setting(undefined, config.prettifyData, undefined, true)
try {
setting.fromFile('./settings.json')
} catch (e) {
console.error("Error loading settings:", e)
console.log(`\n\ncreating diagnostic data`)
makeDiagnostics(undefined, true, true)
// throw new Error("Error loading settings")
}
setting.save('./settings.json')
let settings = setting.data
//*/
// @ts-ignore
settings = {}
let position = new Positions(undefined, config.prettifyData, undefined, true)
try {
position.fromFile('./positions.json')
} catch (e) {
console.error("Error loading positions:", e)
console.log(`\n\ncreating diagnostic data`)
makeDiagnostics(undefined, true, true)
// throw new Error("Error loading positions")
}
let noPos = false
let posStr = JSON.stringify(position.data)
if (posStr == "{}" || posStr == "null") {
console.log(starBoxGen('No positions found. Create positions\nby running the initializePositions.py/exe'))
noPos = true
}
position.save('./positions.json')
if (!fs.existsSync('./saves.json')) {
fs.writeFileSync('./saves.json', "{}")
}
/*
let saves = require('./saves.json')
//*/
let savesConf = new Setting(undefined, config.prettifyData, undefined, true)
try {
savesConf.fromFile('./saves.json')
}
catch (e) {
console.error("Error loading saves:", e)
console.log(`\n\ncreating diagnostic data`)
makeDiagnostics(undefined, true, true)
// throw new Error("Error loading saves")
}
let saves = savesConf.data
console.log(`starting websocket server on port ${config.port}`)
const wss = new ws.Server({ port: config.port })
let connected = false
function makeDiagnostics(guiBuilder, noGui = false, endAfter = false) {
if (!noGui) guiBuilder.buildMessage('Building diagnostic data. this can take a while').serve()
let zip = new jsZip()
let files = zip.folder('')
if (files == null) {
if (!noGui) guiBuilder.buildMessage('Error creating zip file. Check permissions and disk space').serve()
console.error('Error creating zip file. Check permissions and disk space')
if (endAfter) {
console.log('exiting due to diagnostic data creation')
process.exit(1)
}
return
}
// config files
files.file('config.json', fs.readFileSync('./config.json'))
files.file('settings.json', fs.readFileSync('./settings.json'))
files.file('positions.json', fs.readFileSync('./positions.json'))
files.file('saves.json', fs.readFileSync('./saves.json'))
zip.generateAsync({
type: 'nodebuffer',
compression: "DEFLATE",
compressionOptions: {
level: 9
}
}).then((content) => {
fs.writeFileSync('./diagnostic.zip', content)
if (!noGui) guiBuilder.buildMessage('Diagnostic data created. You can find it under ./diagnostics.zip').serve()
let url = `file://${process.cwd()}/diagnostic.zip`
// var start = (process.platform == 'darwin' ? 'open' : process.platform == 'win32' ? 'start' : 'xdg-open')
var start = (process.platform == 'darwin' ? 'open' : process.platform == 'win32' ? 'start' : 'xdg-open')
require('child_process').execSync(start + ' ' + url)
if (endAfter) {
console.log('exiting due to diagnostic data creation')
process.exit(1)
}
})
}
wss.on('connection', (ws) => {
// console.log(ws)
console.log('connection established')
if (config.communication.allowOnlyOneConnection && connected) {
console.log('already connected')
ws.send('already connected')
ws.close()
return
}
let guiBuilder = new GuiBuilder(package, ws, config_, saves)
guiBuilder.checkForUpdates = config.checkForUpdates
guiBuilder.isCheckingForUpdates = isCheckingForUpdates
guiBuilder.updateCheckFailed = updateCheckFailed
tellGui = guiBuilder
connected = true
let drawManager = new DrawManager(config_, guiBuilder)
if (noPos) {
guiBuilder.buildMessage("No positions found. Create positions by running the initializePositions.py/exe", true).serve()
}
if (!noPos) guiBuilder.buildSelection(setting, position).serve()
// console.log(guiBuilder.toStr(selectGui))
let messageCount = config.communication.keepAlive.unreceivedMax
let c = 0
let heartbeat = setInterval(() => {
ws.send(config.communication.keepAlive.messageSender)
// console.log(`keep alive count: ${messageCount}`)
/*
c++
if (c % 5 == 0) {
if (c % 10 == 0) {
let selectGui = guiBuilder.buildSelection(config)
ws.send(guiBuilder.toStr(selectGui))
}
else {
let testGui = guiBuilder.buildTest(config)
ws.send(guiBuilder.toStr(testGui))
}
}
//*/
messageCount--
if (messageCount < 0) {
console.log('client disconnected')
connected = false
drawManager.abort()
ws.close()
console.log(ws.readyState == ws.CLOSED ? 'closed' : 'not closed')
clearInterval(heartbeat)
}
// console.log('pinging client')
}, config.communication.keepAlive.interval)
ws.on('message', async (msg) => {
let message = msg.toString()
if (message == config.communication.keepAlive.messageReceiver) {
messageCount++
return
}
if (message == config.abortKey || message.toLowerCase() == "stop") {
// console.log("aborting draw request")
drawManager.abort()
return
}
let data = JSON.parse(message)
if (data) {
switch (data.type) {
case "buttonPressed":
console.log(`button "${data.button}" pressed`)
switch (data.button) {
case 'diagnosticButton':
makeDiagnostics(guiBuilder)
break
case 'drawButton':
if (fs.existsSync(config.temp + config.abortingFile)) {
fs.unlinkSync(config.temp + config.abortingFile)
}
loadAndSetData(data)
position.fromFile('./positions.json')
position.save('./positions.json')
drawManager.startDraw(setting, position)
break
case 'abortButton':
drawManager.abort()
guiBuilder.buildSelection(setting, position).serve()
break
case 'saveConfig':
loadAndSetData(data)
// console.log(data)
saves[data.data.saveConfigName] = setting.data
fs.writeFileSync('./saves.json', JSON.stringify(saves, null, config.prettifyData ? 2 : undefined))
guiBuilder.buildSelection(setting, position).serve()
break
case 'loadConfig':
if (data.data.loadConfigName == "") {
guiBuilder.buildMessage("You need to select a name").serve()
break
}
if (!saves[data.data.loadConfigName]) {
guiBuilder.buildMessage("This name doesn't exist").serve()
break
}
// to make it compatible with 1.12 saves:
if (saves[data.data.loadConfigName].name && !saves[data.data.loadConfigName].platform) {
saves[data.data.loadConfigName].platform = saves[data.data.loadConfigName].name
delete saves[data.data.loadConfigName].name
}
setting.fromJson(saves[data.data.loadConfigName])
setting.save('./settings.json')
fs.writeFileSync('./saves.json', JSON.stringify(saves, null, config.prettifyData ? 2 : undefined))
guiBuilder.buildSelection(setting, position).serve()
break
case 'ignoreColorsButton':
guiBuilder.buildColorSelector(setting, position).serve()
// let mainCs = await guiBuilder.buildColorSelector(setting, position, true)
// mainCs.serve()
break
case 'transparentPicker':
guiBuilder.buildTransparentPicker(setting).serve()
break
case 'requestTransparencyColor':
let transColor = await guiBuilder.requestColor()
if (!transColor) {
guiBuilder.buildTransparentPicker(setting, "In your last operation something has gone wrong").serve()
break
}
setting.data.transparentColor = transColor
setting.save('./settings.json')
guiBuilder.buildTransparentPicker(setting).serve()
break
case "addIgnoreColor":
let color = await guiBuilder.requestColor()
console.log(`got color: ${color}`)
if (!color) {
// await sleep(1000)
guiBuilder.buildColorSelector(setting, position, "In your last operation something has gone wrong").serve()
// let cs1 = await guiBuilder.buildColorSelector(setting, position, true, "In your last operation something has gone wrong")
// cs1.serve()
break
}
if (setting.data.ignoreColors[color]) {
// await sleep(1000)
guiBuilder.buildColorSelector(setting, position, `The color ${color} already exists`).serve()
// let cs2 = await guiBuilder.buildColorSelector(setting, position, true, "The color already exists")
// cs2.serve()
break
}
console.log(`adding color: ${color} to ignoreColors`)
setting.data.ignoreColors[color] = {}
setting.save('./settings.json')
// await sleep(1000)
guiBuilder.buildColorSelector(setting, position).serve()
// let cs3 = await guiBuilder.buildColorSelector(setting, position, true)
// cs3.serve()
break
case "removeIgnoreColor":
let toRemoveColor = data.data.removeSelectedColor
// console.log(`removing color: ${toRemoveColor} from ignoreColors`)
if (!toRemoveColor) {
guiBuilder.buildColorSelector(setting, position, "You need to select a color to remove").serve()
break
}
delete setting.data.ignoreColors[toRemoveColor]
setting.save('./settings.json')
guiBuilder.buildColorSelector(setting, position).serve()
// let col = await guiBuilder.requestColor(10 * 1000)
// console.log(`got color from promise: ${col}`)
}
break
case "color":
// console.log(`got color: ${data.data}`)
guiBuilder.gotReqCol(data.data)
break
default:
console.log(`got unknown type: ${message}`)
break
}
return
}
console.log(`got message: ${message}`)
})
})
function loadAndSetData(data) {
setting.fromJson(data.data)
// setting.data.ignoreColors = ['#ffffff']
setting.save('./settings.json')
settings = setting.data
}
let tellClock
function tellGuiUpdateInfos() {
function setData() {
tellGui.checkForUpdates = config.checkForUpdates
tellGui.isCheckingForUpdates = isCheckingForUpdates
tellGui.updateCheckFailed = updateCheckFailed
tellGui.checkForUpdatesPreRelease = config.checkForUpdatesPreRelease
tellGui.isUpdateAvailable = config.checkForUpdatesPreRelease ? isUpdateAvailable.latest : isUpdateAvailable.latestStable
tellGui.latestVersion = config.checkForUpdatesPreRelease ? versions.latest : versions.latestStable
clearInterval(tellClock)
return
}
if (tellGui instanceof GuiBuilder) {
setData()
}
else {
tellClock = setInterval(() => {
if (tellGui instanceof GuiBuilder) {
setData()
}
}, 100)
}
}