-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
494 lines (437 loc) · 14.1 KB
/
Copy pathmain.js
File metadata and controls
494 lines (437 loc) · 14.1 KB
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
/*
Copyright (c) [2024] [Pulento - https://github.com/pulento]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, and to permit persons to
whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Redistribution and use in source and binary forms, with or without
modification, are not permitted for commercial purposes without the explicit
permission of the author.
*/
import { app, BrowserWindow, ipcMain, Menu, dialog, shell } from 'electron';
import Store from 'electron-store';
const appTitle = 'A1 Evo Electrified'
import path from 'node:path';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import { spawn, execSync, spawnSync } from 'node:child_process';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const prefStore = new Store();
const appDir = app.getAppPath();
const userDataDir = app.getPath('userData');
const homeDir = app.getPath('home');
// Sets default working directory
let A1EVODir = "";
const workDir = prefStore.get("workdirectory");
if (!workDir || workDir === "A1Evo") {
A1EVODir = path.join(homeDir, "A1Evo");
prefStore.set("workdirectory",A1EVODir);
} else {
A1EVODir = workDir;
}
let runDir = path.join(A1EVODir, getCurrentDateTime(true));
const mDir = "measurements";
let measDirectory = path.join(runDir, mDir);
const targetCurveDir = path.join(appDir.replace('app.asar',''), 'targetcurves');
const isMac = process.platform === 'darwin';
const isWindows = process.platform == 'win32';
const A1EEVersion = app.getVersion();
const browserWindows = [];
let mainWindow;
const menuTemplate = [
// { role: 'appMenu' }
...(isMac
? [{
label: app.name,
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ label: 'Settings', accelerator: 'Cmd+,', click: openSettings },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideOthers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
}]
: []),
// { role: 'fileMenu' }
{
label: 'File',
submenu: [
...(isMac
? [ { role: 'quit' } ]
: [ { label: 'Settings', accelerator: 'Ctrl+,', click: openSettings },
{ role: 'quit' } ]
)]
},
// { role: 'editMenu' }
{
label: 'Edit',
submenu: [
{ role: 'copy' },
{ role: 'paste' },
{ role: 'selectAll' }
]
},
// { role: 'viewMenu' }
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forceReload' },
{ role: 'toggleDevTools' },
{ type: 'separator' },
{ role: 'resetZoom' },
{ role: 'zoomIn' },
{ role: 'zoomOut' },
]
},
// { role: 'windowMenu' }
{
label: 'Window',
submenu: [
{ role: 'minimize' },
{ role: 'zoom' },
...(isMac
? [
{ type: 'separator' },
{ role: 'front' },
{ type: 'separator' },
{ role: 'window' }
]
: [
{ role: 'close' }
])
]
},
{
role: 'help',
submenu: [
{ role: 'about' },
{ label: 'Learn More',
click: async () => {
await shell.openExternal('https://www.youtube.com/watch?v=lmZ5yV1-wMI')
}}
]
}
]
// Squirrel nightmare to just install/uninstall a shortcut on Windows
var squirrelCommand = process.argv[1];
const appFolder = path.resolve(process.execPath, '..');
const rootAtomFolder = path.resolve(appFolder, '..');
const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
const exeName = path.basename(process.execPath);
switch (squirrelCommand) {
case '--squirrel-install':
case '--squirrel-updated':
console.log(`Install process..`);
spawn(updateDotExe, ['--createShortcut', exeName]);
prefStore.set("targetcurve", "");
setTimeout(app.quit, 1000);
case '--squirrel-uninstall':
// Remove desktop and start menu shortcuts
console.log(`Uninstall process..`);
spawn(updateDotExe,['--removeShortcut', exeName]);
prefStore.set("targetcurve", "");
setTimeout(app.quit, 1000);
app.quit();
}
app.setName(appTitle);
console.log(`App directory: ${appDir}`);
console.log(`Home directory: ${homeDir}`);
console.log(`Working directory: ${runDir}`);
console.log(`Measurement directory: ${measDirectory}`);
console.log(`User data directory: ${userDataDir}`);
console.log(`Target curves directory: ${targetCurveDir}`);
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1200,
height: 1000,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
ipcMain.on('set-title', (event, title) => {
const webContents = event.sender
const win = BrowserWindow.fromWebContents(webContents)
win.setTitle(title)
app.setName(title)
})
ipcMain.handle("make-run-dir", (event, directory) => {
// Update runnning and measurement directory
runDir = path.join(A1EVODir, getCurrentDateTime(true));
measDirectory = path.join(runDir, mDir);
fs.mkdirSync(path.join(runDir, directory), { recursive: true }, err => {
if (err) {
if (err.code !== "EEXIST") {
console.error(`Error creating folder: ${directory} - ${err}`);
} else {
// directory exists
console.warn(`Directory already exists: ${directory}`)
}
} else {
// file written successfully
console.log(`Folder created: ${path.join(A1EVODir, directory)}`)
}
})
})
ipcMain.handle("list-dir", (event, directory) => {
let files, err;
try {
files = fs.readdirSync(path.join(runDir, directory), err => {
if (err) {
console.error(`Error reading folder ${directory} - ${err}`);
return {undefined, err};
}
})
} catch(err) {
console.error(`Error reading folder - ${err}`);
return {undefined, err};
}
// Convert to full paths
let mFiles = []
for (let file of files) {
let mFile = path.resolve(measDirectory, file);
mFiles.push(mFile)
}
return {mFiles, err};
})
ipcMain.handle("get-date", (event, timestamp = false) => {
return getCurrentDateTime(timestamp);
})
ipcMain.on("save-file", (event, file_name, contents) => {
try {
fs.writeFileSync(path.join(runDir, file_name), contents, err => {
if (err) {
console.error(`Error saving: ${file_name} - ${err}`);
}
})
} catch(err) {
console.error(`Error saving: ${file_name} - ${err}`);
}
})
ipcMain.handle("check-file", (event, file_name) => {
let tcFile = false;
try {
tcFile = fs.statSync(file_name, (err, stats) => {
console.log(`Checking ${file_name}`);
if (err) {
console.error(`Error checking file: ${file_name} - ${err}`);
return false;
} else {
console.log(stats);
if (stats.isFile()) {
return true;
} else {
return false;
}
}
});
} catch(err) {
console.error(`Error checking file. ${err}`);
return false;
}
return tcFile;
})
ipcMain.on("save-measurement", (event, file_name, contents) => {
try {
fs.writeFileSync(path.join(measDirectory, file_name), contents, err => {
if (err) {
console.error(`Error saving: ${file_name} - ${err}`);
}
})
} catch(err) {
console.error(`Error saving: ${file_name} - ${err}`);
}
})
ipcMain.handle('get-version', (event) => {
return A1EEVersion;
})
ipcMain.handle('get-dir', (event, dir) => {
switch (dir) {
case "targetcurve":
return targetCurveDir;
case "home":
return homeDir;
case "measurements":
return measDirectory;
}
})
ipcMain.handle('get-config-key', (event, key) => {
return prefStore.get(key);
})
ipcMain.handle('set-config-key', (event, key, value) => {
console.log(`Set default ${key} to ${value}`);
prefStore.set(key, value);
})
ipcMain.handle('delete-config-key', (event, key) => {
console.log(`Delete ${key}`);
prefStore.delete(key);
})
ipcMain.handle('dialog', async (event, method, params) => {
const filePath = await dialog[method](params);
//console.log(filePath[0]);
if (filePath)
return filePath[0];
else
return "";
})
ipcMain.handle('show-error-box', (event, title, message) => {
dialog.showErrorBox(title, message);
mainWindow.reload();
})
ipcMain.handle('show-restart-box', (event, restartmessage) => {
dialog.showMessageBoxSync(mainWindow,{ message: restartmessage, type: "warning", buttons: ["OK"]});
app.relaunch();
app.quit();
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
}
// Return current date in readeable format or timestamp
function getCurrentDateTime(timestamp = false) {
const now = new Date();
if (!timestamp) {
return now.toLocaleString('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: true
});
} else {
// obtain TZ offset
let tzOffset = now.getTimezoneOffset() * 60000;
let localTS = new Date(now - tzOffset);
return (localTS.toISOString().replace(/[^\d]/g,'').slice(0, -3)).slice(0,12);
}
}
function openSettings(menuItem, browserWindow, event) {
console.log('Settings invoked');
if (BrowserWindow.getAllWindows().length <= 1) {
// Create the settings window.
const settingsWindow = new BrowserWindow({
show: false,
width: 1100,
height: 520,
parent: mainWindow,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
}
});
settingsWindow.removeMenu();
settingsWindow.once('ready-to-show', () => {
settingsWindow.show();
});
settingsWindow.loadFile('settings.html')
}
}
// Create default preferences
function createDefaultConf() {
prefStore.store = {
"version": app.getVersion(),
"workdirectory": A1EVODir,
"forceMLP": false,
"forceSmall": false,
"forceWeak": false,
"forceCentre": false,
"forceLarge": false,
"noInversion": false,
"limitLPF": false,
"endFrequency": 250,
"maxBoost": 0,
"omaxBoost": 0,
"targetcurve": "",
"XO": { "BDL": [], "C": [], "CH": [], "FDL": [], "FHL": [], "FL": [], "FWL": [], "RHL": [],
"SBL": [], "SDL": [], "SLA": [], "SHL": [], "TFL": [], "TML": [], "TRL": [], "TS": [], },
};
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
// Menu
const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu);
console.log("Starting REW ...");
let confVersion = prefStore.get('version');
if (!confVersion) {
console.log('Creating default preferences.');
createDefaultConf();
} else {
console.log(`Config Version: ${confVersion}`);
}
if (isMac) {
let plutil = spawn("plutil", ["-replace", '/.room eq wizard/.dropsmallfilters', "-string", "false", homeDir + "/Library/Preferences/com.apple.java.util.prefs.plist"]);
plutil.stderr.on("data", (err) => {
console.error(err);
});
plutil.stdout.on('data', (data) => {
console.log(data.toString());
});
plutil.on('close', (code) => {
if (code == 0)
console.log(`REW preferences set.`);
else
console.warn('Something went wrong setting REW preferences, check "Drop filters is gain is small" on Eq section to be unchecked.')
});
console.log("Starting REW.")
let rew = spawn("open", ["-a", "REW.app", "--args", "-api"]);
rew.stderr.on("data", (err) => {
console.error(err);
});
} else {
const powerShellScript = `
$ErrorActionPreference = 'Stop'
$keyPath = "HKCU:\\SOFTWARE\\JavaSoft\\Prefs\\room eq wizard"
$key = "dropsmallfilters"
$value = "false"
try {
Set-ItemProperty -Path $keyPath -Name $key -value $value
} catch {
Write-Error "Error accessing the registry: $_"
}`
let pscript = spawn(powerShellScript, { shell: 'powershell.exe' });
pscript.on('close', (code) => {
if (code == 0)
console.log(`REW preferences set.`);
else
console.warn('Something went wrong setting REW preferences, check "Drop filters is gain is small" on Eq section to be unchecked.')
});
console.log("Starting REW.")
let rew = spawn("C:\\Program Files\\REW\\roomeqwizard.exe", ["-api"]);
rew.stderr.on("data", (err) => {
console.error(err);
});
}
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0)
createWindow();
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.