This repository has been archived by the owner on Sep 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js.bak
262 lines (221 loc) · 6.81 KB
/
main.js.bak
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
const { app, BrowserWindow, ipcMain, Menu, nativeImage, Notification, Tray } = require('electron')
const { autoUpdater } = require('electron-updater')
const { download } = require('electron-dl')
const extract = require('extract-zip')
const fs = require('fs'), sfs = require('fs'), vfs = require('fs')
const gameDig = require('gamedig')
const http = require('http')
const log = require('electron-log')
const path = require('path')
const rimraf = require('rimraf')
const host = {
protocol: "http://",
domain: "mod.a3sog.org",
port: 80,
path: "/SOG.zip",
gameIP: "127.0.0.1",
gamePORT: "2302"
}
const sPath = path.join(app.getPath("userData"), 'settings.json')
let dlServerUp = true
let settings = JSON.stringify({"arma3path": "C:\\Program Files (x86)\\Steam\\steamapps\\common\\Arma 3", "version": "-1", "last-modified": "-1"})
log.transports.file.resolvePath = () => path.join(app.getPath("userData"), 'logs/main.log')
log.log('Application Version v' + app.getVersion())
if (sfs.existsSync(sPath)) {
settings = sfs.readFileSync(sPath)
} else {
if (!sfs.existsSync(app.getPath("userData"))) {
sfs.mkdirSync(app.getPath("userData"))
}
sfs.writeFile(sPath, settings, (e) => { if (e) throw e })
}
let settingsData = JSON.parse(settings)
let version = "-1"
const vPath = JSON.parse(settings).arma3path + "\\SOG\\Version"
if (vfs.existsSync(vPath)) {
version = vfs.readFileSync(vPath)
}
settingsData.version = version.toString()
const hbs = require('electron-handlebars')({
title: app.getName(),
data: settingsData,
installed: (version != "-1"),
version: app.getVersion()
})
let splash = null
let tray = null
let win = null
function showNotification (NOTIF_TITLE, NOTIF_BODY) {
const NOTIF_ICON = path.join(__dirname, 'images/favicon.ico')
new Notification({ title: NOTIF_TITLE, body: NOTIF_BODY, icon: nativeImage.createFromPath(NOTIF_ICON) }).show()
}
function createWindow() {
win = new BrowserWindow({
title: 'SOG Arma 3 Launcher',
width: 1280,
height: 720,
autoHideMenuBar: true,
icon: path.join(__dirname, 'images/favicon.ico'),
resizable: false,
show: false,
webPreferences: {
nodeIntegration: true,
preload: path.join(__dirname, 'preload.js')
}
})
win.loadURL(`file://${__dirname}/index.hbs`)
win.on('close', (event) => {
if (!app.isQuiting) {
const NOTIF_TITLE = 'Minimizing App'
const NOTIF_BODY = 'App has been minimized to tray, and is running in the background'
showNotification(NOTIF_TITLE, NOTIF_BODY)
event.preventDefault()
win.hide()
}
return false
})
}
function createSplash() {
splash = new BrowserWindow({
titleBarStyle: 'hidden',
width: 256,
height: 256,
alwaysOnTop: true,
frame: false,
transparent: true
})
splash.loadURL(`file://${__dirname}/splash.hbs`)
}
ipcMain.on('minimize', (event) => {
event.preventDefault()
win.hide()
})
ipcMain.on('showNotification', (event, info) => {
const NOTIF_TITLE = info.nTitle
const NOTIF_BODY = info.nBody
showNotification(NOTIF_TITLE, NOTIF_BODY)
})
ipcMain.on('download', (event, info) => {
settingsData.arma3path = info.arma3path
settings = JSON.stringify(settingsData)
sfs.writeFile(sPath, settings, (e) => { if (e) throw e })
let req = http.request({method: 'HEAD', host: host.domain, port: host.port, path: host.path}, (res) => {
if (res.headers["last-modified"] != settingsData["last-modified"] || settingsData.version == "-1") {
log.log("Mismatch: " + res.headers["last-modified"] + " != " + settingsData["last-modified"])
settingsData["last-modified"] = res.headers["last-modified"]
if (fs.existsSync(`${info.arma3path}\\SOG`)) {
rimraf(`${info.arma3path}\\SOG`, (e) => { if (e) throw e })
}
download(BrowserWindow.getFocusedWindow(), host.protocol + host.domain + host.path, {
directory: `${app.getPath("userData")}\\mods`, onProgress: (state) => win.webContents.send('dlProgress', state)
}).then(dl => {
let file = dl.getSavePath()
extract(file, {dir: info.arma3path}, (e) => {
if (e) throw e
}).then(function unlinkFile() {
fs.unlink(file, (e) => { if (e) throw e })
}).then(function readWriteVersion() {
version = vfs.readFileSync(info.arma3path + "\\SOG\\Version")
settingsData.version = version.toString()
settings = JSON.stringify(settingsData)
vfs.writeFile(sPath, settings, (e) => { if (e) throw e })
}).then(function sendComplete() {
win.webContents.send('dlComplete', {version: settingsData.version, ip: host.gameIP, port: host.gamePORT, join: info.join})
})
}).catch((e) => {
if (e) log.log(e)
dlServerUp = false
win.webContents.send('serverDown', {download: true})
})
} else {
log.log("No Mismatch Found")
win.webContents.send('dlComplete', {version: settingsData.version, ip: host.gameIP, port: host.gamePORT, join: info.join})
}
})
req.on('abort', (e) => {
if (e) log.log(e)
dlServerUp = false
win.webContents.send('serverDown', {download: true})
})
req.on('error', (e) => {
if (e) log.log(e)
dlServerUp = false
win.webContents.send('serverDown', {download: true})
})
req.end()
})
ipcMain.on('refresh', (event, info) => {
gameDig.query({
type: 'arma3',
host: host.gameIP
}).then((state) => {
if (dlServerUp) {
win.webContents.send('serverUp', state)
} else {
win.webContents.send('serverDown', {download: true})
}
}).catch((e) => {
win.webContents.send('serverDown', {download: false})
})
})
app.disableHardwareAcceleration()
app.whenReady().then(() => {
createWindow()
createSplash()
autoUpdater.checkForUpdatesAndNotify()
}).then(function createTray() {
const iconPath = path.join(__dirname, 'images/favicon.ico')
tray = new Tray(nativeImage.createFromPath(iconPath))
const contextMenu = Menu.buildFromTemplate([
{ label: 'Restore', click: () => {
win.show()
}},
{ label: 'Exit', click: () => {
app.isQuiting = true
app.quit()
}}
])
tray.setToolTip('Arma 3 SOG Launcher')
tray.setContextMenu(contextMenu)
}).then(() => {
win.once('ready-to-show', () => {
setTimeout(() => {
splash.destroy()
win.show()
}, 3000)
})
})
app.on('activate', () => {
if (win === null) {
createWindow()
}
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.getFileIcon('images/favicon.ico')
app.setAppUserModelId(process.execPath)
autoUpdater.on('checking-for-update', () => {
log.info('Checking For Update...')
})
autoUpdater.on('update-available', (info) => {
log.info('Update Available.')
})
autoUpdater.on('update-not-available', (info) => {
log.info('Update Not Aavailable.')
})
autoUpdater.on('error', (e) => {
log.info('Error in Auto Updater. ' + e)
})
autoUpdater.on('download-progress', (progressTrack) => {
log.info('\n\ndownload-progress')
log.info(progressTrack)
})
autoUpdater.on('update-downloaded', () => {
log.info('Update Downloaded.')
})
try {
require('electron-reloader')(module)
} catch (_) {}