-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
179 lines (149 loc) · 5.79 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
// Modules to control application life and create native browser window
const { app, ipcMain, dialog, BrowserWindow } = require('electron');
const path = require('path');
const fs = require('fs');
const Store = require('electron-store');
const { nanoid } = require('nanoid');
const store = new Store();
console.log('Rabbitwave Streamer 1.0 (C) 2022 tomcat^mwi. All rights reserved.');
console.log('Start with -h or --help for command line options.');
// Process command line arguments --------------------------------------------------------------------------------------------
// The function is only here I can collapse it in VSCode...
const argv = process.argv.slice(2, process.argv.length);
(() => {
let argvIndex;
if (argv.includes('-h') || argv.includes('--help')) {
console.log(`
Available command line parameters:
-c, --config [filename] Load the specified configuration file, and remember it as the last one
-h, --help Show this help
-x, --clear Clear local storage (forget last saved config / asset list, etc.)
`);
process.exit();
}
// Clear local storage --------------------------------------------------------------------------------------
if (argv.includes('-x') || argv.includes('--clear')) {
console.log('Stored values cleared!');
store.clear();
}
// Get config file --------------------------------------------------------------------------------------
if (argv.includes('-c') || argv.includes('--config')) {
argvIndex = argv.findIndex(x => x === '-c' || x === '--config') + 1;
if (argvIndex < argv.length - 1) {
console.err('No configuration file was specified.');
process.exit();
}
store.add('lastConfig', argv[index]);
console.log(`Attempting to load configuration file: "${argv[index]}"`);
// Load config file --------------------------------------------------------------------------------------
if (!fs.existsSync(argv[index])) {
console.err(`Configuration file "${argv[index]}" doesn't exist. If the savePath contains spaces, please specify it between quotes.`);
process.exit();
}
}
})();
// ---------------------------------------------------------------------------------------------------
function createWindow() {
// Create the browser window.
const mainWindow = new BrowserWindow({
backgroundColor: '#000000',
center: true,
fullscreenable: true,
title: 'Rabbitwave Vlogger',
fullScreenWindowTitle: true,
autoHideMenuBar: true,
nodeIntegrationInWorker: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
enableRemoteModule: true,
nodeIntegration: true,
devTools: true,
webgl: false
},
fullscreen: true
});
mainWindow.loadFile('index.html');
// Clear browser cache
mainWindow.webContents.session.clearStorageData()
mainWindow.webContents.openDevTools();
mainWindow.on('close', function (e) {
var choice = dialog.showMessageBox(this,
{
type: 'question',
buttons: ['Yes', 'No'],
title: 'Confirm',
message: 'Are you sure you want to quit?'
});
if (choice == 1)
e.preventDefault();
});
}
// 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(async () => {
// Exposing methods to templates
ipcMain.handle('dialog', (event, method, params) => dialog[method](params));
ipcMain.handle('pathSeparator', () => path.sep);
ipcMain.handle('nanoid', () => nanoid());
ipcMain.handle('saveJSON', (event, savePath, data) => saveJSON(savePath, data));
ipcMain.handle('loadJSON', (event, savePath) => loadJSON(savePath));
ipcMain.handle('store_get', (event, key) => store.get(key));
ipcMain.handle('store_set', (event, key, value) => store.set(key, value));
ipcMain.handle('store_delete', (event, key) => store.delete(key));
ipcMain.handle('quit', () => process.exit());
ipcMain.handle('saveVideoFile', (event, savePath, videoBinary) => saveVideoFile(savePath, videoBinary));
ipcMain.handle('verifyAssets', (event, files) => verifyAssets(files));
createWindow();
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()
})
app.on('browser-window-created', function (e, window) {
window.setMenu(null);
});
// Saves a JSON file
function saveJSON(savePath, data) {
return new Promise((resolve, reject) => {
try {
fs.writeFileSync(savePath, JSON.stringify(data, null, 2), { encoding: 'utf8' });
resolve();
} catch (err) {
reject(err);
}
});
}
// Loads a JSON file
function loadJSON(savePath) {
return new Promise((resolve, reject) => {
try {
const data = JSON.parse(fs.readFileSync(savePath, { encoding: 'utf8' }));
resolve(data);
} catch (err) {
reject(err);
}
});
}
// Saves a video file
function saveVideoFile(savePath, videoBinary) {
return new Promise((resolve, reject) => {
fs.writeFile(savePath, Buffer.from(videoBinary, 'base64'), error => !!error ? reject(error) : resolve(savePath));
});
}
// Verifies if all file assets exist
function verifyAssets(assetList) {
const missingAssets = [];
assetList.filter(x => ['video', 'audio', 'image'].includes(x.type)).forEach(asset => {
if (!fs.existsSync(asset.filename))
missingAssets.push(asset);
});
return missingAssets;
}