-
Notifications
You must be signed in to change notification settings - Fork 893
/
main.js
425 lines (378 loc) · 13.1 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
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
const electron = require('electron');
const path = require('path');
const app = electron.app; // Module to control application life.
const BrowserWindow = electron.BrowserWindow; // Module to create native browser window.
const Menu = electron.Menu;
const Notification = electron.Notification;
const protocol = electron.protocol;
const parseArgs = require('minimist');
const isDev = require('electron-is-dev');
const ipcMain = electron.ipcMain;
const autoUpdater = require('electron-updater').autoUpdater;
const log = require('electron-log');
const { uploadLocalFile } = require('./LocalFileUploader');
const { serveFolder, stopServer } = require('./ServeFolder');
const { startDebuggerServer, sendMessage } = require('./DebuggerServer');
const {
buildElectronMenuFromDeclarativeTemplate,
buildPlaceholderMainMenu,
} = require('./MainMenu');
const { loadExternalEditorWindow } = require('./LocalExternalEditorWindow');
const { load, registerGdideProtocol } = require('./Utils/UrlLoader');
const throttle = require('lodash.throttle');
const { findLocalIp } = require('./Utils/LocalNetworkIpFinder');
const setUpDiscordRichPresence = require('./DiscordRichPresence');
const {
downloadLocalFile,
saveLocalFileFromArrayBuffer,
} = require('./LocalFileDownloader');
const { openPreviewWindow, closePreviewWindow } = require('./PreviewWindow');
const {
setupLocalGDJSDevelopmentWatcher,
closeLocalGDJSDevelopmentWatcher,
} = require('./LocalGDJSDevelopmentWatcher');
const { setupWatcher, disableWatcher } = require('./LocalFilesystemWatcher');
// Initialize `@electron/remote` module
require('@electron/remote/main').initialize();
log.info('GDevelop Electron app starting...');
// Logs made with electron-logs can be found
// on Linux: ~/.config/<app name>/log.log
// on OS X: ~/Library/Logs/<app name>/log.log
// on Windows: %USERPROFILE%\AppData\Roaming\<app name>\log.log
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
autoUpdater.autoDownload = false;
// 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 = null;
// Parse arguments (knowing that in dev, we run electron with an argument,
// so have to ignore one more).
const args = parseArgs(process.argv.slice(isDev ? 2 : 1), {
// "Officially" supported arguments and their types:
boolean: ['dev-tools', 'disable-update-check'],
string: '_', // Files are always strings
});
// See registerGdideProtocol (used for HTML modules support)
protocol.registerSchemesAsPrivileged([{ scheme: 'gdide' }]);
// Notifications on Microsoft Windows platforms show the app user model id.
// If not set, defaults to `electron.app.{app.name}`.
if (process.platform === 'win32') {
app.setAppUserModelId('gdevelop.ide');
}
// Quit when all windows are closed.
app.on('window-all-closed', function() {
app.quit();
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
const isIntegrated = args.mode === 'integrated';
const devTools = !!args['dev-tools'];
if (isIntegrated && app.dock) {
app.dock.hide();
}
registerGdideProtocol({ isDev });
// Create the browser window.
const options = {
width: args.width || 800,
height: args.height || 600,
x: args.x,
y: args.y,
titleBarStyle: 'hidden',
titleBarOverlay: {
color: '#000000',
symbolColor: '#ffffff',
},
trafficLightPosition: { x: 12, y: 12 },
webPreferences: {
webSecurity: false, // Allow to access to local files,
// Allow Node.js API access in renderer process, as long
// as we've not removed dependency on it and on "@electron/remote".
nodeIntegration: true,
contextIsolation: false,
},
enableLargerThanScreen: true,
backgroundColor: '#000',
};
if (isIntegrated) {
options.acceptFirstMouse = true;
options.skipTaskbar = true;
options.hasShadow = false;
options.frame = false;
options.minimizable = false;
options.movable = false;
options.resizable = false;
options.fullscreenable = false;
options.show = false;
}
mainWindow = new BrowserWindow(options);
if (!isIntegrated) mainWindow.maximize();
// Enable `@electron/remote` module for renderer process
require('@electron/remote/main').enable(mainWindow.webContents);
if (isDev)
mainWindow.webContents.session.loadExtension(
path.join(__dirname, 'extensions/ReactDeveloperTools/4.24.3_0/')
);
// Expose program arguments (to be accessed by mainWindow)
global['args'] = args;
// Load the index.html of the app.
load({
window: mainWindow,
isDev,
path: '/index.html',
devTools,
});
Menu.setApplicationMenu(buildPlaceholderMainMenu());
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;
stopServer(() => {});
});
ipcMain.on('set-main-menu', (event, mainMenuTemplate) => {
Menu.setApplicationMenu(
buildElectronMenuFromDeclarativeTemplate(mainWindow, mainMenuTemplate)
);
});
//Prevent any navigation inside the main window.
mainWindow.webContents.on('will-navigate', (e, url) => {
if (url !== mainWindow.webContents.getURL()) {
console.info('Opening in browser (because of will-navigate):', url);
e.preventDefault();
electron.shell.openExternal(url);
}
});
// Prevent opening any website or url inside Electron
mainWindow.webContents.setWindowOpenHandler(details => {
console.info('Opening in browser (because of new window): ', details.url);
electron.shell.openExternal(details.url);
return { action: 'deny' };
});
ipcMain.handle('preview-open', async (event, options) => {
return openPreviewWindow({
parentWindow: mainWindow,
previewBrowserWindowOptions: options.previewBrowserWindowOptions,
previewGameIndexHtmlPath: options.previewGameIndexHtmlPath,
alwaysOnTop: options.alwaysOnTop,
hideMenuBar: options.hideMenuBar,
numberOfWindows: options.numberOfWindows,
captureOptions: options.captureOptions,
openEvent: event,
});
});
ipcMain.handle('preview-close', async (event, options) => {
return closePreviewWindow(options.windowId);
});
// Piskel image editor
ipcMain.handle('piskel-load', (event, externalEditorInput) => {
return loadExternalEditorWindow({
parentWindow: mainWindow,
devTools,
indexSubPath: 'piskel/piskel-electron-index.html',
externalEditorInput,
});
});
// JFXR sound effect generator
ipcMain.handle('jfxr-load', (event, externalEditorInput) => {
return loadExternalEditorWindow({
parentWindow: mainWindow,
devTools,
indexSubPath: 'jfxr/jfxr-electron-index.html',
externalEditorInput,
});
});
// Yarn Dialogue Tree Editor
ipcMain.handle('yarn-load', (event, externalEditorInput) => {
return loadExternalEditorWindow({
parentWindow: mainWindow,
devTools,
indexSubPath: 'yarn/yarn-electron-index.html',
externalEditorInput,
});
});
// LocalFileUploader events:
ipcMain.on('local-file-upload', (event, localFilePath, uploadOptions) => {
log.info(
'Received event local-file-upload with localFilePath=',
localFilePath
);
uploadLocalFile(
localFilePath,
uploadOptions,
throttle((current, max) => {
event.sender.send('local-file-upload-progress', current, max);
}, 300)
).then(
() => {
log.info('Local file upload successfully done');
event.sender.send('local-file-upload-done', null);
},
uploadError => {
log.error('Local file upload errored', uploadError);
event.sender.send('local-file-upload-done', uploadError);
}
);
});
// Titlebar handling:
ipcMain.handle(
'titlebar-set-overlay-options',
async (event, overlayOptions) => {
if (!mainWindow) return;
// setTitleBarOverlay seems not defined on macOS.
if (mainWindow.setTitleBarOverlay)
mainWindow.setTitleBarOverlay(overlayOptions);
}
);
// LocalFileDownloader events:
ipcMain.handle('local-file-download', async (event, url, outputPath) => {
const result = await downloadLocalFile(url, outputPath);
return result;
});
ipcMain.handle(
'local-file-save-from-arraybuffer',
async (event, arrayBuffer, outputPath) => {
const result = await saveLocalFileFromArrayBuffer(
arrayBuffer,
outputPath
);
return result;
}
);
// LocalFilesystemWatcher events:
ipcMain.handle(
'local-filesystem-watcher-setup',
(event, folderPath, options) => {
const subscriptionId = setupWatcher(
folderPath,
changedFilePath => {
event.sender.send('project-file-changed', changedFilePath);
},
options
);
return subscriptionId;
}
);
ipcMain.handle(
'local-filesystem-watcher-disable',
(event, subscriptionId) => {
disableWatcher(subscriptionId);
}
);
// ServeFolder events:
ipcMain.on('serve-folder', (event, options) => {
log.info('Received event to server folder with options=', options);
serveFolder(options, (err, serverParams) => {
// Using JSON to copy the config strips unserializable properties
// (like middleware functions) automatically.
const configCopy = JSON.parse(JSON.stringify(serverParams));
event.sender.send('serve-folder-done', err, configCopy);
});
});
ipcMain.on('stop-server', event => {
log.info('Received event to stop server');
stopServer(err => {
event.sender.send('stop-server-done', err);
});
});
ipcMain.on('get-local-network-ip', event => {
event.sender.send('local-network-ip', findLocalIp());
});
// LocalGDJSDevelopmentWatcher events:
ipcMain.on('setup-local-gdjs-development-watcher', event => {
setupLocalGDJSDevelopmentWatcher();
});
ipcMain.on('close-local-gdjs-development-watcher', event => {
closeLocalGDJSDevelopmentWatcher();
});
// DebuggerServer events:
ipcMain.on('debugger-start-server', (event, options) => {
log.info('Received event to start debugger server with options=', options);
startDebuggerServer({
onMessage: ({ id, message }) =>
event.sender.send('debugger-message-received', { id, message }),
onError: error => event.sender.send('debugger-error-received', error),
onConnectionClose: ({ id }) =>
event.sender.send('debugger-connection-closed', { id }),
onConnectionOpen: ({ id }) =>
event.sender.send('debugger-connection-opened', { id }),
onConnectionError: ({ id, errorMessage }) =>
event.sender.send('debugger-connection-errored', { id, errorMessage }),
onListening: ({ address }) =>
event.sender.send('debugger-start-server-done', { address }),
});
});
ipcMain.on('debugger-send-message', (event, message) => {
sendMessage(message, err =>
event.sender.send('debugger-send-message-done', err)
);
});
ipcMain.on('updates-check-and-download', event => {
// This will immediately download an update, then install when the
// app quits.
log.info('Starting check for updates (with auto-download if any)');
autoUpdater.autoDownload = true;
autoUpdater.checkForUpdatesAndNotify();
});
ipcMain.on('updates-check', event => {
log.info('Starting check for updates (without auto-download)');
autoUpdater.autoDownload = false;
autoUpdater.checkForUpdates();
});
function sendUpdateStatus(status) {
log.info(status);
if (mainWindow) mainWindow.webContents.send('update-status', status);
}
autoUpdater.on('checking-for-update', () => {
sendUpdateStatus({
message: 'Checking for update...',
status: 'checking-for-update',
});
});
autoUpdater.on('update-available', info => {
sendUpdateStatus({
message: 'Update available.',
status: 'update-available',
});
});
autoUpdater.on('update-not-available', info => {
sendUpdateStatus({
message: 'Update not available.',
status: 'update-not-available',
});
});
autoUpdater.on('error', err => {
sendUpdateStatus({
message: 'Error in auto-updater. ' + err,
status: 'error',
err,
});
});
autoUpdater.on('download-progress', progressObj => {
let logMessage = 'Download speed: ' + progressObj.bytesPerSecond;
logMessage = logMessage + ' - Downloaded ' + progressObj.percent + '%';
logMessage =
logMessage +
' (' +
progressObj.transferred +
'/' +
progressObj.total +
')';
sendUpdateStatus({
message: logMessage,
status: 'download-progress',
bytesPerSecond: progressObj.bytesPerSecond,
percent: progressObj.percent,
transferred: progressObj.transferred,
total: progressObj.total,
});
});
autoUpdater.on('update-downloaded', info => {
sendUpdateStatus({
message: 'Update downloaded',
status: 'update-downloaded',
info,
});
});
setUpDiscordRichPresence(ipcMain);
});