-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
368 lines (324 loc) · 8.82 KB
/
main.ts
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
import { app, BrowserWindow, Menu, Tray, screen, ipcMain } from 'electron';
import * as path from 'path';
import * as url from 'url';
import * as ffxivStream from './src/engine/stream';
import { Settings } from './src/engine/settings';
var fs = require('fs');
let isQuitting = false;
let devModeEnabled = false;
let overlayHidden = true;
const cureAppName = 'Cure';
const cureWebsite = 'https://www.munsy.io/cure/';
const mainX = 850;
const mainY = 450;
const log = require('electron-log');
let win, tray, overlay, serve, ffxivEngine;
const args = process.argv.slice(1);
serve = args.some(val => val === '--serve');
function maximize() {
const electronScreen = screen;
const size = electronScreen.getPrimaryDisplay().workAreaSize;
win.width = size.width;
win.height = size.height;
}
function defaultScreenSize() {
win.setSize(mainX, mainY);
}
function createWindow() {
log.info('Creating the main window');
var electronScreen = screen;
var size = electronScreen.getPrimaryDisplay().workAreaSize;
let cureAppIcon = devModeEnabled ? url.format({
pathname: path.join(__dirname, 'src/assets/images/cure-mini.png'),
protocol: 'file:',
slashes: true
}) : url.format({
pathname: path.join(__dirname, 'dist/assets/images/cure-mini.png'),
protocol: 'file:',
slashes: true
});
// Create the browser window.
win = new BrowserWindow({
x: 0,
y: 0,
width: mainX,
height: mainY,
resizable: false,
frame: false,
webPreferences: {
nodeIntegration: true,
},
});
win.setMenu(null);
win.center();
defaultScreenSize();
if (serve) {
require('electron-reload')(__dirname, {
electron: require(`${__dirname}/node_modules/electron`)
});
win.loadURL('http://localhost:4200');
} else {
win.loadURL(url.format({
pathname: path.join(__dirname, 'dist/index.html'),
protocol: 'file:',
slashes: true
}));
}
// Emitted when the window is closed.
win.on('closed', (event) => {
if(!isQuitting) {
event.preventDefault();
win.hide();
} else {
win = null;
if(overlay !== null) {
overlay.close();
}
}
});
// Intercepts the window close event before it
// executes and minimizes the program instead.
win.on('close', function (event) {
if(!isQuitting){
event.preventDefault();
win.hide();
}
return false;
});
}
function createOverlay() {
if(!ffxivEngine) {
log.info('Initializing FFXIV engine...');
ffxivEngine = new ffxivStream.FFXIVStream();
console.log(ffxivEngine);
}
var electronScreen = screen;
var size = electronScreen.getPrimaryDisplay().workAreaSize;
overlay = new BrowserWindow({
x: 0,
y: 0,
width: size.width,
height: size.height,
type: 'toolbar',
transparent: true,
frame: false,
alwaysOnTop: true,
resizable: false,
webPreferences: {
nodeIntegration: true,
webSecurity: false
},
});
if (serve) {
require('electron-reload')(__dirname, {
electron: require(`${__dirname}/node_modules/electron`)
});
overlay.loadURL('http://localhost:4200/#/overlay');
} else {
overlay.loadURL(url.format({
pathname: path.join(__dirname, 'dist/index.html'),
protocol: 'file:',
slashes: true,
hash: '/overlay'
}));
}
overlay.setSkipTaskbar(true);
overlay.setIgnoreMouseEvents(true);
overlay.on('hide', function() {
if(!overlayHidden) {
overlayHidden = true;
overlay.hide();
}
});
overlay.on('show', function() {
if(overlayHidden) {
overlayHidden = false;
overlay.show();
}
});
overlay.on('closed', function () {
overlay = null;
});
}
function overlayStartup() {
if(overlay == null || overlay === null) {
createOverlay();
overlay.hide();
}
}
function buildTray() {
const nativeImage = require('electron').nativeImage;
let cureAppIcon = serve ? url.format({
pathname: path.join(__dirname, 'src/assets/images/cure-mini.png'),
protocol: 'file:',
slashes: true
}) : url.format({
pathname: path.join(__dirname, 'dist/assets/images/cure-mini.png'),
protocol: 'file:',
slashes: true
});
let trayIcon = nativeImage.createFromPath(cureAppIcon);
trayIcon = trayIcon.resize({ width: 16, height: 16 });
tray = new Tray(trayIcon);
const enableOverlayContextMenu = Menu.buildFromTemplate([
{ label: 'Launch website', click: async () => {
const { shell } = require('electron');
await shell.openExternal(cureWebsite);
}
},
{ label: 'Open Cure', click: function() {
win.show();
} },
{ label: 'Enable Overlay', click: function(){
overlay.show();
tray.setContextMenu(disableOverlayContextMenu);
overlayHidden = false;
} },
{ label: 'Quit', click: function() {
isQuitting = true;
app.quit();
} }
]);
const disableOverlayContextMenu = Menu.buildFromTemplate([
{ label: 'Launch website', click: async () => {
const { shell } = require('electron');
await shell.openExternal(cureWebsite);
}
},
{ label: 'Open Cure', click: function() {
win.show();
} },
{ label: 'Disable Overlay', click: function() {
overlay.hide();
tray.setContextMenu(enableOverlayContextMenu);
overlayHidden = true;
} },
{ label: 'Quit', click: function() {
isQuitting = true;
app.quit();
} }
]);
tray.setToolTip(cureAppName);
if(overlayHidden) {
tray.setContextMenu(enableOverlayContextMenu);
} else {
tray.setContextMenu(disableOverlayContextMenu);
}
}
function timer(ms: number) {
return new Promise(res => setTimeout(res, ms));
}
try {
app.on('ready', createWindow);
app.on('ready', overlayStartup);
app.on('ready', buildTray);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (win === null) {
createWindow();
}
});
ipcMain.on('launchBrowser', async () => {
const { shell } = require('electron')
await shell.openExternal(cureWebsite)
});
ipcMain.on('minimizeMain', () => {
win.hide();
});
ipcMain.on('quitProgram', () => {
win.hide();
});
ipcMain.on('devMode', () => {
if(!devModeEnabled) {
win.webContents.openDevTools();
if(overlay === null) {
if(devModeEnabled) {
console.log('null overlay');
}
return;
}
overlay.webContents.openDevTools();
overlay.setIgnoreMouseEvents(false);
} else {
win.webContents.closeDevTools();
if(overlay === null) {
if(devModeEnabled) {
console.log('null overlay');
}
return;
}
overlay.webContents.closeDevTools();
overlay.setIgnoreMouseEvents(true);
}
devModeEnabled = !devModeEnabled;
});
ipcMain.on("overlay", (event, arg) => {
switch(arg.case) {
case "on":
if(overlay === null) { createOverlay(); }
overlay.show();
log.info('overlay on');
break;
case "off":
if(overlay !== null) { overlay.hide(); }
log.info('overlay off');
break;
case "lock":
overlay.setIgnoreMouseEvents(true);
overlay.webContents.send("overlay", {"case": "lock", "arg": ""});
log.info('overlay lock');
break;
case "unlock":
overlay.maximize();
overlay.setIgnoreMouseEvents(false);
overlay.webContents.send("overlay", {"case": "unlock", "arg": ""});
log.info('unlock');
break;
case "test":
overlay.webContents.send("overlay", {"case": "update", "arg": arg.arg});
log.info('overlay test');
break;
default:
overlay.webContents.send("overlay", "default");
log.info('overlay default');
return;
}
});
ipcMain.on("combat", async (event, arg) => {
console.log(arg);
switch(arg.case) {
case "update":
overlay.webContents.send("combat", {"case": "update", "arg": arg.arg});
break;
case "sleepyTest":
console.log('sleepyTest called');
console.log(arg);
for(var i = +arg.arg; i < (+arg.arg + 20); i++) {
await timer(1000);
console.log(i);
overlay.webContents.send("combat", {"case": "sleepy", "arg": i});
}
break;
default:
overlay.webContents.send("combat", {"case": "", "arg": ""});
return;
}
});
ipcMain.on('get-ffxiv-dir', (event, arg) => {
const files = fs.readdirSync(__dirname);
const electron = require('electron');
const dialog = electron.dialog;
const dir = dialog.showOpenDialog(win, {
defaultPath: arg,
properties: ['openDirectory']
});
win.webContents.send('get-ffxiv-dir-response', dir);
});
} catch (e) {
console.log(e);
log.error(e);
}