-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
392 lines (324 loc) · 13.6 KB
/
main.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
//console.time("startup");
//console.time("interactable");
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
logQueue = []
// special log function that should also log to the browser window devtools console
function log(msg, opts) {
console.log(msg, opts??"");
mainWindow ? // we dont want to refrence the mainWindow if it doesnt exist yet
mainWindow.webContents.send("console:log", msg) :
logQueue.push(msg) // so if it doesnt exist, save it to a queue that will be requested by the window when it is ready to
}
global.log = log; // allows me to use this anywhere (but mostly for the node debug console)
log("loading required modules...")
// Modules to control application life and create native browser window
const {app, ipcMain, systemPreferences, dialog, globalShortcut} = require('electron');
var isFirstInstance = app.requestSingleInstanceLock();
if (!isFirstInstance) return app.quit();
const path = require('path');
const platFolders = require('platform-folders');
//const ewc = require('@svensken/ewc');
const { BrowserWindow } = require("electron-acrylic-window");
const { autoUpdater } = require("electron-updater");
const { checkForUpdates } = require("./updater")
const os = require("os");
const fs = require("fs");
const exec = require('child_process').exec;
const WebSocket = require("ws")
const DiscordRPC = require("discord-rpc");
log("loading player modules")
const songsModule = require("./js/songs.js")
const serverModule = require("./js/server.js");
//const { stdout } = require('process');
const clientID = "659092610400387093";
var rpc = null;
var rpcReady = false
var rpcCache = null
//ipcMain.on("songs:getSongList", (e) => {
// e.returnValue = songs
//})
log("registering handlers and listeners...")
ipcMain.on("console:getlogs", (e) => {
//console.log("sending logs to browser window")
//console.timeEnd("interactable")
while (logQueue.length > 0) {
mainWindow.webContents.send("console:log", logQueue.shift());
}
mainWindow.setThumbarButtons([
{
tooltip: 'Previous Song',
icon: path.join(__dirname, "assets/play.png"),
flags: ["disabled"],
click () { keybind_prev() }
},
{
tooltip: 'Pause/Play',
icon: path.join(__dirname, "assets/play.png"),
click () { keybind_pauseplay() }
},
{
tooltip: 'Next Song',
icon: path.join(__dirname, "assets/skip.png"),
click () { keybind_next() }
}
])
})
// sends the song list to the browser window when requested to
ipcMain.handle("songs:getSongList", (e) => {
log("sending songs to browser")
return songs
})
ipcMain.handle("main:getVersion", e=>{
log("sending version to browser")
return {
isPackaged: app.isPackaged,
version: app.isPackaged ? app.getVersion() : (require("./package.json").version + " - Development Build")
}
})
// opens the the music folders file, and creates it first if it does not exist
ipcMain.on("file:openMusicFoldersFile", () => {
let ext = "ssbu-music" + ( app.isPackaged ? "" : "-dev" )
if (!fs.existsSync(platFolders.getMusicFolder() + "/folders." + ext)) {
fs.writeFileSync(platFolders.getMusicFolder() + "/folders." + ext, "// Hey, just write a list of folders you want to load from here\n// Start a line with any of ['#', '//', '$', '!', '-'] and it will be ignored when being parsed (to write comments)\n// Start a line with '%' to tell the player to also look in subfolders of the folder on that line")
// i use sync here because i want to be 100% sure it exists before trying to open it.
}
exec(platFolders.getMusicFolder() + "/folders." + ext)
})
ipcMain.on("file:openSettingsFile", () => {
let ext = "ssbu-music" + ( app.isPackaged ? "" : "-dev" )
if (!fs.existsSync(platFolders.getMusicFolder() + "/settings." + ext)) {
fs.writeFileSync(platFolders.getMusicFolder() + "/settings." + ext, "// Hey, just write a list of folders you want to load from here\n// Start a line with any of ['#', '//', '$', '!', '-'] and it will be ignored when being parsed (to write comments)\n// Start a line with '%' to tell the player to also look in subfolders of the folder on that line")
// i use sync here because i want to be 100% sure it exists before trying to open it.
}
exec(platFolders.getMusicFolder() + "/settings." + ext)
})
// allows the browser window to set a discord status
ipcMain.on("rpc:setactivity", (e, activity) => {
//if (!isWindows10()) return false // only works on windows for now (not sure why), too lazy to check for other windows versions though
if (!rpcReady) {
rpcCache = activity;
return
}
rpc.setActivity(activity)
//log("rpc: " + JSON.stringify(activity))
})
async function startRPC() {
//if (!isWindows10()) return false
log("setting up discord rpc...")
DiscordRPC.register(clientID);
rpc = new DiscordRPC.Client({ transport: 'ipc' });
//global.rpc = rpc
rpc.on('ready', () => {
rpcReady = true
rpcCache ??= {"state": "idle"}
rpc.setActivity(rpcCache)
rpcCache = null;
//setInterval(() => {updateRPC()}, 15e3);
})
rpc.on('error', (e) => {
log(e)
})
rpc.login({ clientId: clientID }).then(() => {
rpc.subscribe('ACTIVITY_JOIN', ({ secret }) => {
log(secret);
startWSInstance(secret);
})
rpc.subscribe('ACTIVITY_JOIN_REQUEST', (user) => {
log(user)
})
}).catch(console.error);
}
log("registering all functions...")
var ws = null;
function startWSInstance(dest) {
log("creating websocket client...")
ws = new WebSocket("ws://" + dest, {});
log("assigning websocket listeners...")
ws.on('open', () => {
ws.send("ws:connected");
})
ws.on("message", (data) => {
log(data)
})
log("created websocket client.")
}
//rpc.subscribe("")
//let popupWindow;
// function to check if the user is on windows 10 or not. this checks *specifically* for windows 10, and not just windows in general
// this function is only used to see if it should attempt to use acrylic or not
function isWindows10() {
if (process.platform !== 'win32') return false;
return os.release().split('.')[0] === '10';
}
function supportsVibrancy() {
return process.platform === "darwin" || isWindows10();
}
function getVibrancySettings() {
if (isWindows10()) return {
theme: '#22222222',
effect: 'acrylic',
useCustomWindowRefreshMethod: true,
disableOnBlur: true,
debug: false
};
return 'dark'; // if not on windows 10, this is used as the "vibrancy" setting on OSX, which is similar to acrylic
// if not on OSX, this setting is just ignored at that point
}
var twitchOauthWindow = null;
async function createWindow () {
// Create the browser window.
log("creating browser window...")
let browserWindowProperties = {
width: 900,
height: 600,
minWidth: 550,
minHeight: 475,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: false
//webSecurity: false // only ever disabled for testing purposes. a build will never intentionally be released with webSecurity disabled
},
frame: false,
};
supportsVibrancy() ?
mainWindow = new BrowserWindow({
...browserWindowProperties,
vibrancy: getVibrancySettings(),
//frame: process.platform !== "win32" // on windows, you need the frame disabled for acrylic to work. on mac, it doesnt matter (i think)
}) :
mainWindow = new BrowserWindow({
...browserWindowProperties,
backgroundColor: '#444444',
//frame: process.platform !== "win32" // if we arent on windows, use the native title bar. only windows has the font used for title bar buttons
});
mainWindow.menuBarVisible = false;
songsModule.loadSongsFromMusicFolder().then((ret) => { // waits for songs to finish loading
console.log("sending updated list")
mainWindow.webContents.send("songs:refreshList", ret); // then tells the browser window the new songs and that it should refresh them
});
//ewc.setAcrylic(mainWindow, 0x14800020);
log("loading music player page...")
// and load the index.html of the app.
mainWindow.loadFile('index.html')
//mainWindow.loadURL("chrome://chrome-urls")
// Open the DevTools if app is not packaged. (so only in dev environment)
if (!app.isPackaged) {
log("opening devtools...");
mainWindow.webContents.openDevTools();
}
log("registering window listeners...")
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
});
mainWindow.webContents.on('will-navigate', (e, url) => {
if(url != mainWindow.webContents.getURL()) {
e.preventDefault()
require('electron').shell.openExternal(url)
}
})
mainWindow.webContents.on('new-window', (e, url) => {
if(url != mainWindow.webContents.getURL() && !url.startsWith("https://id.twitch.tv")) {
e.preventDefault()
require('electron').shell.openExternal(url)
}
if (url.startsWith("https://id.twitch.tv")) {
console.log(e)
}
})
mainWindow.on("maximize", () => {mainWindow.webContents.send("win.maxres", "max")});
mainWindow.on("unmaximize", () => {mainWindow.webContents.send("win.maxres", "res")});
startRPC()
log("registering media key handlers...")
// registers global shortcut handlers for media keys on the keyboard
globalShortcut.register("MediaPlayPause", keybind_pauseplay);
globalShortcut.register("MediaStop", keybind_stop);
globalShortcut.register("MediaNextTrack", keybind_next);
globalShortcut.register("MediaPreviousTrack", keybind_prev);
log("Done!")
}
function keybind_pauseplay() { mainWindow.webContents.send("updateState", "playpause"); /*log("playpause");*/ };
function keybind_stop() { mainWindow.webContents.send("updateState", "stop"); /*log("stop");*/ };
function keybind_prev() { mainWindow.webContents.send("updateState", "prev"); /*log("prev");*/ };
function keybind_next() { mainWindow.webContents.send("updateState", "next"); /*log("next");*/ };
log("registering application listeners")
app.on("will-quit", () => {
log("closing.")
rpcReady = false
globalShortcut.unregisterAll();
})
app.on("second-instance", (e, args, dir) => {
mainWindow?.webContents.send("secondinst:args", {args, dir});
})
// 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.on('ready', async () => {
log("application ready, preparing to create window...")
createWindow()
})
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
// disabled because idc about how mac works differently and because
// it doesnt currently support reopening the main window or running
// in the background (while closed)
//if (process.platform !== 'darwin') app.quit()
rpcReady = false
rpc.destroy().then(() => {
app.quit()
}).catch(() => {
app.quit()
})
})
/*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 (mainWindow === null) createWindow()
})*/
// 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.
ipcMain.on('acrylic-enable', (_) => {
if (supportsVibrancy()) mainWindow.setVibrancy(getVibrancySettings())
});
ipcMain.on('acrylic-disable', (_) => {
if (supportsVibrancy()) mainWindow.setVibrancy(null)
});
ipcMain.on("updateCheck", function() {
checkForUpdates()
})
ipcMain.on("win.control.min", () => {mainWindow.minimize()});
ipcMain.on("win.control.maxres", () => {
mainWindow.isMaximized() ?
mainWindow.unmaximize() :
mainWindow.maximize()
});
ipcMain.on("win.sizehack", () => {
let s = mainWindow.getSize();
mainWindow.setSize(s[0], s[1]+1);
mainWindow.setSize(s[0], s[1]);
})
ipcMain.on("win.beforeunload", () => {
//mainWindow.removeAllListeners();
})
/*
ipcMain.on('blurBehind', (_) => {
ewc.setBlurBehind(mainWindow, 0x14800020);
});
ipcMain.on('gradient', (_) => {
ewc.setGradient(mainWindow, 0x14800020);
});
ipcMain.on('trGradient', (_) => {
ewc.setTransparentGradient(mainWindow, 0x14800020);
});
*/
log("Finished main file sequence.")
//console.timeEnd("startup")